:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-alt: #eeebe3;
  --ink: #16202b;
  --muted: #4a5561;
  --line: #d9d4c7;
  --brand: #13263a;
  --accent: #d13b2a;
  --accent-hover: #b22f20;
  --clear: #1f7a4d;
  --amber: #e2a72e;
  --unknown: #8a93a0;
  --focus: #2f6fd0;
  --radius: 14px;
  --map-bg: #fbfaf7;
  --map-road: #d6d0c2;
  --map-road-major: #c7bfae;
  --map-road-hi: #8a93a0;
  --map-rail: #16202b;
  --map-rail-bed: #fbfaf7;
  --map-label: #16202b;
  --map-label-ctx: #7a838e;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151c;
    --surface: #17202a;
    --surface-alt: #121a22;
    --ink: #e8ecf0;
    --muted: #a9b4bf;
    --line: #2a3643;
    --brand: #0b1219;
    --accent: #e5533f;
    --accent-hover: #f06a57;
    --clear: #3fb27a;
    --unknown: #6f7c8a;
    --focus: #6ea3ff;
    --map-bg: #141d26;
    --map-road: #2b3743;
    --map-road-major: #354352;
    --map-road-hi: #6f7c8a;
    --map-rail: #e8ecf0;
    --map-rail-bed: #141d26;
    --map-label: #e8ecf0;
    --map-label-ctx: #8290a0;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
h1 { font-size: 1.7rem; margin: .2em 0 .4em; line-height: 1.2; }
h2 { font-size: 1.2rem; margin: 0 0 .5em; line-height: 1.3; }
p { margin: 0 0 .9em; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: .92rem; }

