/* Layout: sticky top bar; three panes on desktop (section tree | you-are-here + keypad | detail), stacked on phones. */
:root {
  --bg: #eef0ec;
  --surface: #fbfcfa;
  --surface-2: #e4e8e2;
  --ink: #1b2220;
  --muted: #56625d;
  --line: #cfd6cf;
  --accent: #a4570a;          /* "you are here" amber, readable on light */
  --accent-soft: #f6e6cf;
  --danger: #b3261e;
  --danger-soft: #f8dcd8;
  --on: #1f7a3a;
  --on-soft: #d8efdd;

  --plastic: #2a2e30;         /* keypad body */
  --plastic-edge: #1a1d1e;
  --key: #f1f1ec;
  --key-ink: #202425;
  --key-shadow: #9fa39d;
  --lcd: #b9cc86;
  --lcd-ink: #1d2810;
  --lcd-dim: #8fa262;
  --led-off: #3d4244;
  --led-ready: #39d353;
  --led-armed: #ff4b3e;
  --led-trouble: #ffb020;

  --font-body: "Barlow", "Segoe UI", system-ui, sans-serif;
  --font-label: "Barlow Condensed", "Arial Narrow", "Segoe UI", sans-serif;
  --font-mono: "Share Tech Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;

  --radius: 6px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121615; --surface: #191e1c; --surface-2: #222826; --ink: #e5ebe7; --muted: #9aa7a1; --line: #2f3734;
    --accent: #f0a640; --accent-soft: #3a2a14; --danger: #ff8a80; --danger-soft: #3d1c19; --on: #6fd38c; --on-soft: #183324;
    --plastic: #0d0f10; --plastic-edge: #000; --key: #d9dad4; --key-ink: #161a1b; --key-shadow: #5c605a;
    --lcd: #a7bb74; --lcd-ink: #182109; --lcd-dim: #7e9150; --led-off: #2c3133;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121615; --surface: #191e1c; --surface-2: #222826; --ink: #e5ebe7; --muted: #9aa7a1; --line: #2f3734;
  --accent: #f0a640; --accent-soft: #3a2a14; --danger: #ff8a80; --danger-soft: #3d1c19; --on: #6fd38c; --on-soft: #183324;
  --plastic: #0d0f10; --plastic-edge: #000; --key: #d9dad4; --key-ink: #161a1b; --key-shadow: #5c605a;
  --lcd: #a7bb74; --lcd-ink: #182109; --lcd-dim: #7e9150; --led-off: #2c3133;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10; background: var(--surface);
}
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand h1 { margin: 0; font-family: var(--font-label); font-weight: 700; font-size: 1.35rem; letter-spacing: 0.02em; }
.brand .model { font-family: var(--font-mono); color: var(--muted); font-size: 0.85rem; }
.search { flex: 1 1 260px; display: flex; gap: 8px; max-width: 520px; margin-left: auto; position: relative; }
.search input {
  flex: 1; min-width: 0; padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg);
}
.search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 6;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.18); max-height: 60vh; overflow: auto; padding: 4px;
}
.search-results button {
  display: flex; gap: 10px; width: 100%; text-align: left; border: 0; background: none; padding: 7px 8px; border-radius: 4px; cursor: pointer;
}
.search-results button:hover, .search-results button.is-active { background: var(--accent-soft); }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(230px, 280px) minmax(0, 1fr);
  gap: 20px;
  padding-block: 20px 40px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  max-width: 1480px; margin: 0 auto;
}
.main { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 20px; align-content: start; min-width: 0; }
.main > * { min-width: 0; }
.where { grid-column: 1; }
.keypad-wrap { grid-column: 2; grid-row: 1 / span 2; }
.detail { grid-column: 1; }

