* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1c1e21;
  --bg-panel: #26292e;
  --bg-input: #16181a;
  --border: #3a3e45;
  --text: #d7dade;
  --text-dim: #8a8f98;
  --accent: #2196f3;
  --accent-warm: #ff9800;
  --danger: #e5534b;
}

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
}

#viewer {
  flex: 1;
  min-width: 0;
  position: relative;
  background: #0f1011;
}

#viewer-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

/* 3D preview canvas: overlays the 2D canvas, shown only in preview mode. */
#preview-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

#viewer-canvas[hidden],
#preview-canvas[hidden] { display: none; }

#panel {
  width: 320px;
  flex: none;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* The Objects section absorbs overflow: its tool list scrolls internally so
   the Tray box stays flush beneath it and the nav/status stay reachable. */
#section-tools {
  display: flex;
  flex-direction: column;
  flex: 0 1 auto;
  min-height: 0;
}
#section-tools #tool-list {
  overflow-y: auto;
  min-height: 0;
}

.panel-section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.panel-section h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

button {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 10px;
  font: inherit;
  cursor: pointer;
}

button:hover { border-color: var(--text-dim); }

button.active,
button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.field-row label {
  flex: none;
  min-width: 88px;
  color: var(--text-dim);
}

input[type="number"], input[type="text"], select {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  font: inherit;
  width: 100%;
  min-width: 0;
}

input[type="number"]:focus, input[type="text"]:focus, select:focus, button:focus-visible {
  outline: 1px solid var(--accent);
  outline-offset: 0;
}

input[type="range"] { flex: 1; accent-color: var(--accent); }
input[type="checkbox"] { accent-color: var(--accent); }
input[type="file"] { color: var(--text-dim); max-width: 100%; }

.hint { color: var(--text-dim); font-size: 12px; }

/* Square graphic buttons (Add Tool / Add Tray / Add Grid). */
.icon-btn {
  width: 48px;
  height: 48px;
  padding: 5px;
  flex: none;
}
.icon-btn svg { width: 100%; height: 100%; display: block; }

#tool-list { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; flex: 0 1 auto; }

.tool-item {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
}

.tool-item.selected { border-color: var(--accent-warm); }

/* Mode-switch nav (Trace/Arrange/3D) pins to the panel bottom, status below it. */
#section-nav { margin-top: auto; flex: none; }

#status {
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
  min-height: 32px;
}

#boot-error {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  font-size: 13px;
}

