/* Terrain Atlas — overlay chrome above a full-bleed WebGL viewport.
   Every surface declares foreground and background together, in both themes. */

:root {
  color-scheme: dark;
  --panel-bg: rgba(14, 20, 28, 0.78);
  --panel-bg-solid: #0e141c;
  --panel-border: rgba(158, 190, 218, 0.22);
  --panel-shadow: 0 12px 34px rgba(2, 6, 12, 0.48);
  --fg: #e8eef5;
  --fg-muted: #9fb2c6;
  --fg-dim: #7a8ea3;
  --accent: #6fc3e8;
  --accent-fg: #04222f;
  --row-hover: rgba(111, 195, 232, 0.12);
  --row-active: rgba(111, 195, 232, 0.2);
  --control-bg: rgba(255, 255, 255, 0.07);
  --control-border: rgba(158, 190, 218, 0.28);
  --control-fg: #e8eef5;
  --track: rgba(255, 255, 255, 0.16);
  --focus: #9ad8f5;
  --danger-bg: #3a1418;
  --danger-fg: #ffc8ce;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --panel-bg: rgba(250, 252, 254, 0.86);
    --panel-bg-solid: #fafcfe;
    --panel-border: rgba(28, 52, 76, 0.18);
    --panel-shadow: 0 12px 30px rgba(20, 40, 60, 0.2);
    --fg: #10202e;
    --fg-muted: #45596b;
    --fg-dim: #63788a;
    --accent: #10658c;
    --accent-fg: #f2fbff;
    --row-hover: rgba(16, 101, 140, 0.1);
    --row-active: rgba(16, 101, 140, 0.16);
    --control-bg: rgba(16, 32, 46, 0.06);
    --control-border: rgba(28, 52, 76, 0.24);
    --control-fg: #10202e;
    --track: rgba(16, 32, 46, 0.18);
    --focus: #0d5a7e;
    --danger-bg: #ffe6e8;
    --danger-fg: #7a1420;
  }
}

* { box-sizing: border-box; }

/* Author `display` declarations outrank the UA's `[hidden] { display: none }`,
   so the hidden attribute has to be enforced explicitly or overlays such as
   .fatal and .busy stay painted over the viewport. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #0b1017;
  color: var(--fg);
  font: 13px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

#viewport {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* The overlay never eats pointer events; only its panels do. */
.overlay {
  position: fixed;
  inset: 0;
  padding: 14px;
  display: grid;
  grid-template-columns: minmax(210px, 258px) 1fr minmax(0, 300px);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "title  .        drawer"
    "rail   .        drawer"
    "readout actions drawer";
  gap: 12px;
  pointer-events: none;
}

