:root {
  --ink: #2c2416;
  --muted: #6b5d4d;
  --paper: #f5f0e8;
  --paper-deep: #ede6da;
  --line: rgba(44, 36, 22, 0.14);
  --copper: #a67c52;
  --copper-deep: #8b6342;
  --copper-soft: #e9dccd;
  --blue: #315f7a;
  --blue-soft: #e3ecef;
  --amber: #ef9f2f;
  --purple: #78568c;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: "Source Sans 3", system-ui, sans-serif; color: var(--ink); }
#map { width: 100%; height: 100vh; }

#panel {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1000;
  background: var(--paper);
  border-radius: 12px;
  box-shadow: 0 14px 34px rgba(44, 36, 22, 0.16);
  padding: 22px;
  max-width: 370px;
  min-width: 260px;
  max-height: calc(100vh - 24px);
  overflow-y: auto;
  line-height: 1.5;
}
#panel h2 { font-family: "Fraunces", Georgia, serif; font-size: 24px; line-height: 1.08; letter-spacing: -0.02em; margin-bottom: 12px; }
#panel .school-name { font-size: 15px; font-weight: 700; color: var(--blue); }
#panel .school-map-link {
  display: block;
  width: fit-content;
  max-width: 100%;
  padding: 0;
  border: 0;
  background: none;
  font-family: inherit;
  line-height: 1.35;
  text-align: left;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  cursor: pointer;
}
#panel .school-map-link:hover { color: var(--copper-deep); }
#panel .school-type { font-size: 13px; color: var(--muted); }
#panel .school-admission { font-size: 12px; color: var(--muted); }
#panel .distance { font-size: 14px; color: var(--ink); margin-top: 4px; font-weight: 600; }
#panel .hint { font-size: 13px; line-height: 1.45; color: var(--muted); margin-top: 9px; }
#panel .address { font-size: 13px; color: var(--muted); }
#panel .source-note { font-size: 11px; color: var(--muted); margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
#panel .source-note a, #panel .metrics-link { color: var(--blue); text-underline-offset: 2px; }
.school-metrics { margin-top: 6px; font-size: 12px; color: var(--muted); }
.school-result-link {
  display: inline-block;
  margin-top: 5px;
  color: var(--copper-deep);
  font-size: 12px;
  font-weight: 600;
  text-underline-offset: 2px;
}

.school-marker-hitarea {
  display: grid;
  width: 100%;
  height: 100%;
  place-items: center;
}
.school-icon {
  background: var(--blue);
  border: 2px solid white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  box-shadow: 0 2px 5px rgba(44, 36, 22, 0.3);
}
.school-icon.application {
  background: #65747c;
}
.school-icon.special {
  background: var(--purple);
}
.school-icon.highlighted {
  background: var(--amber);
  width: 24px;
  height: 24px;
  border: 3px solid white;
}

.leaflet-tooltip.school-name-tooltip {
  padding: 4px 8px;
  border: 1px solid rgba(44, 36, 22, 0.16);
  border-radius: 6px;
  background: var(--paper);
  box-shadow: 0 5px 14px rgba(44, 36, 22, 0.16);
  color: var(--ink);
  font-family: "Source Sans 3", system-ui, sans-serif;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
}
.leaflet-tooltip-top.school-name-tooltip::before { border-top-color: var(--paper); }

.school-popup {
  color: var(--ink);
  font-family: "Source Sans 3", system-ui, sans-serif;
  line-height: 1.4;
}
.school-popup h3 {
  margin: 0 0 5px;
  color: var(--ink);
  font-family: "Fraunces", Georgia, serif;
  font-size: 18px;
  line-height: 1.15;
}
.school-popup p { margin: 2px 0; color: var(--muted); font-size: 13px; }
.school-popup-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.school-website-link {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  color: var(--blue);
  font-size: 13px;
  font-weight: 700;
  text-underline-offset: 2px;
}
.analyze-school-btn {
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--copper-deep);
  border-radius: 7px;
  background: transparent;
  color: var(--copper-deep);
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.analyze-school-btn:hover { background: var(--copper-soft); color: var(--ink); }

.distance-label {
  background: rgba(245, 240, 232, 0.94);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
}
.distance-label.is-loading {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--blue);
  border-color: rgba(20, 93, 160, 0.35);
}
.distance-label.is-fallback {
  border-style: dashed;
  color: var(--muted);
}
.distance-label.route-retry {
  cursor: pointer;
  text-align: center;
}
.route-retry-action {
  display: block;
  margin-top: 1px;
  color: var(--blue);
  font-size: 10px;
  font-weight: 700;
}
.leaflet-marker-icon:focus-visible .route-retry {
  outline: 2px solid var(--copper);
  outline-offset: 3px;
}
.route-spinner {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border: 2px solid rgba(20, 93, 160, 0.22);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: route-spin 0.75s linear infinite;
}
.route-loading-line {
  animation: route-dash 0.9s linear infinite;
}
@keyframes route-spin {
  to { transform: rotate(360deg); }
}
@keyframes route-dash {
  to { stroke-dashoffset: -28; }
}
@media (prefers-reduced-motion: reduce) {
  .route-spinner,
  .route-loading-line { animation: none; }
}

