/* =========================================================================
   Touge Assist – design tokens
   Spacing is a strict 4px scale, radii/typography/controls are tokenised so
   every surface, gap and control is derived from the same set of values.
   ========================================================================= */
:root {
  color-scheme: light dark;

  /* palette */
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #1b1f24;
  --text-dim: #5b6470;
  --border: #e0e4e9;
  --accent: #2f7bd8;
  --accent-2: #f59e0b;
  --on-accent: #ffffff;
  --danger: #d64545;
  --danger-bg: #fdecec;
  --danger-border: #f4c9c9;
  --danger-text: #a12727;
  --shadow: 0 1px 2px rgba(20, 24, 31, .06), 0 2px 6px rgba(20, 24, 31, .08);

  /* spacing – 4px scale */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* radii – sharp, terminal-style boxes everywhere */
  --r-sm: 0px;
  --r-md: 0px;
  --r-lg: 0px;
  --r-pill: 0px;

  /* layout */
  --maxw: 1400px;
  --gutter: 20px;
  --bar-h: 56px;

  /* typography */
  --fs-label: 11px;
  --fs-sm: 12px;
  --fs-cap: 13px;
  --fs-base: 14px;
  --fs-lg: 15px;
  --fs-xl: 18px;

  /* controls */
  --control-py: 9px;
  --control-px: 14px;
}

:root[data-theme="dark"] {
  /* OLED black, plain white text, basic accent colours – no green anywhere. */
  --bg: #000000;
  --surface: #0a0a0a;
  --surface-2: #161616;
  --text: #e8e8e8;
  --text-dim: #8a8a8a;
  --border: #2c2c2c;
  --accent: #3b82f6;
  --accent-2: #f59e0b;
  --on-accent: #ffffff;
  --danger: #ff5c5c;
  --danger-bg: #2a0e0e;
  --danger-border: #5c1f1f;
  --danger-text: #ff9d9d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .6), 0 2px 8px rgba(0, 0, 0, .65);
}

/* No-JS / pre-paint fallback (the inline head script normally sets data-theme first). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --surface: #0a0a0a;
    --surface-2: #161616;
    --text: #e8e8e8;
    --text-dim: #8a8a8a;
    --border: #2c2c2c;
    --accent: #3b82f6;
    --accent-2: #f59e0b;
    --on-accent: #ffffff;
    --danger: #ff5c5c;
    --danger-bg: #2a0e0e;
    --danger-border: #5c1f1f;
    --danger-text: #ff9d9d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .6), 0 2px 8px rgba(0, 0, 0, .65);
  }
}

/* =========================================================================
   Base
   ========================================================================= */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: var(--fs-lg) / 1.5 "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
a { color: var(--accent); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* Shared width + gutter for every top-level block */
.control-strip,
.readout,
.layout,
.stats-panel,
.export-panel,
.app-foot {
  max-width: var(--maxw);
  margin-inline: auto;
  padding-inline: var(--gutter);
}

/* Section heading – one style for every section label */
.stats-panel h2,
.export-panel h2,
.charts-toolbar-label {
  font-size: var(--fs-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-dim);
}
.stats-panel h2,
.export-panel h2 { margin: var(--sp-6) 0 var(--sp-3); }

/* =========================================================================
   Controls – one shared look for every button
   ========================================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--control-py) var(--control-px);
  font-size: var(--fs-base);
  font-family: inherit;
  text-transform: uppercase;
  letter-spacing: .04em;
  line-height: 1.2;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn.ghost { background: transparent; }
.btn.small { padding: calc(var(--control-py) - 2px) var(--sp-3); font-size: var(--fs-cap); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }

.btn-record {
  display: inline-flex;
  align-items: center;
  padding: var(--control-py) 20px;
  font-size: var(--fs-lg);
  font-family: inherit;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  line-height: 1.2;
  white-space: nowrap;
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
}
.btn-record[data-state="recording"],
.btn-record[data-state="paused"] { background: var(--danger); }
.btn-record::before {
  content: "";
  width: 9px;
  height: 9px;
  margin-right: var(--sp-2);
  border-radius: 50%;
  background: currentColor;
}
body.is-recording .btn-record::before { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }

/* While a recording is in progress (recording or paused), keep the UI focused
   on the map + live readout: demo/discard, export and the charts stay hidden. */
body.is-active #btn-demo,
body.is-active #btn-discard,
body.is-active .export-panel,
body.is-active .charts-panel { display: none; }
@media (min-width: 1000px) {
  body.is-active .layout { grid-template-columns: 1fr; }
}

.icon-btn {
  padding: var(--sp-2) var(--sp-3);
  min-width: 44px;
  font-size: var(--fs-sm);
  font-family: inherit;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
  line-height: 1.2;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Segmented control */
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
}
.seg button {
  padding: calc(var(--control-py) - 2px) var(--sp-4);
  font-size: var(--fs-cap);
  font-family: inherit;
  text-transform: uppercase;
  letter-spacing: .03em;
  line-height: 1.2;
  border: 0;
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
}
.seg button:hover:not(.active) { color: var(--accent); }
.seg button.active { background: var(--accent); color: var(--on-accent); }

/* =========================================================================
   App bar
   ========================================================================= */
.app-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--sp-3);
  padding-block: var(--sp-3);
  padding-top: max(var(--sp-3), env(safe-area-inset-top));
  /* full-bleed bar, content aligned to the same --maxw column as everything else */
  padding-inline: max(var(--gutter), calc((100% - var(--maxw)) / 2));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 1200;
}
.brand { display: flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-xl); font-weight: 700; }
.brand-mark { width: 22px; height: 22px; flex: none; }
.theme-btn { min-width: 64px; }