/* App bar: sits under the iPhone status bar in standalone mode */
.appbar { position: sticky; top: 0; z-index: 10; background: var(--brand); color: #fff; padding-top: env(safe-area-inset-top); }
.appbar-inner { display: flex; align-items: center; justify-content: space-between; height: 54px; padding: 0 16px; max-width: 720px; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 800; font-size: 1.08rem; }
.brand-mark { width: 28px; height: 28px; border-radius: 7px; }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: .88rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: rgba(255,255,255,.12); }
.live-dot { width: 9px; height: 9px; border-radius: 50%; background: #3fb27a; box-shadow: 0 0 0 0 rgba(63,178,122,.6); animation: livepulse 2s infinite; }
.live.offline .live-dot { background: var(--amber); animation: none; }
@keyframes livepulse { 0% { box-shadow: 0 0 0 0 rgba(63,178,122,.6); } 70% { box-shadow: 0 0 0 8px rgba(63,178,122,0); } 100% { box-shadow: 0 0 0 0 rgba(63,178,122,0); } }

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

/* Bottom tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a { display: grid; justify-items: center; gap: 2px; padding: 8px 2px 6px; font-size: .74rem; font-weight: 600; color: var(--muted); text-decoration: none; }
.tabbar a span { font-size: 1.3rem; line-height: 1.2; filter: grayscale(1); opacity: .7; }
.tabbar a.active { color: var(--accent); }
.tabbar a.active span { filter: none; opacity: 1; }

/* Cards, forms, buttons */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.signin { margin-top: 24px; }
.field { display: grid; gap: 6px; font-weight: 600; margin-bottom: 14px; }
.field em { font-style: normal; font-weight: 500; color: var(--muted); font-size: .9rem; }
input[type=email], input[type=text], input[type=time], input[inputmode=numeric], select {
  font: inherit; font-size: 17px; width: 100%; min-height: 50px; padding: .65rem .8rem;
  border: 1.5px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink);
}
#signin-code { letter-spacing: .4em; font-size: 1.4rem; text-align: center; }
fieldset { border: 0; padding: 0; margin: 0 0 18px; min-width: 0; }
legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.check { display: flex; align-items: flex-start; gap: 12px; padding: 12px 14px; margin-bottom: 8px; border: 1.5px solid var(--line); border-radius: 10px; background: var(--bg); }
.check input { width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.check small { display: block; color: var(--muted); font-size: .88rem; }
.check.locked { opacity: .65; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; font: inherit; font-weight: 700;
  min-height: 50px; padding: .7rem 1.2rem; border-radius: 12px; border: 2px solid transparent; cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: .6; }
.btn-block { width: 100%; }
.btn-small { min-height: 40px; padding: .4rem .8rem; font-size: .92rem; white-space: nowrap; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: transparent; color: var(--ink); border-color: var(--line); margin-top: 6px; }
.btn-danger { background: color-mix(in srgb, var(--accent) 14%, var(--surface)); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.btn-ok { background: color-mix(in srgb, var(--clear) 14%, var(--surface)); color: var(--clear); border-color: color-mix(in srgb, var(--clear) 40%, transparent); }
.btn-link { background: none; border: 0; color: var(--muted); font: inherit; text-decoration: underline; padding: 10px 0; cursor: pointer; display: block; margin: 4px auto 0; }
.btn-link.danger { color: var(--accent); }
.form-msg { min-height: 0; margin: 10px 0 0; font-weight: 600; }
.form-msg:empty { display: none; }
.form-msg.err, .err { color: var(--accent); }
.form-msg.ok { color: var(--clear); }
.dev-note { padding: 10px 12px; border-radius: 10px; background: color-mix(in srgb, var(--amber) 18%, transparent); border: 1px solid color-mix(in srgb, var(--amber) 50%, transparent); font-size: .95rem; }

/* Map */
.banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; margin-bottom: 14px; border-radius: 12px; background: color-mix(in srgb, var(--amber) 16%, var(--surface)); border: 1px solid color-mix(in srgb, var(--amber) 50%, transparent); font-weight: 600; }
.map-card { border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: var(--map-bg); min-height: 180px; }
.crossing-map { display: block; width: 100%; height: auto; }
.map-bg { fill: var(--map-bg); }
.crossing-map path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.map-i565 { stroke: var(--map-road-major); stroke-width: 7; }
.map-road-major { stroke: var(--map-road-major); stroke-width: 4; }
.map-road-minor { stroke: var(--map-road); stroke-width: 3; }
.map-road-hi { stroke: var(--map-road-hi); stroke-width: 5; }
.map-rail-bed { stroke: var(--map-rail); stroke-width: 7; }
.map-rail { stroke: var(--map-rail-bed); stroke-width: 3; stroke-dasharray: 8 6; }
.map-label { fill: var(--map-label); font: 800 40px system-ui, sans-serif; paint-order: stroke; stroke: var(--map-bg); stroke-width: 9px; }
.map-label-ctx, .map-label-dir { fill: var(--map-label-ctx); font: 600 26px system-ui, sans-serif; paint-order: stroke; stroke: var(--map-bg); stroke-width: 6px; }
.map-label-rail { display: none; }
.map-dir { stroke: var(--map-label-ctx) !important; stroke-width: 3; stroke-dasharray: 2 7; }
.map-arrow-head { fill: var(--map-label-ctx) !important; stroke: none !important; }
.map-north { fill: var(--map-label-ctx); font: 800 24px system-ui, sans-serif; }
.map-north-arrow { fill: var(--map-label-ctx) !important; }
.map-attrib { fill: var(--map-label-ctx); font: 18px system-ui, sans-serif; }
/* Crossing indicators */
.map-dot { fill: var(--unknown); stroke: var(--map-bg); stroke-width: 4; r: 14; transition: fill .3s; }
.map-halo { fill: var(--unknown); opacity: 0; r: 22; }
.state-clear .map-dot { fill: var(--clear); }
.state-blocked .map-dot { fill: var(--accent); }
.state-blocked .map-halo { fill: var(--accent); opacity: .35; transform-box: fill-box; transform-origin: center; animation: halo 1.6s ease-out infinite; }
@keyframes halo { 0% { transform: scale(.7); opacity: .55; } 100% { transform: scale(2.2); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .state-blocked .map-halo { animation: none; } .live-dot { animation: none; } }

.crossings { list-style: none; padding: 0; margin: 14px 0 6px; display: grid; gap: 10px; }
.crossing { display: grid; grid-template-columns: 16px 1fr auto; column-gap: 12px; align-items: center; padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.crossing .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--unknown); }
.crossing.state-clear .dot { background: var(--clear); }
.crossing.state-blocked { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.crossing.state-blocked .dot { background: var(--accent); }
.crossing-name { font-weight: 700; font-size: 1.08rem; }
.crossing-state { font-weight: 700; font-size: .95rem; }
.state-blocked .crossing-state { color: var(--accent); }
.state-clear .crossing-state { color: var(--clear); }
.state-unknown .crossing-state { color: var(--muted); }
.crossing-detail { grid-column: 2 / -1; color: var(--muted); font-size: .92rem; }
.section-title { margin-top: 22px; }
.estimate { margin-top: 18px; font-size: .92rem; color: var(--muted); }

/* Alerts */
.alerts { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.alert { padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--clear); border-radius: 12px; display: grid; gap: 4px; }
.alert-blocked { border-left-color: var(--accent); }
.alert-badge { font-size: .76rem; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; color: var(--clear); }
.alert-blocked .alert-badge { color: var(--accent); }
.alert-text { font-weight: 600; font-size: 1.04rem; }
.alert-time { color: var(--muted); font-size: .9rem; }
.feedback { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 6px; font-size: .92rem; color: var(--muted); }
.chip { font: inherit; font-weight: 600; padding: 6px 14px; min-height: 36px; border-radius: 999px; border: 1.5px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; }
.chip.chosen { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.empty { color: var(--muted); padding: 10px 2px; }

/* Notifications */
.steps { padding-left: 1.3em; display: grid; gap: 8px; }
.ok-dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; background: var(--clear); margin-right: 8px; }
.devices { list-style: none; padding: 0; margin: 0 0 8px; }
.devices li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.devices .btn-link { margin: 0; }
.sim-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.legal-links { margin: 14px 0 0; text-align: center; font-size: .9rem; color: var(--muted); }
.legal-links a { color: inherit; }

/* Install banner and help */
.install-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: 696px; margin: 0 auto; display: flex; align-items: center; gap: 10px; padding: 10px 8px 10px 12px;
  background: var(--surface); border: 1.5px solid var(--accent); border-radius: 16px; box-shadow: 0 8px 28px rgba(0, 0, 0, .25);
}
.install-banner[hidden] { display: none; }
.tabbar:not([hidden]) ~ .install-banner { bottom: calc(76px + env(safe-area-inset-bottom)); }
.install-banner img { border-radius: 10px; flex: none; }
.install-text { flex: 1; display: grid; font-size: .95rem; line-height: 1.3; }
.install-text span { color: var(--muted); font-size: .88rem; }
.install-close { flex: none; width: 40px; height: 40px; border: 0; background: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; border-radius: 50%; }
.install-help { width: min(92vw, 480px); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; background: var(--surface); color: var(--ink); }
.install-help::backdrop { background: rgba(0, 0, 0, .55); }
.install-help h2 { margin-top: 0; font-size: 1.25rem; }
