/* ============================================================
   在留外国人マップ — Style
   ============================================================ */
:root {
  --bg: #0f1118;
  --surface: #181b25;
  --surface-hover: #1f2333;
  --border: #2a2e3d;
  --text-primary: #e8eaf0;
  --text-secondary: #a3a8b8;
  --accent: #3498db;
  --accent-light: #5dade2;
  --very-high: #e74c3c;
  --high: #f39c12;
  --moderate: #3498db;
  --low: #27ae60;
  --very-low: #2ecc71;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,.4);
}

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

body {
  font-family: 'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
  gap: 12px;
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.header h1 { font-size: 1.15rem; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.header-icon { font-size: 1.2rem; display: inline-flex; align-items: center; }
.header-icon svg { vertical-align: middle; }
.subtitle { font-size: .75rem; color: var(--text-secondary); }
.stats-bar { font-size: .78rem; color: var(--text-secondary); text-align: right; white-space: nowrap; }

/* Container */
.container { display: flex; flex: 1; position: relative; overflow: hidden; }

/* Map */
.map { flex: 1; z-index: 1; }
.leaflet-container { background: #0a0c14; }

/* Custom marker popup */
.foreign-popup .leaflet-popup-content-wrapper {
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.foreign-popup .leaflet-popup-tip { background: var(--surface); }
.foreign-popup .leaflet-popup-content { margin: 12px 16px; font-size: .85rem; line-height: 1.6; }
.popup-name { font-weight: 700; font-size: 1rem; margin-bottom: 4px; }
.popup-total { font-size: 1.3rem; font-weight: 900; }
.popup-total.very_high { color: var(--very-high); }
.popup-total.high { color: var(--high); }
.popup-total.moderate { color: var(--moderate); }
.popup-total.low { color: var(--low); }
.popup-total.very_low { color: var(--very-low); }
.popup-nations { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.popup-detail-btn {
  display: inline-block; margin-top: 8px; padding: 4px 12px;
  background: var(--accent); color: #fff; border-radius: 4px;
  font-size: .78rem; cursor: pointer; border: none; font-family: inherit;
}
.popup-detail-btn:hover { background: var(--accent-light); }

/* Nation tags */
.nation-tag {
  display: inline-block;
  padding: 2px 7px;
  background: rgba(52, 152, 219, 0.15);
  border: 1px solid rgba(52, 152, 219, 0.3);
  border-radius: 4px;
  font-size: .7rem;
  color: var(--accent-light);
  white-space: nowrap;
}

/* Sidebar */
.sidebar {
  width: 380px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 500;
}
.sidebar-controls { padding: 12px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
.search-input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: .85rem;
  font-family: inherit;
  outline: none;
}
.search-input:focus { border-color: var(--accent); }
.filter-row { display: flex; gap: 6px; }
.filter-select {
  flex: 1;
  min-height: 44px;
  padding: 6px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: .82rem;
  font-family: inherit;
  cursor: pointer;
}
.filter-select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  font-size: .78rem;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
}

.pref-list { list-style: none; overflow-y: auto; flex: 1; }
.pref-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .15s;
}
.pref-item:hover, .pref-item:focus-visible { background: var(--surface-hover); }
.pref-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.pref-rank {
  width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  font-size: .72rem; font-weight: 700;
  background: var(--border); color: var(--text-secondary);
  flex-shrink: 0;
}
.pref-rank.top3 { background: var(--accent); color: #fff; }

.pref-info { flex: 1; min-width: 0; }
.pref-name { font-weight: 600; font-size: .88rem; }
.pref-region { font-size: .72rem; color: var(--text-secondary); margin-left: 6px; }
.pref-sub { font-size: .75rem; color: var(--text-secondary); margin-top: 2px; display: flex; flex-wrap: wrap; gap: 3px; align-items: center; }

.pref-total-wrap { text-align: right; flex-shrink: 0; }
.pref-total { font-size: 1.05rem; font-weight: 900; }
.pref-total.very_high { color: var(--very-high); }
.pref-total.high { color: var(--high); }
.pref-total.moderate { color: var(--moderate); }
.pref-total.low { color: var(--low); }
.pref-total.very_low { color: var(--very-low); }
.pref-change { font-size: .72rem; margin-top: 2px; }
.pref-change.up { color: var(--very-high); }
.pref-change.down { color: var(--very-low); }

/* Sidebar Toggle (mobile) */
.sidebar-toggle {
  display: none;
  position: fixed;
  bottom: 60px;
  right: 16px;
  z-index: 1200;
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: var(--shadow);
  align-items: center;
  justify-content: center;
}

/* Modal */
.modal-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.7);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 560px;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
  box-shadow: var(--shadow);
}
.modal-close {
  position: absolute; top: 12px; right: 16px;
  background: none; border: none; color: var(--text-secondary);
  font-size: 1.5rem; cursor: pointer;
}
.modal h2 { font-size: 1.2rem; margin-bottom: 12px; }
.modal-stats {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 8px; margin-bottom: 16px;
}
.modal-stat {
  background: var(--bg);
  border-radius: 6px;
  padding: 10px;
  text-align: center;
}
.modal-stat-label { font-size: .72rem; color: var(--text-secondary); }
.modal-stat-value { font-size: 1.1rem; font-weight: 900; margin-top: 2px; }
.modal-chart-wrap { height: 220px; }

/* Footer */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: .72rem;
  color: var(--text-secondary);
  flex-shrink: 0;
  z-index: 1000;
}
.footer a { color: var(--accent); text-decoration: none; }
.footer a:hover { text-decoration: underline; }
/* Footer touch target (#9) */
.footer a { min-height: 44px; display: inline-flex; align-items: center; }
.footer-links { display: flex; gap: 16px; }

/* Legend */
.legend {
  position: absolute;
  bottom: 60px; left: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  z-index: 800;
  font-size: .75rem;
  box-shadow: var(--shadow);
}
.legend h3 { font-size: .78rem; margin-bottom: 6px; color: var(--text-secondary); }
.legend-item { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; }

/* Header nav */
.header-nav { display: flex; align-items: center; gap: 12px; }
.header-link {
  color: var(--accent);
  text-decoration: none;
  font-size: .82rem;
  padding: 6px 14px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  transition: background .15s, color .15s;
  white-space: nowrap;
}
.header-link:hover { background: var(--accent); color: #fff; }
/* Touch target (#2) */
.header-link { min-height: 44px; display: inline-flex; align-items: center; }
/* Nav active (#5) */
.header-link.active { background: var(--accent); color: #fff; }

/* Nationality page */
.nationality-page { overflow-y: auto; }
.nationality-page.detail-active { overflow: hidden; }
.nationality-page .container { display: block; }

.nat-overview {
  max-width: 800px;
  margin: 0 auto;
  padding: 24px 20px;
}
.nat-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 24px;
}
.nat-chart-wrap {
  height: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.nat-hint {
  text-align: center;
  font-size: .78rem;
  color: var(--text-secondary);
  margin-top: 12px;
}

/* Nationality detail */
.nat-detail { display: flex; flex-direction: column; flex: 1; }
.nat-detail-header { padding: 16px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.nat-detail-header h2 { font-size: 1.3rem; margin-top: 4px; }
.nat-detail-sub { font-size: .82rem; color: var(--text-secondary); margin-top: 4px; }
.nat-back-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: .82rem;
  transition: background .15s;
}
.nat-back-btn:hover { background: var(--surface-hover); }

.nat-detail-split {
  display: flex;
  flex: 1;
  min-height: 0;
}
.nat-detail-map {
  flex: 1;
  min-height: 500px;
  z-index: 1;
}
.nat-detail-map .leaflet-container { background: #0a0c14; }
.nat-detail-list-wrap {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.nat-disclaimer { font-size: .68rem; color: var(--text-secondary); }

/* Dashboard (crime-stats) */
.dashboard {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
}
.dash-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 28px;
}
.dash-section { margin-bottom: 36px; }
.dash-title { font-size: 1rem; font-weight: 700; margin-bottom: 4px; }
.dash-sub { font-size: .78rem; color: var(--text-secondary); margin-bottom: 12px; }
.dash-chart-wrap {
  height: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.dash-chart-tall { height: 400px; }
.dash-chart-half { flex: 1; min-width: 0; }
.dash-row { display: flex; gap: 12px; }
.dash-note {
  font-size: .7rem;
  color: var(--text-secondary);
  margin-top: 8px;
  line-height: 1.5;
}

/* Responsive */
@media (max-width: 768px) {
  .header { padding: 8px 12px; }
  .subtitle { display: none; }
  .stats-bar { font-size: .7rem; white-space: normal; text-align: right; }
  .sidebar {
    position: fixed; top: 0; right: -100%; bottom: 0;
    width: 85%; max-width: 380px;
    transition: right .3s ease;
    z-index: 1100;
  }
  .sidebar.open { right: 0; }
  .sidebar-toggle { display: flex; }
  .legend { bottom: 120px; }
  .modal { width: 95%; padding: 16px; }
  .dash-summary { grid-template-columns: repeat(2, 1fr); }
  .dash-row { flex-direction: column; }
  .dash-chart-wrap { padding: 10px; }
  .nat-summary { grid-template-columns: repeat(2, 1fr); }
  .nat-chart-wrap { height: 350px; padding: 14px; }
  .nat-detail-split { flex-direction: column; }
  .nat-detail-map { min-height: 300px; }
  .nat-detail-list-wrap { width: 100%; max-height: 50vh; }
}

@media (max-width: 480px) {
  .header h1 { font-size: 1rem; }
}

/* a11y */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* Focus rings (#4) */
a:focus-visible, button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Tabular nums (#6) */
.pref-total, .popup-total, .modal-stat-value, .pref-change {
  font-variant-numeric: tabular-nums;
}

/* Skip link (#10) */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}
.sr-only:focus {
  position: fixed;
  top: 8px; left: 8px;
  width: auto; height: auto;
  padding: 12px 20px;
  margin: 0;
  overflow: visible;
  clip: auto;
  white-space: normal;
  background: var(--accent);
  color: #fff;
  font-size: .9rem;
  font-weight: 700;
  border-radius: 6px;
  z-index: 9999;
  text-decoration: none;
}

/* 主要導線は塗りつぶしで最も目立たせる */
.header-link-primary{background:var(--accent);color:#0f172a;font-weight:700;border-color:var(--accent)}
.header-link-primary:hover{filter:brightness(1.12);color:#0f172a}
@media(max-width:600px){
  .header-link{font-size:.78rem;padding:6px 10px}
}

/* ── ヘッダーの折返し対応（導線ボタン追加で幅が不足するため） ── */
.header{flex-wrap:wrap;gap:8px 12px}
.header-left{min-width:0;flex:1 1 auto}
.header-left h1{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.header-controls,.header-nav{flex-wrap:wrap;flex-shrink:0}
@media(max-width:600px){
  .header .subtitle{display:none}   /* 狭い画面では副題を隠して導線を優先 */
}
