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

:root {
  --bg:        #0f1117;
  --surface:   #1a1d27;
  --surface2:  #252836;
  --border:    #2e3247;
  --text:      #e8eaf0;
  --muted:     #8890aa;
  --gold:      #f5c842;
  --amber:     #ffaa33;
  --sunset:    #ff5e2b;
  --sunrise:   #ffd166;
  --panel-w:   320px;
}

/* ── Light theme colours ──────────────────────────────────────
   Auto mode: follows system preference when no explicit choice is saved.
   Explicit: [data-theme="light"] / [data-theme="dark"] set by the toggle.
───────────────────────────────────────────────────────────────────────── */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg:      #f4f5f8;
    --surface: #ffffff;
    --surface2: #eef0f6;
    --border:  #d0d4e8;
    --text:    #1a1d2a;
    --muted:   #6670a0;
    --gold:    #b07800;
    --amber:   #8a5600;
    --sunrise: #9a6800;
  }
}
:root[data-theme="light"] {
  --bg:      #f4f5f8;
  --surface: #ffffff;
  --surface2: #eef0f6;
  --border:  #d0d4e8;
  --text:    #1a1d2a;
  --muted:   #6670a0;
  --gold:    #b07800;
  --amber:   #8a5600;
  --sunrise: #9a6800;
}

html, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); overflow: hidden; }

/* ── Layout ───────────────────────────────────────────────── */
#app { display: flex; height: 100vh; height: 100dvh; width: 100vw; }

#map-wrap { position: relative; flex: 1; min-width: 0; -webkit-user-select: none; user-select: none; }
#map { width: 100%; height: 100%; }

/* ── Sidebar ──────────────────────────────────────────────── */
#sidebar {
  width: var(--panel-w);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
  z-index: 10;
}

.sidebar-header {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.sidebar-header h1 { font-size: 1.05rem; font-weight: 700; color: var(--gold); letter-spacing: .03em; }
.sidebar-header p  { font-size: .72rem; color: var(--muted); margin-top: 2px; }

/* Theme toggle button — icon only, no chrome */
.theme-btn {
  background: none;
  border: none;
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.05rem;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition: color .15s, background .15s;
}
.theme-btn:hover { color: var(--text); background: var(--surface2); }

/* Date picker */
.date-row {
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.date-row label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; white-space: nowrap; flex-shrink: 0; }
#date-input {
  width: auto;
  max-width: 160px;
  padding: 6px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: .85rem;
  cursor: pointer;
}
#date-input::-webkit-calendar-picker-indicator { filter: invert(.6); }
/* Light mode: picker icon is already dark, no invert needed */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) #date-input::-webkit-calendar-picker-indicator { filter: none; }
}
:root[data-theme="light"] #date-input::-webkit-calendar-picker-indicator { filter: none; }

/* Sun info cards */
.cards { padding: 14px 20px; display: flex; flex-direction: column; gap: 10px; border-bottom: 1px solid var(--border); }

.card {
  background: var(--surface2);
  border-radius: 8px;
  padding: 12px 14px;
  border-left: 3px solid var(--border);
}
.card.rise { border-left-color: var(--sunrise); }
.card.set  { border-left-color: var(--sunset); }

