:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --border: #dcd9d2;
  --text: #16181b;
  --muted: #5d6168;
  --accent: #1f5fbf;
  --accent-text: #ffffff;

  --text-danger: #b3140f;
  --bg-danger: #fde3e1;
  --text-warning: #8a5300;
  --bg-warning: #fff0cc;
  --text-info: #16181b;
  --bg-info: #e9eef5;
  --text-personal: #4b2aa8;

  --lao: "Noto Sans Lao", "Noto Sans Lao UI", "Phetsarath OT", "Saysettha OT", "Lao UI", system-ui, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1114;
    --surface: #181b20;
    --border: #2c3139;
    --text: #f1f2f4;
    --muted: #a3a9b3;
    --accent: #5b9bff;
    --accent-text: #0b1220;

    --text-danger: #ff6b63;
    --bg-danger: #3a0f0d;
    --text-warning: #ffc44d;
    --bg-warning: #33260a;
    --text-info: #f1f2f4;
    --bg-info: #1c232d;
    --text-personal: #b9a4ff;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--lao);
  font-size: 17px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

small { font-size: 0.72em; color: var(--muted); font-weight: 400; }
.muted { color: var(--muted); font-size: 0.9rem; }
.mono { font-family: ui-monospace, "Cascadia Mono", Menlo, monospace; font-size: 0.8rem; white-space: pre-wrap; }
[hidden] { display: none !important; }

/* ── layout ───────────────────────────────────────────────────── */

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; }
.pills { display: flex; gap: 6px; }
.pill {
  font-size: 0.78rem; padding: 2px 10px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted); white-space: nowrap;
}
.pill.on { color: var(--accent); border-color: var(--accent); }

main {
  max-width: 640px; margin: 0 auto;
  padding: 12px 16px calc(env(safe-area-inset-bottom, 0px) + 96px);
}

h2 { font-size: 1.05rem; margin: 22px 0 8px; }

.row { display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0; }
.row > .btn { flex: 1 1 140px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px;
}

/* ── alert ────────────────────────────────────────────────────── */

.alert {
  min-height: 42vh;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  text-align: center; gap: 6px;
  padding: 20px 16px; border-radius: 18px;
  border: 2px solid var(--border);
  background: var(--surface);
  transition: background-color 0.15s, border-color 0.15s;
}
.alert-tier { font-size: 1rem; font-weight: 700; letter-spacing: 0.02em; min-height: 1.5em; }
.alert-text {
  font-size: clamp(2.2rem, 11vw, 4.2rem); font-weight: 800; line-height: 1.25;
  overflow-wrap: anywhere;
}
.alert-meta { color: var(--muted); font-size: 0.95rem; }

.alert.tier-idle .alert-text { font-size: clamp(1.6rem, 7vw, 2.4rem); font-weight: 600; color: var(--muted); }
.alert.tier-danger { background: var(--bg-danger); border-color: var(--text-danger); color: var(--text-danger); }
.alert.tier-caution { background: var(--bg-warning); border-color: var(--text-warning); color: var(--text-warning); }
.alert.tier-info { background: var(--bg-info); color: var(--text-info); }
.alert.tier-danger .alert-meta, .alert.tier-caution .alert-meta { color: inherit; font-weight: 600; } /* no opacity: keeps WCAG AA contrast */
.alert.flash { animation: flash 0.5s ease-out 3; }
@keyframes flash { 50% { filter: brightness(1.35); transform: scale(1.01); } }
@media (prefers-reduced-motion: reduce) { .alert.flash { animation: none; } }

.meter { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-top: 10px; }
#meter-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.1s linear; }

/* ── buttons & inputs ─────────────────────────────────────────── */

.btn {
  font: inherit; font-weight: 700; font-size: 1.1rem;
  min-height: 56px; padding: 10px 16px;
  border-radius: 14px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  cursor: pointer;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-primary.stop { background: var(--text-danger); border-color: var(--text-danger); color: #fff; }
.btn-wide { width: 100%; }
.btn-ghost {
  font: inherit; margin-top: 8px; padding: 6px 14px; border-radius: 999px;
  background: transparent; color: inherit; border: 1px solid currentColor; font-weight: 600;
}
.btn:focus-visible, .btn-ghost:focus-visible, input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; }
.field input[type=text], .field input[type=url], .field input[type=password] {
  font: inherit; padding: 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); width: 100%;
}
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; margin: 10px 0; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }

