:root {
  --bg: #101317;
  --bg-panel: #171b21;
  --bg-sunken: #0c0e12;
  --line: #262c35;
  --line-soft: #1e232b;
  --text: #e3e8ef;
  --text-dim: #8d97a5;
  --text-faint: #626c7a;
  --accent: #4c9aff;
  --accent-dim: #2f6fc4;
  --warm: #ffab40;
  --good: #4ad991;
  --bad: #ff6b6b;
  --radius: 7px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

/* Elements below set an explicit `display`, which would otherwise win against
   the user-agent rule for [hidden] and leave them stuck on screen. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h1, h2 { margin: 0; font-weight: 600; }

/* ---------------------------------------------------------- top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-panel);
  flex: none;
}

.brand { display: flex; align-items: center; gap: 11px; }

.brand h1 { font-size: 15px; letter-spacing: -0.01em; }
.brand p { margin: 0; font-size: 11.5px; color: var(--text-faint); }

.mark {
  width: 22px;
  height: 22px;
  border: 1.5px solid var(--accent);
  border-radius: 3px;
  position: relative;
  transform: rotate(-12deg) skewX(-10deg);
}
.mark::after {
  content: "";
  position: absolute;
  inset: 4px 4px auto auto;
  width: 8px;
  height: 8px;
  border: 1.5px solid var(--warm);
  border-radius: 50%;
}

.topbar-actions { display: flex; align-items: center; gap: 14px; }

.model-stats {
  font: 11.5px/1.4 var(--mono);
  color: var(--text-faint);
  text-align: right;
  white-space: pre-line;
}

/* ---------------------------------------------------------- layout */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 296px minmax(0, 1fr) 372px;
}

/* Maximised: the working surface lifts out and floats over everything at 80%
   of the window. Nothing else is torn down — the panels stay where they were
   and simply end up behind it. */
body.maximised .preview-float {
  position: fixed;
  left: 10vw;
  top: 10vh;
  width: 80vw;
  height: 80vh;
  z-index: 60;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
}
body.maximised .preview-actions { padding: 8px 10px; }
/* At this width the buttons have room to be legible again. */
body.maximised .preview-actions .chip { flex: 0 0 auto; padding: 5px 12px; }

.preview-scrim {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(8, 10, 13, 0.6);
  backdrop-filter: blur(2px);
}
.preview-scrim[hidden] { display: none; }

.panel {
  background: var(--bg-panel);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.controls { border-right: 1px solid var(--line); }
.preview {
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------------------------------------------------------- tabs */

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--bg-panel);
  z-index: 3;
}

.tab {
  appearance: none;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 12px;
  padding: 11px 4px;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-panel { display: none; padding: 14px 14px 4px; }
.tab-panel.active { display: block; }

.group {
  padding: 14px;
  border-top: 1px solid var(--line);
}
.group h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
  margin-bottom: 11px;
}

.hint {
  margin: 0 0 14px;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.45;
}

/* ---------------------------------------------------------- fields */

.field { display: block; margin-bottom: 14px; }

.field > span {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 5px;
}
.field em {
  font-style: normal;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text);
}
/* The permitted range, shown beside the label so the scale of the setting is
   visible without having to probe it. */
.field em.range {
  color: var(--text-faint);
  font-size: 10.5px;
  letter-spacing: 0.01em;
}

.field small {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-faint);
  line-height: 1.4;
}

input[type="number"], input[type="text"] {
  width: 100%;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font: 12.5px var(--mono);
  padding: 7px 9px;
}
input[type="number"]:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--accent-dim);
}

/* Briefly flags a value that was pulled back into range, so the number
   changing under the cursor doesn't look like a glitch. */
input.clamped {
  border-color: var(--warm);
  transition: border-color 0.5s ease-out;
}

/* Slider to scrub, box to type. The box is sized for the numbers it actually
   shows rather than stretching, so the slider keeps the room. */
.pair {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  align-items: center;
  gap: 8px;
}
.pair input[type="range"] { margin: 0; }
.pair input[type="number"] {
  padding: 5px 7px;
  font-size: 11.5px;
}


input[type="range"] {
  width: 100%;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  margin: 6px 0 0;
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-panel);
  cursor: pointer;
}
input[type="range"]:disabled { opacity: 0.4; }

