/* ============================================================
   SINGAPORE RAIL - MP2025 ENRICHED
   Visual language adapted from the "Beacon" instrument theme:
   state-as-environment glass surfaces, oklch tokens, display/
   body/mono type split. Dark is the primary presentation.
   ============================================================ */
:root {
  --font-display: 'Chivo', 'Segoe UI', system-ui, sans-serif;
  --font-body: 'Source Sans 3', 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'DM Mono', ui-monospace, monospace;
  --s: 1;
  --r: 13px;
}
html[data-theme="dark"] {
  --scroll: oklch(0.85 0.03 264 / 0.22);
  --scroll-hover: oklch(0.85 0.03 264 / 0.40);
  --bg:      oklch(0.15 0.020 264);
  --surface: oklch(0.21 0.024 264 / 0.88);
  --raised:  oklch(0.27 0.026 264);
  --fg:      oklch(0.96 0.008 264);
  --muted:   oklch(0.71 0.020 264);
  --border:  oklch(0.85 0.03 264 / 0.17);
  --hair:    oklch(0.85 0.03 264 / 0.10);
  --accent:  oklch(0.62 0.09 210);
  --shadow:  0 22px 54px oklch(0.08 0.02 264 / 0.62);
  --m-label: oklch(0.90 0.012 264);
  --m-halo:  #060911;
  --pill-rim: oklch(0.10 0.02 264);
}
html[data-theme="light"] {
  --scroll: oklch(0.24 0.02 264 / 0.24);
  --scroll-hover: oklch(0.24 0.02 264 / 0.42);
  --bg:      oklch(0.97 0.005 264);
  --surface: oklch(1 0 0 / 0.90);
  --raised:  oklch(1 0 0);
  --fg:      oklch(0.24 0.022 264);
  --muted:   oklch(0.52 0.020 264);
  --border:  oklch(0.24 0.02 264 / 0.15);
  --hair:    oklch(0.24 0.02 264 / 0.08);
  --accent:  oklch(0.58 0.10 225);
  --shadow:  0 16px 40px oklch(0.35 0.03 264 / 0.18);
  --m-label: oklch(0.28 0.02 264);
  --m-halo:  #ffffff;
  --pill-rim: #ffffff;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }

/* themed scrollbars, both engines */
* { scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--scroll); border-radius: 8px;
  border: 2px solid transparent; background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--scroll-hover); background-clip: padding-box; }
::-webkit-scrollbar-corner { background: transparent; }
body {
  background: var(--bg); color: var(--fg);
  font-family: var(--font-body); font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
#map { position: absolute; inset: 0; z-index: 0; background: var(--bg); }
.leaflet-container { font-family: var(--font-body); background: var(--bg); }
html[data-theme="dark"] .leaflet-tile { filter: saturate(0.86) brightness(0.94); }

.ui { position: absolute; z-index: 20; }
.lab {
  font-size: 10.5px; font-weight: 700; letter-spacing: 0.11em;
  text-transform: uppercase; color: var(--muted);
}
.num { font-family: var(--font-mono); font-feature-settings: 'tnum' 1; }

/* ---------------- glass primitives ---------------- */
.glass {
  background: var(--surface);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow);
}

/* ---------------- top bar ---------------- */
#topbar {
  top: 14px; left: 14px; right: 14px;
  display: flex; align-items: stretch; gap: 10px; pointer-events: none;
}
#topbar > * { pointer-events: auto; }
/* brand strip: height matches the tool rail's width (46px) */
#brand { display: flex; align-items: center; gap: 10px; height: 46px; padding: 0 16px 0 9px; border-radius: 999px; }
#brand .mark {
  width: 27px; height: 27px; border-radius: 9px; flex: none;
  background: conic-gradient(from 210deg, #d42e12, #fa9e0d, #97c616, #0099aa, #005ec4, #9900aa, #d42e12);
  position: relative;
}
#brand .mark::after {
  content: ''; position: absolute; inset: 5.5px; border-radius: 5px; background: var(--raised);
}
#brand h1 {
  margin: 0; font-family: var(--font-display); font-size: 13.5px; font-weight: 500;
  letter-spacing: 0.07em; line-height: 1; white-space: nowrap;
}

