:root {
  --bg: #0b1f2a;
  --panel: #0e2734;
  --line: #16323f;
  --ink: #eaf2f6;
  --muted: #7fa6b5;
  --accent: #fbbf24;
  --f4: #1d4ed8;
  --f7: #0d9488;
}

* {
  box-sizing: border-box;
  margin: 0;
}

html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "Segoe UI", Roboto, system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
}

.board {
  height: 100vh;
  width: 100%;
  overflow: hidden;
  /* size container: descendants scale/lay out relative to the board, not the
     raw viewport, so it adapts the same whether fullscreen on an iPad or in a
     window on a computer. */
  container-type: size;
  container-name: board;
}

.frame {
  height: 100%;
  display: flex;
  flex-direction: column;
  /* one knob, sized to the board's smaller edge. cqmin tracks the container;
     the vmin line is a fallback for engines without container-query units. */
  --u: clamp(13px, 2.2vmin, 26px);
  --u: clamp(13px, 2.4cqmin, 26px);
}

/* ---- top bar ---- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: calc(var(--u) * 0.8) calc(var(--u) * 1.4);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar .loc {
  font-size: calc(var(--u) * 1.1);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.topbar .clock {
  font-size: calc(var(--u) * 2.4);
  font-weight: 800;
  letter-spacing: 0.04em;
}

/* ---- main grid: two routes + frequency rail ---- */
.grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr 0.85fr;
  min-height: 0;
}

.route {
  padding: calc(var(--u) * 1.2) calc(var(--u) * 1.4);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.route-head {
  display: flex;
  align-items: baseline;
  gap: calc(var(--u) * 0.6);
}
.pill {
  display: inline-block;
  border-radius: 0.3em;
  padding: 0.08em 0.5em;
  font-size: calc(var(--u) * 1.2);
  font-weight: 800;
  color: #fff;
  background: var(--muted);
}
.line-F4 .pill {
  background: var(--f4);
}
.line-F7 .pill {
  background: var(--f7);
}
.route-head .dest {
  font-size: calc(var(--u) * 1.5);
  font-weight: 700;
}
.route-head .trip {
  margin-left: auto;
  font-size: calc(var(--u) * 0.95);
  color: var(--muted);
}

.next {
  display: flex;
  align-items: baseline;
  gap: calc(var(--u) * 0.8);
  margin: calc(var(--u) * 0.9) 0 calc(var(--u) * 0.6);
}
.next-time {
  font-size: calc(var(--u) * 3.6);
  font-weight: 800;
  line-height: 1;
}
.next-time.muted {
  color: var(--muted);
}
.next-sub {
  font-size: calc(var(--u) * 1.1);
  color: var(--muted);
}

.upcoming {
  list-style: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}
.upcoming::-webkit-scrollbar {
  width: 8px;
}
.upcoming::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 4px;
}
.uprow {
  display: flex;
  justify-content: space-between;
  padding: calc(var(--u) * 0.45) 0;
  border-bottom: 1px dashed var(--line);
  font-size: calc(var(--u) * 1.4);
}
.uprow .t {
  font-weight: 600;
}
.uprow .d {
  color: var(--muted);
  font-size: calc(var(--u) * 0.95);
  align-self: center;
}
.uprow.is-tomorrow .t {
  color: var(--muted);
  font-weight: 500;
}
.daydiv {
  list-style: none;
  margin: calc(var(--u) * 0.5) 0 calc(var(--u) * 0.2);
  font-size: calc(var(--u) * 0.85);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 2px solid var(--line);
  padding-bottom: calc(var(--u) * 0.2);
}

/* ---- frequency rail ---- */
.freq {
  padding: calc(var(--u) * 1.2) calc(var(--u) * 1.2);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  gap: calc(var(--u) * 1.1);
}
.freq-title {
  font-size: calc(var(--u) * 0.95);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.freq-route {
  display: flex;
  gap: calc(var(--u) * 0.7);
}
.freq-rows {
  flex: 1;
}
.freq-band {
  display: flex;
  justify-content: space-between;
  padding: calc(var(--u) * 0.3) 0;
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--u) * 1.0);
}
.freq-band .every {
  color: var(--muted);
}
.freq-band.muted {
  color: var(--muted);
}
.freq-toggle {
  display: none;
}
.freq-content {
  display: contents;
}
.times-toggle {
  display: none;
}

