/* 3DMap — app chrome. Dark, out of the way; the terrain is the subject. */

:root {
  --bg: #0d1117;
  --panel: #161b22e6;
  --line: #ffffff1f;
  --text: #e6edf3;
  --dim: #9aa7b4;
  --accent: #4c9aff;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Any `display` set by a class beats the UA stylesheet's `[hidden] { display: none }`, so a
   flex or grid row stays visible however often you set `hidden`. State it once, with weight. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 ui-sans-serif, system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

#app { position: relative; height: 100%; }

.view { position: absolute; inset: 0; }
.view[hidden] { display: none; }

/* ---- viewer ------------------------------------------------------------- */

#gl { display: block; width: 100%; height: 100%; touch-action: none; }

/* ---- floating chrome ---------------------------------------------------- */

.hud {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  backdrop-filter: blur(12px);
  padding: 10px 12px;
}

#titlebar {
  top: 12px; left: 12px;
  display: flex; align-items: center; gap: 12px;
}
#titlebar h1 { font-size: 13px; font-weight: 600; margin: 0; letter-spacing: .02em; }
#region { color: var(--dim); max-width: 26ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 288, not the mock's 264: `scrollbar-gutter` below reserves 15px that a mock rendered against
   macOS's overlay scrollbars never paid, and without them back the longest group title ("Lighting
   — sun & sky") ellipsises its own summary chip away — which is the one thing the chip is for. */
#panel {
  top: 12px; right: 12px;
  width: 288px;
  display: grid; gap: 0;
  /* `minmax(0, 1fr)`, not the default `auto`: a grid track sized to `auto` takes the widest
     item's min-content width, so one long <option> or a canvas's intrinsic 300px pushes the
     whole column past the panel's own 288px and everything to the right is clipped. */
  grid-template-columns: minmax(0, 1fr);
  /* T18: the padding moved into the groups, so a section separator can run the full width of
     the panel and a header's hover fill reaches its edges. */
  padding: 0;
  border-radius: 12px;
  /* The panel grew a sun rig, a skirt and an export row — it has to be able to scroll on a
     short window rather than run off the bottom of the screen. */
  max-height: calc(100% - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
/* Sticky, so "close" is reachable from anywhere in a panel that scrolls; scrolled content
   passes *under* it, hence the near-opaque fill over `.hud`'s translucent one. */
#panel-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 11px 14px;
  background: #161b22f5;
  border-bottom: 1px solid var(--line);
  color: var(--text); font-size: 12px; font-weight: 600; letter-spacing: .03em;
}
#panel-close, #panel-open {
  padding: 4px 9px;
  line-height: 1;
  font-size: 14px;
  color: var(--dim);
}
#panel-close:hover, #panel-open:hover { color: var(--text); }

/* The other half of the toggle: same corner, so the panel appears to fold into it. */
#panel-open { top: 12px; right: 12px; padding: 8px 11px; z-index: 12; }

/* Nothing in the panel may be wider than the panel: a flex item's default `min-width: auto`
   lets a <select> with a long option push the whole row past the right edge. */
#panel select, #panel input[type=range], #panel input[type=datetime-local] { max-width: 100%; }
.field > select, .field > input[type=range] { width: 100%; }
.field .row > * { min-width: 0; }

/* ---- panel groups (T18) --------------------------------------------------- */
/* Four sections instead of nine peer fields. A group title is the only 12.5px/600 text in the
   panel, so the eye lands on it before the 10.5px field labels underneath — that hierarchy is
   the whole redesign, and it is why the field labels lost their uppercase treatment. */