#modes {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 2px 0 4px;
}
.mode {
  border: 1px solid var(--border); cursor: pointer; user-select: none;
  font: 600 12px var(--font-body); color: var(--muted); text-align: center;
  background: var(--raised); padding: 8px 6px; border-radius: 9px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.mode:hover { border-color: var(--accent); color: var(--fg); }
.mode[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: oklch(0.14 0.02 264); }
html[data-theme="light"] .mode[aria-pressed="true"] { color: #ffffff; }

/* ---------------- left tool rail + flyout (compact by default) ------ */
/* iMaps-style capsule: vertically centred pill on the left edge */
#rail {
  top: 50%; left: 14px; width: 46px; transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: center;
  padding: 9px 6px; gap: 4px;
  border-radius: 999px;
}
.rtool {
  width: 34px; height: 34px; border: 0; border-radius: 999px; cursor: pointer;
  background: transparent; color: var(--fg);
  display: grid; place-items: center; padding: 0;
}
.rtool svg { width: 17px; height: 17px; }
.rtool:hover { background: var(--hair); }
.rtool[aria-pressed="true"] { background: var(--accent); color: oklch(0.14 0.02 264); }
html[data-theme="light"] .rtool[aria-pressed="true"] { color: #ffffff; }
.rsep { width: 20px; height: 1px; background: var(--hair); margin: 3px 0; flex: none; }

#flyout {
  top: 50%; left: 72px; width: 300px;
  max-height: min(76vh, calc(100vh - 120px));
  display: none; flex-direction: column; padding: 13px; overflow: hidden;
  transform: translateY(-50%);
}
#flyout.open { display: flex; animation: flyIn 0.16s ease; }
@keyframes flyIn {
  from { opacity: 0; transform: translateY(-50%) translateX(-8px); }
  to { opacity: 1; transform: translateY(-50%) translateX(0); }
}
#sec-search, #sec-lines { display: none; flex-direction: column; min-height: 0; }
#sec-search.on, #sec-lines.on { display: flex; }
#sec-lines { flex: 1; }

#search {
  width: 100%; padding: 10px 13px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--raised); color: var(--fg);
  font: 400 13px var(--font-body); outline: none;
}
#search::placeholder { color: var(--muted); }
#search:focus { border-color: var(--accent); }
#search-results { max-height: min(340px, calc(100vh - 210px)); overflow: auto; margin-top: 4px; }
.sr {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 9px; border-radius: 9px; cursor: pointer; font-size: 13px;
}
.sr:hover { background: var(--hair); }
.sr .pills { display: flex; gap: 2px; flex: none; }

.sect {
  margin: 14px 0 7px; display: flex; justify-content: space-between; align-items: baseline;
}
.sect a { font-size: 10.5px; color: var(--accent); cursor: pointer; font-weight: 700; }
.sect a + a { margin-left: 10px; }

#legend { overflow-y: auto; flex: 1; padding-right: 4px; }
.chip {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 9px; border-radius: 9px; cursor: pointer;
  font-size: 12.5px; user-select: none;
}
.chip:hover { background: var(--hair); }
.chip i { width: 22px; height: 8px; border-radius: 4px; flex: none; }
.chip.dash i {
  background: repeating-linear-gradient(90deg, var(--muted) 0 4px, transparent 4px 8px) !important;
}
.chip .code { font: 500 11px var(--font-mono); width: 26px; flex: none; color: var(--muted); }
.chip .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip.off { opacity: 0.32; }
.chip.off i { background: var(--muted) !important; }