.seg { border: 0; padding: 0; margin: 10px 0; display: flex; flex-wrap: wrap; gap: 8px; }
.seg legend { margin-bottom: 6px; }
.seg label { flex: 1 1 90px; }
.seg input { position: absolute; opacity: 0; }
.seg span {
  display: block; text-align: center; padding: 10px; border-radius: 10px;
  border: 1px solid var(--border); font-weight: 600;
}
.seg input:checked + span { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.seg input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ── narration ───────────────────────────────────────────────── */

.narration { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; }
.narration-text { font-size: 1.5rem; font-weight: 700; line-height: 1.4; }

/* ── lists ───────────────────────────────────────────────────── */

.events { list-style: none; padding: 0; margin: 0; }
.events li {
  display: flex; justify-content: space-between; gap: 10px; align-items: baseline;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.events li .lbl { font-weight: 700; font-size: 1.1rem; }
.events li.tier-danger .lbl { color: var(--text-danger); }
.events li.tier-caution .lbl { color: var(--text-warning); }
.events li .when { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.tag-personal { color: var(--text-personal); font-size: 0.75rem; margin-left: 6px; }

.sounds { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.sounds li { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; }
.sounds .head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.sounds .name { font-weight: 700; font-size: 1.15rem; overflow-wrap: anywhere; }
.sounds .sim { font-family: ui-monospace, Menlo, monospace; font-weight: 700; color: var(--muted); }
.sounds .sim.hit { color: var(--text-personal); }
.sounds .actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.sounds .actions input { flex: 1 1 160px; accent-color: var(--accent); }
.sounds .del { font: inherit; background: none; border: 1px solid var(--border); color: var(--text-danger); border-radius: 10px; padding: 6px 12px; }

.dots { display: flex; gap: 10px; justify-content: center; margin: 14px 0 4px; }
.dots i { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--accent); }
.dots i.done { background: var(--accent); }
.dots i.busy { animation: pulse 0.6s infinite alternate; }
@keyframes pulse { to { background: var(--accent); } }
.reg-status { text-align: center; font-size: 1.1rem; font-weight: 600; min-height: 3em; }

.debug { margin-top: 20px; }
.debug summary { cursor: pointer; color: var(--muted); }
.privacy { margin-top: 24px; text-align: center; }

/* ── tabs ─────────────────────────────────────────────────────── */

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabs button {
  font: inherit; font-weight: 700; background: none; border: 0; color: var(--muted);
  padding: 10px 4px; display: flex; flex-direction: column; align-items: center; min-height: 60px;
}
.tabs button.active { color: var(--accent); box-shadow: inset 0 3px 0 var(--accent); }

/* ── icons in the alert card ─────────────────────────────────── */

.alert-icon { font-size: clamp(3rem, 16vw, 5rem); line-height: 1; min-height: 1em; }
.alert.tier-idle .alert-icon { display: none; }

/* ── full-screen alert (danger / caution) ────────────────────── */

.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; text-align: center;
  padding: calc(env(safe-area-inset-top, 0px) + 24px) 20px calc(env(safe-area-inset-bottom, 0px) + 24px);
  background: var(--bg-info); color: var(--text-info);
}
.overlay.tier-danger { background: var(--bg-danger); color: var(--text-danger); box-shadow: inset 0 0 0 10px var(--text-danger); }
.overlay.tier-caution { background: var(--bg-warning); color: var(--text-warning); box-shadow: inset 0 0 0 10px var(--text-warning); }
.overlay.flash { animation: flash 0.5s ease-out 3; }
@media (prefers-reduced-motion: reduce) { .overlay.flash { animation: none; } }

.ov-tier { font-size: clamp(1.4rem, 6vw, 2rem); font-weight: 800; letter-spacing: 0.02em; }
.ov-icon { font-size: clamp(6rem, 38vw, 12rem); line-height: 1.05; }
.ov-text { font-size: clamp(2.4rem, 12vw, 5rem); font-weight: 800; line-height: 1.25; overflow-wrap: anywhere; max-width: 18ch; }
.ov-meta { font-size: 1.05rem; font-weight: 600; }
.ov-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 18px; width: min(100%, 520px); }
.ov-btn { flex: 1 1 180px; min-height: 64px; font-size: 1.25rem; background: var(--surface); color: var(--text); border: 2px solid currentColor; }
.ov-ghost { background: transparent; color: inherit; }

/* ── banner ──────────────────────────────────────────────────── */

.banner {
  margin-top: 10px; padding: 10px 14px; border-radius: 12px;
  background: var(--bg-warning); color: var(--text-warning); font-weight: 700;
  border: 1px solid var(--text-warning);
}
.banner small { display: block; color: inherit; font-weight: 500; }

/* ── first-run guide ─────────────────────────────────────────── */

.guide {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; text-align: center; overflow-y: auto;
  padding: calc(env(safe-area-inset-top, 0px) + 24px) 20px calc(env(safe-area-inset-bottom, 0px) + 24px);
  background: var(--bg); color: var(--text);
}
.guide .row { width: min(100%, 520px); }
.guide-pic { font-size: clamp(5rem, 30vw, 9rem); line-height: 1.1; }
.guide-text { font-size: clamp(1.6rem, 7vw, 2.4rem); font-weight: 800; line-height: 1.35; max-width: 20ch; }
.guide-extra { display: grid; gap: 10px; width: min(100%, 520px); }
.guide-extra .btn { width: 100%; }
.guide-extra .tier-danger { background: var(--bg-danger); color: var(--text-danger); border-color: var(--text-danger); }
.guide-extra .tier-caution { background: var(--bg-warning); color: var(--text-warning); border-color: var(--text-warning); }
.guide-extra .tier-info { background: var(--bg-info); color: var(--text-info); }
.dots i.on { background: var(--accent); }

/* ── threshold tuning list ───────────────────────────────────── */

.thresholds { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 8px; }
.thresholds li { border-bottom: 1px solid var(--border); padding: 8px 0; }
.thresholds .t-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.thresholds .t-name { font-weight: 700; }
.thresholds .t-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.thresholds input { flex: 1 1 160px; accent-color: var(--accent); }
.thresholds .t-val { font-family: ui-monospace, Menlo, monospace; min-width: 3.2em; text-align: right; }
.thresholds .t-changed .t-val { color: var(--accent); font-weight: 700; }
.thresholds .t-suggest {
  font: inherit; font-size: 0.85rem; border-radius: 999px; padding: 4px 10px;
  border: 1px solid var(--text-warning); background: var(--bg-warning); color: var(--text-warning);
}
.sounds .add { font: inherit; font-weight: 700; background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 8px 16px; }

.viz { width: 100%; height: 120px; display: block; margin-top: 10px; border-radius: 10px; image-rendering: pixelated; }

/* ═══════════════════════════════════════════════════════════════════════════
   Glass layer (liquid-glass, selective). Glass is used where depth helps:
   the top bar, tab bar, the live alert, full-screen alerts/modes and the mode
   tiles. Dense content (lists, settings, forms) sits on calmer, mostly
   opaque panels so text keeps high contrast. Primary #1856FF, WCAG 2.2 AA,
   explicit hover / active / focus-visible / disabled states.
   ═══════════════════════════════════════════════════════════════════════════ */

@font-face {
  font-family: "Plus Jakarta Sans"; font-style: normal; font-weight: 400 800; font-display: swap;
  src: url("fonts/plus-jakarta-sans-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212;
}

:root {
  --lao: "Plus Jakarta Sans", "Noto Sans Lao", "Noto Sans Lao UI", "Phetsarath OT", "Saysettha OT", "Lao UI", system-ui, sans-serif;

  --accent: #1856ff;
  --accent-text: #ffffff;
  --text: #141414;
  --muted: #4a5060;

  --g-base: linear-gradient(165deg, #fff4ec 0%, #f6efff 48%, #e8f8f1 100%);
  --g-glow-1: rgba(255, 138, 76, 0.34);     /* coral / amber */
  --g-glow-2: rgba(168, 85, 247, 0.26);     /* violet */
  --g-glow-3: rgba(7, 202, 107, 0.26);      /* mint */
  --g-glow-4: rgba(24, 86, 255, 0.2);       /* blue, one light of four */
  --g-fallback: #f6efff;
  --go: #07ca6b;                            /* "start / safe" */
  --go-text: #04210f;
  --stop-text: #ffffff;

  --g-glass: rgba(255, 255, 255, 0.56);     /* hero / chrome */
  --g-panel: rgba(255, 255, 255, 0.82);     /* content */
  --g-field: rgba(255, 255, 255, 0.95);
  --g-hi: rgba(255, 255, 255, 0.95);        /* luminous edge, lit side */
  --g-lo: rgba(24, 86, 255, 0.22);          /* luminous edge, shade side */
  --g-line: rgba(20, 30, 70, 0.12);
  --g-shadow: 0 10px 30px -8px rgba(30, 50, 120, 0.28), 0 2px 6px rgba(30, 50, 120, 0.08);
  --g-inset: inset 0 1px 0 rgba(255, 255, 255, 0.9), inset 0 -1px 0 rgba(24, 86, 255, 0.06);
  --g-ring: 0 0 0 2px #fff, 0 0 0 5px var(--accent);

  --g-danger: rgba(255, 232, 230, 0.92);
  --g-warning: rgba(255, 241, 208, 0.94);
  --g-info: rgba(236, 241, 250, 0.9);
  --g-ok: rgba(216, 246, 226, 0.94);
  --text-ok: #0b5d2a;
  --glow-danger: rgba(234, 33, 67, 0.35);
  --glow-warning: rgba(232, 149, 88, 0.4);
  --glow-ok: rgba(7, 202, 107, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #86a8ff;
    --accent-text: #0a1230;
    --text: #f1f4ff;
    --muted: #bcc4d9;

    --g-base: linear-gradient(165deg, #140c1e 0%, #1a1030 50%, #0c1a1a 100%);
    --g-glow-1: rgba(255, 120, 60, 0.3);
    --g-glow-2: rgba(214, 48, 127, 0.36);
    --g-glow-3: rgba(7, 202, 107, 0.24);
    --g-glow-4: rgba(24, 86, 255, 0.38);
    --stop-text: #2a0705;
    --g-fallback: #1a1030;

    --g-glass: rgba(34, 26, 54, 0.66);
    --g-panel: rgba(30, 23, 46, 0.82);
    --g-field: rgba(12, 8, 22, 0.7);
    --g-hi: rgba(255, 255, 255, 0.28);
    --g-lo: rgba(134, 168, 255, 0.2);
    --g-line: rgba(255, 255, 255, 0.12);
    --g-shadow: 0 12px 34px -8px rgba(0, 0, 0, 0.6), 0 2px 6px rgba(0, 0, 0, 0.3);
    --g-inset: inset 0 1px 0 rgba(255, 255, 255, 0.16), inset 0 -1px 0 rgba(0, 0, 0, 0.25);
    --g-ring: 0 0 0 2px #060a17, 0 0 0 5px var(--accent);

    --g-danger: rgba(62, 14, 14, 0.9);
    --g-warning: rgba(54, 38, 8, 0.9);
    --g-info: rgba(26, 34, 56, 0.86);
    --g-ok: rgba(8, 52, 30, 0.9);
    --text-ok: #7be0a0;
    --glow-danger: rgba(255, 107, 99, 0.35);
    --glow-warning: rgba(255, 196, 77, 0.3);
    --glow-ok: rgba(123, 224, 160, 0.28);
  }
}

html {
  background-color: var(--g-fallback);
  background-image: var(--g-base);
  background-attachment: fixed; background-repeat: no-repeat; background-size: cover;
}
body {
  background: none;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
body::before {          /* gradient mesh: three soft light sources, no filter cost */
  content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(55vmax 55vmax at 4% 2%, var(--g-glow-1), transparent 62%),
    radial-gradient(50vmax 50vmax at 100% 22%, var(--g-glow-2), transparent 62%),
    radial-gradient(55vmax 55vmax at 0% 78%, var(--g-glow-3), transparent 62%),
    radial-gradient(50vmax 50vmax at 96% 100%, var(--g-glow-4), transparent 62%);
}

small, .muted { color: var(--muted); }
h2 { font-size: 0.9rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); font-weight: 700; margin: 26px 2px 10px; }
h2 small { text-transform: none; letter-spacing: 0; }
h2::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 8px; vertical-align: 1px; background: linear-gradient(135deg, #ff8a4c, #d6307f); }

/* ── chrome: top bar + tab bar (glass) ────────────────────────────────── */

.top, .tabs {
  background: var(--g-glass);
  -webkit-backdrop-filter: blur(16px) saturate(160%); backdrop-filter: blur(16px) saturate(160%);
  border: 0; box-shadow: var(--g-inset);
}
.top { border-bottom: 1px solid var(--g-line); }
.tabs { border-top: 1px solid var(--g-line); padding: 6px 10px calc(env(safe-area-inset-bottom, 0px) + 6px); gap: 6px; }
.brand { letter-spacing: -0.01em; font-weight: 800; }
.tabs button { color: var(--muted); border-radius: 16px; min-height: 56px; box-shadow: none; transition: background-color 0.15s, color 0.15s; }
.tabs button.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 35%, transparent); }
.tabs button:active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.tabs button:focus-visible { outline: none; box-shadow: var(--g-ring); }
@media (hover: hover) { .tabs button:hover { background: color-mix(in srgb, var(--accent) 9%, transparent); } }

.pill { background: var(--g-panel); border: 1px solid var(--g-line); color: var(--muted); font-weight: 600; }
.pill.on { color: var(--text-ok); border-color: var(--text-ok); background: color-mix(in srgb, var(--go) 16%, var(--g-panel)); }

/* ── the live alert (hero glass) ──────────────────────────────────────── */

.alert {
  position: relative;
  background: var(--g-glass);
  -webkit-backdrop-filter: blur(18px) saturate(170%); backdrop-filter: blur(18px) saturate(170%);
  border: 0; border-radius: 30px; box-shadow: var(--g-shadow), var(--g-inset);
}
.alert::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit; padding: 1px; pointer-events: none;
  background: linear-gradient(140deg, var(--g-hi), var(--g-lo) 42%, var(--g-lo) 60%, var(--g-hi));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor; mask-composite: exclude;
}
.alert.tier-idle .alert-text, .alert.tier-idle .alert-meta { color: var(--muted); }
.alert.tier-info { background: var(--g-info); }
.alert.tier-danger { background: var(--g-danger); color: var(--text-danger); box-shadow: var(--g-shadow), 0 0 0 2px var(--text-danger), 0 0 44px -4px var(--glow-danger); }
.alert.tier-caution { background: var(--g-warning); color: var(--text-warning); box-shadow: var(--g-shadow), 0 0 0 2px var(--text-warning), 0 0 44px -4px var(--glow-warning); }
.alert.tier-danger::before, .alert.tier-caution::before { display: none; }

.meter { background: var(--g-panel); border: 1px solid var(--g-line); height: 8px; border-radius: 999px; box-shadow: var(--g-inset); }
#meter-bar { background: linear-gradient(90deg, var(--go), #f5c518 60%, #ff5a4f); border-radius: 999px; }
.viz { border: 1px solid var(--g-line); border-radius: 22px; box-shadow: var(--g-shadow); }

/* ── bento tiles: demo + modes (glass, the one place it repeats) ─────── */

.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 8px 0 4px; }
.mode-btn, #btn-demo {
  position: relative; font: inherit; color: var(--text); cursor: pointer;
  background: var(--g-glass);
  -webkit-backdrop-filter: blur(14px) saturate(160%); backdrop-filter: blur(14px) saturate(160%);
  border: 0; border-radius: 24px; box-shadow: var(--g-shadow), var(--g-inset);
  transition: transform 0.14s, box-shadow 0.18s, background-color 0.18s;
}
.mode-btn::before, #btn-demo::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit; padding: 1px; pointer-events: none;
  background: linear-gradient(140deg, var(--g-hi), var(--g-lo) 42%, var(--g-lo) 60%, var(--g-hi));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor; mask-composite: exclude;
}
/* each tile carries its own colour; --tint is an r,g,b triple */
.mode-btn[data-mode="crossing"] { --tint: 7, 202, 107; }
.mode-btn[data-mode="finder"] { --tint: 255, 150, 60; }
.mode-btn[data-mode="guardian"] { --tint: 138, 92, 246; }
#btn-demo { --tint: 255, 90, 110; }
.mode-btn, #btn-demo { background: linear-gradient(160deg, rgba(var(--tint), 0.26), rgba(var(--tint), 0.07)), var(--g-glass); }
.mode-btn { min-height: 108px; padding: 12px 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.mode-btn span { font-size: 1.8rem; line-height: 1; width: 54px; height: 54px; display: grid; place-items: center; border-radius: 18px; background: rgba(var(--tint), 0.3); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4); }
.mode-btn b { font-size: 1rem; }
#btn-demo { width: 100%; min-height: 64px; margin-top: 12px; padding: 12px 18px; font-weight: 700; font-size: 1.05rem; text-align: left; }
.mode-btn:active, #btn-demo:active { transform: scale(0.985); }
.mode-btn:focus-visible, #btn-demo:focus-visible { outline: none; box-shadow: var(--g-shadow), var(--g-ring); }
#btn-demo:disabled { opacity: 0.6; cursor: progress; }
@media (hover: hover) {
  .mode-btn:hover, #btn-demo:hover { transform: translateY(-2px); box-shadow: 0 16px 36px -10px rgba(24, 86, 255, 0.4), var(--g-inset); }
}

