:root {
  --ink: #15201d;
  --muted: #66736f;
  --paper: #f4f7f6;
  --surface: rgba(255, 255, 255, 0.94);
  --line: #dce5e2;
  --green: #0d6b58;
  --green-dark: #084f43;
  --inbound: #1788d4;
  --inbound-soft: #e4f3fc;
  --outbound: #e45c43;
  --outbound-soft: #fff0ec;
  --shadow: 0 18px 50px rgba(28, 48, 43, 0.13);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
}

button, select { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }
button:disabled { opacity: .45; cursor: not-allowed; transform: none !important; }

.app-shell {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.topbar {
  min-height: 82px;
  padding: 16px 24px 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: rgba(244, 247, 246, 0.97);
  z-index: 1000;
}

.eyebrow, .section-label {
  margin: 0 0 4px;
  color: var(--green);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .16em;
}

h1 { margin: 0; font-size: clamp(21px, 2vw, 28px); letter-spacing: .01em; }
h2, h3 { margin: 0; }

.topbar-actions { display: flex; align-items: center; gap: 9px; }
.ghost-button, .icon-button {
  min-height: 40px;
  border: 1px solid #cdd8d5;
  background: #fff;
  border-radius: 12px;
  cursor: pointer;
  transition: .18s ease;
}
.ghost-button { padding: 0 16px; font-weight: 700; }
.icon-button { width: 40px; font-weight: 900; }
.ghost-button:hover, .icon-button:hover { border-color: var(--green); color: var(--green); transform: translateY(-1px); }

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(380px, 30vw);
}

.map-section { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: #dfe8e5; }
.map { position: absolute; inset: 0; background: #dfe8e5; }
.panel-surface { background: var(--surface); border: 1px solid rgba(211, 224, 220, .9); box-shadow: var(--shadow); backdrop-filter: blur(10px); }

.map-toolbar {
  position: absolute;
  z-index: 700;
  top: 18px;
  left: 18px;
  display: flex;
  gap: 10px;
  padding: 10px;
  border-radius: 16px;
}
.select-group { display: grid; gap: 3px; min-width: 170px; }
.select-group.compact { min-width: 130px; }
.select-group span { padding-left: 2px; color: var(--muted); font-size: 10px; font-weight: 800; }
select {
  height: 38px;
  padding: 0 34px 0 11px;
  color: var(--ink);
  border: 0;
  border-radius: 9px;
  background: #edf3f1;
  font-weight: 750;
  outline: none;
}
select:focus-visible { box-shadow: 0 0 0 3px rgba(13, 107, 88, .2); }

.legend {
  position: absolute;
  z-index: 700;
  left: 18px;
  bottom: 18px;
  min-width: 228px;
  padding: 12px 14px;
  border-radius: 14px;
  font-size: 12px;
}
.legend-title { display: block; margin-bottom: 8px; font-size: 11px; }
.amount-legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
.amount-legend span { display: flex; align-items: center; gap: 6px; font-size: 10px; font-weight: 750; white-space: nowrap; }
.amount-legend i { width: 22px; height: 4px; border-radius: 99px; background: var(--swatch); }
.legend p { margin: 2px 0 0; color: var(--muted); font-size: 10px; }
.amount-legend + p { margin-top: 8px; }

.map-status {
  position: absolute;
  z-index: 700;
  right: 18px;
  bottom: 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 11px;
}
.pulse-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 5px rgba(13,107,88,.12); }

.side-panel {
  min-height: 0;
  overflow: auto;
  padding: 24px 22px 18px;
  border-left: 1px solid var(--line);
  background: #fbfcfc;
}
.selection-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 15px; }
.selection-heading h2 { font-size: 30px; }
.year-badge, .count-badge {
  padding: 6px 10px;
  color: var(--green-dark);
  border: 1px solid #cfe0db;
  border-radius: 999px;
  background: #edf5f2;
  font-size: 11px;
  font-weight: 800;
}

