*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1923;
  --surface: #1a2535;
  --border: rgba(255, 255, 255, 0.08);
  --text: #c9d1d9;
  --text-muted: #8b949e;
  --blue-solid: rgb(56, 139, 253);
  --error-bg: #5c1a1a;
  --error-text: #ff8080;
  --warning-bg: #3d2b00;
  --warning-text: #ffc166;
  --active-tab: #2d4a6e;
  --header-h: 48px;
  --legend-h: 30px;
  --footer-h: 32px;
  --thead-h: 40px;
}

html, body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  min-height: 100vh;
}

/* ── Header ───────────────────────────────────────── */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#location-display {
  display: flex;
  align-items: center;
  gap: 8px;
}

#location-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

#location-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 7px;
  font-size: 15px;
  line-height: 1.5;
  transition: color 0.15s, border-color 0.15s;
}

#location-btn:hover {
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.25);
}

#unit-toggle {
  display: flex;
  gap: 4px;
}

#unit-toggle button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 10px;
  font-size: 13px;
  transition: all 0.15s;
}

#unit-toggle button.active {
  background: var(--active-tab);
  border-color: var(--blue-solid);
  color: #fff;
}

#unit-toggle button:hover:not(.active) {
  color: var(--text);
  border-color: rgba(255, 255, 255, 0.25);
}

/* ── Search bar ───────────────────────────────────── */

#search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#search-bar.hidden {
  display: none;
}

#city-input-wrap {
  flex: 1;
  position: relative;
}

#city-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 14px;
  padding: 6px 10px;
  outline: none;
  transition: border-color 0.15s;
}

#city-input:focus {
  border-color: var(--blue-solid);
}

#city-suggestions {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  list-style: none;
  z-index: 20;
  overflow: hidden;
}

#city-suggestions.hidden {
  display: none;
}

#city-suggestions li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  cursor: pointer;
}

#city-suggestions li:hover,
#city-suggestions li.selected {
  background: var(--active-tab);
}

.sug-name {
  font-size: 13px;
  color: var(--text);
}

.sug-sub {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

#city-submit, #city-cancel {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px 12px;
  font-size: 13px;
  transition: all 0.15s;
}

#city-submit:hover {
  background: var(--active-tab);
  color: #fff;
  border-color: var(--blue-solid);
}

#city-cancel:hover {
  color: var(--error-text);
  border-color: var(--error-text);
}

/* ── Status bar ───────────────────────────────────── */

#status-bar {
  padding: 7px 16px;
  font-size: 13px;
  text-align: center;
  flex-shrink: 0;
}

#status-bar.hidden {
  display: none;
}

#status-bar.error {
  background: var(--error-bg);
  color: var(--error-text);
}

#status-bar.warning {
  background: var(--warning-bg);
  color: var(--warning-text);
}

/* ── Grid wrapper ─────────────────────────────────── */

#grid-wrapper {
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  overflow-y: auto;
  position: relative;
}

#loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 14px;
  z-index: 10;
}

#loading-overlay.hidden {
  display: none;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--blue-solid);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Weather table ────────────────────────────────── */

.weather-table {
  border-collapse: separate;    /* Safari sticky-border fix */
  border-spacing: 0;
  width: 100%;
  table-layout: fixed;
}

/* Column widths */
.weather-table colgroup col:first-child {
  width: 52px;
}

/* Sticky header row */
.weather-table thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  padding: 6px 4px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  height: var(--thead-h);
  min-width: 120px;
  width: calc((100% - 52px) / 7);
}

/* Corner cell — sticky both top and left */
.weather-table thead th.corner-th {
  position: sticky;
  left: 0;
  top: 0;
  z-index: 4;
  width: 52px;
  min-width: 52px;
}

.col-wd {
  display: block;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}

.col-dt {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
  margin-top: 1px;
}

/* Today column header highlight */
.now-col-header .col-wd {
  color: var(--blue-solid);
}

.now-col-header {
  border-bottom-color: var(--blue-solid) !important;
}

/* Sticky hour column */
.hour-cell {
  position: sticky;
  left: 0;
  z-index: 2;
  width: 52px;
  min-width: 52px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  text-align: right;
  padding: 0 6px 0 0;
  white-space: nowrap;
  vertical-align: middle;
  user-select: none;
}

/* Data cells */
.data-cell {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.2);
  text-align: center;
  vertical-align: middle;
  padding: 0;
  cursor: default;
  transition: filter 0.12s;
  height: calc((100vh - var(--header-h) - var(--legend-h) - var(--footer-h) - var(--thead-h) - 5px) / 24);
  min-height: 22px;
}

.data-cell:hover {
  filter: brightness(1.2);
  z-index: 1;
}

/* Rain fill — blue bar growing from left */
.rain-fill {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: rgba(56, 139, 253, 0.6);
  pointer-events: none;
  /* width set inline as `${precip}%` of cell width */
}

/* Good cycling conditions — green corner triangle (top-right) */
.data-cell.good-cycling::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 20px 20px 0;
  border-color: transparent rgba(192, 132, 252, 0.9) transparent transparent;
  z-index: 2;
  pointer-events: none;
}

/* Temperature text */
.temp-label {
  position: relative;
  z-index: 1;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  display: block;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* Rain drop icon */
.rain-icon {
  position: relative;
  z-index: 1;
  font-size: 9px;
  display: block;
  line-height: 1;
  margin-top: 1px;
}

/* NOW row — current hour */
.now-row .hour-cell {
  background: #1e2f45;
  color: var(--text);
  border-left: 3px solid var(--blue-solid);
}

.now-row .data-cell {
  box-shadow: inset 0 0 0 2px rgba(56, 139, 253, 0.55);
}

.now-badge {
  display: inline-block;
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--blue-solid);
  margin-left: 3px;
  vertical-align: middle;
}

/* ── Legend bar ───────────────────────────────────── */

#legend-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  height: var(--legend-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
  overflow: hidden;
}

.legend-temp {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-gradient {
  width: 120px;
  height: 10px;
  border-radius: 3px;
  background: linear-gradient(to right,
    #4a1060, #1a3f8f, #2471cc, #2ebc8f, #e8c840, #f08030, #c03020
  );
}

.legend-rain {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-rain-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  background: rgba(56, 139, 253, 0.6);
  border: 1px solid rgba(56, 139, 253, 0.3);
}

.legend-cycling {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-cycling-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  background: var(--surface);
  border: 1px solid rgba(255, 255, 255, 0.1);
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}

.legend-cycling-swatch::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 9px 9px 0;
  border-color: transparent rgba(192, 132, 252, 0.9) transparent transparent;
}

/* ── Footer ───────────────────────────────────────── */

footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  flex-shrink: 0;
  height: var(--footer-h);
}

/* ── Responsive ───────────────────────────────────── */

@media (max-width: 600px) {
  #location-name {
    font-size: 14px;
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .weather-table thead th {
    min-width: 80px;
  }

  footer {
    font-size: 11px;
    padding: 6px 10px;
  }
}