/* =========================================================================
   Banner
   ========================================================================= */
.banner {
  /* Unlike the other top-level blocks, the banner paints its own background/border,
     so it needs a real inline margin (not padding-inline) to sit inset from the
     viewport edge the same way their content does – matching --gutter directly on
     narrow screens, and the same column edge as everything else once --maxw kicks in. */
  margin-block-start: var(--sp-3);
  margin-inline: max(var(--gutter), calc((100% - var(--maxw)) / 2 + var(--gutter)));
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-cap);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
  color: var(--text);
}
.banner.error {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger-text);
}

/* =========================================================================
   Control strip + live readout (one visual block)
   ========================================================================= */
.control-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-3);
  padding-block: var(--sp-4) var(--sp-3);
}
.readout { padding-block: 0; }

/* =========================================================================
   Metric boxes – shared by live readout and summary
   ========================================================================= */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(104px, 1fr));
  gap: var(--sp-3);
}
.metric {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-width: 0;
  padding: var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.metric-v {
  font-size: var(--fs-xl);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.metric-v small { margin-left: 2px; font-size: var(--fs-label); font-weight: 500; color: var(--text-dim); }
.metric-l {
  font-size: var(--fs-label);
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* =========================================================================
   Layout: map + charts
   ========================================================================= */
.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--sp-4);
  padding-block: var(--sp-4) 0;
}
@media (min-width: 1000px) {
  .layout { grid-template-columns: 1fr 1fr; }
}

.map-panel { position: relative; }
@media (min-width: 1000px) {
  /* Must come after the unconditional `position: relative` above, or that
     later rule wins the cascade on `position` while this rule's `top` still
     applies – shifting the (no-longer-sticky) box down instead of sticking it. */
  .map-panel { position: sticky; top: calc(var(--bar-h) + var(--sp-4)); }
}
#map {
  width: 100%;
  height: 56vh;
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--surface-2);
  z-index: 0;
}
@media (min-width: 1000px) {
  /* .layout defaults to align-items: stretch, so both columns share the row's
     height – matching the charts panel's natural (content-driven) height. */
  #map { height: 100%; }
}

.map-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: var(--sp-6);
  text-align: center;
  font-size: var(--fs-base);
  color: var(--text-dim);
}

/* Map overlays */
.map-legend,
.map-recenter {
  position: absolute;
  z-index: 1000;
  box-shadow: var(--shadow);
}
.map-legend {
  left: var(--sp-3);
  bottom: var(--sp-3);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  font-size: var(--fs-label);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
}
.lg-bar { display: inline-block; width: 120px; height: 8px; border-radius: var(--r-sm); }
.map-recenter { right: var(--sp-3); top: var(--sp-3); background: var(--surface); }

/* MapLibre chrome, themed to match */
.maplibregl-map { font: inherit; }
.maplibregl-ctrl-attrib { font-size: var(--fs-label); }
.maplibregl-ctrl-attrib.maplibregl-compact { background: var(--surface); color: var(--text-dim); }
.maplibregl-ctrl-group { border-radius: var(--r-md); overflow: hidden; }
:root[data-theme="dark"] .maplibregl-ctrl-group { background: var(--surface); }
:root[data-theme="dark"] .maplibregl-ctrl-group button + button { border-top-color: var(--border); }
:root[data-theme="dark"] .maplibregl-ctrl-group button span { filter: invert(1); }
.map-cursor-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, .4);
}

/* =========================================================================
   Charts
   ========================================================================= */
.charts-toolbar {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}
.chart-card {
  padding: var(--sp-3);
  margin-bottom: var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.chart-card:last-child { margin-bottom: 0; }
.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-2);
}
.chart-head h3 { font-size: var(--fs-base); font-weight: 600; }
.chart-head .unit { color: var(--text-dim); font-weight: 400; }
.chart-wrap { position: relative; height: 190px; }
.chart-empty { margin-top: var(--sp-2); font-size: var(--fs-cap); color: var(--text-dim); text-align: center; }

/* =========================================================================
   Summary + export
   ========================================================================= */
.export-panel { padding-bottom: var(--sp-6); }
.export-buttons { display: flex; flex-wrap: wrap; gap: var(--sp-3); }
.hint { margin-top: var(--sp-3); font-size: var(--fs-sm); color: var(--text-dim); }

/* =========================================================================
   Footer
   ========================================================================= */
.app-foot {
  padding-block: var(--sp-6) calc(var(--sp-6) + env(safe-area-inset-bottom));
  text-align: center;
  font-size: var(--fs-sm);
  line-height: 1.7;
  color: var(--text-dim);
}
.app-foot a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.app-foot a:hover { color: var(--accent); }

/* =========================================================================
   Mobile
   ========================================================================= */
@media (max-width: 620px) {
  :root { --gutter: 14px; }
  .brand { font-size: var(--fs-lg); }
  .control-strip { gap: var(--sp-2); }
  .btn-record { flex: 1 1 100%; justify-content: center; }
  .control-strip .btn { flex: 1 1 auto; }
  .metric-grid { grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: var(--sp-2); }
  .metric { padding: var(--sp-2) var(--sp-3); }
  .metric-v { font-size: var(--fs-lg); }
  #map { height: 52vh; min-height: 300px; }
  .export-buttons { gap: var(--sp-2); }
  .export-buttons .btn { flex: 1 1 calc(50% - var(--sp-1)); }
  .chart-wrap { height: 168px; }
}

@media (hover: none) {
  .btn:hover:not(:disabled),
  .icon-btn:hover,
  .seg button:hover:not(.active) { border-color: var(--border); color: inherit; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