/* Number inputs: the tiny spinner arrows are useless at this size. */
input[type=number] {
  -moz-appearance: textfield;
  appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Step completion: finished sections outline green and collapse to their
   header; the header (with a checkmark) toggles them back open. */
.panel-section.done {
  outline: 1px solid #2e9e4f;
  outline-offset: -1px;
  border-radius: 6px;
}
.panel-section.done > h2 { color: #4ccb72; }
.panel-section > h2 { cursor: pointer; user-select: none; }
.panel-section > h2::after { content: ' ▾'; font-size: 10px; opacity: 0.55; }
.panel-section.collapsed > h2::after { content: ' ▸'; font-size: 10px; opacity: 0.55; }
.panel-section.done > h2::after { content: ' ✓ ▾'; font-size: 10px; opacity: 1; }
.panel-section.done.collapsed > h2::after { content: ' ✓ ▸'; font-size: 10px; opacity: 1; }
.panel-section.collapsed > *:not(h2) { display: none !important; }

/* Blocked nav button: looks disabled but still receives clicks so we can
   flash the prerequisite boxes. */
button.blocked { opacity: 0.45; cursor: not-allowed; }

/* Attention flash on the boxes a blocked action needs. */
.flash-missing {
  animation: flash-missing-anim 0.45s ease-in-out 3;
  border-radius: 6px;
}
@keyframes flash-missing-anim {
  0%, 100% { outline: 2px solid transparent; outline-offset: -1px; }
  50% { outline: 2px solid #ff1744; outline-offset: -1px; }
}

/* The hidden attribute must always win, even over display:flex rules. */
[hidden] { display: none !important; }

/* ---- start modal ---------------------------------------------------------- */
#start-modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 11, 13, 0.72);
  backdrop-filter: blur(3px);
}
#start-card {
  background: #26292d; border: 1px solid #4a4f55; border-radius: 10px;
  padding: 28px 32px; max-width: 460px; text-align: center;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6);
}
#start-card h1 { margin: 0 0 8px; font-size: 22px; letter-spacing: 0.5px; }
#start-card p { margin: 0 0 18px; font-size: 12.5px; color: #a8adb4; line-height: 1.5; }
#start-grid {
  display: flex; flex-direction: column; gap: 12px;
}
#start-grid button {
  padding: 16px 12px; font-size: 15px; font-weight: 600;
  border-radius: 8px;
  background: #2b6fd4; border: 1px solid #3c82e8; color: #fff;
  cursor: pointer;
}
#start-grid button:hover { background: #3c82e8; }
/* Load Project: the inverted twin of Upload Image (user order). */
#start-grid button.start-inverted {
  background: #fff; color: #2b6fd4; border-color: #3c82e8;
}
#start-grid button.start-inverted:hover { background: #dfe9fb; }
/* The two example shortcuts sit side by side below Load Project. */
#start-examples { display: flex; gap: 12px; }
#start-examples button { flex: 1; }
/* The example shortcuts are secondary: smaller, outlined. */
#start-grid button.start-secondary {
  padding: 9px 12px; font-size: 12.5px;
  background: transparent; border: 1px solid #2b6fd4; color: #7db2f5;
}
#start-grid button.start-secondary:hover { background: rgba(43, 111, 212, 0.15); }

/* ---- busy overlay (segmentation, 3D build) -------------------------------- */
#busy-overlay {
  position: absolute; inset: 0; z-index: 20;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; background: rgba(15, 16, 18, 0.45);
  pointer-events: none;
}
#busy-text {
  font-size: 26px; font-weight: 600; color: #e8ecf1;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
}
.spinner {
  width: 68px; height: 68px; border-radius: 50%;
  border: 6px solid rgba(255, 255, 255, 0.18);
  border-top-color: #4da3ff;
  animation: spinner-rot 0.8s linear infinite;
}
@keyframes spinner-rot { to { transform: rotate(360deg); } }

/* ---- toast (top-center, auto-dismissing) ---------------------------------- */
#toast {
  /* below the floating Save/Load/Undo/Redo row, never covering it */
  position: absolute; top: 64px; left: 50%; transform: translateX(-50%);
  z-index: 30; max-width: 80%;
  background: #26292d; border: 1px solid #4a4f55; border-radius: 12px;
  padding: 18px 32px; font-size: 26px; color: #e8ecf1;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.55);
  animation: toast-in 0.25s ease-out;
  pointer-events: none;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap; justify-content: center;
}
/* Walkthrough steps carry controls (Done, length field). Only the controls
   catch the pointer — the popup body lets clicks fall through to the canvas
   beneath, so a large popup never blocks vertex/outline interaction. */
#toast.interactive { pointer-events: none; }
#toast.interactive #toast-controls { pointer-events: auto; }
#toast-controls { display: flex; align-items: center; gap: 10px; }
#toast-controls:empty { display: none; }
#toast-controls button {
  padding: 10px 28px; font-size: 20px; font-weight: 600; border-radius: 8px;
  background: #2b6fd4; border: 1px solid #3c82e8; color: #fff; cursor: pointer;
}
#toast-controls button:hover { background: #3c82e8; }
#toast-controls input[type="number"] {
  width: 130px; font-size: 22px; padding: 8px 10px; border-radius: 8px;
}
#toast-controls select { font-size: 20px; padding: 8px 6px; border-radius: 8px; }

