:root {
  color-scheme: dark;
  --bg: #05070d;
  --fg: #e6e9f0;
  --muted: #8b93a7;
  --panel: rgba(12, 16, 26, 0.82);
  --border: rgba(255, 255, 255, 0.08);
  --accent: #6aa8ff;

  --c-fajr: #5B3A93;
  --c-dhuhr: #F2B33D;
  --c-asr: #E07A3E;
  --c-maghrib: #C44569;
  --c-isha: #283F6E;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app { position: fixed; inset: 0; }

#globe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}
#globe:active { cursor: grabbing; }

#topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  pointer-events: none;
  z-index: 5;
}

#topbar .title {
  font-weight: 600;
  letter-spacing: 0.2px;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

.muted { color: var(--muted); font-weight: 400; }

#clock {
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  color: var(--muted);
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

#legend {
  position: absolute;
  left: 20px;
  bottom: 20px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  width: 220px;
  z-index: 5;
}

#legend h3 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

#infoBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  background: rgba(106, 168, 255, 0.08);
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
  font-style: italic;
  font-family: Georgia, serif;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  flex-shrink: 0;
}
#infoBtn:hover { background: rgba(106, 168, 255, 0.18); }

#legend ul { list-style: none; padding: 0; margin: 0 0 10px; }
#legend li {
  padding: 1px 0;
  font-size: 13px;
}

.prayer-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  padding: 2px 0;
}
.prayer-toggle input[type="checkbox"] {
  width: 13px;
  height: 13px;
  accent-color: var(--accent);
  margin: 0;
  cursor: pointer;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,0.15);
}

#legend .hint {
  margin: 8px 0 0;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.4;
}

#legend .toggles {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--fg);
  cursor: pointer;
  user-select: none;
}
.toggle input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: var(--accent);
  margin: 0;
  cursor: pointer;
}

#scrubber {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  z-index: 5;
  width: min(560px, calc(100vw - 280px));
}

#liveBtn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-weight: 600;
}
#scrubMode {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 11px;
  cursor: pointer;
  text-transform: lowercase;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  min-width: 24px;
  text-align: center;
}
#scrubMode.date-mode {
  border-color: var(--accent);
  color: var(--accent);
}
#liveBtn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(106, 168, 255, 0.08);
}

#scrub {
  flex: 1;
  appearance: none;
  height: 4px;
  background: rgba(255,255,255,0.12);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}
#scrub::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--fg);
  cursor: grab;
  border: 2px solid var(--accent);
}
#scrub::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--fg);
  cursor: grab;
  border: 2px solid var(--accent);
}

#scrubLabel {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-width: 60px;
  text-align: right;
}

#panelQibla {
  margin-top: 10px;
  font-size: 12px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* info modal */
#infoOverlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
}
#infoOverlay[hidden] { display: none; }

#infoModal {
  position: relative;
  background: var(--panel);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px 28px 28px;
  max-width: 620px;
  max-height: 80vh;
  overflow-y: auto;
  color: var(--fg);
  font-size: 13px;
  line-height: 1.6;
}
#infoModal h2 {
  margin: 0 0 14px;
  font-size: 17px;
}
#infoModal h3 {
  margin: 16px 0 6px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent);
}
#infoModal p { margin: 6px 0; }
#infoModal ul.how-to {
  margin: 4px 0 6px;
  padding-left: 20px;
}
#infoModal ul.how-to li { margin: 6px 0; }
#infoModal dl { margin: 0; }
#infoModal dt {
  font-weight: 600;
  margin-top: 8px;
}
#infoModal dd {
  margin: 2px 0 0 16px;
  color: var(--muted);
}
#infoModal code {
  background: rgba(255,255,255,0.06);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
}
#infoModal a {
  color: var(--accent);
  text-decoration: none;
}
#infoModal a:hover { text-decoration: underline; }

#infoModal .evidence {
  margin-top: 8px;
  padding: 4px 0 4px 12px;
  border-left: 2px solid rgba(106, 168, 255, 0.35);
}
#infoModal .evidence p {
  margin: 6px 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.55;
}
#infoModal .evidence [lang="ar"] {
  display: block;
  font-family: "SBL Bibliit", "Geeza Pro", "Noto Naskh Arabic", "Amiri", serif;
  font-size: 16px;
  line-height: 1.9;
  color: var(--fg);
  margin-bottom: 4px;
}
#infoModal .evidence cite {
  display: inline;
  font-style: normal;
  color: var(--accent);
}

#infoClose {
  position: absolute;
  top: 8px;
  right: 12px;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}
#infoClose:hover { color: var(--fg); }

#panel {
  position: absolute;
  right: 20px;
  top: 60px;
  width: 300px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px 18px;
  z-index: 5;
}

#panel[hidden] { display: none; }

#panelClose {
  position: absolute;
  top: 6px;
  right: 8px;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}
#panelClose:hover { color: var(--fg); }

#panel h2 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
}

#panelCoords, #panelDate { font-size: 12px; }
#panelDate { margin-bottom: 10px; }

#panelCurrent {
  font-size: 12px;
  margin: 8px 0 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
}
#panelCurrent .label {
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  font-size: 10px;
  margin-bottom: 2px;
}
#panelCurrent .value {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
}

#panelTimes {
  width: 100%;
  border-collapse: collapse;
}
#panelTimes td {
  padding: 6px 0;
  font-size: 13px;
  border-top: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
