/* ═══════════════════════════════════════════════════════════
   bus.of.je  ·  style.css
   ═══════════════════════════════════════════════════════════ */

/* ─── Custom properties ─────────────────────────────────── */
:root {
  --primary: #bf1b2c;
  --primary-hover: #a01525;
  --primary-light: #d4233a;
  --accent: #f59e0b;
  --next-bg: #d1fae5;
  --next-border: #059669;
  --next-text: #064e3b;
  --next-head: #059669;
  --fri-bg: #dbeafe;
  --fri-border: #3b82f6;
  --fri-text: #1e40af;
  --fri-head: #2563eb;
  --alert-bg: #fffbeb;
  --alert-border: #d97706;
  --alert-badge: #d97706;
  --alert-text: #92400e;
  --alert-badge-text: #1a0a00;
  --alert-error-bg: #fef2f2;
  --alert-error-bd: #dc2626;
  --bg: #f0f4f8;
  --surface: #fff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #1a202c;
  --text-muted: #5d6a7e;
  --radius: 8px;
  --radius-sm: 5px;
  --shadow: 0 1px 3px rgb(0 0 0 / 8%), 0 1px 2px rgb(0 0 0 / 5%);
  --shadow-md: 0 4px 8px rgb(0 0 0 / 9%), 0 2px 4px rgb(0 0 0 / 6%);
  --header-h: 54px;
}

/* ─── Reset ──────────────────────────────────────────────── */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Ensure [hidden] is never overridden by display:flex/grid on elements */
[hidden] {
  display: none !important;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  text-size-adjust: 100%;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: inherit;
}

/* ─── Skip link ──────────────────────────────────────────── */
.skip-link {
  position: absolute;
  top: -100%;
  left: 1rem;
  background: var(--primary);
  color: #fff;
  padding: 0.5rem 1rem;
  border-radius: 0 0 var(--radius) var(--radius);
  font-weight: 700;
  font-size: 0.9rem;
  text-decoration: none;
  z-index: 200;
  transition: top 0.1s;
}

.skip-link:focus {
  top: 0;
}

/* ─── Header ─────────────────────────────────────────────── */
#site-header {
  background: var(--primary);
  color: #fff;
  height: var(--header-h);
  padding: 0 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.875rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-md);
}

#site-title {
  font-size: 1.2rem;
  font-weight: 800;
  color: #fff;
  text-decoration: none;
  letter-spacing: -0.5px;
  flex-shrink: 0;
  line-height: 1;
}

#site-title:hover {
  opacity: 0.85;
}

#header-sub {
  font-size: 0.78rem;
  color: rgb(255 255 255 / 85%);
  display: none;
}

@media (width >= 480px) {
  #header-sub {
    display: block;
  }
}

/* ─── Main ───────────────────────────────────────────────── */
#main {
  flex: 1;
  padding: 1rem 1rem 2rem;
  width: 100%;
}

/* Route list is content-width constrained; timetable fills the viewport */
#view-list {
  max-width: 1440px;
  margin: 0 auto;
}

/* ─── Service updates panel ──────────────────────────────── */
#service-updates-panel {
  background: var(--alert-bg);
  border: 1.5px solid var(--alert-border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: 0.875rem;
  color: var(--alert-text);
}

#service-updates-panel summary {
  padding: 0.75rem 1rem;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#service-updates-panel summary::marker,
#service-updates-panel summary::-webkit-details-marker {
  display: none;
}

#service-updates-panel summary::after {
  content: '▼';
  font-size: 0.7em;
  margin-left: auto;
  transition: transform 0.2s;
}

#service-updates-panel[open] summary::after {
  transform: rotate(-180deg);
}

#service-updates-panel[open] summary {
  border-bottom: 1px solid var(--alert-border);
}

.atags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.25rem;
}

.atag {
  display: inline-flex;
  align-items: center;
  background: var(--primary);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.05rem 0.4rem;
  border-radius: 3px;
}

.diversion-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--alert-badge);
  color: var(--alert-badge-text);
  font-size: 0.72rem;
  font-weight: 700;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.diversions-list {
  padding: 0.5rem 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.diversion-item {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.diversion-title {
  font-weight: 500;
}

.diversion-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.diversion-link {
  font-size: 0.8rem;
  background: rgb(217 119 6 / 12%);
  border: 1px solid var(--alert-border);
  color: var(--alert-text);
  border-radius: var(--radius-sm);
  padding: 0.2rem 0.55rem;
  text-decoration: none;
  white-space: nowrap;
}

.diversion-link:hover {
  background: rgb(217 119 6 / 25%);
}

/* ─── List toolbar ───────────────────────────────────────── */
#list-toolbar {
  margin-bottom: 0.875rem;
}

#search-wrap {
  position: relative;
  display: inline-block;
  width: 100%;
  max-width: 340px;
}

