:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #f0f2ef;
  --ink: #18201f;
  --ink-2: #4a5553;
  --muted: #7a8583;
  --line: #e2e6e3;
  --accent: #155e75;
  --accent-2: #0e7490;
  --accent-soft: #e0f0f4;
  --elem: #d97706;
  --elem-soft: rgba(217, 119, 6, 0.14);
  --jr: #7c3aed;
  --jr-soft: rgba(124, 58, 237, 0.1);
  --good: #15803d;
  --bad: #b91c1c;
  --star: #f59e0b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 22, 0.04), 0 4px 16px rgba(16, 24, 22, 0.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #101514;
    --surface: #171e1d;
    --surface-2: #1f2827;
    --ink: #e8eeec;
    --ink-2: #b6c1bf;
    --muted: #84918f;
    --line: #2a3533;
    --accent: #5fb8cf;
    --accent-2: #7cc9dc;
    --accent-soft: #15303a;
    --elem: #f59e0b;
    --elem-soft: rgba(245, 158, 11, 0.18);
    --jr: #a78bfa;
    --jr-soft: rgba(167, 139, 250, 0.14);
    --good: #4ade80;
    --bad: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Noto Sans JP', system-ui, -apple-system, 'Hiragino Sans', sans-serif;
  font-size: 15px;
  line-height: 1.65;
}
button, input { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 2000;
  display: flex; align-items: center; gap: 20px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); text-decoration: none; white-space: nowrap; }
.brand-mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; }
.search { flex: 1; display: flex; max-width: 620px; }
.search input {
  flex: 1; min-width: 0;
  padding: 10px 14px; border: 1px solid var(--line); border-right: 0;
  border-radius: 10px 0 0 10px; background: var(--surface); outline: none;
}
.search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search button {
  padding: 0 20px; border: 0; border-radius: 0 10px 10px 0;
  background: var(--accent); color: #fff; font-weight: 700; cursor: pointer; white-space: nowrap;
}
.search button:hover { background: var(--accent-2); }

main { max-width: 1280px; margin: 0 auto; padding: 28px 24px 40px; }

/* ---------- intro */
.intro { max-width: 760px; margin: 48px auto; text-align: center; }
.intro h1 { font-size: clamp(28px, 5vw, 40px); margin: 0 0 12px; letter-spacing: 0.02em; }
.intro p { color: var(--ink-2); margin: 0 0 24px; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.examples button, .ghost {
  padding: 7px 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); cursor: pointer; font-size: 13.5px;
}
.examples button:hover, .ghost:hover { border-color: var(--accent); color: var(--accent); }
.intro-points { list-style: none; padding: 0; margin: 40px 0 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; text-align: left; }
.intro-points li { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; font-size: 13.5px; color: var(--ink-2); }
.intro-points span { display: block; font-weight: 700; color: var(--accent); font-size: 15px; margin-bottom: 4px; }

/* ---------- report */
.report-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; }
.report-head h1 { margin: 0; font-size: clamp(20px, 3vw, 28px); line-height: 1.35; }
.eyebrow { margin: 0 0 2px; color: var(--accent); font-weight: 700; font-size: 13px; }

