:root {
  --ink: #18282e;
  --muted: #62767e;
  --page: #edf2f2;
  --surface: #ffffff;
  --line: #d2dddf;
  --accent: #176b72;
  --accent-dark: #10545a;
  --danger: #a33b2b;
  --warning: #b77812;
  --map-panel: rgba(255, 255, 255, 0.96);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  color: var(--ink);
  background: var(--page);
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  letter-spacing: 0;
}
button, input, select { font: inherit; }
a { color: var(--accent-dark); }

.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-shell { width: min(420px, 100%); }
.auth-panel {
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 12px 36px rgba(24, 40, 46, 0.12);
}
.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  margin-bottom: 18px;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  background: var(--accent);
  font-size: 1.25rem;
  font-weight: 800;
}
.auth-panel h1 { margin: 0; font-size: 1.55rem; }
.auth-copy { margin: 8px 0 20px; color: var(--muted); line-height: 1.5; }
.auth-form { display: grid; gap: 9px; }
.auth-form label, .settings-form label { color: var(--muted); font-size: 0.82rem; font-weight: 650; }
.auth-form input, .settings-form input, .control-select, .control-input {
  width: 100%;
  min-height: 44px;
  padding: 9px 11px;
  border: 1px solid #b8c8cb;
  border-radius: 6px;
  color: var(--ink);
  background: #fff;
}
.auth-form input:focus, .settings-form input:focus, .control-select:focus, .control-input:focus {
  outline: 3px solid rgba(23, 107, 114, 0.18);
  border-color: var(--accent);
}
.button {
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #fff;
  cursor: pointer;
  font-weight: 700;
}
.button-primary { border-color: var(--accent); color: #fff; background: var(--accent); }
.button-primary:hover { background: var(--accent-dark); }
.button-danger { border-color: #e2b9b1; color: #812b20; background: #fff5f3; }
.auth-form .button { margin-top: 8px; }
.back-link { display: inline-block; margin-top: 20px; color: var(--muted); font-size: 0.82rem; }
.alert { margin: 12px 0; padding: 10px 12px; border-radius: 6px; font-size: 0.86rem; }
.alert-error { border: 1px solid #ebc2bb; color: #78291f; background: #fff3f1; }
.alert-success { border: 1px solid #b9ddd2; color: #155f4c; background: #eefaf6; }

.app-header {
  z-index: 1000;
  display: flex;
  min-height: 56px;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.app-brand { color: var(--ink); text-decoration: none; font-size: 1.05rem; font-weight: 800; }
.header-nav { display: flex; align-items: center; gap: 4px; }
.header-nav a {
  min-height: 38px;
  padding: 9px 11px;
  border-radius: 6px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.84rem;
  font-weight: 700;
}
.header-nav a[aria-current="page"] { color: #fff; background: var(--accent); }

.settings-main { width: min(900px, 100%); margin: 0 auto; padding: 22px 16px 50px; }
.settings-title h1 { margin: 0; font-size: 1.6rem; }
.settings-title p { margin: 5px 0 20px; color: var(--muted); }
.settings-section {
  margin-bottom: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}
.settings-section h2 { margin: 0 0 14px; font-size: 1rem; }
.device-list { display: grid; }
.device-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.device-row:last-child { border-bottom: 0; }
.device-row strong, .device-row span, .device-row small { display: block; }
.device-row span { margin-top: 3px; color: var(--muted); font-size: 0.8rem; }
.device-row small { max-width: 620px; margin-top: 3px; overflow: hidden; color: #809198; text-overflow: ellipsis; white-space: nowrap; }
.settings-form { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; align-items: end; }
.settings-form label { display: grid; gap: 5px; }
.settings-note { margin: -4px 0 12px; color: var(--muted); font-size: 0.8rem; }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.provider-health { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; overflow: hidden; border: 1px solid var(--line); border-radius: 7px; background: var(--line); }
.provider-health div { min-width: 0; padding: 10px; background: #fff; }
.provider-health strong, .provider-health span, .provider-health small { display: block; overflow-wrap: anywhere; }
.provider-health span { margin-top: 4px; color: var(--accent); font-size: 0.78rem; }
.provider-health small { margin-top: 3px; color: var(--muted); font-size: 0.68rem; }

@media (max-width: 620px) {
  .auth-page { padding: 12px; }
  .auth-panel { padding: 20px; }
  .settings-form { grid-template-columns: 1fr; }
  .device-row { align-items: flex-start; }
  .device-row small { max-width: 220px; }
}

.monitor-page, .history-page { height: 100vh; overflow: hidden; }
.header-identity { display: flex; align-items: center; gap: 10px; min-width: 0; }
.status-chip {
  overflow: hidden;
  max-width: 190px;
  padding: 5px 8px;
  border-radius: 5px;
  color: #35545a;
  background: #e8f0f1;
  font-size: 0.72rem;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-chip.live { color: #155f4c; background: #e5f5ef; }
.status-chip.warning { color: #7a4c08; background: #fff2da; }
.monitor-layout {
  position: relative;
  display: grid;
  height: calc(100vh - 56px);
  grid-template-columns: minmax(0, 1fr) 370px;
}
.map-region { position: relative; min-width: 0; }
#storm-map, #history-map { width: 100%; height: 100%; background: #dfe8e8; }
.map-toolbar {
  position: absolute;
  z-index: 700;
  top: 10px;
  left: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: calc(100% - 20px);
}
.window-control {
  display: flex;
  padding: 3px;
  border: 1px solid rgba(166, 184, 188, 0.9);
  border-radius: 7px;
  background: var(--map-panel);
  box-shadow: 0 2px 8px rgba(24, 40, 46, 0.14);
}
.window-button, .tool-button {
  min-width: 42px;
  min-height: 38px;
  padding: 6px 10px;
  border: 0;
  border-radius: 5px;
  color: #455e65;
  background: transparent;
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 750;
}
.window-button.active { color: #fff; background: var(--accent); }
.tool-button {
  border: 1px solid rgba(166, 184, 188, 0.9);
  background: var(--map-panel);
  box-shadow: 0 2px 8px rgba(24, 40, 46, 0.14);
}
.tool-button[aria-pressed="true"] { color: #fff; border-color: var(--accent); background: var(--accent); }
.tool-button:disabled { cursor: not-allowed; opacity: 0.55; }
.map-legend {
  position: absolute;
  z-index: 700;
  bottom: 84px;
  left: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  max-width: calc(100% - 20px);
  padding: 8px 10px;
  border: 1px solid rgba(166, 184, 188, 0.9);
  border-radius: 7px;
  color: #455e65;
  background: var(--map-panel);
  font-size: 0.7rem;
}
.radar-controls {
  position: absolute;
  z-index: 700;
  right: 10px;
  bottom: 10px;
  left: 10px;
  display: grid;
  grid-template-columns: auto minmax(120px, 1fr) auto auto auto;
  gap: 9px;
  align-items: center;
  padding: 7px 9px;
  border: 1px solid rgba(166, 184, 188, 0.9);
  border-radius: 7px;
  background: var(--map-panel);
  box-shadow: 0 2px 8px rgba(24, 40, 46, 0.14);
  font-size: 0.69rem;
}
.radar-window { display: flex; gap: 2px; }
.radar-window button { min-height: 30px; padding: 4px 7px; border: 0; border-radius: 4px; color: var(--muted); background: transparent; cursor: pointer; font-size: 0.68rem; font-weight: 700; }
.radar-window button.active { color: #fff; background: var(--accent); }
.radar-controls input[type="range"] { min-width: 80px; accent-color: var(--accent); }
.radar-controls time { white-space: nowrap; font-weight: 700; }
.opacity-control { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.opacity-control input { width: 75px; }
.radar-controls a { color: var(--muted); white-space: nowrap; }
.map-legend span { display: inline-flex; align-items: center; gap: 5px; }
.map-options-panel {
  position: absolute;
  z-index: 850;
  top: 58px;
  right: 10px;
  width: 220px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 5px 18px rgba(24, 40, 46, 0.18);
}
.map-options-panel[hidden] { display: none; }
.map-options-panel strong, .map-options-panel small { display: block; }
.map-options-panel strong { margin-bottom: 8px; font-size: 0.78rem; }
.map-options-panel .map-option-heading { margin-top: 12px; }
.map-options-panel label { display: flex; align-items: center; gap: 7px; margin-top: 10px; font-size: 0.75rem; }
.map-options-panel small { margin-top: 8px; color: var(--muted); font-size: 0.66rem; line-height: 1.35; }
.map-style-control, .loop-speed-control { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; }
.map-style-control button, .loop-speed-control button { min-height: 34px; border: 1px solid var(--line); border-radius: 5px; color: var(--muted); background: #fff; cursor: pointer; font-size: 0.72rem; font-weight: 700; }
.map-style-control button.active, .loop-speed-control button.active { color: #fff; border-color: var(--accent); background: var(--accent); }
.legend-bolt, .lightning-bolt {
  display: inline-block;
  background: var(--bolt-color);
  clip-path: polygon(45% 0, 100% 0, 65% 39%, 93% 39%, 25% 100%, 39% 55%, 7% 55%);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.78));
}
.legend-bolt { width: 9px; height: 14px; }
.age-now { --bolt-color: #ffffff; }
.age-5 { --bolt-color: #ff4fe1; }
.age-10 { --bolt-color: #41f4ff; }
.age-20 { --bolt-color: #e9ff4f; }
.age-older { --bolt-color: #c5a7ff; opacity: 0.78; }
.lightning-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  border: 2px solid rgba(255, 255, 255, 0.96);
  border-radius: 50%;
  background: rgba(8, 12, 18, 0.88);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.95), 0 1px 4px rgba(0, 0, 0, 0.9);
}
.lightning-bolt { width: 62%; height: 72%; }
.lightning-bolt.age-now { filter: drop-shadow(0 0 3px #fff) drop-shadow(0 1px 1px #000); }
.lightning-bolt.polarity-positive { outline: 1px solid #fff; outline-offset: 1px; }
.lightning-bolt.type-ic { transform: scale(0.78); }
.town-label-marker { border: 0; background: transparent; }
.town-label-marker span {
  display: block;
  overflow: visible;
  color: #fff;
  font-size: 11px;
  font-weight: 750;
  line-height: 20px;
  text-align: center;
  text-shadow: -1px -1px 0 #23343a, 1px -1px 0 #23343a, -1px 1px 0 #23343a, 1px 1px 0 #23343a, 0 1px 2px #000;
  white-space: nowrap;
}
.town-label-far span { font-size: 8px; line-height: 16px; }
.town-label-mid span { font-size: 10px; line-height: 18px; }
.town-label-near span { font-size: 11px; line-height: 20px; }
.leaflet-radar-pane .leaflet-layer,
.leaflet-history-radar-pane .leaflet-layer { transition: opacity 180ms linear; }

.storm-drawer {
  z-index: 800;
  overflow: hidden;
  border-left: 1px solid var(--line);
  background: var(--surface);
}
.drawer-handle {
  display: none;
  width: 100%;
  min-height: 42px;
  border: 0;
  color: var(--ink);
  background: var(--surface);
}
.drawer-grip { width: 38px; height: 4px; border-radius: 4px; background: #a9b8bc; }
.drawer-handle-summary { display: none; }
.drawer-content { height: 100%; overflow-y: auto; padding: 12px; }
.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border: 1px solid var(--line);
  border-radius: 8px;
}
.summary-strip div { min-width: 0; padding: 10px; border-right: 1px solid var(--line); }
.summary-strip div:last-child { border-right: 0; }
.summary-strip span, .summary-strip strong { display: block; }
.summary-strip span { color: var(--muted); font-size: 0.7rem; }
.summary-strip strong { margin-top: 3px; overflow-wrap: anywhere; font-size: 1.08rem; }
.source-message {
  margin: 10px 0 16px;
  padding: 9px 10px;
  border-left: 3px solid var(--warning);
  border-radius: 4px;
  color: #6d4c18;
  background: #fff7e9;
  font-size: 0.76rem;
  line-height: 1.4;
}
.source-message.live { border-color: var(--accent); color: #31585d; background: #edf7f6; }
.source-message.test { border-color: #b65c08; color: #713c08; background: #fff3df; }
.test-badge { display: inline-block; padding: 2px 4px; border-radius: 3px; color: #fff; background: #b65c08; font-size: 0.58rem; font-weight: 800; letter-spacing: 0; vertical-align: 1px; }
.drawer-section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 10px 0 7px; }
.drawer-section-title h2 { margin: 0; font-size: 0.9rem; }
.drawer-section-title span { color: var(--muted); font-size: 0.69rem; }
.cell-list { display: grid; gap: 7px; }
.cell-card {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfdfd;
  cursor: pointer;
}
.cell-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cell-card h3 { margin: 0; font-size: 0.88rem; }
.trend { font-size: 0.69rem; font-weight: 750; text-transform: capitalize; }
.trend.strengthening { color: #a33b2b; }
.trend.weakening { color: #176b72; }
.cell-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; margin-top: 7px; color: var(--muted); font-size: 0.72rem; }
.empty-state { padding: 18px 10px; color: var(--muted); text-align: center; font-size: 0.8rem; }
.pws-panel, .quota-panel { margin-top: 16px; }
.pws-readings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 7px; }
.pws-reading { min-width: 0; padding: 8px; border-right: 1px solid var(--line); }
.pws-reading:nth-child(3n) { border-right: 0; }
.pws-reading span, .pws-reading strong { display: block; }
.pws-reading span { overflow: hidden; color: var(--muted); font-size: 0.66rem; text-overflow: ellipsis; white-space: nowrap; }
.pws-reading strong { margin-top: 2px; font-size: 0.78rem; }
.quota-track { height: 8px; overflow: hidden; border-radius: 6px; background: #e4eaeb; }
.quota-track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 0.2s; }

.history-layout {
  display: grid;
  width: 100%;
  min-width: 0;
  height: calc(100vh - 56px);
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(250px, 1fr) auto;
}
.history-controls { z-index: 600; width: 100%; min-width: 0; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
.history-range { display: grid; min-width: 0; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1.2fr) minmax(0, 1.2fr) minmax(0, 0.6fr) minmax(0, 0.7fr) auto; gap: 8px; align-items: end; }
.history-range label { display: grid; min-width: 0; gap: 4px; color: var(--muted); font-size: 0.69rem; font-weight: 700; }
.control-input, .control-select { min-width: 0; min-height: 38px; padding: 6px 8px; font-size: 0.78rem; }
.preset-row { display: flex; gap: 5px; margin-top: 7px; overflow-x: auto; }
.preset-row button, .playback-actions button {
  min-height: 34px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 5px;
  color: #455e65;
  background: #fff;
  cursor: pointer;
  white-space: nowrap;
  font-size: 0.74rem;
  font-weight: 700;
}
.history-map-wrap { position: relative; width: 100%; min-width: 0; min-height: 0; }
.history-map-button { position: absolute; z-index: 700; top: 10px; right: 10px; }
.history-map-options { top: 54px; }
.playback-panel { z-index: 600; width: 100%; min-width: 0; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--surface); }
.playback-status { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.playback-status strong { font-size: 0.88rem; }
.playback-status span { color: var(--muted); font-size: 0.72rem; }
.timeline { width: 100%; margin: 8px 0; accent-color: var(--accent); }
.playback-actions { display: flex; align-items: center; gap: 5px; overflow-x: auto; }
.playback-actions label { display: flex; align-items: center; gap: 5px; margin-left: auto; color: var(--muted); font-size: 0.72rem; }
.event-summary { display: grid; grid-template-columns: repeat(5, 1fr); gap: 1px; margin-top: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--line); overflow: hidden; }
.event-summary div { padding: 6px 8px; background: #fff; }
.event-summary span, .event-summary strong { display: block; }
.event-summary span { color: var(--muted); font-size: 0.64rem; }
.event-summary strong { margin-top: 2px; font-size: 0.82rem; }

@media (max-width: 800px) {
  .monitor-page .app-header { padding: 7px 9px; }
  .monitor-page .header-nav a { padding: 9px 8px; font-size: 0.76rem; }
  .monitor-layout { display: block; }
  .map-region { width: 100%; height: 100%; min-height: 280px; }
  .storm-drawer {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: min(58%, 520px);
    max-height: calc(100% - 72px);
    border-top: 1px solid var(--line);
    border-left: 0;
    border-radius: 0;
    box-shadow: 0 -3px 14px rgba(24, 40, 46, 0.14);
    transition: height 0.2s ease;
    z-index: 900;
  }
  .storm-drawer.collapsed { height: 58px; }
  .drawer-handle { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 58px; padding: 11px 12px 7px; }
  .drawer-grip { position: absolute; top: 5px; left: 50%; transform: translateX(-50%); }
  .drawer-handle strong { flex: 0 0 auto; font-size: 0.76rem; }
  .drawer-handle-summary { display: flex; min-width: 0; align-items: center; justify-content: flex-end; gap: 9px; color: var(--muted); background: transparent; font-size: 0.65rem; white-space: nowrap; }
  .drawer-handle-summary span { width: auto; height: auto; background: transparent; }
  .drawer-handle-summary b { color: var(--ink); font-size: 0.7rem; }
  .drawer-content { height: calc(100% - 58px); padding-top: 4px; }
  .drawer-content > .summary-strip { display: none; }
  .source-message { display: none; }
  .monitor-page .leaflet-bottom { bottom: 58px; }
  .monitor-page .leaflet-control-attribution { font-size: 8px; }
  .monitor-page.drawer-expanded .leaflet-control-attribution { display: none; }
  .map-legend { bottom: 154px; padding: 5px 7px; gap: 4px 8px; font-size: 0.61rem; }
  .radar-controls { bottom: 66px; grid-template-columns: minmax(0, 1fr) auto; gap: 5px; }
  .radar-controls > #radar-timeline { grid-column: 1 / -1; grid-row: 2; width: 100%; }
  .radar-controls .opacity-control, .radar-controls a { display: none; }
  .monitor-page .leaflet-control-zoom { display: none; }
  .status-chip { display: none; }
  .history-range { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .history-range > * { min-width: 0; max-width: 100%; }
  .history-page { max-width: 100vw; }
  .playback-actions { max-width: 100%; }
  .history-range .button { width: 100%; }
  .history-layout { grid-template-rows: auto minmax(220px, 1fr) auto; }
  .event-summary { grid-template-columns: repeat(5, minmax(72px, 1fr)); overflow-x: auto; }
}

@media (max-width: 430px) {
  .header-nav a { padding: 9px 7px; }
  .map-toolbar { top: 7px; left: 7px; max-width: calc(100% - 14px); }
  .window-button { min-width: 37px; padding: 5px 7px; }
  .tool-button { padding: 5px 8px; }
  #fullscreen-button, #cells-toggle { display: none; }
  .map-legend { left: 7px; max-width: calc(100% - 14px); }
  .history-controls { padding: 8px; }
  .history-range { grid-template-columns: 1fr 1fr; }
  .history-range label:nth-child(3), .history-range label:nth-child(4) { min-width: 0; }
  .playback-panel { padding: 8px; }
  .playback-actions { flex-wrap: wrap; overflow-x: visible; }
  .playback-actions label { width: 100%; margin-top: 2px; margin-left: 0; }
  .playback-actions label .control-select { width: auto; min-width: 110px; }
  .playback-status { align-items: flex-start; flex-direction: column; gap: 2px; }
  .provider-health { grid-template-columns: 1fr; }
}