.segmented {
  display: flex;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 2px;
  gap: 2px;
}
.segmented button {
  flex: 1;
  appearance: none;
  background: none;
  border: 0;
  border-radius: 5px;
  color: var(--text-dim);
  font: inherit;
  font-size: 11.5px;
  padding: 6px 4px;
  cursor: pointer;
}
.segmented button:hover { color: var(--text); }
.segmented button.active { background: var(--accent-dim); color: #fff; }

/* ---------------------------------------------------------- buttons */

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-sunken);
  color: var(--text);
  font: inherit;
  font-size: 12.5px;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent-dim); }
.btn.primary { background: var(--accent-dim); border-color: var(--accent-dim); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); }
.btn.wide { width: 100%; display: block; text-align: center; }
.btn:disabled { opacity: 0.4; cursor: default; }

.chip {
  appearance: none;
  background: rgba(16, 19, 23, 0.82);
  border: 1px solid var(--line);
  color: var(--text-dim);
  font: inherit;
  font-size: 11.5px;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.chip:hover { color: var(--text); border-color: var(--accent-dim); }
.chip.toggle.active { color: var(--accent); border-color: var(--accent-dim); }
.chip:disabled {
  opacity: 0.35;
  cursor: default;
}
.chip:disabled:hover { color: var(--text-dim); border-color: var(--line); }

/* ---------------------------------------------------------- regions */

.region-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  max-height: 300px;
  overflow-y: auto;
  border-top: 1px solid var(--line-soft);
}
.region-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 7px 6px;
  border-bottom: 1px solid var(--line-soft);
  cursor: pointer;
  font-size: 12px;
}
.region-list li:hover { background: rgba(76, 154, 255, 0.08); }
.region-list li.active { background: rgba(76, 154, 255, 0.16); }
.region-list .area { font-family: var(--mono); font-size: 11.5px; }
.region-list .meta { color: var(--text-faint); font-size: 11px; font-family: var(--mono); }

/* ---------------------------------------------------------- viewport */

.viewport { position: relative; background: var(--bg-sunken); overflow: hidden; }
.viewport canvas { display: block; }

.viewport-toolbar {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 6px;
  z-index: 2;
  flex-wrap: wrap;
}

.dropzone {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--text-faint);
  border: 2px dashed transparent;
  transition: border-color 0.15s, background 0.15s;
  pointer-events: none;
}
.dropzone strong { display: block; font-size: 14px; color: var(--text-dim); font-weight: 500; }
.dropzone span { font-size: 12px; }
.dropzone.hot {
  border-color: var(--accent-dim);
  background: rgba(76, 154, 255, 0.06);
}
.dropzone.hidden { display: none; }

.busy {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(12, 14, 18, 0.72);
  z-index: 4;
}
.busy-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px 20px;
  min-width: 250px;
}
.busy-card > div:last-child { flex: 1; font-size: 12.5px; }
.bar {
  height: 3px;
  background: var(--line);
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.18s; }

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------- preview */

.preview-head {
  padding: 11px 14px 8px;
  flex: none;
}
.preview-head h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
}

/* The float holds the toolbar, canvas and edit bar together, so maximising
   lifts the whole working surface rather than just the picture. */