/* ---------- tree ---------- */
.tree {
  align-self: start; position: sticky; top: calc(env(safe-area-inset-top, 0px) + 70px);
  max-height: calc(100vh - 90px); overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 6px;
}
.tree-toggle { display: none; }
.tree h2 {
  font-family: var(--font-label); text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.78rem;
  color: var(--muted); margin: 10px 8px 4px; font-weight: 600;
}
.tree details { margin: 0; }
.tree summary {
  list-style: none; cursor: pointer; padding: 5px 8px; border-radius: 4px; font-weight: 600;
  display: flex; justify-content: space-between; align-items: center;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary::after { content: "+"; font-family: var(--font-mono); color: var(--muted); }
.tree details[open] > summary::after { content: "\2212"; }
.tree summary:hover { background: var(--surface-2); }
.tree ul { list-style: none; margin: 0 0 6px; padding: 0 0 0 6px; }
.tree li button {
  display: flex; gap: 8px; align-items: baseline; width: 100%; text-align: left; border: 0; background: none;
  padding: 4px 8px; border-radius: 4px; cursor: pointer; color: var(--ink);
}
.tree li button:hover { background: var(--surface-2); }
.tree li button.is-current { background: var(--accent-soft); color: var(--ink); box-shadow: inset 3px 0 0 var(--accent); }
.code {
  font-family: var(--font-mono); font-size: 0.86rem; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums;
}
.is-current .code { color: var(--accent); }

/* ---------- where am I ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.eyebrow {
  font-family: var(--font-label); text-transform: uppercase; letter-spacing: 0.1em; font-size: 0.78rem; color: var(--muted); font-weight: 600; margin: 0 0 6px;
}
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; margin: 0 0 10px; padding: 0; list-style: none; }
.crumbs li { display: flex; align-items: center; gap: 6px; }
.crumbs li + li::before { content: "\203A"; color: var(--muted); }
.crumbs button { border: 0; background: none; padding: 2px 4px; border-radius: 4px; cursor: pointer; color: var(--ink); }
.crumbs button:hover { background: var(--surface-2); }
.crumbs li:last-child button { color: var(--accent); font-weight: 600; }
.waiting { font-size: 1.12rem; margin: 0 0 14px; text-wrap: balance; }
.waiting strong { color: var(--accent); }
.trail-label { font-size: 0.85rem; color: var(--muted); margin: 0 0 6px; }
.trail { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 8px; margin: 0; padding: 0; list-style: none; }
.trail li { display: grid; justify-items: center; gap: 2px; }
.keycap {
  font-family: var(--font-mono); font-size: 1rem; min-width: 34px; padding: 4px 8px; text-align: center;
  background: var(--key); color: var(--key-ink); border-radius: 5px; box-shadow: 0 2px 0 var(--key-shadow); border: 1px solid var(--key-shadow);
}
.trail li:last-child .keycap { outline: 2px solid var(--accent); outline-offset: 1px; }
.trail small { font-size: 0.72rem; color: var(--muted); max-width: 90px; text-align: center; line-height: 1.15; }
.led-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 14px 0 0; font-size: 0.86rem; color: var(--muted); }
.led-legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--led-off); }
.dot.ready { background: var(--led-ready); }
.dot.armed { background: var(--led-armed); }

/* ---------- keypad ---------- */
.keypad {
  background: linear-gradient(180deg, var(--plastic), var(--plastic-edge));
  border-radius: 18px; padding: 18px 18px 22px; color: #d8dcd6;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.25), inset 0 1px 0 rgb(255 255 255 / 0.06);
  position: sticky; top: calc(env(safe-area-inset-top, 0px) + 70px);
}
.keypad-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.keypad-brand { font-family: var(--font-label); letter-spacing: 0.25em; font-size: 0.8rem; color: #9aa09a; }
.leds { display: flex; gap: 12px; }
.led { display: grid; justify-items: center; gap: 3px; font-family: var(--font-label); font-size: 0.66rem; letter-spacing: 0.08em; color: #9aa09a; }
.led i { width: 12px; height: 12px; border-radius: 50%; background: var(--led-off); box-shadow: inset 0 1px 2px rgb(0 0 0 / 0.6); }
.led.on.ready i { background: var(--led-ready); box-shadow: 0 0 8px var(--led-ready); }
.led.on.armed i { background: var(--led-armed); box-shadow: 0 0 8px var(--led-armed); }
.led.on.trouble i { background: var(--led-trouble); box-shadow: 0 0 8px var(--led-trouble); }
.led.on.ac i { background: var(--led-ready); box-shadow: 0 0 6px var(--led-ready); }
.led.flash i { animation: blink 0.6s steps(1) infinite; }
@keyframes blink { 50% { background: var(--led-off); box-shadow: none; } }
.lcd {
  background: var(--lcd); color: var(--lcd-ink); border-radius: 6px; padding: 10px 12px;
  font-family: var(--font-mono); font-size: 1.15rem; line-height: 1.35; letter-spacing: 0.06em;
  box-shadow: inset 0 2px 6px rgb(0 0 0 / 0.35); border: 3px solid #111;
  white-space: pre; overflow: hidden; min-height: 3.6em;
}
.lcd.flash { animation: lcdflash 0.25s linear 2; }
@keyframes lcdflash { 50% { background: var(--lcd-dim); } }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 16px; }
.keys button {
  font-family: var(--font-mono); font-size: 1.35rem; padding: 10px 0; border-radius: 9px; border: 0; cursor: pointer;
  background: var(--key); color: var(--key-ink); box-shadow: 0 3px 0 var(--key-shadow);
  touch-action: manipulation;
}
.keys button:active, .keys button.pressed { transform: translateY(2px); box-shadow: 0 1px 0 var(--key-shadow); }
.keys .scroll { display: flex; gap: 10px; grid-column: 1 / -1; }
.keys .scroll button { flex: 1; font-size: 1rem; }
.keypad-foot { display: flex; justify-content: space-between; gap: 10px; margin-top: 14px; font-size: 0.8rem; color: #9aa09a; }
.keypad-foot button { background: none; border: 1px solid #555b58; color: #cfd3cd; border-radius: 5px; padding: 4px 10px; cursor: pointer; }
.keypad-note { margin: 10px 2px 0; font-size: 0.85rem; color: var(--muted); }

/* ---------- detail ---------- */
.detail-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; }
.detail-head h2 { margin: 0; font-family: var(--font-label); font-size: 1.7rem; font-weight: 700; line-height: 1.1; text-wrap: balance; }
.detail-head .big-code { font-family: var(--font-mono); font-size: 1.4rem; color: var(--accent); }
.badge {
  display: inline-block; font-family: var(--font-label); font-size: 0.78rem; letter-spacing: 0.06em; text-transform: uppercase;
  padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-weight: 600;
}
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.detail p.desc { max-width: 68ch; margin: 10px 0 0; }
.detail h3 { font-family: var(--font-label); font-size: 1.05rem; letter-spacing: 0.04em; margin: 22px 0 8px; text-transform: uppercase; color: var(--muted); }
.tips { margin: 12px 0 0; padding: 10px 14px 10px 30px; background: var(--surface-2); border-radius: var(--radius); max-width: 72ch; }
.tips li + li { margin-top: 4px; }
.picker { display: flex; flex-wrap: wrap; gap: 6px; }
.picker button {
  font-family: var(--font-mono); font-size: 0.9rem; border: 1px solid var(--line); background: var(--bg); border-radius: 5px; padding: 4px 8px; cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.picker button:hover { border-color: var(--accent); }
.picker button.is-current { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.picker-label { display: block; font-size: 0.86rem; color: var(--muted); margin-bottom: 6px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 0.93rem; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-family: var(--font-label); font-weight: 600; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
tbody tr.is-current { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
tbody tr.is-future td { color: var(--muted); }
td.num, th.num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.pill { display: inline-block; min-width: 38px; text-align: center; padding: 1px 8px; border-radius: 99px; font-family: var(--font-label); font-weight: 700; font-size: 0.8rem; letter-spacing: 0.05em; }
.pill.on { background: var(--on-soft); color: var(--on); }
.pill.off { background: var(--surface-2); color: var(--muted); }
.here { color: var(--accent); font-weight: 600; white-space: nowrap; }
.meaning { color: var(--muted); font-size: 0.86rem; display: block; }
.related { display: flex; flex-wrap: wrap; gap: 8px; }
.related button {
  border: 1px solid var(--line); background: var(--bg); border-radius: 99px; padding: 4px 12px; cursor: pointer; display: inline-flex; gap: 6px; align-items: baseline;
}
.related button:hover { border-color: var(--accent); }
.source { margin-top: 26px; font-size: 0.82rem; color: var(--muted); max-width: 72ch; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .main { grid-template-columns: minmax(0, 1fr); }
  .keypad-wrap { grid-column: 1; grid-row: auto; max-width: 380px; width: 100%; justify-self: center; }
  .keypad { position: static; }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .tree { position: static; max-height: none; }
  .tree-toggle {
    display: flex; justify-content: space-between; width: 100%; border: 0; background: none; padding: 4px 8px; cursor: pointer;
    font-family: var(--font-label); font-weight: 700; font-size: 1rem;
  }
  .tree:not(.is-open) .tree-body { display: none; }
  .search { margin-left: 0; max-width: none; }
  .panel { padding: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .led.flash i, .lcd.flash { animation: none; }
}
.linklike {
  font-family: var(--font-mono); font-size: 0.86rem; border: 0; background: none; color: var(--accent); cursor: pointer; padding: 1px 2px; text-decoration: underline; text-underline-offset: 2px;
}
td.num .meaning { white-space: normal; font-family: var(--font-body); min-width: 14ch; }
.search-empty { padding: 8px; }
