:root {
  --bg: #ffffff;
  --sidebar-bg: #fafafa;
  --border: #ddd;
  --text: #1b1b1b;
  --muted: #555;
  --heading-muted: #666;
  --input-border: #bbb;
  --link: #1565c0;
  --btn-bg: #1565c0;
  --btn-bg-hover: #0d47a1;
  --btn-text: #fff;
  --status-error: #c62828;
  --status-warning: #e65100;
  --hit-bg: #e8f5e9;
  --hit-text: #2e7d32;
  --miss-bg: #fdecea;
  --miss-text: #c62828;
  --toggle-bg: #ffffff;
  --toggle-border: #ccc;
  --leaflet-control-bg: #fff;
  --leaflet-control-text: #333;
  --leaflet-popup-bg: #fff;
  --leaflet-popup-text: #1b1b1b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --sidebar-bg: #181818;
    --border: #333;
    --text: #e8e8e8;
    --muted: #aaa;
    --heading-muted: #999;
    --input-border: #444;
    --link: #64b5f6;
    --btn-bg: #1565c0;
    --btn-bg-hover: #1976d2;
    --btn-text: #fff;
    --status-error: #ef5350;
    --status-warning: #ffb74d;
    --hit-bg: #1b3a20;
    --hit-text: #81c784;
    --miss-bg: #3a1a1a;
    --miss-text: #ef9a9a;
    --toggle-bg: #242424;
    --toggle-border: #444;
    --leaflet-control-bg: #2a2a2a;
    --leaflet-control-text: #e8e8e8;
    --leaflet-popup-bg: #2a2a2a;
    --leaflet-popup-text: #e8e8e8;
  }
}

/* Manual overrides via the theme toggle win over the system preference
   in both directions, because an attribute selector on :root is more
   specific than the plain :root inside the media query above. */
:root[data-theme="dark"] {
  --bg: #121212;
  --sidebar-bg: #181818;
  --border: #333;
  --text: #e8e8e8;
  --muted: #aaa;
  --heading-muted: #999;
  --input-border: #444;
  --link: #64b5f6;
  --btn-bg: #1565c0;
  --btn-bg-hover: #1976d2;
  --btn-text: #fff;
  --status-error: #ef5350;
  --status-warning: #ffb74d;
  --hit-bg: #1b3a20;
  --hit-text: #81c784;
  --miss-bg: #3a1a1a;
  --miss-text: #ef9a9a;
  --toggle-bg: #242424;
  --toggle-border: #444;
  --leaflet-control-bg: #2a2a2a;
  --leaflet-control-text: #e8e8e8;
  --leaflet-popup-bg: #2a2a2a;
  --leaflet-popup-text: #e8e8e8;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --sidebar-bg: #fafafa;
  --border: #ddd;
  --text: #1b1b1b;
  --muted: #555;
  --heading-muted: #666;
  --input-border: #bbb;
  --link: #1565c0;
  --btn-bg: #1565c0;
  --btn-bg-hover: #0d47a1;
  --btn-text: #fff;
  --status-error: #c62828;
  --status-warning: #e65100;
  --hit-bg: #e8f5e9;
  --hit-text: #2e7d32;
  --miss-bg: #fdecea;
  --miss-text: #c62828;
  --toggle-bg: #ffffff;
  --toggle-border: #ccc;
  --leaflet-control-bg: #fff;
  --leaflet-control-text: #333;
  --leaflet-popup-bg: #fff;
  --leaflet-popup-text: #1b1b1b;
}

* {
  box-sizing: border-box;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app {
  display: flex;
  height: 100vh;
}

.sidebar {
  position: relative;
  width: 320px;
  flex-shrink: 0;
  padding: 20px;
  overflow-y: auto;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
}

.theme-toggle {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
  background: var(--toggle-bg);
  border: 1px solid var(--toggle-border);
  border-radius: 50%;
  cursor: pointer;
}

.logo {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto 12px;
}

.sidebar h1 {
  font-size: 20px;
  margin: 0 0 4px;
}

.subtitle {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--muted);
}

.search-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

#address-input {
  padding: 10px;
  font-size: 14px;
  border: 1px solid var(--input-border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
}

#search-btn {
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--btn-text);
  background: var(--btn-bg);
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

#search-btn:hover {
  background: var(--btn-bg-hover);
}

#locate-btn {
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--link);
  background: transparent;
  border: 1px solid var(--link);
  border-radius: 4px;
  cursor: pointer;
}

#locate-btn:hover {
  background: var(--link);
  color: var(--btn-text);
}

#search-btn:disabled,
#locate-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.privacy-note {
  font-size: 12px;
  color: var(--heading-muted);
  margin: 0 0 12px;
}

.privacy-note a {
  color: var(--link);
}

.status {
  min-height: 18px;
  font-size: 13px;
  color: var(--muted);
}

.status.error {
  color: var(--status-error);
}

.status.warning {
  color: var(--status-warning);
}

.result {
  min-height: 20px;
  font-size: 14px;
  font-weight: 600;
  padding: 10px;
  border-radius: 4px;
}

.result.hit {
  background: var(--hit-bg);
  color: var(--hit-text);
}

.result.miss {
  background: var(--miss-bg);
  color: var(--miss-text);
}

.school-info {
  font-size: 13px;
  color: var(--text);
  padding: 0 10px;
}

.school-info-row {
  margin-bottom: 4px;
}

.school-info-row a {
  color: var(--link);
}

.sidebar h2 {
  font-size: 14px;
  margin: 20px 0 8px;
  text-transform: uppercase;
  color: var(--heading-muted);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-bottom: 6px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  flex-shrink: 0;
}

#map {
  flex: 1;
  background: var(--bg);
}

.leaflet-control-zoom a,
.leaflet-bar a {
  background-color: var(--leaflet-control-bg) !important;
  color: var(--leaflet-control-text) !important;
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--leaflet-popup-bg) !important;
  color: var(--leaflet-popup-text) !important;
}

.leaflet-container a.leaflet-popup-close-button {
  color: var(--leaflet-popup-text) !important;
}

@media (max-width: 700px) {
  .app {
    flex-direction: column;
  }
  .sidebar {
    width: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 45vh;
  }
}