.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 0; }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 20px; font-weight: 700; line-height: 1.35; margin: 2px 0; overflow-wrap: anywhere; }
.kpi-value small { font-size: 13px; font-weight: 500; color: var(--ink-2); margin-left: 2px; }
.kpi-sub { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; }
#k-elem .kpi-value { color: var(--elem); }
#k-jr .kpi-value { color: var(--jr); }
.skel { border-radius: 6px; background: linear-gradient(90deg, var(--surface-2), var(--line), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; color: transparent; }
@keyframes shimmer { to { background-position: -200% 0; } }

.grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 18px; align-items: start; margin-bottom: 18px; }
.map-col { position: sticky; top: 76px; }
.side-col { display: flex; flex-direction: column; gap: 18px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 16px 18px 0; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 16px; }
.card-body { padding: 14px 18px 16px; }
.source { padding: 10px 18px; border-top: 1px solid var(--line); font-size: 11.5px; color: var(--muted); background: var(--surface-2); }
.source:empty { display: none; }
.loading { padding: 18px 0; color: var(--ink-2); font-size: 14px; }
.loading::before { content: ''; display: inline-block; width: 14px; height: 14px; margin-right: 8px; vertical-align: -2px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.note { font-size: 12.5px; color: var(--ink-2); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin: 12px 0 0; }
.warn { color: var(--bad); }

/* map */
.map-card { padding: 0; }
#map { height: min(72vh, 640px); min-height: 380px; background: var(--surface-2); }
.map-legend { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px; border-top: 1px solid var(--line); }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 12.5px; cursor: pointer; user-select: none; }
.chip[aria-pressed='false'] { opacity: .45; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.sw { width: 14px; height: 10px; border-radius: 3px; flex: none; border: 2px solid; }
.pin { width: 26px; height: 26px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); background: var(--bad); border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
.fmark { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.35); }
.smark { display: grid; place-items: center; width: 26px; height: 26px; border-radius: 8px; color: #fff; font-size: 13px; font-weight: 700; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.leaflet-container { font-family: inherit; }
.leaflet-popup-content { margin: 10px 12px; font-size: 13px; line-height: 1.5; }

/* schools */
.school { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; padding: 12px 0; border-bottom: 1px dashed var(--line); }
.school:last-of-type { border-bottom: 0; }
.school-tag { grid-row: span 2; align-self: start; padding: 3px 8px; border-radius: 6px; font-size: 12px; font-weight: 700; color: #fff; }
.school-tag.elem { background: var(--elem); }
.school-tag.jr { background: var(--jr); }
.school-name { font-weight: 700; font-size: 16px; }
.school-meta { font-size: 13px; color: var(--ink-2); }
.school-meta b { color: var(--ink); }
.linkbtn { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 13px; text-decoration: underline; text-underline-offset: 2px; }

/* population */
.pop-kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.pop-kpi { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.pop-kpi .l { font-size: 11.5px; color: var(--muted); }
.pop-kpi .v { font-size: 18px; font-weight: 700; }
.pop-kpi .v small { font-size: 12px; font-weight: 500; }
.up { color: var(--good); } .down { color: var(--bad); }
.chart-title { font-size: 13px; font-weight: 700; margin: 14px 0 6px; color: var(--ink-2); }
.chart-box { position: relative; height: 200px; }
.agebar { display: flex; height: 28px; border-radius: 8px; overflow: hidden; margin: 4px 0 6px; }
.agebar div { display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 700; min-width: 0; white-space: nowrap; }
.age-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--ink-2); }
.age-legend span { display: inline-flex; align-items: center; gap: 5px; }
.compare { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* facilities */
.fac-overview { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.fac-tile { text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 10px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.fac-tile:hover { border-color: var(--accent); }
.fac-tile[aria-selected='true'] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.fac-tile .t { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 700; }
.fac-tile .n { font-size: 22px; font-weight: 700; line-height: 1.2; }
.fac-tile .n small { font-size: 12px; font-weight: 500; color: var(--muted); }
.fac-tile .near { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meter { height: 5px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.meter i { display: block; height: 100%; border-radius: 3px; }

.fac-panel { display: block; }
.fac-panel h3 { font-size: 14px; margin: 0 0 8px; display: flex; align-items: center; gap: 8px; }
.fac-list { list-style: none; margin: 0; padding: 0; max-height: 460px; overflow: auto; border: 1px solid var(--line); border-radius: 12px; }
.fac-list li { display: flex; justify-content: space-between; gap: 12px; padding: 9px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.fac-list li:last-child { border-bottom: 0; }
.fac-list li:hover { background: var(--surface-2); }
.fac-list .nm { font-weight: 500; font-size: 14px; }
.fac-list .dt { font-size: 12px; color: var(--muted); }
.fac-list .wk { flex: none; text-align: right; font-size: 13px; font-weight: 700; color: var(--accent); }
.fac-list .wk span { display: block; font-size: 11px; font-weight: 400; color: var(--muted); }
.empty { padding: 16px; color: var(--muted); font-size: 13px; }

.primary { padding: 9px 16px; border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 700; cursor: pointer; }
.primary:hover { background: var(--accent-2); }
.primary:disabled { opacity: .6; cursor: default; }

.error { max-width: 640px; margin: 40px auto; padding: 18px 20px; border-radius: var(--radius); border: 1px solid var(--bad); background: var(--surface); color: var(--bad); }

.foot { max-width: 1280px; margin: 0 auto; padding: 0 24px 40px; font-size: 11.5px; color: var(--muted); }
.foot p { margin: 4px 0; }

/* ---------- responsive */
@media (max-width: 1080px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .map-col { position: static; }
  #map { height: 420px; }
}
@media (max-width: 680px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 16px; }
  .search { max-width: none; flex-basis: 100%; }
  main { padding: 18px 16px 32px; }
  .foot { padding: 0 16px 32px; }
  .intro { margin: 20px auto; }
  .intro-points { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi-value { font-size: 17px; }
  .report-head { flex-direction: column; align-items: flex-start; }
  .fac-panel { grid-template-columns: 1fr; }
  .pop-kpis { grid-template-columns: 1fr 1fr 1fr; }
  #map { height: 340px; min-height: 0; }
}

/* 推定（学区データがない地域） */
.school-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding-top: 12px; }
.est-badge { padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 700; border: 1px solid var(--muted); color: var(--ink-2); }
.school.compact { padding: 8px 0 8px 4px; }
.school.compact .school-name { font-size: 14.5px; }
.rank { grid-row: span 2; align-self: start; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--surface-2); font-size: 12px; font-weight: 700; color: var(--ink-2); }
.smark.est { opacity: .8; border-style: dashed; }

/* 市の通学区域表 */
.city-badge { padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 700; background: var(--accent-soft); color: var(--accent); }
.area-note { margin-top: 4px; font-size: 12px; color: var(--muted); }
.school.plain { display: block; padding-left: 4px; }

/* 用途地域 */
.zone-main { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.zone-sw { width: 18px; height: 18px; border-radius: 5px; flex: none; border: 1px solid rgba(0,0,0,.15); }
.zone-name { font-size: 18px; font-weight: 700; }
.zone-nums { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.zone-info { font-size: 13px; color: var(--ink-2); margin: 0; }
.zone-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 12px; font-size: 12px; color: var(--ink-2); }
.zone-legend span { display: inline-flex; align-items: center; gap: 5px; }
.zone-legend i { width: 11px; height: 11px; border-radius: 3px; border: 1px solid rgba(0,0,0,.15); }

/* 背景地図の切り替え */
.basemap { display: flex; background: var(--surface); border-radius: 10px; box-shadow: 0 1px 5px rgba(0,0,0,.25); overflow: hidden; }
.basemap button { border: 0; background: none; padding: 6px 10px; font-size: 12.5px; cursor: pointer; color: var(--ink-2); border-left: 1px solid var(--line); white-space: nowrap; }
.basemap button:first-child { border-left: 0; }
.basemap button:hover { background: var(--surface-2); }
.basemap button[aria-pressed='true'] { background: var(--accent); color: #fff; font-weight: 700; }
@media (max-width: 680px) { .basemap button { padding: 6px 8px; font-size: 12px; } }

/* 近隣の分譲マンション */
.ms-range { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--ink-2); }
.ms-range b { color: var(--accent); display: inline-block; min-width: 3.4em; }
.ms-range input { width: 180px; accent-color: var(--accent); cursor: pointer; }
#mansions { margin-top: 16px; }
.ms-ctrl { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ms-ctrl select { padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 13px; }
.ms-ctrl label { display: flex; align-items: center; gap: 4px; color: var(--ink-2); cursor: pointer; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-left: 1px solid var(--line); background: var(--surface); color: var(--ink-2); padding: 5px 11px; font: inherit; font-size: 13px; cursor: pointer; }
.seg button:first-child { border-left: 0; }
.seg button:hover { background: var(--surface-2); }
.seg button[aria-pressed='true'] { background: var(--accent); color: #fff; font-weight: 700; }
.ms-progress { text-align: left; display: flex; flex-direction: column; gap: 6px; }
.ms-cities { list-style: none; padding: 0; margin: 6px 0 0; display: flex; flex-wrap: wrap; gap: 6px; }
.ms-cities li { font-size: 12.5px; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.ms-cities li span { margin-left: 6px; }
.ms-cities li.done { color: var(--accent); border-color: var(--accent); }
.ms-cities li.done::before { content: '✓ '; }
.ms-cities li.now { color: var(--ink); border-color: var(--ink-2); font-weight: 700; }
.ms-summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) minmax(0, 2fr); gap: 12px; margin-bottom: 14px; }
.ms-summary > div { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.ms-summary b { font-size: 22px; }
.ms-summary b small { font-size: 12px; font-weight: 500; margin-left: 2px; color: var(--ink-2); }
.ms-bars { display: flex; align-items: flex-end; gap: 3px; height: 56px; margin-top: 4px; }
.ms-bar { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; min-width: 0; }
.ms-bar span { display: block; width: 100%; background: #d35400; border-radius: 3px 3px 0 0; min-height: 1px; }
.ms-bar.plan span { background: #8e44ad; }
.ms-bar em { font-style: normal; font-size: 9.5px; color: var(--muted); margin-top: 2px; }
.ms-list { list-style: none; padding: 0; margin: 0; max-height: 560px; overflow-y: auto; border-top: 1px solid var(--line); }
.ms-item { display: flex; justify-content: space-between; gap: 12px; padding: 10px 2px; border-bottom: 1px solid var(--line); }
.ms-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.ms-name { font-weight: 700; text-decoration: none; }
.ms-name:hover { text-decoration: underline; }
.ms-addr { font-size: 13px; color: var(--ink-2); }
.ms-meta { font-size: 12px; color: var(--muted); }
.ms-side { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; text-align: right; flex-shrink: 0; }
.ms-age { font-weight: 700; font-size: 13px; color: #d35400; }
.ms-item.plan .ms-age { color: #8e44ad; }
.ms-dist { border: 1px solid var(--line); background: var(--surface); color: var(--accent); border-radius: 999px; padding: 2px 10px; font: inherit; font-size: 12.5px; cursor: pointer; }
.ms-dist:hover { border-color: var(--accent); }
@media (max-width: 680px) {
  .ms-summary { grid-template-columns: 1fr 1fr; }
  .ms-chart { grid-column: 1 / -1; }
  .ms-item { flex-direction: column; gap: 6px; }
  .ms-side { flex-direction: row; align-items: center; flex-wrap: wrap; gap: 8px; text-align: left; }
}
.static #mansions { display: none; }