#panel .school-entry { margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
#panel .school-entry:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
#panel .rank { font-size: 12px; color: var(--muted); font-weight: 600; }

/* Stage group styles for proximity panel */
.stage-group { margin-bottom: 12px; }
.stage-group:last-of-type { margin-bottom: 0; }
.stage-group-header {
  font-size: 13px;
  font-weight: 700;
  padding: 7px 9px;
  background: var(--paper-deep);
  border-radius: 8px;
  margin-bottom: 5px;
}
.stage-school { padding: 3px 0 3px 11px; }
.stage-school .school-name { font-size: 14px; font-weight: 600; color: var(--blue); }
.stage-school-detail { font-size: 12px; color: var(--muted); }

#filters {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 1000;
  background: var(--paper);
  border-radius: 12px;
  box-shadow: 0 14px 34px rgba(44, 36, 22, 0.16);
  padding: 14px 16px;
  line-height: 1.8;
  font-size: 14px;
}
#filters h3 { font-family: "Fraunces", Georgia, serif; font-size: 15px; margin-bottom: 4px; }
#filters summary { display: none; font-family: "Fraunces", Georgia, serif; font-size: 17px; font-weight: 700; cursor: pointer; }
#filters label { display: block; cursor: pointer; user-select: none; }
#filters label input { margin-right: 6px; vertical-align: middle; }
input { accent-color: var(--copper-deep); }
.filter-count { font-size: 11px; color: var(--muted); }
.grade-distance-note {
  max-width: 230px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
}
.nav-link {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  margin-top: 14px;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  color: var(--paper);
  text-decoration: none;
  padding: 10px 14px;
  border-radius: 7px;
  background: var(--copper-deep);
  box-shadow: 0 5px 14px rgba(44, 36, 22, 0.16);
  transition: background-color 0.15s, box-shadow 0.15s;
}
.nav-link:hover {
  background: var(--ink);
  color: var(--paper);
  box-shadow: 0 7px 18px rgba(44, 36, 22, 0.22);
}
.home-link {
  display: block;
  padding: 4px 0 1px;
  color: var(--muted);
  font-family: "Fraunces", Georgia, serif;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
}
.home-link:hover { color: var(--copper-deep); }
.language-switch {
  display: flex;
  justify-content: center;
  gap: 2px;
  width: fit-content;
  margin: 8px auto 0;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
}
.language-switch button {
  min-width: 34px;
  min-height: 28px;
  padding: 3px 7px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.language-switch button:hover { color: var(--ink); }
.language-switch button[aria-pressed="true"] { background: var(--copper-soft); color: var(--ink); }
.map-language-switch { display: none; }
.feedback-link {
  display: block;
  margin-top: 5px;
  padding: 5px 0 1px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
  text-align: center;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}
.feedback-link:hover { color: var(--copper-deep); }
.filter-divider { border: none; border-top: 1px solid var(--line); margin: 8px 0; }
button:focus-visible, a:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--copper); outline-offset: 3px; }

/* Buffer ring labels */
.buffer-ring-label {
  background: rgba(245, 240, 232, 0.9);
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid var(--line);
}

/* Loading indicator */
#loading-indicator {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1100;
  background: var(--paper);
  border-radius: 12px;
  box-shadow: 0 16px 38px rgba(44, 36, 22, 0.24);
  padding: 14px 18px;
  width: min(360px, calc(100vw - 24px));
  font-size: 13px;
  color: var(--ink);
}
#loading-indicator[hidden] { display: none; }
.loading-status-row, .analysis-loading-header {
  display: flex;
  align-items: center;
  gap: 11px;
}
.loading-status-row strong, .analysis-loading-header strong { display: block; font-size: 14px; line-height: 1.25; }
#loading-text, .analysis-loading-header p { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; }
.progress-bar {
  height: 6px;
  background: var(--paper-deep);
  border-radius: 3px;
  margin-top: 10px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--copper-deep);
  border-radius: 3px;
  transform-origin: left center;
  transition: transform 220ms ease-out;
}
.analysis-spinner {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  border: 3px solid var(--copper-soft);
  border-top-color: var(--copper-deep);
  border-radius: 50%;
  animation: route-spin 0.8s linear infinite;
}

/* Analysis panel header */
.analysis-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.analysis-header h2 { margin-bottom: 0; }
.close-btn {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
  padding: 0 2px;
}
.close-btn:hover { color: var(--ink); }

