* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  /* 100dvh = visible viewport (excludes mobile URL/tool bars). 100vh alone
     is often taller than the screen, so the last list/table rows were clipped. */
  height: 100vh;
  height: 100dvh;
  max-height: 100vh;
  max-height: 100dvh;
  overflow: hidden;
}

header {
  position: relative;
  padding: 1rem;
  background: #0f2a43;
  color: white;
  flex: none;
}

header h1 {
  margin: 0 0 0.25rem;
  font-size: 1.25rem;
}

header p {
  margin: 0;
  color: #b9c7d6;
  font-size: 0.9rem;
}

.view-toggle {
  margin-top: 0.6rem;
  display: inline-flex;
  border: 1px solid #3b5875;
  border-radius: 6px;
  overflow: hidden;
}

.view-toggle button {
  border: none;
  background: transparent;
  color: #cbd5e1;
  font: inherit;
  font-size: 0.85rem;
  padding: 0.3rem 0.9rem;
  cursor: pointer;
}

.view-toggle button + button {
  border-left: 1px solid #3b5875;
}

.view-toggle button.active {
  background: #ffffff;
  color: #0f2a43;
  font-weight: 600;
}

/* Map pane (top) and table pane (bottom) with a draggable handle between.
   --split is the fraction of height given to the map pane in "split" view. */
.split {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.layout {
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.split[data-view="map"] .layout {
  flex: 1 1 0;
}
.split[data-view="split"] .layout {
  flex: var(--split, 0.6) 1 0;
}
.split[data-view="table"] .layout {
  display: none;
}

.split[data-view="map"] #event-table-view {
  display: none;
}
.split[data-view="split"] #event-table-view {
  flex: calc(1 - var(--split, 0.6)) 1 0;
}
.split[data-view="table"] #event-table-view {
  flex: 1 1 0;
}

.split-handle {
  flex: none;
  height: 16px;
  background: #0f2a43;
  cursor: row-resize;
  position: relative;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
}
.split-handle::before,
.split-handle::after {
  content: "";
  width: 70px;
  height: 2px;
  border-radius: 1px;
  background: #94a3b8;
}
.split-handle:hover {
  background: #1b3d5c;
}
.split-handle:hover::before,
.split-handle:hover::after {
  background: #e2e8f0;
}
.split-handle:active {
  background: #2563eb;
}
.split-handle:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: -2px;
}
.split:not([data-view="split"]) .split-handle {
  display: none;
}

#map {
  flex: var(--split-x, 0.66) 1 0;
  min-width: 0;
  min-height: 320px;
  position: relative;
}

#map.leaflet-fallback {
  background: #e5e7eb;
}

#event-list {
  flex: calc(1 - var(--split-x, 0.66)) 1 0;
  min-width: 0;
  min-height: 0; /* allow the list to shrink and scroll inside the pane */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* Extra room so the last event clears sticky headers / home indicator */
  padding-bottom: calc(2.5rem + env(safe-area-inset-bottom, 0px));
}

/* Vertical resizer between the map and the list. */
.split-handle-v {
  flex: none;
  width: 16px;
  background: #0f2a43;
  cursor: col-resize;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
}
.split-handle-v::before,
.split-handle-v::after {
  content: "";
  width: 2px;
  height: 70px;
  border-radius: 1px;
  background: #94a3b8;
}
.split-handle-v:hover {
  background: #1b3d5c;
}
.split-handle-v:hover::before,
.split-handle-v:hover::after {
  background: #e2e8f0;
}
.split-handle-v:active {
  background: #2563eb;
}
.split-handle-v:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: -2px;
}