#panelTimes td:first-child {
  display: flex;
  align-items: center;
  gap: 8px;
}
#panelTimes td:last-child {
  text-align: right;
  color: var(--muted);
}
#panelTimes tr.active td { color: var(--fg); font-weight: 600; }
#panelTimes tr.active td:last-child { color: var(--fg); }

#hud {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  width: 280px;
}

#search {
  width: 100%;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--fg);
  font-size: 13px;
  outline: none;
}
#search:focus { border-color: var(--accent); }

#searchResults {
  margin-top: 4px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  max-height: 280px;
  overflow-y: auto;
}

#searchResults .result {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  border-top: 1px solid var(--border);
}
#searchResults .result:first-child { border-top: 0; }
#searchResults .result:hover { background: rgba(255,255,255,0.05); }
#searchResults .result .sub { font-size: 11px; color: var(--muted); }

/* ---------- Mobile / responsive ---------- */

#legendToggle { display: none; }
#panelHandle { display: none; }
#panelPeek { display: none; }

@media (max-width: 768px) {
  #topbar {
    padding: 10px 14px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    padding-left: calc(14px + env(safe-area-inset-left, 0px));
    padding-right: calc(14px + env(safe-area-inset-right, 0px));
    justify-content: flex-end;
  }
  #topbar .title { display: none; }

  #hud {
    width: calc(100vw - 32px);
    max-width: 420px;
    left: calc(16px + env(safe-area-inset-left, 0px));
    top: calc(46px + env(safe-area-inset-top, 0px));
    transform: none;
  }
  #searchResults { max-height: 50vh; }

  #legend {
    left: calc(12px + env(safe-area-inset-left, 0px));
    right: calc(12px + env(safe-area-inset-right, 0px));
    top: calc(98px + env(safe-area-inset-top, 0px));
    bottom: auto;
    width: auto;
    max-width: 320px;
  }
  #legend h3 { gap: 6px; }
  #legendToggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 18px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
  }
  #legend:not(.expanded) > ul,
  #legend:not(.expanded) > .toggles,
  #legend:not(.expanded) > .hint { display: none; }
  #legend.expanded #legendToggle { color: var(--accent); border-color: var(--accent); }

  #scrubber {
    left: calc(12px + env(safe-area-inset-left, 0px));
    right: calc(12px + env(safe-area-inset-right, 0px));
    transform: none;
    width: auto;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  #panel {
    top: auto;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-height: 65vh;
    overflow-y: auto;
    border-radius: 14px 14px 0 0;
    padding: 14px 18px calc(18px + env(safe-area-inset-bottom, 0px));
    border-bottom: 0;
    transition: transform 0.25s ease;
    will-change: transform;
  }
  #panel.dragging { transition: none; }
  #panelHandle {
    display: block;
    position: sticky;
    top: 0;
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: var(--muted);
    opacity: 0.45;
    margin: -4px auto 8px;
    touch-action: none;
    cursor: grab;
  }
  #panelHandle:active { cursor: grabbing; }

  #panelPeek {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--panel);
    backdrop-filter: blur(10px);
    border: 0;
    border-top: 1px solid var(--border);
    color: var(--fg);
    padding: 14px 16px calc(12px + env(safe-area-inset-bottom, 0px));
    z-index: 5;
    display: flex;
    align-items: baseline;
    gap: 10px;
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    touch-action: none;
    transition: transform 0.25s ease;
    will-change: transform;
  }
  #panelPeek[hidden] { display: none; }
  #panelPeek.dragging { transition: none; }
  #panelPeek .peekHandle {
    position: absolute;
    top: 4px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: var(--muted);
    opacity: 0.45;
  }
  #panelPeek .peekName {
    flex: 1;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #panelPeek .peekCur {
    color: var(--muted);
    font-size: 12px;
    flex-shrink: 0;
  }
  body:has(#panel:not([hidden])) #scrubber { display: none; }
  body:has(#panelPeek:not([hidden])) #scrubber {
    bottom: calc(58px + env(safe-area-inset-bottom, 0px));
  }

  #panelClose {
    width: 36px;
    height: 36px;
    top: 8px;
    right: 10px;
    font-size: 26px;
  }

  #infoModal {
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
    padding: 18px 20px 22px;
  }
  #infoClose { width: 36px; height: 36px; }
}

@media (max-width: 480px) {
  #legend { padding: 10px 12px; }
  #panel h2 { font-size: 15px; }
}

@media (pointer: coarse) {
  #infoBtn,
  #panelClose,
  #infoClose,
  #liveBtn,
  #scrubMode,
  #legendToggle {
    min-width: 44px;
    min-height: 44px;
  }
  .prayer-toggle,
  .toggle { padding: 8px 0; }
  #scrub::-webkit-slider-thumb { width: 22px; height: 22px; }
  #scrub::-moz-range-thumb { width: 22px; height: 22px; }

  /* backdrop-filter forces the compositor to re-blur the canvas behind
     every overlay on every frame the canvas redraws. That's the dominant
     cost during a fling on iOS Safari. Turn it off on touch devices and
     compensate with a more opaque solid background. */
  #legend,
  #panel,
  #scrubber,
  #search,
  #searchResults,
  #infoModal,
  #panelPeek {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(12, 16, 26, 0.95);
  }
  #infoOverlay { backdrop-filter: none; -webkit-backdrop-filter: none; }
}