/* Walkthrough end state: marching ants around the Arrange button until the
   user goes (four edge gradients sliding in a loop — no canvas needed). */
.ants-cta {
  background-image:
    repeating-linear-gradient(90deg, #4da3ff 0 6px, transparent 6px 12px),
    repeating-linear-gradient(90deg, #4da3ff 0 6px, transparent 6px 12px),
    repeating-linear-gradient(0deg, #4da3ff 0 6px, transparent 6px 12px),
    repeating-linear-gradient(0deg, #4da3ff 0 6px, transparent 6px 12px);
  background-size: 100% 2px, 100% 2px, 2px 100%, 2px 100%;
  background-position: 0 0, 0 100%, 0 0, 100% 0;
  background-repeat: no-repeat;
  animation: ants-cta-move 0.5s linear infinite;
}
@keyframes ants-cta-move {
  to { background-position: 12px 0, -12px 100%, 0 -12px, 100% 12px; }
}

/* Settings modal (same look as the start modal). */
#settings-modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 11, 13, 0.72);
  backdrop-filter: blur(3px);
}
#settings-card {
  background: #26292d; border: 1px solid #4a4f55; border-radius: 10px;
  padding: 24px 32px; min-width: 300px; text-align: center;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6);
}
#settings-card h1 { margin: 0 0 14px; font-size: 18px; }

/* The calibrate tool's own banner yields to the walkthrough popup. */
body.wizard-active #calib-instructions { display: none !important; }
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(-8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* The panel section a tutorial window is anchored to. */
.panel-section.tutorial-target {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-radius: 4px;
}

/* Per-mode cursors: the mode is visible on the canvas, not only in the
   status line. Set from the mode subscriber in js/ui/main.js. */
body[data-mode='msam'] #viewer-canvas { cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><g fill='none' stroke='%23000' stroke-opacity='.65' stroke-width='3' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><rect x='14.5' y='14.5' width='7' height='7' stroke-dasharray='2 1.6'/></g><g fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><rect x='14.5' y='14.5' width='7' height='7' stroke-dasharray='2 1.6'/></g></svg>") 12 12, crosshair; }
body[data-mode='draw-tray'] #viewer-canvas { cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><g fill='none' stroke='%23000' stroke-opacity='.65' stroke-width='3' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><rect x='14.5' y='14.5' width='7' height='7'/></g><g fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><rect x='14.5' y='14.5' width='7' height='7'/></g></svg>") 12 12, crosshair; }
body[data-mode='add-relief'] #viewer-canvas { cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><g fill='none' stroke='%23000' stroke-opacity='.65' stroke-width='3' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><circle cx='18' cy='18' r='3.6'/></g><g fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><circle cx='18' cy='18' r='3.6'/></g></svg>") 12 12, crosshair; }
body[data-mode='calibrate'] #viewer-canvas { cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><g fill='none' stroke='%23000' stroke-opacity='.65' stroke-width='3' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><path d='M14 21h8M14 19v4M22 19v4'/></g><g fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round'><path d='M12 2v6M12 16v6M2 12h6M16 12h6'/><path d='M14 21h8M14 19v4M22 19v4'/></g></svg>") 12 12, crosshair; }
body[data-mode='arrange'] #viewer-canvas { cursor: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><g fill='none' stroke='%23000' stroke-opacity='.65' stroke-width='3' stroke-linecap='round'><path d='M12 3v18M3 12h18M12 3l-3 3M12 3l3 3M12 21l-3-3M12 21l3-3M3 12l3-3M3 12l3 3M21 12l-3-3M21 12l-3 3'/></g><g fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round'><path d='M12 3v18M3 12h18M12 3l-3 3M12 3l3 3M12 21l-3-3M12 21l3-3M3 12l3-3M3 12l3 3M21 12l-3-3M21 12l-3 3'/></g></svg>") 12 12, crosshair; }
body[data-mode='select'] #viewer-canvas { cursor: default; }