.event-row {
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid #e2e8f0;
  border-left: 4px solid var(--row-color, #6b7280);
  cursor: pointer;
}

.event-row:hover {
  background: #f8fafc;
}

.event-row.active {
  background: #eef2f7;
  border-left-width: 8px;
}

.event-row .event-name {
  font-weight: 600;
  font-size: 0.9rem;
  color: #0f2a43;
}

.rsvp-badge,
.recurring-badge {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0 0.3rem;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-radius: 3px;
  vertical-align: middle;
  white-space: nowrap;
}

.rsvp-badge {
  color: #b45309;
  background: #fef3c7;
  border: 1px solid #fcd34d;
}

.recurring-badge {
  font-size: 0.75rem;
  padding: 0 0.25rem;
  color: #475569;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
}

.flyer-badge {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0 0.3rem;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-radius: 3px;
  vertical-align: middle;
  white-space: nowrap;
  color: #1d4ed8;
  background: #dbeafe;
  border: 1px solid #93c5fd;
}

.event-row .event-when {
  font-size: 0.8rem;
  color: #475569;
  margin-top: 0.15rem;
}

.event-row .event-where {
  font-size: 0.8rem;
  color: #64748b;
  margin-top: 0.1rem;
}

.day-header {
  position: sticky;
  top: 0;
  padding: 0.5rem 0.9rem;
  background: var(--day-color, #0f2a43);
  color: #0f2a43;
  font-weight: 700;
  font-size: 0.9rem;
  letter-spacing: 0.02em;
}

.event-details {
  margin-top: 0.4rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.9rem;
}

.event-details-link {
  font-size: 0.8rem;
  color: #2563eb;
  text-decoration: none;
  font-weight: 600;
}

.event-details-link:hover {
  text-decoration: underline;
}

#map-static-notice {
  flex: none;
  padding: 0.6rem 1rem;
  background: #eff6ff;
  color: #1e40af;
  font-size: 0.8rem;
  border-bottom: 1px solid #bfdbfe;
}

.map-popup {
  font-size: 0.82rem;
  line-height: 1.4;
  color: #0f2a43;
}

.map-popup a {
  color: #2563eb;
}

.map-popup-desc {
  display: block;
  margin-top: 0.3rem;
  color: #475569;
}

.map-popup-contact {
  display: block;
  margin-top: 0.3rem;
  color: #475569;
}

.map-popup-links {
  margin-top: 0.4rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.9rem;
}

/* Full-size flyer overlay */
#lightbox {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: rgba(15, 42, 67, 0.88);
}

#lightbox[hidden] {
  display: none;
}

.lightbox-img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 6px;
  box-shadow: 0 10px 44px rgba(0, 0, 0, 0.5);
}

.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1.25rem;
  width: 2.5rem;
  height: 2.5rem;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  color: #0f2a43;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.lightbox-close:hover {
  background: #ffffff;
}

/* Always-on labels on the Leaflet fallback map - mirrors the GL map's boxed
   labels. */
.leaflet-tooltip.map-label {
  margin: 0; /* neutralise Leaflet's direction margins - we position these ourselves */
  padding: 3px 7px;
  background: rgba(255, 255, 255, 0.72);
  border: 2px solid rgba(15, 42, 67, 0.35); /* border-color set per-pin from JS */
  border-radius: 7px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
  color: #0f2a43;
  font: inherit;
  line-height: 1.2;
  white-space: nowrap;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff; /* keep text legible over the wash */
}

.leaflet-tooltip.map-label::before {
  display: none; /* drop the callout arrow */
}

.leaflet-tooltip.map-label.selected {
  border-width: 3px;
  padding: 2px 6px;
}

.map-label-name {
  display: block;
  font-weight: 600;
  font-size: 11px;
}

.map-label-date {
  display: block;
  font-size: 10px;
  color: #475569;
}

/* ------------------------------------------------------------------ */
/* Table view                                                          */
/* ------------------------------------------------------------------ */

#event-table-view {
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.table-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #e2e8f0;
  background: #f8fafc;
}

.table-toolbar label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: #475569;
}

.table-toolbar select,
.table-toolbar input {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.25rem 0.4rem;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: white;
}

.table-toolbar .table-search input {
  min-width: 180px;
}

.table-count {
  margin-left: auto;
  font-size: 0.8rem;
  color: #64748b;
}

.table-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0px));
}

#events-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

#events-table thead th {
  position: sticky;
  top: 0;
  background: #0f2a43;
  color: white;
  text-align: left;
  padding: 0.5rem 0.75rem;
  white-space: nowrap;
  user-select: none;
}

#events-table th[data-sort] {
  cursor: pointer;
}

#events-table th[data-sort]:hover {
  background: #1b3d5c;
}