/* mode-specific panel sections (visibility driven by updatePanel in JS) */
#newlist, #footlist { display: none; overflow-y: auto; min-height: 0; }
#newlist.on, #footlist.on { display: block; flex: 1; }

/* live-data UI (armed only when /api/health reports a key) */
.live-only { display: none; }
body.live-ok .live-only { display: initial; }
.cd { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; }
.upd { font-size: 10.5px; color: var(--muted); padding: 8px 2px 0; }

/* ---------------- live layer selector + env markers ---------------- */
.ovl {
  display: flex; align-items: center; gap: 9px;
  padding: 6px 9px; border-radius: 9px; cursor: pointer;
  font-size: 12.5px; user-select: none;
}
.ovl:hover { background: var(--hair); }
.ovl .box {
  width: 14px; height: 14px; border-radius: 5px; flex: none;
  border: 1.5px solid var(--ec); background: transparent; transition: background 0.12s;
}
.ovl.on .box { background: var(--ec); }
.ovl .nm { flex: 1; }
.ovl .cnt { font-size: 10.5px; color: var(--muted); }

.envwrap { pointer-events: auto; }
.envp {
  position: absolute; transform: translate(-50%, -50%);
  font: 500 10px var(--font-mono); padding: 2px 6px 2.5px; border-radius: 999px;
  background: var(--surface); border: 1.5px solid var(--ec); color: var(--fg);
  white-space: nowrap; box-shadow: 0 2px 8px rgb(0 0 0 / 0.3);
}

/* lightning strikes */
.bolt { pointer-events: auto; }
.bolt svg {
  position: absolute; transform: translate(-50%, -50%);
  width: 15px; height: 15px; fill: #f7d154;
  filter: drop-shadow(0 0 4px rgb(247 209 84 / 0.8));
  animation: boltIn 0.3s ease-out;
}
.bolt.g svg {
  width: 19px; height: 19px; fill: #ff7a3d;
  filter: drop-shadow(0 0 6px rgb(255 122 61 / 0.9));
}
@keyframes boltIn {
  0% { transform: translate(-50%, -50%) scale(0.2); opacity: 0; }
  55% { transform: translate(-50%, -50%) scale(1.35); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1); }
}