.group { border-bottom: 1px solid #ffffff14; }
.group:last-child { border-bottom: 0; }

/* Overrides the shared `button` rule below wholesale rather than `all: unset`, which would also
   throw away the focus ring and leave the element `display: inline`. */
.group-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  width: 100%;
  /* 12, against the group body's 14: the header's own left edge is the decorative pip, not text,
     so nothing lines up with the fields below either way — and the 4px buys the longest title
     enough room to keep its chip whole. */
  padding: 13px 12px;
  background: none; border: 0; border-radius: 0;
  font: inherit; color: var(--text); text-align: left;
  cursor: pointer;
  transition: background .15s;
}
.group-head.static { cursor: default; }           /* export — no accordion to open */
.group-head:hover:not(.static) { background: #ffffff08; }
.group-head:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.group-title { display: flex; align-items: center; gap: 7px; min-width: 0; }
.group-title .name { font-size: 12.5px; font-weight: 600; letter-spacing: .02em; white-space: nowrap; }

/* The coloured pip. `background` is the tint, `color` the pip itself, so one class sets both. */
.dot {
  flex: none;
  width: 18px; height: 18px; border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
}
.dot::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.dot-terrain { background: #4c9aff26; color: #4c9aff; }
.dot-light   { background: #f0c67426; color: #f0c674; }
.dot-surface { background: #9aa7b426; color: #9aa7b4; }
.dot-export  { background: #4c9aff26; color: #4c9aff; }
.dot-export::before { border-radius: 0; transform: rotate(45deg); }

/* What a shut group is worth knowing about without opening it. */
.chip {
  font: 10.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #6b7684;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.chev { flex: none; color: var(--dim); font-size: 11px; transition: transform .2s ease; }
.group[data-open] .chev { transform: rotate(90deg); }

/* `0fr` → `1fr` rather than a max-height guess: a max-height has to be a number larger than the
   content, which then sets the animation's real speed and clips the group the day it grows. The
   grid track animates to the content's *own* height, so neither is true here.
   The padding is on the *container*, not on the item the track sizes: `min-height: 0` bounds a
   content box, so 18px of vertical padding on the item left every shut group 18px tall. Hence
   horizontal padding here — it does not enter the row's height — and the vertical inset as
   margins on the first and last field, which do sit inside the box that gets clipped. */
.group-body {
  display: grid;
  grid-template-rows: 0fr;
  padding: 0 14px;
  transition: grid-template-rows .28s ease;
}
.group[data-open] > .group-body { grid-template-rows: 1fr; }
.group-body > .group-inner { min-height: 0; overflow: hidden; }
.group-body > .group-inner > :first-child { margin-top: 2px; }
.group-body > .group-inner > :last-child { margin-bottom: 16px; }

.group-inner { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
#group-export .group-inner { padding: 0 14px 14px; }

.divider { height: 1px; background: #ffffff14; margin: 2px 0; }
.checkrow { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
/* Label and its readout on one line, so the value sits beside the name rather than stealing
   width from the slider under it. */
.label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.export-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.export-row button { padding: 7px 0; text-align: center; }

/* Colour, intensity, ambient — four items on one line inside a 243px column. The basis is small
   because a range input's *automatic* minimum size is Chrome's intrinsic ~129px, which is what
   breaks the flex line; at 60px all four fit, and they wrap gracefully rather than overflow if a
   narrower panel ever makes them not. */
.light-row { flex-wrap: wrap; }
.light-row > input[type=range] { flex: 1 1 60px; }
.light-row > .sublabel { flex: none; }

@media (prefers-reduced-motion: reduce) {
  .group-body, .chev, .group-head { transition: none; }
}

#stats {
  bottom: 12px; left: 12px;
  font: 11px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--dim);
  padding: 7px 10px;
}

#status {
  bottom: 12px; left: 50%; transform: translateX(-50%);
  padding: 8px 14px;
  transition: opacity .2s;
}
#status[hidden] { display: none; }
#status.error { border-color: #f8514966; color: #ff9c94; }

/* ---- picker ------------------------------------------------------------- */

#map { position: absolute; inset: 0; }

/* The shift-drag rectangle: a plain overlay, so dragging never touches a map layer. */
#drag-rect {
  position: absolute;
  border: 1.5px solid var(--accent);
  background: #4c9aff2e;
  pointer-events: none;
  z-index: 5;
}
#drag-rect[hidden] { display: none; }

#search-panel {
  top: 12px; left: 12px;
  width: 340px;
  padding: 8px;
  z-index: 10;
}
#search { width: 100%; }

#results { list-style: none; margin: 6px 0 0; padding: 0; max-height: 45vh; overflow-y: auto; }
#results:empty { margin: 0; }
#results li {
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
}
#results li:hover { background: #ffffff1f; }
#results li span { color: var(--dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#picker-actions {
  bottom: 12px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  z-index: 10;
}
#selection { color: var(--dim); font: 11px/1.4 ui-monospace, Menlo, monospace; }

/* MapLibre's own chrome, toned down to match. */
.maplibregl-ctrl-attrib { font-size: 10px; }
.maplibregl-ctrl-attrib.maplibregl-compact { background: var(--panel); }

/* ---- controls ----------------------------------------------------------- */

/* Same `minmax(0, 1fr)` as #panel, one level down: without it a .field's single auto track is
   sized to its widest child's min-content and the dials spill out of the panel. */
.field { display: grid; gap: 5px; grid-template-columns: minmax(0, 1fr); }
.field > * { min-width: 0; }
/* Not `.field > label`: the exaggeration and skirt labels now sit inside a `.label-row` beside
   their readout. Uppercase and 11px are gone — the group titles carry the hierarchy now, and a
   panel where nine labels all shout is the thing T18 set out to fix. */
.field label:not(.inline) { color: var(--dim); font-size: 10.5px; letter-spacing: .03em; }
.field .row { display: flex; align-items: center; gap: 8px; }
.field output { font: 11px/1 ui-monospace, Menlo, monospace; min-width: 4ch; text-align: right; color: var(--text); }

select, button, input[type=text], input[type=search] {
  font: inherit;
  color: var(--text);
  background: #ffffff14;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 9px;
}
button { cursor: pointer; }
button:hover, select:hover { background: #ffffff24; }
button.primary { background: var(--accent); border-color: transparent; color: #06121f; font-weight: 600; }
button.primary:hover { background: #6badff; }
button:disabled { opacity: .45; cursor: default; }

input[type=range] { flex: 1; accent-color: var(--accent); }

.note { color: var(--dim); font-size: 11px; }
.note.warn { color: #f0c674; }

label.inline {
  display: flex; align-items: center; gap: 6px;
  color: var(--text); font-size: 12px; text-transform: none; letter-spacing: 0;
  cursor: pointer;
}
label.inline input { accent-color: var(--accent); margin: 0; }
.sublabel { color: var(--dim); font-size: 10.5px; min-width: 4ch; }
/* A row whose control is disabled but still in place — the sun-disc size, when the disc is off. */
.row.off { opacity: .45; }

/* ---- sun dials (T9) ------------------------------------------------------ */
/* Ports of SunTopClass / SunProfileClass: drawn on a canvas, dragged with the pointer. */

.dials { display: flex; gap: 6px; }
.dials canvas {
  flex: 1 1 0;
  height: 96px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff0a;
  cursor: grab;
  touch-action: none;          /* the drag is ours, not the browser's scroll */
  /* iOS otherwise starts a selection / callout on a press-and-hold and takes the finger with
     it mid-drag. `touch-action` alone does not cover that — see the touch path in sun.js. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.dials canvas:active { cursor: grabbing; }

#sun-color, #sky-ground {
  flex: none;
  width: 30px; height: 24px;
  padding: 2px;
  background: #ffffff14;
  border: 1px solid var(--line);
  border-radius: 6px;
}
#sun-time { flex: 1; min-width: 0; font-size: 11px; padding: 4px 6px; }
/* A full-width grid item, so centring it puts the readout under the pair of dials it describes
   rather than against the left-hand one. */
#sun-readout { min-width: 0; text-align: center; color: var(--dim); }

/* ---- phone ---------------------------------------------------------------- */
/* One breakpoint, and it is about width, not about touch: the chrome here is laid out in
   fixed pixels against a 1280px window, and below ~560px the pieces meet in the middle.
   `main.js` opens collapsed under the same 560px — the acceptance viewports start at 640. */

@media (max-width: 560px) {
  /* Same 288 as the desktop panel, but clamped: on a screen narrower than 312px it would
     otherwise run under the ☰ in the same corner and off the far edge. */
  #panel { width: min(288px, calc(100% - 24px)); }
  #titlebar { max-width: calc(100% - 84px); }
  #region { max-width: 12ch; }
  /* An open panel reaches back across the titlebar, and half a "Choose region" button sticking
     out from behind it is neither usable nor honest about being covered. Close the panel and
     it is there again — which is also the only way to press it. */
  body.panel-open #titlebar { display: none; }

  /* Both of these live at the bottom, and at 390 px the centred toast lands squarely on top of
     the stats line. Stack them instead. */
  #status { left: 12px; right: 12px; bottom: 52px; transform: none; text-align: center; }

  /* Both of these are 340px wide or centred on a `left: 50%`, which overflows a 360px phone. */
  #search-panel { width: auto; left: 12px; right: 12px; }
  #picker-actions {
    left: 12px; right: 12px; transform: none;
    flex-wrap: wrap; justify-content: center;
  }

  /* A finger is coarser than a cursor: a 96px dial gives the elevation arc barely 60px of
     usable arc, and the azimuth compass a 30px radius. */
  .dials canvas { height: 120px; }

  #stats { max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
