:root {
  --ink: #1f2937;
  --muted: #607087;
  --paper: #f8fafc;
  --panel: rgba(255, 255, 255, .96);
  --line: rgba(43, 76, 126, .18);
  --accent: #0f2744;
  --accent-soft: #eaf4f6;
  --a: #c62828;
  --b: #ef6c00;
  --c: #f9c74f;
  --d: #2e7d32;
  --shadow: 0 16px 48px rgba(15, 39, 68, .16);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { color: var(--ink); background: var(--paper); font-family: "DM Sans", sans-serif; }
button, input { font: inherit; }
button { color: inherit; }

.app-shell {
  display: grid;
  grid-template-columns: 356px minmax(0, 1fr);
  grid-template-rows: 76px minmax(0, 1fr);
  width: 100%; height: 100%;
}

.topbar {
  grid-column: 1 / -1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px 0 24px;
  background: rgba(250, 251, 247, .94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 13px; }
.brand-mark { width: 37px; height: 37px; border-radius: 12px; background: var(--accent); display: grid; place-content: center; gap: 3px; box-shadow: 0 6px 16px rgba(23,79,60,.22); transform: rotate(-2deg); }
.brand-mark span { display: block; height: 3px; border-radius: 4px; background: #d7eee2; }
.brand-mark span:nth-child(1) { width: 13px; margin-left: 7px; }
.brand-mark span:nth-child(2) { width: 20px; }
.brand-mark span:nth-child(3) { width: 10px; margin-left: 3px; }
.eyebrow, .section-kicker { margin: 0 0 2px; color: #728078; font-size: 10px; font-weight: 800; letter-spacing: .15em; text-transform: uppercase; }
.brand h1 { margin: 0; font: 800 19px/1 "Manrope", sans-serif; letter-spacing: -.03em; }
.topbar-meta { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: #40ad78; box-shadow: 0 0 0 4px rgba(64,173,120,.12); }
.icon-button { width: 30px; height: 30px; margin-left: 8px; border: 1px solid var(--line); border-radius: 50%; background: white; cursor: pointer; font-weight: 700; }
.icon-button:hover { background: var(--accent-soft); border-color: #bdcfc5; }

.sidebar { grid-row: 2; overflow-y: auto; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); z-index: 5; }
.intro-block { padding: 27px 26px 22px; }
.intro-block h2 { margin: 6px 0 8px; font: 800 25px/1.13 "Manrope", sans-serif; letter-spacing: -.045em; }
.intro-block p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.control-section, .stats-section { padding: 18px 26px; border-top: 1px solid var(--line); }
.section-heading { min-height: 24px; display: flex; align-items: center; justify-content: space-between; margin-bottom: 13px; }
.section-heading h3 { margin: 0; font: 700 13px/1.2 "Manrope", sans-serif; }
.text-button { border: 0; background: transparent; color: var(--accent); font-size: 11px; font-weight: 700; cursor: pointer; padding: 3px; }
.text-button:hover { text-decoration: underline; }
.text-button:disabled { color: #9aa49f; cursor: default; text-decoration: none; }

.filter-instruction, .control-help { margin: -4px 0 10px; color: #7b8881; font-size: 9.5px; line-height: 1.4; }
.group-filter { display: grid; grid-template-columns: repeat(2, 1fr); gap: 7px; }
.group-chip { min-height: 42px; display: grid; grid-template-columns: 15px 8px 1fr; grid-template-rows: 1fr 1fr; align-items: center; column-gap: 6px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 10px; background: #f3f5f2; color: #89938e; text-align: left; font-size: 11px; font-weight: 700; cursor: pointer; opacity: 1; transition: .18s ease; }
.group-chip:hover { transform: translateY(-1px); border-color: #b9c7bf; }
.group-chip.is-active { color: var(--ink); background: #fff; border-color: #aebdb4; box-shadow: inset 0 0 0 1px rgba(255,255,255,.8), 0 2px 7px rgba(25,52,40,.06); }
.group-chip .chip-check { grid-row: 1 / 3; width: 15px; height: 15px; display: grid; place-items: center; border: 1px solid #bfc8c2; border-radius: 4px; color: transparent; background: #fff; font-size: 10px; }
.group-chip.is-active .chip-check { color: white; border-color: var(--accent); background: var(--accent); }
.group-chip .chip-dot { grid-column: 2; grid-row: 1; align-self: end; margin-bottom: 1px; filter: grayscale(1); opacity: .45; }
.group-chip.is-active .chip-dot { filter: none; opacity: 1; }
.group-chip > span:nth-of-type(3) { grid-column: 3; grid-row: 1; align-self: end; }
.group-chip small { grid-column: 2 / 4; grid-row: 2; align-self: start; margin-top: 1px; color: #969f9a; font-size: 8px; font-weight: 600; }
.group-chip.is-active small { color: #4c755f; }
.chip-dot, .stat-name i, .map-legend i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: 0 0 auto; }
.select-label { display: block; margin-bottom: 6px; color: #66756e; font-size: 9.5px; font-weight: 700; }
.select-wrap { position: relative; }
.select-wrap::after { content: "⌄"; position: absolute; top: 50%; right: 11px; transform: translateY(-56%); color: var(--accent); pointer-events: none; font-size: 16px; }
.select-wrap select { width: 100%; height: 39px; appearance: none; padding: 0 34px 0 11px; border: 1px solid #cfd8d1; border-radius: 10px; color: var(--ink); background: #fff; font-size: 11px; font-weight: 700; cursor: pointer; outline: none; }
.select-wrap select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(23,79,60,.1); }
.select-wrap + .control-help { margin: 7px 0 0; }
.group-a { background-color: var(--a) !important; }
.group-b { background-color: var(--b) !important; }
.group-c { background-color: var(--c) !important; }
.group-d { background-color: var(--d) !important; }

.layer-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; padding: 4px; border-radius: 12px; background: #edf1ec; }
.layer-tab { min-height: 48px; display: grid; place-items: center; align-content: center; gap: 2px; border: 0; border-radius: 9px; background: transparent; color: #7b8780; font-size: 10px; font-weight: 700; cursor: pointer; transition: .18s ease; }
.layer-tab.is-active { color: var(--accent); background: #fff; box-shadow: 0 3px 9px rgba(31,58,47,.11); }
.tab-icon { min-height: 16px; font-size: 17px; line-height: 1; letter-spacing: -3px; }
.resolution-control { margin-top: 15px; }
.control-label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 10px; font-weight: 700; }
.territory-modes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px; margin-bottom: 9px; }
.territory-mode { min-width: 0; min-height: 34px; padding: 5px 7px; border: 1px solid rgba(43,76,126,.18); border-radius: 8px; color: #607087; background: #fff; font-size: 9px; line-height: 1.15; font-weight: 800; cursor: pointer; transition: .15s ease; }
.territory-mode:hover { border-color: #aebdb4; }
.territory-mode.is-active { color: white; border-color: var(--accent); background: var(--accent); box-shadow: 0 3px 8px rgba(23,79,60,.18); }
.territory-explanation { min-height: 32px; margin: 0 0 12px; padding: 8px 9px; border-radius: 8px; color: #5e6d65; background: #edf2ed; font-size: 9.5px; line-height: 1.45; }
.resolution-control label { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; font-weight: 600; }
.resolution-control output { color: var(--accent); font-weight: 800; }
.resolution-control input { width: 100%; margin: 11px 0 3px; accent-color: var(--accent); }
.range-labels { display: flex; justify-content: space-between; color: #97a19b; font-size: 9px; }

.stats-section { padding-top: 20px; }
.stats-section .section-heading { align-items: flex-end; }
.total-stat { display: flex; align-items: baseline; gap: 9px; margin: 3px 0 14px; }
.total-stat strong { font: 800 29px/1 "Manrope", sans-serif; letter-spacing: -.05em; }
.total-stat span { color: var(--muted); font-size: 10px; }
.composition-bar { display: flex; width: 100%; height: 7px; overflow: hidden; gap: 2px; border-radius: 6px; background: #e7ebe7; }
.composition-bar span { transition: width .25s ease; }
.group-stats { margin-top: 11px; }
.stat-row { display: grid; grid-template-columns: 1fr auto 40px; align-items: center; gap: 10px; min-height: 30px; font-size: 11px; }
.stat-row.is-filtered { opacity: .34; }
.stat-name { display: flex; align-items: center; gap: 8px; color: #52625a; }
.stat-count { font-size: 11px; }
.stat-percent { color: #8a958f; text-align: right; font-variant-numeric: tabular-nums; }
.sidebar-footer { margin-top: auto; display: flex; justify-content: space-between; gap: 8px; padding: 13px 26px; border-top: 1px solid var(--line); color: #929c96; font-size: 9px; }

.map-stage { grid-column: 2; grid-row: 2; position: relative; min-width: 0; min-height: 0; background: #dce5de; }
#map { position: absolute; inset: 0; }
.mapboxgl-ctrl-bottom-right { right: 12px; bottom: 12px; }
.mapboxgl-ctrl-group { overflow: hidden; border-radius: 11px !important; box-shadow: 0 8px 24px rgba(20,45,35,.18) !important; }
.mapboxgl-popup-content { border-radius: 14px; padding: 13px 15px; box-shadow: var(--shadow); font: 12px/1.4 "DM Sans", sans-serif; }
.popup-title { font: 800 14px/1.2 "Manrope",sans-serif; margin-bottom: 5px; }
.popup-grid { display: grid; grid-template-columns: 1fr auto; gap: 3px 14px; color: var(--muted); }
.popup-grid strong { color: var(--ink); }

.map-toolbar { position: absolute; top: 18px; left: 18px; display: flex; gap: 7px; z-index: 3; }
.tool-button { min-height: 39px; display: inline-flex; align-items: center; gap: 9px; padding: 0 13px; border: 1px solid rgba(216,226,219,.9); border-radius: 11px; background: rgba(252,253,249,.93); box-shadow: 0 8px 24px rgba(20,45,35,.14); backdrop-filter: blur(12px); font-size: 11px; font-weight: 700; cursor: pointer; }
.tool-button:hover, .tool-button.is-active { background: var(--accent); border-color: var(--accent); color: white; }
.tool-button.compact { width: 39px; padding: 0; justify-content: center; font-size: 20px; }
.polygon-icon { width: 16px; height: 14px; border: 1.7px solid currentColor; clip-path: polygon(18% 0, 100% 18%, 83% 100%, 0 75%); }
.map-legend { position: absolute; left: 18px; bottom: 20px; min-width: 210px; padding: 10px 12px; border: 1px solid rgba(216,226,219,.9); border-radius: 12px; background: rgba(252,253,249,.94); box-shadow: 0 8px 24px rgba(20,45,35,.12); backdrop-filter: blur(12px); color: #53635b; font-size: 10px; font-weight: 700; }
.legend-title { margin-bottom: 7px; color: var(--ink); font: 800 10px/1.2 "Manrope", sans-serif; }
.legend-items { display: flex; align-items: center; gap: 13px; }
.map-legend span { display: flex; align-items: center; gap: 5px; }
.map-legend i { width: 7px; height: 7px; }
.legend-note { margin-top: 7px; color: #849088; font-size: 8.5px; font-weight: 500; }
.legend-gradient { width: 190px; height: 8px; border-radius: 8px; background: var(--legend-gradient, linear-gradient(90deg,#2e7d32,#f9c74f,#c62828)); }
.legend-scale { display: flex; justify-content: space-between; margin-top: 5px; color: #7c8982; font-size: 8px; font-weight: 600; }
.draw-hint { position: absolute; top: 18px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 11px; color: white; background: rgba(18,56,43,.92); box-shadow: var(--shadow); z-index: 4; }
.draw-hint strong { font-size: 11px; }
.draw-hint span { color: #c7dbd1; font-size: 10px; }

.loading-panel { position: absolute; left: 50%; bottom: 34px; transform: translateX(-50%); width: min(480px, calc(100% - 36px)); display: grid; grid-template-columns: 40px 1fr; align-items: center; gap: 15px; padding: 16px 18px; border: 1px solid rgba(255,255,255,.75); border-radius: 16px; background: rgba(250,252,248,.95); box-shadow: var(--shadow); backdrop-filter: blur(16px); z-index: 6; transition: .25s ease; }
.loading-panel.is-complete { opacity: 0; transform: translate(-50%, 12px); pointer-events: none; }
.loader-orbit { width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid #bdd0c5; border-radius: 50%; }
.loader-orbit:before { content:""; width: 5px; height: 5px; border-radius: 50%; background: var(--accent); box-shadow: 11px 0 0 #90b3a1, -11px 0 0 #d2ded7; animation: spin 1.3s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-copy { display: flex; justify-content: space-between; gap: 12px; font-size: 12px; }
.loading-copy span { color: var(--accent); font-weight: 800; }
.loading-panel p { margin: 4px 0 10px; color: var(--muted); font-size: 10px; }
.progress-track { height: 4px; overflow: hidden; border-radius: 4px; background: #e1e8e2; }
.progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #0f2744, #4fb3bf); transition: width .2s ease; }

.file-fallback { position: absolute; inset: 0; z-index: 8; display: grid; place-content: center; justify-items: center; padding: 30px; text-align: center; background: rgba(234,239,233,.92); backdrop-filter: blur(12px); }
.file-fallback[hidden] { display: none; }
.file-fallback h2 { margin: 15px 0 7px; font: 800 23px/1.2 "Manrope",sans-serif; }
.file-fallback p { max-width: 440px; margin: 0 0 20px; color: var(--muted); font-size: 13px; line-height: 1.55; }
.fallback-icon { width: 58px; height: 58px; display: grid; place-items: center; border-radius: 18px; color: white; background: var(--accent); font-size: 12px; font-weight: 800; letter-spacing: .08em; box-shadow: 0 12px 30px rgba(23,79,60,.25); }
.file-button { padding: 11px 18px; border-radius: 11px; color: white; background: var(--accent); font-size: 12px; font-weight: 700; cursor: pointer; }
.toast { position: absolute; right: 18px; bottom: 74px; max-width: 320px; padding: 10px 13px; border-radius: 10px; color: white; background: rgba(20,42,33,.94); box-shadow: var(--shadow); font-size: 11px; opacity: 0; transform: translateY(8px); pointer-events: none; transition: .2s ease; z-index: 7; }
.toast.is-visible { opacity: 1; transform: translateY(0); }

.about-dialog { width: min(450px, calc(100% - 32px)); padding: 28px; border: 0; border-radius: 20px; color: var(--ink); background: #fbfcf8; box-shadow: 0 30px 90px rgba(18,45,34,.28); }
.about-dialog::backdrop { background: rgba(15,34,27,.45); backdrop-filter: blur(5px); }
.about-dialog h2 { margin: 7px 0 10px; font: 800 23px/1.2 "Manrope",sans-serif; }
.about-dialog > p { color: var(--muted); font-size: 13px; line-height: 1.6; }
.dialog-close { position: absolute; top: 13px; right: 14px; width: 32px; height: 32px; border: 0; border-radius: 50%; background: #edf1ec; cursor: pointer; font-size: 20px; }
.privacy-note { display: flex; gap: 12px; margin-top: 18px; padding: 13px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); }
.privacy-note span { font-weight: 800; }
.privacy-note p { margin: 0; font-size: 11px; line-height: 1.5; }

@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; grid-template-rows: 64px minmax(0, 1fr) auto; }
  .topbar { grid-row: 1; padding: 0 15px; }
  .topbar-meta > span:not(.live-dot) { display: none; }
  .map-stage { grid-column: 1; grid-row: 2; }
  .sidebar { grid-column: 1; grid-row: 3; max-height: 44vh; border-top: 1px solid var(--line); border-right: 0; }
  .intro-block { display: none; }
  .sidebar-footer { display: none; }
  .control-section, .stats-section { padding: 14px 18px; }
  .map-legend { bottom: 14px; }
  .loading-panel { bottom: 16px; }
}

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