.kpi-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.kpi-card { min-width: 0; padding: 13px 11px 12px; border: 1px solid var(--line); border-radius: 14px; background: #fff; }
.kpi-card span { display: block; min-height: 30px; color: var(--muted); font-size: 10px; font-weight: 750; }
.kpi-card strong { font-size: clamp(18px, 1.7vw, 25px); font-variant-numeric: tabular-nums; }
.kpi-card small { margin-left: 3px; color: var(--muted); }
.inbound-card { border-top: 3px solid var(--inbound); }
.outbound-card { border-top: 3px solid var(--outbound); }
.net-card.positive { background: var(--inbound-soft); border-top: 3px solid var(--inbound); }
.net-card.negative { background: var(--outbound-soft); border-top: 3px solid var(--outbound); }

.mode-section { margin: 20px 0 18px; }
.segmented-control { display: grid; grid-template-columns: repeat(3, 1fr); padding: 4px; border-radius: 13px; background: #e9efed; }
.segmented-control button { height: 38px; border: 0; border-radius: 10px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 800; cursor: pointer; }
.segmented-control button.active { color: var(--ink); background: #fff; box-shadow: 0 4px 12px rgba(35,57,51,.11); }
.mode-description { margin: 8px 3px 0; color: var(--muted); font-size: 11px; }

.ranking-section { min-height: 0; }
.ranking-header { display: flex; align-items: end; justify-content: space-between; margin-bottom: 10px; }
.ranking-header h3 { font-size: 18px; }
.table-head, .ranking-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 62px 62px 65px;
  gap: 5px;
  align-items: center;
}
.table-head { padding: 0 10px 7px; color: #89938f; font-size: 9px; font-weight: 800; text-align: right; }
.table-head span:first-child { text-align: left; }
.ranking-list { margin: 0; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.ranking-row { position: relative; min-height: 46px; padding: 7px 9px; border-bottom: 1px solid #e7edeb; cursor: pointer; transition: background .15s ease; }
.ranking-row:hover, .ranking-row:focus-visible { background: #f0f6f4; outline: none; }
.pref-cell { position: relative; z-index: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.rank-number { width: 21px; color: #9aa4a0; font-size: 10px; font-weight: 800; text-align: right; }
.pref-name { overflow: hidden; font-size: 12px; font-weight: 800; white-space: nowrap; text-overflow: ellipsis; }
.flow-bar { position: absolute; z-index: 0; left: 0; bottom: 3px; height: 3px; border-radius: 0 3px 3px 0; opacity: .55; }
.flow-bar.inbound { background: var(--inbound); }
.flow-bar.outbound { background: var(--outbound); }
.flow-bar.positive { background: var(--inbound); }
.flow-bar.negative { background: var(--outbound); }
.number-cell { position: relative; z-index: 1; font-size: 10px; font-variant-numeric: tabular-nums; text-align: right; }
.net-positive { color: #0875b8; font-weight: 800; }
.net-negative { color: #cf482f; font-weight: 800; }

.source-note { display: grid; gap: 3px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); color: var(--muted); font-size: 10px; }
.source-note a { color: var(--green); text-decoration: none; }

.leaflet-container { font-family: inherit; }
.leaflet-control-zoom { border: 0 !important; box-shadow: var(--shadow) !important; }
.leaflet-control-zoom a { color: var(--ink) !important; border-color: var(--line) !important; }
.leaflet-popup-content-wrapper { border-radius: 14px; box-shadow: var(--shadow); }
.leaflet-popup-content { margin: 13px 15px; }
.map-popup h4 { margin: 0 0 7px; font-size: 14px; }
.map-popup dl { display: grid; grid-template-columns: auto auto; gap: 4px 12px; margin: 0; font-size: 11px; }
.map-popup dt { color: var(--muted); }
.map-popup dd { margin: 0; font-weight: 800; text-align: right; }
.flow-arrow { width: 18px; height: 18px; display: grid; place-items: center; color: currentColor; text-shadow: 0 1px 2px rgba(0,0,0,.3); }
.flow-arrow::before { content: "➤"; font-size: 17px; line-height: 1; }
.flow-arrow.inbound { color: var(--inbound); }
.flow-arrow.outbound { color: var(--outbound); }

dialog { width: min(520px, calc(100% - 28px)); padding: 28px; border: 0; border-radius: 20px; color: var(--ink); box-shadow: 0 30px 90px rgba(10, 25, 21, .35); }
dialog::backdrop { background: rgba(12, 28, 24, .55); backdrop-filter: blur(3px); }
dialog h2 { margin-bottom: 12px; }
dialog p { color: #52605c; line-height: 1.8; }
dialog dl { margin: 20px 0; }
dialog dl div { display: grid; grid-template-columns: 70px 1fr; padding: 10px 0; border-bottom: 1px solid var(--line); }
dialog dt { font-weight: 900; }
dialog dd { margin: 0; color: #52605c; }
.dialog-close { position: absolute; top: 12px; right: 14px; width: 34px; height: 34px; border: 0; border-radius: 50%; background: #edf2f0; font-size: 22px; cursor: pointer; }
.dialog-note { padding: 12px; border-radius: 12px; background: #f1f5f4; font-size: 12px; }

.loading-screen { position: fixed; z-index: 5000; inset: 0; display: grid; place-content: center; justify-items: center; gap: 16px; background: var(--paper); transition: opacity .35s ease, visibility .35s ease; }
.loading-screen.hidden { opacity: 0; visibility: hidden; }
.loading-mark { width: 42px; height: 42px; border: 4px solid #c9d9d4; border-top-color: var(--green); border-radius: 50%; animation: spin .8s linear infinite; }
.loading-screen p { color: var(--muted); font-size: 12px; font-weight: 800; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 980px) {
  .app-shell { height: auto; min-height: 100%; overflow: visible; }
  .workspace { grid-template-columns: 1fr; }
  .map-section { height: 61vh; min-height: 470px; }
  .side-panel { overflow: visible; border-left: 0; border-top: 1px solid var(--line); }
}

@media (max-width: 560px) {
  .topbar { min-height: 68px; padding: 12px 13px 10px 16px; }
  .eyebrow { font-size: 8px; }
  h1 { font-size: 18px; }
  .ghost-button { display: none; }
  .map-section { height: 59vh; min-height: 430px; }
  .map-toolbar { top: 10px; left: 10px; right: 10px; gap: 6px; }
  .select-group { min-width: 0; flex: 1; }
  .select-group.compact { min-width: 108px; flex: 0 0 108px; }
  .legend { left: 10px; bottom: 10px; min-width: 212px; padding: 9px 11px; }
  .amount-legend { gap: 5px 9px; }
  .map-status { right: 10px; bottom: 10px; max-width: 145px; padding: 8px 9px; }
  .leaflet-control-zoom { display: none; }
  .side-panel { padding: 20px 13px; }
  .selection-heading h2 { font-size: 27px; }
  .kpi-card { padding: 11px 8px; }
  .kpi-card strong { font-size: 18px; }
  .kpi-card small { display: none; }
  .table-head, .ranking-row { grid-template-columns: minmax(105px, 1fr) 54px 54px 58px; gap: 2px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