.panel, .chip {
  pointer-events: auto;
  background: var(--panel-bg);
  color: var(--fg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  box-shadow: var(--panel-shadow);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
}

.title-chip {
  grid-area: title;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  align-self: start;
  justify-self: start;
}

.mark {
  width: 22px; height: 22px; border-radius: 6px; flex: none;
  background:
    linear-gradient(160deg, #6fc3e8 0%, #2f6a94 48%, #2f5231 49%, #6b8f4a 100%);
}

.title-text { display: flex; flex-direction: column; line-height: 1.25; }
.title-text strong { font-size: 13px; letter-spacing: 0.02em; }
.muted { color: var(--fg-muted); font-size: 11.5px; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 9px 12px;
  border-bottom: 1px solid var(--panel-border);
}
.panel-head h2 {
  margin: 0; font-size: 11px; font-weight: 650;
  letter-spacing: 0.1em; text-transform: uppercase; color: var(--fg-muted);
}

/* ---- Atlas rail ---- */

.rail {
  grid-area: rail;
  align-self: start;
  max-height: calc(100vh - 190px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.rail[data-collapsed="true"] .rail-list,
.rail[data-collapsed="true"] .rail-note { display: none; }

.rail-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; }
.rail-list li { margin: 0; }

.scene-btn {
  width: 100%;
  display: block;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--fg);
  font: inherit;
  cursor: pointer;
}
.scene-btn:hover { background: var(--row-hover); }
.scene-btn[aria-current="true"] {
  background: var(--row-active);
  border-color: var(--panel-border);
}
.scene-btn .name { display: flex; align-items: center; gap: 7px; font-weight: 600; }
.scene-btn .swatch {
  width: 34px; height: 9px; border-radius: 3px; flex: none;
  border: 1px solid rgba(0, 0, 0, 0.28);
}
.scene-btn .blurb { color: var(--fg-muted); font-size: 11.5px; margin-top: 3px; }

.rail-note {
  margin: 0; padding: 8px 12px; font-size: 11px; color: var(--fg-dim);
  border-top: 1px solid var(--panel-border);
}

/* ---- Readout ---- */

.readout { grid-area: readout; align-self: end; padding: 10px 12px; }
.readout-grid {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 14px;
}
.readout-grid div { display: flex; justify-content: space-between; gap: 10px; min-width: 0; }
.readout-grid dt { color: var(--fg-muted); font-size: 11px; white-space: nowrap; }
.readout-grid dd {
  margin: 0; font-size: 11.5px; font-weight: 600;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}

/* ---- Actions ---- */

.actions {
  grid-area: actions;
  align-self: end;
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  flex-wrap: wrap;
}
.action-group { display: flex; gap: 4px; }

button {
  font: inherit;
  color: var(--control-fg);
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: 7px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover { background: var(--row-hover); }
button:active { transform: translateY(1px); }
button[aria-pressed="true"], button.is-active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  font-weight: 600;
}
button.ghost { background: transparent; border-color: transparent; color: var(--fg-muted); padding: 2px 6px; }
button.ghost:hover { color: var(--fg); background: var(--row-hover); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.fps {
  font-variant-numeric: tabular-nums;
  color: var(--fg-dim);
  font-size: 11px;
  min-width: 52px;
  text-align: right;
}

/* ---- Parameter drawer ---- */

.drawer {
  grid-area: drawer;
  align-self: stretch;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: calc(100vh - 28px);
}
.drawer[data-open="false"] { align-self: start; }
.drawer[data-open="false"] .drawer-body { display: none; }
.drawer-body { overflow-y: auto; padding: 4px 12px 12px; }

.group { border-bottom: 1px solid var(--panel-border); padding: 10px 0; }
.group:last-child { border-bottom: none; }
.group h3 {
  margin: 0 0 8px; font-size: 10.5px; font-weight: 650;
  letter-spacing: 0.1em; text-transform: uppercase; color: var(--fg-dim);
}

.field { display: grid; gap: 4px; margin-bottom: 9px; }
.field-head {
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
}
.field label { color: var(--fg-muted); font-size: 11.5px; }
.field .value { font-size: 11px; font-variant-numeric: tabular-nums; color: var(--fg); }

input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 16px; background: transparent; cursor: pointer; margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 2px; background: var(--track);
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--track); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 13px; height: 13px; margin-top: -4.5px; border-radius: 50%;
  background: var(--accent); border: 1px solid var(--panel-bg-solid);
}
input[type="range"]::-moz-range-thumb {
  width: 13px; height: 13px; border-radius: 50%;
  background: var(--accent); border: 1px solid var(--panel-bg-solid);
}

input[type="text"], select {
  width: 100%;
  font: inherit;
  color: var(--control-fg);
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: 7px;
  padding: 5px 8px;
}
select { cursor: pointer; }
select option { background: var(--panel-bg-solid); color: var(--fg); }

.seed-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; }

.toggles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.toggle {
  display: flex; align-items: center; gap: 7px;
  padding: 5px 8px; border-radius: 7px;
  background: var(--control-bg); border: 1px solid var(--control-border);
  color: var(--fg); cursor: pointer; font-size: 11.5px;
}
.toggle input { accent-color: var(--accent); margin: 0; }

/* ---- Transient UI ---- */

.busy {
  position: fixed; left: 50%; top: 16px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  padding: 7px 13px; border-radius: 999px;
  background: var(--panel-bg); color: var(--fg);
  border: 1px solid var(--panel-border); box-shadow: var(--panel-shadow);
  backdrop-filter: blur(14px); font-size: 12px; z-index: 30;
}
.spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--track); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  padding: 8px 14px; border-radius: 9px;
  background: var(--panel-bg-solid); color: var(--fg);
  border: 1px solid var(--panel-border); box-shadow: var(--panel-shadow);
  font-size: 12px; z-index: 30; max-width: min(90vw, 460px);
}
.toast[data-kind="error"] { background: var(--danger-bg); color: var(--danger-fg); border-color: var(--danger-fg); }

.fatal {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-content: center; gap: 8px; text-align: center; padding: 24px;
  background: var(--panel-bg-solid); color: var(--fg);
}
.fatal h1 { margin: 0; font-size: 18px; }
.fatal p { margin: 0; color: var(--fg-muted); max-width: 44ch; }

/* ---- Narrow viewports ---- */

@media (max-width: 900px) {
  .overlay {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr auto auto;
    grid-template-areas: "title" "rail" "." "readout" "actions";
    padding: 10px;
    gap: 8px;
  }
  .drawer {
    grid-area: unset;
    position: fixed;
    right: 10px; top: 58px; width: min(300px, calc(100vw - 20px));
    max-height: calc(100vh - 200px);
  }
  .rail { max-height: 34vh; }
  .actions { justify-self: stretch; justify-content: center; }
  .readout-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
