:root {
  color-scheme: dark;
  --paper: #f3efe2;
  --muted: #aeb7ac;
  --panel: rgba(18, 21, 20, 0.9);
  --panel-strong: rgba(12, 14, 13, 0.96);
  --line: rgba(243, 239, 226, 0.16);
  --amber: #f7b955;
  --amber-strong: #ffcf6d;
  --blue: #78bdf9;
  --green: #5dd6a8;
  --map-bg: #17201d;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  min-height: 100vh;
  min-height: 100svh;
  background: var(--map-bg);
  color: var(--paper);
  overflow: hidden;
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  color: var(--paper);
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
}

button:hover {
  border-color: rgba(247, 185, 85, 0.55);
  background: rgba(247, 185, 85, 0.14);
}

button:active {
  transform: translateY(1px);
}

.app-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100svh;
  height: 100dvh;
  min-width: 320px;
  overflow: hidden;
}

#mapCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.control-panel,
.horizon-panel,
.time-panel,
.mobile-tabs,
.map-help,
.attribution {
  position: absolute;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 18px 70px rgba(0, 0, 0, 0.34);
  backdrop-filter: blur(18px);
}

.mobile-tabs {
  display: none;
}

.control-panel {
  top: 16px;
  left: 16px;
  width: min(360px, calc(100vw - 32px));
  padding: 16px;
  border-radius: 8px;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--green);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.05;
  letter-spacing: 0;
}

.icon-button,
.small-button,
.button-row button {
  min-height: 36px;
  padding: 0 11px;
  border-radius: 7px;
  white-space: nowrap;
}

.field-row,
.coord-row,
.button-row {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.field-row {
  grid-template-columns: 1fr auto;
  align-items: end;
}

.coord-row,
.button-row,
.segmented {
  grid-template-columns: 1fr 1fr;
}

.coord-row {
  grid-template-columns: 1fr 1fr minmax(82px, 0.8fr);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  margin-top: 10px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.18);
}

.segmented button {
  min-width: 0;
  min-height: 32px;
  border-color: transparent;
  border-radius: 6px;
  padding: 0 6px;
  color: var(--muted);
  background: transparent;
  font-size: 0.78rem;
  font-weight: 800;
}

.segmented button[aria-pressed="true"] {
  color: #151917;
  background: var(--amber);
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

input[type="date"],
input[type="number"] {
  width: 100%;
  min-width: 0;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 10px;
  color: var(--paper);
  background: rgba(0, 0, 0, 0.2);
  outline: none;
}

input:focus {
  border-color: rgba(120, 189, 249, 0.8);
  box-shadow: 0 0 0 3px rgba(120, 189, 249, 0.14);
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin: 14px 0 0;
}

.stats-grid div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.055);
}

dt {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

dd {
  margin: 0;
  min-height: 1.35em;
  overflow-wrap: anywhere;
  font-size: 1rem;
  font-weight: 750;
  letter-spacing: 0;
}

.status-text {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.horizon-panel {
  right: 16px;
  bottom: 104px;
  width: min(390px, calc(100vw - 32px));
  height: 244px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel-strong);
}

#horizonCanvas {
  display: block;
  width: 100%;
  height: 208px;
  touch-action: manipulation;
}

.horizon-readout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  height: 36px;
  padding: 0 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.04);
}

.horizon-readout > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.horizon-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.horizon-toggle input {
  width: 14px;
  height: 14px;
  margin: 0;
  accent-color: var(--amber);
}

.time-panel {
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  min-height: 72px;
  border-radius: 8px;
  padding: 12px 14px 14px;
}

.time-meta {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.time-meta strong {
  color: var(--paper);
}

#riseLabel {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#currentClock {
  color: rgba(243, 239, 226, 0.78);
  white-space: nowrap;
}

.range-shell {
  position: relative;
  height: 26px;
}

input[type="range"] {
  position: absolute;
  left: 0;
  right: 0;
  top: 4px;
  z-index: 2;
  width: 100%;
  margin: 0;
  accent-color: var(--amber);
  background: transparent;
}

.event-marker {
  --marker-left: -10%;
  position: absolute;
  top: 0;
  left: var(--marker-left);
  z-index: 1;
  width: 2px;
  height: 24px;
  border-radius: 999px;
  transform: translateX(-1px);
}

.event-marker::before {
  position: absolute;
  top: -4px;
  left: 50%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  content: "";
  transform: translateX(-50%);
}

.rise-marker {
  background: var(--amber-strong);
  box-shadow: 0 0 16px rgba(255, 207, 109, 0.75);
}

.rise-marker::before {
  background: var(--amber-strong);
}

.set-marker {
  background: var(--blue);
  box-shadow: 0 0 16px rgba(120, 189, 249, 0.72);
}

.set-marker::before {
  background: var(--blue);
}

.now-marker {
  top: -3px;
  z-index: 3;
  width: 2px;
  height: 30px;
  background: #f3efe2;
  box-shadow: 0 0 12px rgba(243, 239, 226, 0.78);
}

.now-marker::before {
  top: 25px;
  width: 9px;
  height: 9px;
  border: 2px solid rgba(12, 14, 13, 0.78);
  background: #f3efe2;
}

