/* Fixed-viewport composition. The map is the page; rails scroll internally. */

#app {
  display: grid;
  height: 100dvh;
  grid-template-rows: var(--header-h) 1fr var(--ticker-h);
  grid-template-columns: var(--rail-l) 1fr var(--rail-r);
  grid-template-areas:
    'header header header'
    'left   map    right'
    'ticker ticker ticker';
}

/* ── Header — navy command band with the Thai-flag pentaband on top ─────── */
header {
  grid-area: header;
  background: var(--th-navy);
  color: var(--ink-inv);
  display: flex;
  align-items: stretch;
  gap: 0;
  border-top: 6px solid;
  border-image: linear-gradient(
    to bottom,
    var(--th-red) 0 16.6%, #fff 16.6% 33.3%, var(--th-navy) 33.3% 66.6%,
    #fff 66.6% 83.3%, var(--th-red) 83.3% 100%) 1;
  /* pentaband fallback is painted by .flagband below for cross-browser safety */
  border-top: none;
  position: relative;
}
.flagband {
  position: absolute; top: 0; left: 0; right: 0; height: 6px;
  background: linear-gradient(
    to bottom,
    var(--th-red) 0 16.6%, #F6F4EF 16.6% 33.3%, #3D3663 33.3% 66.6%,
    #F6F4EF 66.6% 83.3%, var(--th-red) 83.3% 100%);
}
.brand {
  display: flex; flex-direction: column; justify-content: center;
  padding: 6px 16px 0 16px;
  border-right: 1px solid rgba(246,244,239,0.25);
}
.brand .name {
  font-family: var(--font-num);
  font-weight: 700; font-size: 19px; letter-spacing: 0.04em;
}
.brand .name .accent { color: #E8B33C; }
.brand .sub { font-size: 11px; opacity: 0.85; font-weight: 500; }

.national {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 14px 0 14px;
  border-right: 1px solid rgba(246,244,239,0.25);
}
.national .plate {
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-num); font-weight: 700; font-size: 15px; color: #fff;
}
.national .label { display: flex; flex-direction: column; line-height: 1.2; }
.national .label .th { font-weight: 600; font-size: 14px; }
.national .label .en { font-family: var(--font-num); font-size: 9.5px; letter-spacing: 0.1em; text-transform: uppercase; opacity: 0.8; }

.levelcounts { display: flex; align-items: center; gap: 6px; padding: 6px 14px 0 14px; }
.levelcounts .lc { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.levelcounts .lc .n { font-family: var(--font-num); font-size: 11px; font-weight: 500; color: var(--ink-inv); }

header .spacer { flex: 1; }

.pipes { display: flex; align-items: center; gap: 7px; padding: 6px 14px 0 0; }
.pipes .dot { width: 8px; height: 8px; background: #4A4470; }
.pipes .dot.ok { background: #35B558; }
.pipes .dot.bad { background: var(--th-red); }
.pipes .dot.run { background: #E8B33C; }

.clockbox { display: flex; flex-direction: column; align-items: flex-end; justify-content: center; padding: 6px 14px 0 0; }
#clock { font-family: var(--font-num); font-size: 20px; font-weight: 700; line-height: 1; }
.clockbox .date { font-size: 10.5px; opacity: 0.85; }

.focusbox { display: flex; align-items: center; gap: 6px; padding: 6px 12px 0 0; }
.focusbox select {
  font-family: var(--font-ui); font-size: 12.5px; font-weight: 600;
  background: var(--well); color: var(--th-navy);
  border: 1px solid rgba(246,244,239,0.5); padding: 5px 6px; max-width: 190px;
}
.focusbox #compare-btn {
  border: 1px solid rgba(246,244,239,0.5); background: transparent; color: var(--ink-inv);
  font-size: 15px; line-height: 1; padding: 4px 8px;
}
.focusbox #compare-btn:hover { background: var(--ink-inv); color: var(--th-navy); }

.langtoggle { display: flex; align-items: center; padding: 6px 14px 0 0; gap: 0; }
.langtoggle button {
  border: 1px solid rgba(246,244,239,0.5);
  background: transparent; color: var(--ink-inv);
  font-size: 12px; font-weight: 600; padding: 4px 10px;
}
.langtoggle button.active { background: var(--ink-inv); color: var(--th-navy); }

/* ── Rails ────────────────────────────────────────────────────────────────── */
.rail {
  background: var(--paper);
  display: flex; flex-direction: column;
  min-height: 0;                       /* allow children to scroll */
}
#rail-left  { grid-area: left; border-right: 1px solid var(--rule-strong); }
#rail-right { grid-area: right; border-left: 1px solid var(--rule-strong); }

.rail .panel-head {
  padding: 10px 12px 8px;
  border-bottom: 1px solid var(--rule-strong);
  background: var(--well);
  flex: none;
}
.rail .panel-head .sign .th { font-size: 15px; }
.rail .scroll {
  overflow-y: auto;
  overscroll-behavior: contain;
  flex: 1;
  min-height: 0;
}

/* ── Map ─────────────────────────────────────────────────────────────────── */
#map { grid-area: map; background: #E9E6DD; min-height: 0; }
.leaflet-container { font-family: var(--font-ui); background: #E9E6DD; }

/* ── Ticker ──────────────────────────────────────────────────────────────── */
#ticker {
  grid-area: ticker;
  background: var(--th-navy);
  color: var(--ink-inv);
  display: flex; align-items: center;
  overflow: hidden;
  border-top: 1px solid var(--rule-strong);
}
#ticker .live {
  flex: none;
  display: flex; align-items: center; gap: 6px;
  padding: 0 12px;
  font-family: var(--font-num); font-size: 11px; font-weight: 700; letter-spacing: 0.12em;
  border-right: 1px solid rgba(246,244,239,0.3);
  height: 100%;
}
#ticker .live .pip { width: 8px; height: 8px; background: var(--th-red); animation: blink 1.6s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0.25; } }
#ticker .track { flex: 1; overflow: hidden; white-space: nowrap; }
#ticker .track .crawl {
  display: inline-block; padding-left: 100%;
  animation: crawl 55s linear infinite;
  font-size: 13.5px;
}
#ticker .track .crawl span { margin-right: 48px; }
@keyframes crawl { to { transform: translateX(-100%); } }

/* ── Compare overlay: N independent panes for side-by-side ───────────────── */
#compare-overlay {
  position: fixed; inset: 0; z-index: 5000;
  background: var(--paper);
  display: flex; flex-direction: column;
}
#compare-overlay[hidden] { display: none; }
.compare-head {
  height: var(--header-h); flex: none;
  background: var(--th-navy); color: var(--ink-inv);
  display: flex; align-items: center; justify-content: space-between; padding: 0 16px;
  border-bottom: 3px solid var(--th-red);
}
.compare-head .sign .th { font-weight: 600; }
.compare-head .sign .en { font-family: var(--font-num); font-size: 10px; letter-spacing: 0.1em; opacity: 0.8; margin-left: 8px; }
.compare-actions { display: flex; gap: 6px; align-items: center; }
.compare-actions button {
  border: 1px solid rgba(246,244,239,0.5); background: transparent; color: var(--ink-inv);
  font-weight: 600; padding: 5px 11px;
}
.compare-actions button.active { background: var(--ink-inv); color: var(--th-navy); }
#compare-grid { flex: 1; min-height: 0; display: grid; gap: 2px; background: var(--rule-strong); }
#compare-grid.panes-2 { grid-template-columns: 1fr 1fr; }
#compare-grid.panes-3 { grid-template-columns: 1fr 1fr 1fr; }
#compare-grid.panes-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.cmp-pane { display: flex; flex-direction: column; min-height: 0; background: var(--paper); position: relative; }
.cmp-pane .cmp-bar {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 5px 8px;
  background: var(--well); border-bottom: 1px solid var(--rule-strong);
}
.cmp-pane .cmp-bar select { font-family: var(--font-ui); font-size: 12px; font-weight: 600; border: 1px solid var(--rule-strong); padding: 3px 5px; background: var(--paper); color: var(--th-navy); }
.cmp-pane .cmp-map { flex: 1; min-height: 0; }
.cmp-pane .cmp-readout {
  flex: none; max-height: 42%; overflow-y: auto; overscroll-behavior: contain;
  border-top: 1px solid var(--rule-strong); background: var(--well);
}
@media (max-width: 860px) {
  #compare-grid.panes-2, #compare-grid.panes-3, #compare-grid.panes-4 {
    grid-template-columns: 1fr; grid-template-rows: none; grid-auto-rows: 1fr;
    overflow-y: auto;
  }
}

/* ── Mobile: map on top, tabbed sheet below ─────────────────────────────── */
#sheettabs { display: none; }

@media (max-width: 860px) {
  #app {
    grid-template-rows: 46px minmax(0, 46fr) auto minmax(0, 54fr) var(--ticker-h);
    grid-template-columns: 1fr;
    grid-template-areas: 'header' 'map' 'tabs' 'sheet' 'ticker';
  }
  header { overflow: hidden; }
  .brand .sub, .clockbox, .levelcounts, .national .label { display: none; }
  .brand .name { font-size: 16px; }
  .national { padding: 6px 10px 0; }
  .legend { display: none; }

  #rail-left, #rail-right { display: none; }
  #rail-left.sheet-active, #rail-right.sheet-active {
    display: flex;
    grid-area: sheet;
    border: none;
    min-height: 0;
  }
  /* The bottom sheet nav already selects the pane — hide the rail's own tab bar. */
  #rail-right.sheet-active .tabs { display: none; }

  #sheettabs {
    grid-area: tabs;
    display: flex;
    background: var(--well);
    border-top: 1px solid var(--rule-strong);
    border-bottom: 1px solid var(--rule-strong);
  }
  #sheettabs button {
    flex: 1; border: none; border-right: 1px solid var(--rule);
    padding: 8px 4px; font-size: 12.5px; font-weight: 600;
  }
}