.card-title { font-size: .68rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin-bottom: 6px; display:flex; align-items:center; gap:5px; }
.card-time  { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.card.rise .card-time { color: var(--sunrise); }
.card.set  .card-time { color: var(--sunset); }
.card-sub   { font-size: .78rem; color: var(--muted); margin-top: 3px; }
.card-golden  { font-size: .75rem; color: var(--amber); margin-top: 4px; display:flex; align-items:center; gap:4px; }
.card-horizon { font-size: .73rem; margin-top: 6px; display:flex; align-items:center; gap:4px; }
.card-horizon.clear   { color: #4ade80; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .card-horizon.clear { color: #166534; }
}
:root[data-theme="light"] .card-horizon.clear { color: #166534; }
.card-horizon.blocked { color: #f87171; }
.card-horizon.loading { color: var(--muted); font-style: italic; }
.card-horizon.error   { color: var(--amber); font-style: italic; }

/* Visibility quality indicator */
.card-vis              { font-size: .73rem; margin-top: 4px; display:flex; align-items:center; gap:4px; }
.card-vis.loading      { color: var(--muted); font-style: italic; }
.card-vis.unavailable  { color: var(--muted); font-style: italic; }
.card-vis.error        { color: var(--amber); font-style: italic; }
.card-vis.vis-excellent { color: #4ade80; }
.card-vis.vis-good      { color: #86efac; }
.card-vis.vis-fair      { color: var(--amber); }
.card-vis.vis-poor      { color: #f87171; }
.card-vis.vis-verypoor  { color: #f87171; }
.card-vis.stale         { opacity: 0.7; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .card-vis.vis-excellent { color: #166534; }
  :root:not([data-theme="dark"]) .card-vis.vis-good      { color: #166534; }
  :root:not([data-theme="dark"]) .card-vis.vis-poor      { color: #b91c1c; }
  :root:not([data-theme="dark"]) .card-vis.vis-verypoor  { color: #991b1b; }
}
:root[data-theme="light"] .card-vis.vis-excellent { color: #166534; }
:root[data-theme="light"] .card-vis.vis-good      { color: #166534; }
:root[data-theme="light"] .card-vis.vis-poor      { color: #b91c1c; }
:root[data-theme="light"] .card-vis.vis-verypoor  { color: #991b1b; }

/* Day length + azimuth row */
.stats { padding: 14px 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; border-bottom: 1px solid var(--border); }
.stat-box { background: var(--surface2); border-radius: 8px; padding: 10px 12px; }
.stat-label { font-size: .65rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin-bottom: 4px; }
.stat-val   { font-size: 1rem; font-weight: 600; color: var(--text); }
.stat-sub      { font-size: .7rem; color: var(--muted); margin-top: 2px; }
.stat-box.span-full { grid-column: 1 / -1; }

/* Sidebar footer */
.sidebar-footer { padding: 12px 20px; text-align: center; border-top: 1px solid var(--border); }
.about-link { font-size: .7rem; color: var(--muted); text-decoration: none; letter-spacing: .04em; }
.about-link:hover { color: var(--gold); }

/* Viewpoints toggles */
.vp-section { padding: 14px 20px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
.vp-section label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; display: flex; align-items: center; gap: 8px; cursor: pointer; }
.toggle-switch { position: relative; width: 34px; height: 18px; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-track {
  position: absolute; inset: 0;
  background: var(--surface2);
  border-radius: 18px;
  border: 1px solid var(--border);
  transition: background .2s;
}
.toggle-track::after {
  content: '';
  position: absolute;
  left: 2px; top: 1px;
  width: 14px; height: 14px;
  background: var(--muted);
  border-radius: 50%;
  transition: transform .2s, background .2s;
}
.toggle-switch input:checked + .toggle-track { background: var(--gold); border-color: var(--gold); }
.toggle-switch input:checked + .toggle-track::after { transform: translateX(16px); background: #fff; }

/* Compass rose overlay */
#locate-btn {
  position: absolute;
  bottom: 102px; right: 22px;
  width: 44px; height: 44px;
  background: rgba(15,17,23,0.72);
  border: 1.5px solid #2e3247;
  border-radius: 8px;
  color: #e8eaf0;
  cursor: pointer;
  z-index: 400;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
#locate-btn:hover  { background: rgba(37,40,54,0.9); }
#locate-btn.active { color: var(--gold); }
#locate-btn.error  { color: #f87171; }
#locate-btn svg    { pointer-events: none; }

#compass {
  position: absolute;
  bottom: 28px; right: 14px;
  width: 60px; height: 60px;
  z-index: 400;
  cursor: pointer;
}

/* Mobile handle — hidden on desktop */
.mobile-handle { display: none; }

/* Peek bar — compact time summary when drawer is collapsed; hidden on desktop */
.peek-bar { display: none; }

/* Mobile bottom-sheet */
@media (max-width: 640px) {
  #app {
    flex-direction: column;
    height: 100dvh; /* dynamic viewport for mobile browsers */
  }

  #map-wrap { flex: 1 1 0; min-height: 0; }

  #sidebar {
    width: 100%;
    height: 50vh;
    flex-shrink: 0;
    border-left: none;
    border-top: 2px solid var(--border);
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    transition: height .3s ease;
  }

  /* Collapsed: handle pill + peek bar showing the two times */
  #sidebar.collapsed {
    height: 76px;
    overflow: hidden;
  }
  /* Expanded: more content visible */
  #sidebar.expanded { height: 82vh; }

  /* Drag handle pill */
  .mobile-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 6px 0;
    flex-shrink: 0;
    cursor: pointer;
    touch-action: none; /* needed so pointer capture works reliably for swipe */
    height: 32px;
  }

  /* Peek bar — three-column: logo | times | theme toggle */
  .peek-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 14px 8px;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--surface);
  }
  .peek-logo { border-radius: 6px; flex-shrink: 0; }
  .peek-times {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
  }
  .peek-rise,
  .peek-set  { display: flex; align-items: center; gap: 5px; font-size: .95rem; font-weight: 700; }
  .peek-rise { color: var(--sunrise); }
  .peek-set  { color: var(--sunset); }
  .peek-icon { width: 16px; height: 16px; flex-shrink: 0; }

  /* Strip logo and theme toggle from sidebar-header on mobile — both live in peek bar */
  .sidebar-header .header-logo { display: none; }
  .sidebar-header .theme-btn   { display: none; }

  .mobile-handle::before {
    content: '';
    display: block;
    width: 40px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    transition: background .15s;
  }
  .mobile-handle:active::before { background: var(--muted); }
  #sidebar.collapsed .mobile-handle::before { background: var(--muted); }

  .sidebar-header { padding: 10px 14px 8px; }
  .sidebar-header h1 { font-size: .9rem; }
  .sidebar-header p  { font-size: .68rem; }

  .date-row {
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
    border-right: none;
    min-width: unset;
  }

  .cards {
    flex-direction: row;
    flex-wrap: nowrap;
    padding: 10px 12px;
    gap: 8px;
    border-bottom: 1px solid var(--border);
    border-right: none;
    min-width: unset;
  }

  .card { flex: 1; min-width: 0; padding: 10px; }
  .card-time { font-size: 1.2rem; }

  .stats {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    border-right: none;
    min-width: unset;
    align-content: unset;
  }

  .vp-section {
    padding: 8px 14px;
    min-width: unset;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px 20px;
    justify-content: unset;
    border-bottom: 1px solid var(--border);
  }

  #compass { bottom: 16px; right: 10px; width: 44px; height: 44px; }
  #locate-btn { bottom: 74px; right: 10px; width: 44px; height: 44px; }
}

/* ── Dark-mode map tiles ──────────────────────────────────────
   Invert & tint OSM tiles so they look at home in the dark UI.
   Flip off for light theme. Only the tile pane is filtered;
   marker/overlay panes are unaffected.
───────────────────────────────────────────────────────────────────────── */
.leaflet-tile-pane { filter: brightness(.65) invert(1) hue-rotate(200deg) saturate(.75); }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .leaflet-tile-pane { filter: none; }
}
:root[data-theme="light"] .leaflet-tile-pane { filter: none; }

/* ── Theme-aware Leaflet popups ──────────────────────────────── */
.leaflet-popup-content-wrapper,
.leaflet-popup-tip { background: var(--surface); color: var(--text); }
.leaflet-popup-content-wrapper {
  box-shadow: 0 3px 14px rgba(0,0,0,.3);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: 'Segoe UI', system-ui, sans-serif;
}
.leaflet-popup-close-button { color: var(--muted) !important; }
.leaflet-popup-close-button:hover { color: var(--text) !important; }

/* Long-press ring indicator */
#longpress-ring {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  margin-left: -26px;
  margin-top: -26px;
  display: none;
}
#longpress-ring.active { display: block; }
#longpress-ring .ring-fill {
  /* stroke-dasharray matches 2π × r where r=20: ≈125.66 */
  stroke-dasharray: 125.66;
  stroke-dashoffset: 125.66;
  transform-origin: 26px 26px;
  transform: rotate(-90deg);  /* start fill from 12 o'clock */
  animation: ring-fill 0.6s linear forwards;
}
@keyframes ring-fill { to { stroke-dashoffset: 0; } }

/* Leaflet overrides */
.leaflet-container { background: #1a2030; }