.map-help {
  right: 16px;
  top: 16px;
  max-width: 250px;
  padding: 9px 11px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.78rem;
  pointer-events: none;
}

.attribution {
  right: 16px;
  bottom: 88px;
  padding: 5px 8px;
  border-radius: 6px;
  color: rgba(243, 239, 226, 0.66);
  font-size: 0.67rem;
}

.attribution a {
  color: inherit;
  text-underline-offset: 2px;
}

@media (max-width: 780px) {
  .mobile-tabs {
    top: max(10px, env(safe-area-inset-top));
    left: 10px;
    right: 10px;
    z-index: 8;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    padding: 4px;
    border-radius: 8px;
  }

  .mobile-tabs button {
    min-width: 0;
    min-height: 34px;
    border-color: transparent;
    border-radius: 6px;
    color: var(--muted);
    background: transparent;
    font-size: 0.78rem;
    font-weight: 850;
  }

  .mobile-tabs button[aria-pressed="true"] {
    color: #151917;
    background: var(--amber);
  }

  .control-panel {
    display: none;
    top: calc(max(10px, env(safe-area-inset-top)) + 50px);
    left: 10px;
    right: 10px;
    z-index: 7;
    width: calc(100vw - 20px);
    max-height: calc(50dvh - max(10px, env(safe-area-inset-top)) - 60px);
    padding: 13px;
    overflow: auto;
    overscroll-behavior: contain;
  }

  .app-shell.mobile-tab-settings .control-panel {
    display: block;
  }

  .app-shell.mobile-tab-hidden .control-panel,
  .app-shell.mobile-tab-moon .control-panel {
    display: none;
  }

  .panel-heading {
    margin-bottom: 10px;
  }

  h1 {
    font-size: 1.22rem;
  }

  .stats-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
  }

  .stats-grid div {
    padding: 8px;
  }

  dt {
    font-size: 0.6rem;
  }

  dd {
    font-size: 0.78rem;
  }

  .status-text {
    display: none;
  }

  .horizon-panel {
    display: none;
    top: calc(max(10px, env(safe-area-inset-top)) + 50px);
    left: 10px;
    right: 10px;
    bottom: auto;
    z-index: 7;
    width: auto;
    height: calc(50dvh - max(10px, env(safe-area-inset-top)) - 60px);
    min-height: 150px;
  }

  #horizonCanvas {
    height: calc(100% - 34px);
  }

  .app-shell.mobile-tab-moon .horizon-panel {
    display: block;
  }

  .app-shell.mobile-tab-settings .horizon-panel,
  .app-shell.mobile-tab-hidden .horizon-panel {
    display: none;
  }

  .horizon-readout {
    height: 34px;
    padding: 0 9px;
    font-size: 0.68rem;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .horizon-toggle {
    display: none;
  }

  .map-help {
    display: none;
  }

  .attribution {
    display: none;
  }

  .time-panel {
    left: 10px;
    right: 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
  }

  .time-meta {
    grid-template-columns: auto minmax(0, 1fr);
  }

  #currentClock {
    display: none;
  }
}

@media (max-width: 540px) {
  .field-row {
    grid-template-columns: 1fr 74px;
  }

  .coord-row,
  .button-row {
    grid-template-columns: 1fr 1fr;
  }

  .coord-row label:last-child {
    grid-column: 1 / -1;
  }

  .segmented button {
    font-size: 0.68rem;
  }

  .horizon-panel {
    height: calc(50dvh - max(10px, env(safe-area-inset-top)) - 60px);
    min-height: 138px;
  }

  #horizonCanvas {
    height: calc(100% - 30px);
  }

  .horizon-readout {
    height: 30px;
    display: grid;
    gap: 0;
    align-content: center;
    font-size: 0.58rem;
    line-height: 1.15;
  }
}

@media (max-width: 780px) and (orientation: portrait) {
  .mobile-tabs {
    right: calc(max(8px, env(safe-area-inset-right)) + 74px);
  }

  .control-panel,
  .horizon-panel {
    right: calc(max(8px, env(safe-area-inset-right)) + 74px);
    width: auto;
  }

  .time-panel {
    top: max(10px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    left: auto;
    width: 64px;
    min-height: 0;
    padding: 9px 7px 10px;
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 8px;
  }

  .time-meta {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2px;
    margin: 0;
    text-align: center;
    font-size: 0.72rem;
    line-height: 1.1;
  }

  #riseLabel,
  #currentClock {
    display: none;
  }

  .range-shell {
    width: 44px;
    height: 100%;
    min-height: 0;
    justify-self: center;
  }

  input[type="range"] {
    top: 0;
    left: 50%;
    right: auto;
    width: 34px;
    height: 100%;
    transform: translateX(-50%);
    writing-mode: vertical-lr;
    direction: rtl;
  }

  .event-marker {
    top: calc(100% - var(--marker-left));
    left: 50%;
    width: 28px;
    height: 2px;
    transform: translate(-50%, -1px);
  }

  .event-marker::before {
    top: 50%;
    left: 100%;
    transform: translate(-45%, -50%);
  }

  .now-marker {
    top: calc(100% - var(--marker-left));
    width: 34px;
    height: 2px;
  }

  .now-marker::before {
    top: 50%;
    left: 100%;
    transform: translate(-35%, -50%);
  }
}