/* ── About overlay (fine-print, project credit, methodology) ─────────────── */
#about-btn {
  position: fixed; top: 8px; right: 8px; z-index: 4800;
  background: rgba(36,30,78,0.78); color: var(--ink-inv);
  border: 1px solid rgba(246,244,239,0.35);
  font-family: var(--font-ui); font-size: 12px; font-weight: 600;
  padding: 4px 10px; cursor: pointer;
}
#about-btn:hover { background: var(--ink-inv); color: var(--th-navy); }

#about-overlay {
  position: fixed; inset: 0; z-index: 5500;
  background: var(--paper);
  display: flex; flex-direction: column;
  overflow: hidden;
}
#about-overlay[hidden] { display: none; }
.about-head {
  height: var(--header-h); flex: none;
  background: var(--th-navy); color: var(--ink-inv);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  border-bottom: 3px solid var(--th-red);
}
.about-head .sign .th { font-weight: 700; font-size: 16px; }
.about-head .sign .en {
  font-family: var(--font-num); font-size: 10px; letter-spacing: 0.1em;
  text-transform: uppercase; opacity: 0.85; margin-left: 8px;
}
.about-head button {
  border: 1px solid rgba(246,244,239,0.5);
  background: transparent; color: var(--ink-inv);
  font-weight: 600; padding: 5px 12px;
}
.about-head button:hover { background: var(--ink-inv); color: var(--th-navy); }