/* ---------------- crowd orbs: ambient live beacons ---------------- */
.orb { pointer-events: none; }
.orb .core, .orb .ring {
  position: absolute; left: 0; top: 0; border-radius: 50%;
  transform: translate(-50%, -50%);
}
.orb .core {
  width: 9px; height: 9px; background: var(--oc);
  box-shadow: 0 0 8px var(--oc), 0 0 20px var(--oc-soft), 0 0 36px var(--oc-soft);
  animation: orbBreathe var(--od) ease-in-out infinite;
}
.orb .ring {
  width: 36px; height: 36px; border: 1.5px solid var(--oc); opacity: 0;
  animation: orbRipple var(--od) cubic-bezier(0.2, 0.6, 0.4, 1) infinite;
}
.orb .ring.r2 { animation-delay: calc(var(--od) / -2); }
.orb.m { --oc: #f5a623; --oc-soft: rgba(245, 166, 35, 0.35); --od: 2.6s; }
.orb.h { --oc: #e0245e; --oc-soft: rgba(224, 36, 94, 0.42); --od: 1.4s; }
.orb.h .ring { width: 48px; height: 48px; border-width: 2px; }
.orb.h .core { width: 11px; height: 11px; }
@keyframes orbRipple {
  0% { transform: translate(-50%, -50%) scale(0.22); opacity: 0.85; }
  70% { opacity: 0.22; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}
@keyframes orbBreathe {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.3); }
}
@media (prefers-reduced-motion: reduce) {
  .orb .ring { animation: none; opacity: 0.4; }
  .orb .core { animation: none; }
}

/* ---------------- rail alert chip (appears only on disruption) ------ */
#alertchip { right: 14px; bottom: 110px; max-width: 360px; padding: 13px 16px; display: none; }
#alertchip.on { display: block; }
#alertchip b { font-family: var(--font-display); font-size: 13px; font-weight: 700; display: flex; align-items: center; }
#alertchip .seg { font-size: 12px; color: var(--muted); margin-top: 7px; display: flex; gap: 7px; align-items: flex-start; line-height: 1.45; }
#alertchip .seg .pill { flex: none; margin-top: 1px; }
#alertchip .x { float: right; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; margin: -4px -6px 0 10px; }
#alertchip .x:hover { color: var(--fg); }
.fl { display: flex; align-items: center; gap: 10px; padding: 5px 8px; border-radius: 8px; font-size: 12.5px; }
.fl:nth-child(odd) { background: var(--hair); }
.fl .rank { width: 18px; color: var(--muted); font-size: 11px; }
.fl .fn { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fl .fv { font-size: 12.5px; }
.nl {
  padding: 10px 12px; border-radius: 10px; cursor: pointer; margin-bottom: 5px;
  border: 1px solid var(--border); background: var(--raised);
}
.nl:hover { border-color: var(--accent); }
.nl b { font-family: var(--font-display); font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.nl b i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.nl span { font-size: 11.5px; color: var(--muted); display: block; margin-top: 3px; line-height: 1.45; }

/* ---------------- tour card ---------------- */
#tour {
  left: 50%; bottom: 28px; transform: translateX(-50%);
  padding: 16px 26px; text-align: center; display: none; min-width: 320px;
}
#tour.on { display: block; }
#tour .bar { height: 3px; border-radius: 2px; margin-bottom: 12px; background: var(--hair); overflow: hidden; }
#tour .bar div { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
#tour b { font-family: var(--font-display); font-size: 19px; font-weight: 700; }
#tour .codes { margin-top: 5px; display: flex; justify-content: center; gap: 3px; }
#tour small { display: block; font-size: 10.5px; color: var(--muted); margin-top: 9px; }

/* raw-mode banner */
#rawnote {
  left: 50%; bottom: 28px; transform: translateX(-50%);
  padding: 12px 22px; font-size: 12.5px; color: var(--muted);
  display: none; text-align: center; line-height: 1.5; max-width: 480px;
}
#rawnote b { color: var(--fg); }
body[data-mode="raw"] #rawnote { display: block; }

/* ---------------- code pills (shared) ---------------- */
.pillrow {
  display: inline-flex; gap: 1px; padding: 1.5px; border-radius: 999px;
  background: var(--pill-rim);
}
#tour .pill { font-size: 10.5px; padding: 2px 8px 2.5px; }

/* ---------------- map labels ---------------- */
.stlab { pointer-events: none; }
.stlab .wrap {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  transform: translate(-50%, calc(-100% - 9px)); width: max-content;
}
.stlab .pillrow {
  display: inline-flex; gap: 1px; padding: 1.5px; border-radius: 999px;
  background: var(--pill-rim); box-shadow: 0 1px 4px rgb(0 0 0 / 0.35);
}
.pill {
  display: inline-block; padding: 1px 5px 1.5px; border-radius: 999px;
  font: 700 8.5px/1.25 var(--font-mono); letter-spacing: 0.02em; color: #fff;
  white-space: nowrap;
}
.stlab .nm {
  font: 600 11px/1.1 var(--font-body); color: var(--m-label); white-space: nowrap;
  text-shadow: 0 1px 0 var(--m-halo), 0 -1px 0 var(--m-halo), 1px 0 0 var(--m-halo), -1px 0 0 var(--m-halo), 0 0 6px var(--m-halo);
}
.stlab.planned .nm { font-style: italic; opacity: 0.85; }
.stlab.planned .pillrow { opacity: 0.88; }

/* ---------------- popups & tooltips ---------------- */
.leaflet-popup-content-wrapper {
  background: var(--surface); color: var(--fg);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border: 1px solid var(--border); border-radius: var(--r); box-shadow: var(--shadow);
}
.leaflet-popup-content { margin: 13px 16px; font-family: var(--font-body); font-size: 13px; }
.leaflet-popup-tip { background: var(--surface); border: 1px solid var(--border); }
.leaflet-popup-close-button { color: var(--muted) !important; font-size: 18px !important; padding: 6px 8px 0 0 !important; }
.leaflet-container a.leaflet-popup-close-button:hover { color: var(--fg) !important; }

.pp b.ttl { font-family: var(--font-display); font-size: 14.5px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.pp b.ttl i { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.pp .meta { font-size: 11.5px; color: var(--muted); margin-top: 6px; line-height: 1.55; }
.pp .pillrow { display: inline-flex; gap: 1px; padding: 1.5px; border-radius: 999px; background: var(--pill-rim); margin: 7px 0 2px; }
.pp .pill { font-size: 9.5px; padding: 1.5px 6px 2px; }
.pp .badge {
  display: inline-block; font-size: 9.5px; font-weight: 700; letter-spacing: 0.06em; margin-top: 8px;
  background: var(--accent); color: oklch(0.14 0.02 264); border-radius: 6px; padding: 2.5px 8px;
}
html[data-theme="light"] .pp .badge { color: #fff; }

.leaflet-tooltip.linetip {
  background: var(--surface); color: var(--fg); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow); font: 600 11.5px var(--font-body);
  padding: 4px 9px;
}
.leaflet-tooltip.linetip::before { display: none; }

/* leaflet controls in glass */
.leaflet-bar { border: 0 !important; box-shadow: var(--shadow) !important; border-radius: 10px !important; overflow: hidden; }
.leaflet-bar a {
  background: var(--surface) !important; color: var(--fg) !important;
  border-bottom: 1px solid var(--hair) !important; width: 32px !important; height: 32px !important; line-height: 32px !important;
}
.leaflet-bar a:hover { background: var(--raised) !important; }
.leaflet-control-attribution {
  background: var(--surface) !important; color: var(--muted) !important;
  font-size: 10px !important; border-radius: 8px 0 0 0;
}
.leaflet-control-attribution a { color: var(--accent) !important; }

/* ---------------- about modal ---------------- */
#about {
  position: absolute; inset: 0; z-index: 60; display: none;
  background: oklch(0.05 0.01 264 / 0.5); align-items: center; justify-content: center; padding: 20px;
}
#about.on { display: flex; }
#about .card {
  background: var(--raised); border: 1px solid var(--border); border-radius: 18px;
  box-shadow: var(--shadow); max-width: 660px; max-height: 88vh; overflow-y: auto; padding: 30px 34px;
}
#about h2 { margin: 0 0 6px; font-family: var(--font-display); font-size: 21px; font-weight: 700; }
#about h3 { font-size: 11px; margin: 22px 0 8px; text-transform: uppercase; letter-spacing: 0.11em; color: var(--muted); font-weight: 700; }
#about p, #about li { font-size: 13.5px; line-height: 1.65; }
#about ul { padding-left: 18px; margin: 6px 0; }
#about .lic { font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--hair); margin-top: 22px; padding-top: 15px; line-height: 1.6; }
#about .x { float: right; cursor: pointer; font-size: 20px; color: var(--muted); border: none; background: none; }
#about .x:hover { color: var(--fg); }
.tiers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 9px; margin: 12px 0; }
.tier { background: var(--surface); border: 1px solid var(--hair); border-radius: 11px; padding: 12px 10px; text-align: center; }
.tier b { display: block; font: 500 22px var(--font-mono); letter-spacing: -0.02em; }
.tier span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }

@media (max-width: 900px) {
  #brand p { display: none; }
  #flyout { width: min(300px, calc(100vw - 100px)); }
  #topbar { flex-wrap: wrap; }
  .tiers { grid-template-columns: repeat(2, 1fr); }
}