/* ---- status bar ---- */
.status {
  min-height: 0;
  padding: 0 calc(var(--u) * 1.4);
  color: var(--accent);
  font-size: calc(var(--u) * 0.95);
}
.status.is-stale {
  padding: calc(var(--u) * 0.4) calc(var(--u) * 1.4);
  background: rgba(251, 191, 36, 0.12);
}

/* ---- portrait (iPad-style) layout: two routes on top, frequency as a full-width
   band underneath, instead of a third column. ---- */
@container board (aspect-ratio < 1) {
  .grid {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .route:nth-of-type(2) {
    border-right: 0;
  }
  .freq {
    grid-column: 1 / -1;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    column-gap: calc(var(--u) * 2);
    border-top: 1px solid var(--line);
  }
  .freq-title {
    flex-basis: 100%;
  }
  .freq-route {
    flex: 1;
  }
}

/* ---- phone layout: routes first, collapsible times, frequency band below. ---- */
@media (max-width: 640px) {
  .board {
    height: 100dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .frame {
    min-height: 100%;
    height: auto;
    --u: 14px;
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 2;
    align-items: flex-start;
    gap: 12px;
    padding: 12px 14px;
  }

  .topbar .loc {
    max-width: 13rem;
    font-size: 12px;
    line-height: 1.25;
    letter-spacing: 0.08em;
  }

  .topbar .clock {
    flex: none;
    font-size: 28px;
    line-height: 1.1;
    letter-spacing: 0.02em;
  }

  .grid {
    flex: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: auto;
    padding: 8px;
  }

  .route {
    min-height: 0;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(14, 39, 52, 0.62);
  }

  .route-head {
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
  }

  .route-head .dest {
    flex: 1 1 7rem;
    min-width: 0;
    font-size: 17px;
    line-height: 1.1;
  }

  .route-head .trip {
    flex: 0 0 100%;
    margin-left: 0;
    font-size: 12px;
    white-space: nowrap;
  }

  .pill {
    border-radius: 6px;
    padding: 2px 8px;
    font-size: 14px;
    line-height: 1.2;
  }

  .next {
    align-items: start;
    flex-direction: column;
    gap: 2px;
    margin: 10px 0 8px;
  }

  .next-time {
    display: inline-block;
    font-size: 42px;
    line-height: 1.1;
  }

  .next-sub {
    align-self: auto;
    color: var(--accent);
    font-size: 14px;
    font-weight: 700;
  }

  .times-toggle,
  .freq-toggle {
    appearance: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 36px;
    margin-top: 4px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: rgba(11, 31, 42, 0.72);
    color: var(--ink);
    cursor: pointer;
    font: inherit;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.08em;
    line-height: 1.2;
    text-transform: uppercase;
  }

  .times-toggle:focus-visible,
  .freq-toggle:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  .route.is-times-collapsed .upcoming {
    display: none;
  }

  .upcoming {
    flex: none;
    max-height: min(42dvh, 300px);
    margin-top: 8px;
    padding: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .uprow {
    padding: 8px 0;
    font-size: 17px;
  }

  .uprow .d {
    font-size: 13px;
  }

  .daydiv {
    margin: 10px 0 4px;
    font-size: 11px;
  }

  .freq {
    display: grid;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(14, 39, 52, 0.62);
  }

  .freq.is-collapsed .freq-content {
    display: none;
  }

  .freq-content {
    display: grid;
    gap: 12px;
    min-width: 0;
  }

  .freq-title {
    flex-basis: auto;
    font-size: 10px;
    line-height: 1.2;
  }

  .freq-route {
    display: block;
  }

  .freq-route .pill {
    margin-bottom: 6px;
  }

  .freq-band {
    display: block;
    padding: 6px 0;
    font-size: 11px;
    line-height: 1.25;
  }

  .freq-band span {
    display: block;
  }

  .freq-band .every {
    font-size: 10px;
    white-space: normal;
  }

  .status {
    flex: none;
    padding: 0 14px 12px;
  }

  .status.is-stale {
    padding: 8px 14px;
  }
}

@media (max-width: 360px) {
  .frame {
    --u: 13px;
  }

  .topbar .loc {
    max-width: 11rem;
  }

  .next-time {
    font-size: 38px;
  }
}