/* ── content panels (calm, mostly opaque for dense text) ─────────────── */

.card, .narration, .sounds li, .thresholds, .debug[open], .events li {
  background: var(--g-panel);
  border: 1px solid var(--g-line); border-radius: 20px;
  box-shadow: var(--g-shadow), var(--g-inset);
}
.thresholds, .debug[open] { padding: 12px 14px; }
.events li { margin-bottom: 8px; border-radius: 16px; }
.events li .when { color: var(--muted); }
.thresholds li { border-color: var(--g-line); }

/* ── buttons & fields ─────────────────────────────────────────────────── */

.btn {
  font-weight: 700; min-height: 56px; border-radius: 16px; color: var(--text);
  background: linear-gradient(180deg, var(--g-field), var(--g-panel));
  border: 1px solid var(--g-line); box-shadow: 0 2px 8px rgba(30, 50, 120, 0.12), var(--g-inset);
  transition: transform 0.12s, box-shadow 0.16s, background-color 0.16s;
}
.btn:active { transform: translateY(1px) scale(0.99); box-shadow: inset 0 2px 4px rgba(30, 50, 120, 0.16); }
.btn:focus-visible, .btn-ghost:focus-visible, .field input:focus-visible, .ov-btn:focus-visible { outline: none; box-shadow: var(--g-ring); }
.btn:disabled { opacity: 0.5; box-shadow: none; cursor: not-allowed; }
@media (hover: hover) { .btn:not(:disabled):hover { box-shadow: 0 6px 18px -4px rgba(24, 86, 255, 0.35), var(--g-inset); } }
.btn-primary {
  color: var(--go-text); border-color: rgba(255, 255, 255, 0.45);
  background: linear-gradient(180deg, #3ee08f, var(--go));
  box-shadow: 0 8px 22px -6px rgba(7, 202, 107, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.55);
}
.btn-primary.stop { background: linear-gradient(180deg, color-mix(in srgb, var(--text-danger) 88%, #fff), var(--text-danger)); color: var(--stop-text); box-shadow: 0 8px 22px -6px var(--glow-danger), inset 0 1px 0 rgba(255, 255, 255, 0.35); }
.btn-ghost { border-radius: 999px; }

.field input[type=text], .field input[type=url], .field input[type=password] {
  background: var(--g-field); border: 1px solid var(--g-line); border-radius: 14px; box-shadow: inset 0 1px 3px rgba(30, 50, 120, 0.1);
}
.seg span { background: var(--g-field); border: 1px solid var(--g-line); border-radius: 14px; transition: background-color 0.15s; }
.seg input:checked + span { background: var(--accent); color: var(--accent-text); border-color: var(--accent); box-shadow: 0 6px 16px -6px color-mix(in srgb, var(--accent) 70%, transparent); }
.seg input:focus-visible + span { outline: none; box-shadow: var(--g-ring); }
.sounds .del { background: var(--g-field); border-radius: 12px; border-color: var(--g-line); }
.sounds .add { border-radius: 12px; }

/* ── full-screen surfaces (alert overlay, guide, modes) ──────────────── */

.overlay, .guide, .mode {
  -webkit-backdrop-filter: blur(18px) saturate(170%); backdrop-filter: blur(18px) saturate(170%);
}
.overlay { background: var(--g-info); }
.overlay.tier-danger { background: var(--g-danger); box-shadow: inset 0 0 0 10px var(--text-danger), inset 0 0 80px var(--glow-danger); }
.overlay.tier-caution { background: var(--g-warning); box-shadow: inset 0 0 0 10px var(--text-warning), inset 0 0 80px var(--glow-warning); }
.guide { background: var(--g-panel); }
.ov-btn { background: var(--g-field); border-radius: 20px; border: 1px solid var(--g-line); }
.ov-ghost { background: transparent; }

.mode {
  position: fixed; inset: 0; z-index: 18;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center;
  padding: calc(env(safe-area-inset-top, 0px) + 24px) 20px calc(env(safe-area-inset-bottom, 0px) + 24px);
  background: var(--g-panel); color: var(--text);
}
.mode-title { font-size: 1.1rem; font-weight: 800; letter-spacing: -0.01em; }
.mode-icon { font-size: clamp(5rem, 30vw, 9rem); line-height: 1.05; }
.mode-compass { width: min(78vw, 300px); height: auto; aspect-ratio: 1; filter: drop-shadow(0 8px 24px rgba(24, 86, 255, 0.3)); }
.mode-status { font-size: clamp(2.2rem, 11vw, 4rem); font-weight: 800; line-height: 1.15; letter-spacing: -0.02em; overflow-wrap: anywhere; }
.mode-sub { font-size: 1.1rem; font-weight: 600; max-width: 28ch; }
.mode-note { font-size: 0.95rem; color: var(--muted); max-width: 34ch; }
.mode-exit { margin-top: 14px; min-width: 200px; }

.mode-crossing[data-state="clear"] { background: var(--g-ok); color: var(--text-ok); box-shadow: inset 0 0 90px var(--glow-ok); }
.mode-crossing[data-state="near"] { background: var(--g-warning); color: var(--text-warning); }
.mode-crossing[data-state="close"] { background: var(--g-warning); color: var(--text-warning); box-shadow: inset 0 0 0 10px var(--text-warning), inset 0 0 90px var(--glow-warning); }
.mode-crossing[data-state="back"] { background: var(--g-danger); color: var(--text-danger); box-shadow: inset 0 0 0 12px var(--text-danger), inset 0 0 100px var(--glow-danger); animation: flash 0.5s ease-out infinite; }
.mode-crossing .mode-note, .mode-crossing .mode-sub { color: inherit; }
.mode-guardian { background: rgba(4, 6, 14, 0.97); color: #d5dbea; }
.mode-guardian .mode-status { font-size: clamp(3.5rem, 20vw, 7rem); font-variant-numeric: tabular-nums; font-weight: 300; }
.mode-guardian .mode-note { color: #a9b1c3; }
.mode-guardian.summary .mode-status { font-size: clamp(2rem, 9vw, 3.4rem); font-weight: 800; }
.mode-guardian .btn { background: #141828; color: #d5dbea; border-color: #2a3150; box-shadow: none; }
@media (prefers-reduced-motion: reduce) { .mode-crossing[data-state="back"] { animation: none; } .mode-btn, #btn-demo, .btn { transition: none; } }

/* ── fallbacks: no backdrop-filter, more contrast, less transparency ─── */

@supports not ((-webkit-backdrop-filter: blur(1px)) or (backdrop-filter: blur(1px))) {
  .top, .tabs, .alert, .mode-btn, #btn-demo { background: var(--g-panel); }
  .overlay, .guide, .mode { background: var(--g-panel); }
}
@media (prefers-contrast: more), (prefers-reduced-transparency: reduce) {
  .top, .tabs, .alert, .mode-btn, #btn-demo, .overlay, .guide, .mode, .card, .btn {
    -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
  }
  .top, .tabs, .mode-btn, #btn-demo, .card, .btn, .guide { background: var(--surface) !important; }
  .alert.tier-idle, .alert.tier-info { background: var(--surface); }
  body::before { display: none; }
  * { --g-line: currentColor; }
}
@media (forced-colors: active) { .alert, .card, .mode-btn, #btn-demo, .btn { border: 1px solid CanvasText; } }

@media (prefers-color-scheme: dark) {
  .mode-btn, #btn-demo { background: linear-gradient(160deg, rgba(var(--tint), 0.2), rgba(var(--tint), 0.05)), var(--g-glass); }
}

/* ── primary action always in reach ───────────────────────────────────── */

.actions-bar {
  position: sticky; z-index: 6; margin: 12px -4px 8px; padding: 8px 4px;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 78px);
}
.actions-bar .btn { min-height: 64px; font-size: 1.2rem; }
.actions-bar #btn-start { flex: 2 1 160px; }
.actions-bar #btn-narrate { flex: 1 1 110px; }
.more > summary { cursor: pointer; font-weight: 700; padding: 6px 2px; }
.more .viz { margin-top: 10px; }

/* ── plain high-contrast style: no blur, no gradients, solid colours, thick edges.
      Chosen automatically on slower phones; best in bright sun. ──────────── */

html[data-contrast="high"] {
  --g-fallback: #ffffff; --g-base: none;
  --text: #000000; --muted: #1f1f1f;
  --g-glass: #ffffff; --g-panel: #ffffff; --g-field: #ffffff;
  --g-line: #000000; --g-hi: #000000; --g-lo: #000000;
  --g-shadow: none; --g-inset: none; --g-ring: 0 0 0 3px #fff, 0 0 0 6px #000;
  --accent: #0036c7; --accent-text: #ffffff;
  --text-danger: #7a0000; --text-warning: #5a3500; --text-ok: #064d22;
  --g-danger: #ffe1de; --g-warning: #ffe9b0; --g-info: #eaf0fa; --g-ok: #d4f5e0;
  --glow-danger: transparent; --glow-warning: transparent; --glow-ok: transparent;
  --go: #06722f; --go-text: #ffffff; --stop-text: #ffffff;
}
@media (prefers-color-scheme: dark) {
  html[data-contrast="high"] {
    --g-fallback: #000000;
    --text: #ffffff; --muted: #e6e6e6;
    --g-glass: #000000; --g-panel: #000000; --g-field: #000000;
    --g-line: #ffffff; --g-hi: #ffffff; --g-lo: #ffffff;
    --g-ring: 0 0 0 3px #000, 0 0 0 6px #fff;
    --accent: #9db8ff; --accent-text: #000000;
    --text-danger: #ffb4ae; --text-warning: #ffd470; --text-ok: #8ff0b5;
    --g-danger: #2a0000; --g-warning: #2a1d00; --g-info: #0c0c0c; --g-ok: #00200d;
    --go: #3ddc84; --go-text: #000000; --stop-text: #000000;
  }
}
html[data-contrast="high"], html[data-contrast="high"] body { background: var(--g-fallback); }
html[data-contrast="high"] body::before { display: none; }
html[data-contrast="high"] *, html[data-contrast="high"] *::before {
  -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
}
html[data-contrast="high"] .top, html[data-contrast="high"] .tabs { background: var(--g-panel); border-color: var(--g-line); box-shadow: none; }
html[data-contrast="high"] .top { border-bottom: 2px solid var(--g-line); }
html[data-contrast="high"] .tabs { border-top: 2px solid var(--g-line); }
html[data-contrast="high"] .tabs button.active { background: var(--accent); color: var(--accent-text); box-shadow: none; }
html[data-contrast="high"] .alert,
html[data-contrast="high"] .card, html[data-contrast="high"] .narration, html[data-contrast="high"] .events li,
html[data-contrast="high"] .sounds li, html[data-contrast="high"] .thresholds, html[data-contrast="high"] .debug[open],
html[data-contrast="high"] .mode-btn, html[data-contrast="high"] #btn-demo, html[data-contrast="high"] .viz {
  background: var(--g-panel); border: 2px solid var(--g-line); box-shadow: none;
}
html[data-contrast="high"] .alert::before, html[data-contrast="high"] .mode-btn::before, html[data-contrast="high"] #btn-demo::before { display: none; }
html[data-contrast="high"] .alert.tier-idle { background: var(--g-panel); }
html[data-contrast="high"] .alert.tier-info { background: var(--g-info); }
html[data-contrast="high"] .alert.tier-danger { background: var(--g-danger); border: 6px solid var(--text-danger); box-shadow: none; }
html[data-contrast="high"] .alert.tier-caution { background: var(--g-warning); border: 6px solid var(--text-warning); box-shadow: none; }
html[data-contrast="high"] .mode-btn span { background: transparent; box-shadow: none; }
html[data-contrast="high"] .btn { background: var(--g-panel); color: var(--text); border: 2px solid var(--g-line); box-shadow: none; }
html[data-contrast="high"] .btn-primary { background: var(--go); color: var(--go-text); border: 3px solid var(--g-line); box-shadow: none; }
html[data-contrast="high"] .btn-primary.stop { background: var(--text-danger); color: var(--stop-text); }
html[data-contrast="high"] .btn:focus-visible, html[data-contrast="high"] .tabs button:focus-visible { outline: none; box-shadow: var(--g-ring); }
html[data-contrast="high"] .field input[type=text], html[data-contrast="high"] .field input[type=url], html[data-contrast="high"] .field input[type=password] { border: 2px solid var(--g-line); box-shadow: none; }
html[data-contrast="high"] .seg span { background: var(--g-panel); border: 2px solid var(--g-line); }
html[data-contrast="high"] .seg input:checked + span { background: var(--accent); color: var(--accent-text); box-shadow: none; }
html[data-contrast="high"] .seg input:checked + span small { color: inherit; }
html[data-contrast="high"] .pill, html[data-contrast="high"] .pill.on { background: var(--g-panel); border: 2px solid var(--g-line); color: var(--text); }
html[data-contrast="high"] .meter { background: var(--g-panel); border: 2px solid var(--g-line); }
html[data-contrast="high"] .overlay, html[data-contrast="high"] .guide, html[data-contrast="high"] .mode { background: var(--g-panel); }
html[data-contrast="high"] .overlay.tier-danger { background: var(--g-danger); box-shadow: inset 0 0 0 14px var(--text-danger); }
html[data-contrast="high"] .overlay.tier-caution { background: var(--g-warning); box-shadow: inset 0 0 0 14px var(--text-warning); }
html[data-contrast="high"] .overlay.tier-info { background: var(--g-info); }
html[data-contrast="high"] .ov-btn { background: var(--g-panel); border: 3px solid var(--g-line); color: var(--text); }
html[data-contrast="high"] .mode-crossing[data-state="clear"] { background: var(--g-ok); box-shadow: none; }
html[data-contrast="high"] .mode-crossing[data-state="near"] { background: var(--g-warning); }
html[data-contrast="high"] .mode-crossing[data-state="close"] { background: var(--g-warning); box-shadow: inset 0 0 0 14px var(--text-warning); }
html[data-contrast="high"] .mode-crossing[data-state="back"] { background: var(--g-danger); box-shadow: inset 0 0 0 16px var(--text-danger); }
html[data-contrast="high"] .actions-bar { background: var(--g-panel); border-top: 2px solid var(--g-line); border-bottom: 2px solid var(--g-line); margin-left: -16px; margin-right: -16px; padding: 8px 16px; }
html[data-contrast="high"] small { color: inherit; opacity: 1; }