#search-wrap.stop-index-loading::after {
  content: '';
  position: absolute;
  right: 0.6rem;
  top: 50%;
  margin-top: -7px;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
  pointer-events: none;
}

#route-filter {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  transition:
    border-color 0.15s,
    box-shadow 0.15s;
}

#route-filter:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgb(191 27 44 / 15%);
}

/* ─── Routes grid ────────────────────────────────────────── */
#routes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.625rem;
}

.route-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 0.625rem;
  box-shadow: var(--shadow);
  transition:
    box-shadow 0.15s,
    border-color 0.15s,
    transform 0.1s;
  position: relative;
}

.route-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--primary-light);
  transform: translateY(-1px);
}

.route-card:active {
  transform: none;
}

.rnum {
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.2rem 0.5rem;
  border-radius: var(--radius-sm);
  min-width: 2.4rem;
  text-align: center;
  flex-shrink: 0;
  letter-spacing: -0.3px;
}

.rname {
  font-size: 0.875rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.route-card.has-alert .rnum {
  background: var(--alert-badge);
  color: var(--alert-badge-text);
}

.alert-dot {
  position: absolute;
  top: 0.45rem;
  right: 0.55rem;
  width: 16px;
  height: 16px;
  background: var(--alert-badge);
  border-radius: 50%;
  font-size: 0.65rem;
  font-weight: 800;
  color: var(--alert-badge-text);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ─── Spinner ────────────────────────────────────────────── */
.spinner-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 3rem 1rem;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ─── Empty / error states ───────────────────────────────── */
#list-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 1.5rem 0;
}

.state-msg {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.state-error {
  color: #7f1d1d;
}

.state-error a {
  color: #7f1d1d;
  font-weight: 600;
}

/* ─── Timetable view ─────────────────────────────────────── */
.back-link {
  display: inline-block;
  font-size: 0.875rem;
  color: var(--primary);
  text-decoration: none;
  margin-bottom: 0.875rem;
  padding: 0.2rem 0;
}

.back-link:hover {
  text-decoration: underline;
}

#route-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.875rem;
}

#route-num-badge {
  background: var(--primary);
  color: #fff;
  font-weight: 800;
  font-size: 1.4rem;
  padding: 0.2rem 0.6rem;
  border-radius: 7px;
  letter-spacing: -0.5px;
  flex-shrink: 0;
  line-height: 1.3;
  margin-top: 0.1rem;
}

#route-heading {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.2;
}

@media (width >= 480px) {
  #route-heading {
    font-size: 1.35rem;
  }
}

.muted {
  color: var(--text-muted);
  font-size: 0.875rem;
}

/* Route inline alert */
#route-alert-bar {
  background: var(--alert-bg);
  border-left: 4px solid var(--alert-border);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 0.625rem 1rem;
  margin-bottom: 0.875rem;
  font-size: 0.875rem;
  color: var(--alert-text);
}

.route-alert + .route-alert {
  margin-top: 0.35rem;
}

/* ─── Day tabs ───────────────────────────────────────────── */
#day-tabs {
  display: flex;
  gap: 0.125rem;
  border-bottom: 2px solid var(--border);
  margin-bottom: 0.75rem;
}

.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  padding: 0.5rem 0.875rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  transition: color 0.15s;
  white-space: nowrap;
}

.tab:hover {
  color: var(--primary);
}

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* ─── Direction toggle ───────────────────────────────────── */
#dir-toggle {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.875rem;
}

.dir-btn {
  padding: 0.35rem 0.875rem;
  border: 1.5px solid var(--border-strong);
  border-radius: 99px;
  background: var(--surface);
  font-size: 0.83rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
  white-space: nowrap;
}

.dir-btn:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
}

.dir-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.dir-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

/* ─── Legend ─────────────────────────────────────────────── */
#tt-legend {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.1rem;
}

.legend-chip {
  display: inline-block;
  padding: 0.05rem 0.35rem;
  border-radius: 3px;
  font-size: 0.68rem;
  font-weight: 700;
  border: 1px solid currentcolor;
}

.fri-chip {
  background: var(--fri-bg);
  color: var(--fri-text);
  border-color: var(--fri-border);
}

.next-chip {
  background: var(--next-bg);
  color: var(--next-text);
  border-color: var(--next-border);
}

.legend-label {
  font-size: 0.78rem;
}

.legend-sep {
  color: var(--text-muted);
}

/* ─── Table scroll wrapper ───────────────────────────────── */
#tt-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--surface);

  /* Ensure sticky columns work */
  position: relative;

  /* Shrink to content width on small timetables; cap at viewport on large ones */
  width: fit-content;
  max-width: 100%;
  margin: 0 auto;
}

/* ─── Timetable table ────────────────────────────────────── */
#tt-table {
  border-collapse: collapse;
  width: max-content;
  font-size: 0.8rem;
  line-height: 1.3;
}