.about-body {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 28px clamp(20px, 5vw, 80px);
  max-width: 980px; margin: 0 auto; width: 100%;
}
.about-card {
  background: var(--well); border: 1px solid var(--rule);
  padding: 20px 24px; margin-bottom: 18px;
}
.about-owner {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 22px;
  align-items: center;
}
.about-portrait {
  width: 96px; height: 96px;
  background: linear-gradient(135deg, #241E4E 0%, #3D3663 100%);
  color: var(--ink-inv);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-num); font-weight: 700; font-size: 30px;
  letter-spacing: 0.06em;
  border: 2px solid var(--rule-strong);
}
.about-name    { font-size: 18px; font-weight: 700; color: var(--ink); }
.about-name-en { font-family: var(--font-num); font-size: 11.5px;
  letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-mid); margin-top: 1px; }
.about-role    { font-size: 13px; color: var(--ink); margin-top: 8px; line-height: 1.5; }
.about-blurb   { font-size: 13.5px; color: var(--ink-mid); margin-top: 6px; line-height: 1.55; }

.about-partners {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 22px;
  align-items: center;
}
.about-partner {
  display: grid;
  grid-template-columns: 132px 1fr;
  gap: 14px; align-items: center;
}
.partner-logo {
  width: 132px; max-height: 90px; object-fit: contain;
  background: transparent;
}
.partner-text .th { font-weight: 700; font-size: 14px; color: var(--ink); }
.partner-text .en {
  font-family: var(--font-num); font-size: 10.5px; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--ink-mid); margin-top: 2px;
}
.partner-meta { font-size: 11.5px; color: var(--ink-mid); margin-top: 6px; }
.partner-meta a { color: var(--ink); text-decoration: underline dotted; }

.about-links {
  display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px;
}
.about-links a {
  display: inline-block; padding: 5px 12px;
  border: 1px solid var(--rule-strong);
  font-size: 12.5px; font-weight: 600;
  text-decoration: none;
  background: var(--paper);
}
.about-links a:hover { background: var(--th-navy); color: var(--ink-inv); }

.about-fineprint {
  font-size: 11px; color: var(--ink-low);
  line-height: 1.55;
  padding: 14px 4px 8px;
  border-top: 1px solid var(--rule);
}

@media (max-width: 720px) {
  .about-partners { grid-template-columns: 1fr; }
  .about-owner { grid-template-columns: 72px 1fr; }
  .about-portrait { width: 72px; height: 72px; font-size: 22px; }
  .partner-logo { width: 110px; }
}