/* Ring legend */
.ring-legend {
  margin: 10px 0;
  padding: 8px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.ring-legend-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}
.ring-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  margin: 2px 0;
}
.ring-legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px dashed;
  opacity: 0.7;
  flex-shrink: 0;
}

/* Heatmap explanation */
.heatmap-explanation { margin-top: 14px; }
.heatmap-explanation h3 { font-family: "Fraunces", Georgia, serif; font-size: 17px; line-height: 1.2; margin-bottom: 5px; }
.heatmap-explanation > p { color: var(--muted); font-size: 12px; line-height: 1.45; }
.heatmap-legend-bar {
  height: 14px;
  margin-top: 10px;
  border-radius: 5px;
  background: linear-gradient(to right, #d73027, #fc8d59, #fee08b, #d9ef8b, #66bd63, #1a9850);
}
.heatmap-legend-labels {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 10px;
  color: var(--muted);
  line-height: 1.2;
  margin-top: 4px;
}
.heatmap-legend-labels span:last-child { text-align: right; }
.heatmap-key { margin-top: 11px; }
.heatmap-key > div { display: flex; align-items: flex-start; gap: 7px; margin-top: 5px; color: var(--muted); font-size: 12px; line-height: 1.35; }
.heatmap-key strong { color: var(--ink); }
.heatmap-swatch { width: 11px; height: 11px; margin-top: 2px; flex: 0 0 auto; border-radius: 3px; }
.heatmap-swatch.is-red { background: #d73027; }
.heatmap-swatch.is-yellow { background: #fee08b; border: 1px solid rgba(44, 36, 22, 0.18); }
.heatmap-swatch.is-green { background: #1a9850; }
.heatmap-explanation .heatmap-action { margin-top: 11px; color: var(--ink); font-weight: 600; }
.heatmap-explanation .heatmap-caveat { margin-top: 9px; padding-top: 9px; border-top: 1px solid var(--line); font-size: 11px; }

.analysis-loading {
  margin-top: 12px;
  padding: 12px;
  background: var(--blue-soft);
  border-radius: 10px;
}
.analysis-loading .progress-bar { background: rgba(49, 95, 122, 0.16); }
.analysis-loading .progress-fill { background: var(--blue); }
.analysis-loading-explanation { margin-top: 9px; color: var(--muted); font-size: 12px; line-height: 1.4; }
.analysis-loading .analysis-spinner { border-color: rgba(49, 95, 122, 0.18); border-top-color: var(--blue); }

.heatmap-cell { cursor: pointer; }
.heatmap-cell:hover { stroke: rgba(44, 36, 22, 0.55); stroke-width: 2; }
.heatmap-cell:focus-visible { stroke: var(--ink); stroke-width: 3; outline: none; }

/* Proximity tooltip popup */
.proximity-tooltip { font-size: 13px; line-height: 1.6; }
.proximity-tooltip .label { color: var(--muted); font-size: 11px; }
.proximity-tooltip .value { font-weight: 600; }
.proximity-tooltip .score-positive { color: #1a9850; font-weight: 700; }
.proximity-tooltip .score-negative { color: #d73027; font-weight: 700; }
.proximity-tooltip .score-neutral { color: var(--muted); font-weight: 700; }
.proximity-tooltip .explanation { font-size: 11px; color: var(--muted); margin-top: 4px; border-top: 1px solid var(--line); padding-top: 4px; }

/* Analysis note */
.analysis-note {
  font-size: 12px;
  color: var(--muted);
  margin-top: 8px;
  padding: 8px;
  background: var(--paper-deep);
  border-radius: 4px;
}
.analysis-note strong { display: block; margin-bottom: 3px; color: var(--ink); }

@media (max-width: 720px) {
  #filters { top: 10px; left: 10px; right: 92px; width: auto; max-height: 48vh; overflow-y: auto; }
  #filters summary { display: block; }
  #filters .filter-content { margin-top: 10px; }
  #filters:not([open]) .filter-content { display: none; }
  #panel { top: auto; right: 10px; bottom: 10px; left: 10px; min-width: 0; max-width: none; max-height: 42vh; padding: 16px; }
  .leaflet-control-zoom { margin-top: 70px !important; }
  #loading-indicator { top: 78px; bottom: auto; }
  .school-popup-actions { gap: 4px 12px; }
  .school-website-link, .analyze-school-btn { min-height: 44px; }
  .language-switch button { min-height: 38px; }
  .map-language-switch {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1001;
    display: flex;
    margin: 0;
    padding: 3px;
    background: var(--paper);
    box-shadow: 0 8px 20px rgba(44, 36, 22, 0.14);
  }
}

@media (prefers-reduced-motion: reduce) {
  .analysis-spinner { animation: none; }
  .progress-fill { transition: none; }
}