#events-table th.sorted {
  background: #1b3d5c;
}

#events-table tbody td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #e2e8f0;
  border-left: 0 solid transparent;
}

#events-table tbody tr {
  cursor: pointer;
  border-left: 4px solid var(--row-color, #6b7280);
}

#events-table tbody tr:hover {
  background: #f8fafc;
}

#events-table tbody tr.active {
  background: #eef2f7;
  font-weight: 600;
}

#events-table td[data-col="name"] {
  font-weight: 600;
  color: #0f2a43;
}

.table-link a {
  color: #2563eb;
  text-decoration: none;
  white-space: nowrap;
}

.table-link a:hover {
  text-decoration: underline;
}

.table-empty {
  padding: 1.5rem;
  text-align: center;
  color: #64748b;
}

#debug-panel {
  position: fixed;
  bottom: 8px;
  left: 8px;
  z-index: 1000;
  padding: 6px 8px;
  background: rgba(15, 42, 67, 0.85);
  color: #e2e8f0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  line-height: 1.5;
  border-radius: 4px;
  pointer-events: none;
}

@media (max-width: 768px) {
  /* Mobile is map + days table only - no events table, no view toggle,
     no drag handles, no debug panel. */
  .view-toggle,
  .split-handle,
  .split-handle-v,
  #debug-panel {
    display: none;
  }

  /* Belt and braces: even if a desktop "split"/"table" state is stored, mobile
     shows the map + days table and never the events table. */
  #event-table-view {
    display: none !important;
  }
  .split .layout {
    display: flex !important;
  }

  header {
    padding: 0.6rem 1rem;
  }

  header h1 {
    font-size: 1.05rem;
  }

  header p {
    font-size: 0.8rem;
  }

  /* Let the PAGE scroll on mobile. Nested #event-list scrolling inside a
     100dvh flex shell still clipped the last day/event on iPhone. */
  body {
    height: auto;
    max-height: none;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .split,
  .layout {
    flex: none;
    height: auto;
    min-height: 0;
    overflow: visible;
  }

  .layout {
    flex-direction: column;
  }

  #map {
    height: 42vh;
    height: 42dvh;
    min-height: 220px;
    flex: none;
  }

  #event-list {
    flex: none;
    height: auto;
    min-height: 0;
    overflow: visible;
    border-top: 1px solid #e2e8f0;
    /* Plenty of room past the last event (home indicator + thumb reach) */
    padding-bottom: calc(6rem + env(safe-area-inset-bottom, 0px));
  }

  .event-row:last-child {
    margin-bottom: 1rem;
  }

  /* Labels scale with zoom so they don't swamp a small screen when zoomed out.
     --label-scale is set from the map zoom in updateLeafletLabels(). */
  .map-label-name {
    font-size: calc(11px * var(--label-scale, 1));
  }

  .map-label-date {
    font-size: calc(10px * var(--label-scale, 1));
  }
}

/* Shown when the page is opened with ?version (yellow on dark header) */
#map-version {
  margin: 8px 0 0;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: #0f2a43;
  background: #fde68a;
  border: 1px solid #f59e0b;
  border-radius: 6px;
  width: fit-content;
}
#map-version[hidden] {
  display: none !important;
}
#map-version:not([hidden]) {
  display: block !important;
}

/* Quiet "Map by" credit, top-right of header */
header .credit {
  position: absolute;
  top: 0.75rem;
  right: 1rem;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.7rem;
  color: #8fa3b8;
  opacity: 0.85;
  text-decoration: none;
}
header .credit:hover { opacity: 1; }
header .credit-logo {
  display: block;
  height: 32px;
  width: auto;
  padding: 3px 8px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 999px;
}
@media (max-width: 640px) {
  header .credit {
    position: static;
    margin-top: 0.5rem;
  }
  header .credit-logo { height: 28px; }
}

/* Slim Light On credit atop event list/table */
.list-credit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 4px;
  margin: 12px 0 4px;
  font-size: 0.75rem;
  color: #8a96a3;
  text-decoration: none;
}
.list-credit:hover { color: #4a5868; }
.list-credit img { display: block; height: 48px; width: auto; }
.split[data-view="split"] #event-table-view .list-credit { display: none; }