/* Stop name column — sticky */
#tt-table .col-stop {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
  min-width: 130px;
  max-width: 170px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
  border-right: 2px solid var(--border-strong);
}

/* Trip time columns */
#tt-table .col-trip {
  min-width: 46px;
  text-align: center;
  white-space: nowrap;
}

/* Header row */
#tt-table thead tr {
  background: var(--surface-alt);
  color: inherit;
}

#tt-table thead th {
  padding: 0.25rem 0.45rem;
  font-weight: 700;
  border-right: 1px solid rgb(0 0 0 / 8%);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

#tt-table thead th:last-child {
  border-right: none;
}

#tt-table thead .col-stop {
  background: var(--surface-alt);
  z-index: 3;
  padding-left: 0.75rem;
}

/* Fri-only header */
#tt-table thead th.col-fri {
  background: var(--fri-head);
  border-right-color: rgb(0 0 0 / 8%);
  color: #fff;
}

.fri-hdr {
  display: inline-block;
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: rgb(255 255 255 / 25%);
  border-radius: 2px;
  padding: 0 0.25rem;
  line-height: 1.4;
}

/* Next bus header */
#tt-table thead th.col-next {
  background: var(--next-head);
  color: #161c28;
}

/* Tbody */
#tt-table tbody tr:nth-child(even) td {
  background: var(--surface-alt);
}

#tt-table tbody tr:nth-child(even) td.col-stop {
  background: var(--surface-alt);
}

#tt-table tbody tr:hover td {
  background: #fdf2f3;
}

#tt-table tbody tr:hover td.col-stop {
  background: #fdf2f3;
}

#tt-table td {
  padding: 0.25rem 0.45rem;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

#tt-table td:last-child {
  border-right: none;
}

#tt-table td.col-stop {
  padding-left: 0.75rem;
}

/* No-call dash */
#tt-table td.no-call {
  color: var(--text-muted);
  font-size: 0.72rem;
}

/* Fri-only cells */
#tt-table td.col-fri {
  background: var(--fri-bg) !important;
  color: var(--fri-text);
}

#tt-table tbody tr:hover td.col-fri {
  background: #c7dcf8 !important;
}

/* Next bus cells */
#tt-table td.col-next {
  background: var(--next-bg) !important;
  color: var(--next-text);
  font-weight: 700;
}

#tt-table tbody tr:hover td.col-next {
  background: #a7f3d0 !important;
}

/* ─── Wide screen ────────────────────────────────────────── */
@media (width >= 1024px) {
  #tt-table {
    font-size: 0.75rem;
  }
}

/* ─── Timetable period label ─────────────────────────────── */
.period-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-bottom: 0.625rem;
}

/* ─── Date picker ────────────────────────────────────────── */
#date-picker {
  margin-left: auto;
  padding: 0.28rem 0.6rem;
  border: 1.5px solid var(--border-strong);
  border-radius: 99px;
  background: var(--surface);
  font-size: 0.8rem;
  font-family: inherit;
  color: var(--text);
  cursor: pointer;
  align-self: center;
  flex-shrink: 0;
}

#date-picker:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgb(191 27 44 / 12%);
}

@media (width <= 400px) {
  #day-tabs {
    flex-wrap: wrap;
  }

  #date-picker {
    margin-left: 0;
  }
}

/* ─── Footer ─────────────────────────────────────────────── */
footer {
  text-align: center;
  padding: 1.25rem 1rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

footer a {
  color: var(--primary);
  text-decoration: underline;
}

footer a:hover {
  text-decoration: underline;
  opacity: 0.8;
}

/* ─── Print ──────────────────────────────────────────────── */
@media print {
  #site-header,
  #btn-back,
  #day-tabs,
  #dir-toggle,
  #list-toolbar,
  #service-updates-panel,
  #tt-legend,
  #timetable-period,
  #tt-period,
  footer {
    display: none !important;
  }

  body {
    background: #fff;
    font-size: 10pt;
  }

  #main {
    padding: 0;
    max-width: none;
  }

  #tt-scroll {
    overflow: visible;
    border: none;
    box-shadow: none;
    border-radius: 0;
  }

  #tt-table {
    font-size: 8pt;
    width: 100%;
  }

  #tt-table .col-stop {
    position: static !important;
    background: #f0f0f0 !important;
    border-right: 1pt solid #aaa !important;
  }

  #tt-table thead tr {
    background: #f0f0f0 !important;
    border-bottom: 1pt solid #aaa !important;
  }

  #tt-table thead th {
    background: #f0f0f0 !important;
    color: #000 !important;
  }

  #tt-table td.col-fri {
    background: #dde !important;
  }

  #tt-table td.col-next {
    background: #dfd !important;
  }

  #tt-table tbody tr:hover td {
    background: transparent !important;
  }
}