.preview-float {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* The buttons share the row and give up width evenly rather than wrapping or
   overflowing — the panel is narrow, and a control you cannot see is a control
   you do not have. */
.preview-actions {
  display: flex;
  gap: 4px;
  padding: 0 10px 8px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.preview-actions .chip {
  flex: 1 1 0;
  min-width: 0;
  padding: 5px 2px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  background: var(--bg-sunken);
}

#backdrop-canvas,
#preview-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

/* Dimmed, because it is there for reference — the outline has to stay the
   thing your eye lands on. */
#backdrop-canvas { opacity: 0.45; }
#backdrop-canvas[hidden] { display: none; }

/* Controls sitting over the drawing. Quiet until you go looking for them, and
   out of the way of the axis labels along the edges. */
.stage-icons {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  gap: 6px;
  z-index: 3;
}

.stage-button {
  position: absolute;
  left: 10px;
  top: 10px;
  z-index: 3;
  background: rgba(16, 19, 23, 0.82);
  border: 1px solid var(--line);
  color: var(--text-dim);
  font: inherit;
  font-size: 11.5px;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.stage-button:hover { color: var(--text); border-color: var(--accent-dim); }
.stage-button.active { color: var(--accent); border-color: var(--accent-dim); }

.stage-icon {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  padding: 0;
  background: rgba(16, 19, 23, 0.8);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.stage-icon:hover { color: var(--text); border-color: var(--accent-dim); }
.stage-icon.active { color: var(--accent); border-color: var(--accent-dim); }
.stage-icon svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* Arrows point outward to expand, and inward once expanded. */
.stage-icon .icon-in { display: none; }
body.maximised .stage-icon .icon-out { display: none; }
body.maximised .stage-icon .icon-in { display: inline; }

#preview-canvas { cursor: grab; }
#preview-canvas:active { cursor: grabbing; }
.preview.editing #preview-canvas { cursor: default; }

.edit-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
  background: rgba(255, 171, 64, 0.06);
  flex: none;
}
/* Same reasoning as the toolbar: in the docked panel there is not room for
   five buttons and a sentence, so the buttons tighten and the hint drops to
   its own line rather than pushing anything off the edge. */
.edit-bar .chip {
  flex: 0 1 auto;
  min-width: 0;
  padding: 5px 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.edit-bar[hidden] { display: none; }
.edit-sep {
  width: 1px;
  align-self: stretch;
  background: var(--line);
  margin: 0 2px;
}
.edit-hint {
  flex: 1 1 140px;
  font-size: 11px;
  color: var(--text-faint);
  text-align: right;
}
/* The separator only reads as one when the row hasn't wrapped. */
.edit-bar { row-gap: 4px; }

.readout {
  flex: none;
  max-height: 56%;
  overflow-y: auto;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  font-size: 12px;
}
.readout .empty { color: var(--text-faint); margin: 0; }

.readout dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 5px 14px;
  margin: 0 0 12px;
}
.readout dt { color: var(--text-faint); }
.readout dd { margin: 0; font-family: var(--mono); font-size: 11.5px; text-align: right; }
.readout dd.warn { color: var(--warm); }
.readout dd.ok { color: var(--good); }

.readout h3 {
  margin: 0 0 7px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
  font-weight: 600;
}
/* ---------------------------------------------------------- contour list */

.contour-list {
  list-style: none;
  margin: 0 0 8px;
  padding: 0;
}

.contour-list li {
  display: grid;
  grid-template-columns: 16px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 5px 6px;
  border-radius: 5px;
  cursor: pointer;
  user-select: none;
}
.contour-list li:hover { background: rgba(76, 154, 255, 0.1); }

/* Drawn rather than typed: a check character picks up emoji presentation on
   some systems and turns into a coloured tile. */
.contour-list .mark {
  width: 12px;
  height: 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  position: relative;
  margin: 0 auto;
}
.contour-list li:not(.off) .mark {
  background: var(--accent-dim);
  border-color: var(--accent-dim);
}
.contour-list li:not(.off) .mark::after {
  content: "";
  position: absolute;
  left: 3.5px;
  top: 1px;
  width: 3px;
  height: 6px;
  border: solid #fff;
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
}
.contour-list li.off .name,
.contour-list li.off .meta { color: var(--text-faint); text-decoration: line-through; }

.contour-list .name { font-size: 12px; }
.contour-list .meta { font-family: var(--mono); font-size: 11px; color: var(--text-dim); }
.contour-list .meta.dim { color: var(--text-faint); }

.contour-actions {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

.readout .note {
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(255, 171, 64, 0.1);
  border: 1px solid rgba(255, 171, 64, 0.25);
  color: var(--warm);
  font-size: 11.5px;
  line-height: 1.45;
}

/* ---------------------------------------------------------- toast */

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-panel);
  border: 1px solid var(--bad);
  color: var(--text);
  padding: 11px 16px;
  border-radius: 8px;
  font-size: 12.5px;
  max-width: 520px;
  z-index: 20;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

@media (max-width: 1180px) {
  .layout { grid-template-columns: 270px minmax(0, 1fr) 320px; }
}
