/* ===== Reset & Variables ===== */
:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --border: #475569;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --blue: #3b82f6;
  --red: #ef4444;
  --green: #22c55e;
  --yellow: #eab308;
  --purple: #a855f7;
  --teal: #14b8a6;
  --orange: #f97316;
  --pink: #ec4899;
  --font: 'Inter', system-ui, -apple-system, sans-serif;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%; height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Layout (desktop) ===== */
.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-rows: auto 1fr;
  height: 100%; height: 100dvh;
}
.header {
  grid-column: 1 / -1;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10;
  gap: 8px;
}
.header h1 { font-size: 1rem; font-weight: 600; letter-spacing: -0.02em; white-space: nowrap; }
.header-actions { display: flex; gap: 6px; }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sidebar-handle { display: none; }
.sidebar::-webkit-scrollbar { width: 5px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.map-area {
  position: relative;
  overflow: hidden;
  background: #0c1222;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface-2); color: var(--text); font-size: 0.78rem;
  font-family: var(--font); cursor: pointer; transition: background 0.15s;
  white-space: nowrap; -webkit-user-select: none; user-select: none;
  min-height: 36px;
}
.btn:hover { background: var(--border); }
.btn:active { background: var(--border); transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover { background: #2563eb; }
.btn-danger { background: #991b1b; border-color: #991b1b; color: #fca5a5; }
.btn-danger:hover { background: var(--red); }
.btn-share { background: #065f46; border-color: #065f46; color: #6ee7b7; }
.btn-share:hover { background: #047857; }

/* ===== Cards ===== */
.card {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.card-header {
  padding: 8px 12px; font-size: 0.7rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-muted); border-bottom: 1px solid var(--border);
}
.card-body { padding: 10px 12px; }

/* ===== Scores ===== */
.scores-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.score-box {
  text-align: center; padding: 8px 4px;
  background: var(--surface); border-radius: 8px; border: 1px solid var(--border);
}
.score-box .score-val { font-size: 1.3rem; font-weight: 700; line-height: 1.1; }
.score-box .score-label {
  font-size: 0.6rem; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px;
}
.score-box .score-hint {
  font-size: 0.5rem; color: var(--text-muted); opacity: 0.6;
  margin-top: 1px; line-height: 1.2;
}
.score-overall {
  grid-column: 1 / -1;
  text-align: center; padding: 10px 6px; border-radius: 8px;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  border: none;
}
.score-overall .score-val { font-size: 1.8rem; font-weight: 800; }
.score-overall .score-label { color: rgba(255,255,255,0.8); font-size: 0.6rem; }

/* ===== Region list ===== */
.region-list { display: flex; flex-direction: column; gap: 8px; }
.region-item {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; cursor: pointer; transition: border-color 0.15s;
}
.region-item:hover { border-color: var(--blue); }
.region-item.editing { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.region-item-header { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.region-swatch { width: 14px; height: 14px; border-radius: 4px; flex-shrink: 0; }
.region-name { font-weight: 600; font-size: 0.85rem; flex: 1; }
.region-delete {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 1.1rem; padding: 2px 6px; line-height: 1;
  min-width: 28px; min-height: 28px; display: flex; align-items: center; justify-content: center;
}
.region-delete:hover { color: var(--red); }

.region-pop-bar {
  height: 8px; border-radius: 4px; overflow: hidden;
  display: flex; background: var(--bg); margin-bottom: 6px;
}
.region-pop-bar span { height: 100%; transition: width 0.3s; }

.region-stats {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 2px 12px; font-size: 0.72rem; color: var(--text-muted);
}
.region-stats .val { color: var(--text); font-weight: 500; text-align: right; }

.empty-msg {
  text-align: center; color: var(--text-muted); font-size: 0.8rem; padding: 16px 0;
}

/* ===== Legend ===== */
.legend { display: flex; gap: 12px; font-size: 0.72rem; padding: 4px 0; }
.legend-item { display: flex; align-items: center; gap: 4px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* ===== Map SVG ===== */
#mapSvg { width: 100%; height: 100%; cursor: crosshair; touch-action: none; }
.city-dot { pointer-events: none; }
.city-label {
  fill: #e2e8f0; font-family: var(--font); pointer-events: none;
  paint-order: stroke fill; stroke: var(--bg); stroke-width: 3px;
  font-weight: 500;
}
.region-poly {
  stroke: rgba(255,255,255,0.5); stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}
.region-label-text {
  fill: #fff; font-family: var(--font); font-weight: 700;
  text-anchor: middle; dominant-baseline: central;
  pointer-events: none; paint-order: stroke fill;
  stroke: rgba(0,0,0,0.5); stroke-width: 3px;
}
.drawing-path {
  fill: rgba(59,130,246,0.15); stroke: var(--blue); stroke-width: 2;
  stroke-dasharray: 6 3; pointer-events: none; vector-effect: non-scaling-stroke;
}
.boundary-path {
  fill: #162038; stroke: #2d4a7a; stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

/* ===== Naming Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; animation: fadeIn 0.15s;
  padding: 16px;
}
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; width: 360px;
  max-width: 100%; max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow); animation: scaleIn 0.15s;
}
.modal h3 { margin-bottom: 14px; font-size: 1rem; }
.modal input[type="text"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface-2); color: var(--text);
  font-size: 16px; font-family: var(--font); margin-bottom: 10px; outline: none;
}
.modal input[type="text"]:focus { border-color: var(--blue); }
.modal label { font-size: 0.8rem; color: var(--text-muted); display: block; margin-bottom: 6px; }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 20px;
  background: var(--surface-2); color: var(--text); font-size: 0.78rem;
  cursor: pointer; font-family: var(--font); transition: all 0.12s;
  min-height: 32px; display: inline-flex; align-items: center;
}
.chip:hover { border-color: var(--blue); background: rgba(59,130,246,0.15); }
.chip.selected { border-color: var(--blue); background: var(--blue); color: #fff; }

.color-row { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.color-swatch-btn {
  width: 32px; height: 32px; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer; transition: all 0.1s;
}
.color-swatch-btn.selected { border-color: #fff; transform: scale(1.15); }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ===== Share Modal ===== */
.share-modal { width: 480px; }
.share-preview {
  width: 100%; border-radius: 8px; margin-bottom: 12px;
  border: 1px solid var(--border);
}
.share-link-row {
  display: flex; gap: 8px; margin-bottom: 14px;
}
.share-link-row input {
  flex: 1; padding: 10px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface-2); color: var(--text);
  font-size: 0.75rem; font-family: var(--font); outline: none;
  min-width: 0;
}

/* ===== Status bar ===== */
.status-bar {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 14px; font-size: 0.72rem; color: var(--text-muted);
  box-shadow: var(--shadow); pointer-events: none; z-index: 5;
  white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
  transition: opacity 0.3s;
}

/* ===== Zoom controls ===== */
.zoom-controls {
  position: absolute; top: 12px; right: 12px;
  display: flex; flex-direction: column; gap: 4px; z-index: 5;
}
.zoom-controls .btn {
  width: 40px; height: 40px; display: flex;
  align-items: center; justify-content: center;
  padding: 0; font-size: 1.2rem; font-weight: 700;
  backdrop-filter: blur(4px); background: rgba(30,41,59,0.85);
}

/* ===== Totals row ===== */
.totals-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; font-size: 0.75rem; color: var(--text-muted);
  border-top: 1px solid var(--border); margin-top: 4px;
}
.totals-row strong { color: var(--text); }

/* ===== Mobile bottom sheet toggle ===== */
.sidebar-toggle {
  display: none; /* hidden on desktop */
  position: absolute; bottom: 0; left: 0; right: 0;
  z-index: 20;
}
.sidebar-toggle-btn {
  width: 100%; padding: 10px; border: none; border-top: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-family: var(--font);
  font-size: 0.8rem; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding-bottom: calc(10px + var(--safe-bottom));
}
.sidebar-toggle-btn::before {
  content: ''; width: 36px; height: 4px; border-radius: 2px;
  background: var(--border); position: absolute; top: 6px;
}
.sidebar-toggle-btn .toggle-scores {
  display: flex; gap: 12px; font-size: 0.7rem; color: var(--text-muted);
}

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes scaleIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* ===== Responsive — Mobile ===== */
@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .header {
    padding: 6px 12px;
    min-height: 44px;
  }
  .header h1 { font-size: 0.85rem; }
  .header-actions .btn {
    padding: 4px 8px; font-size: 0.7rem; min-height: 32px;
  }
  /* Map fills remaining space */
  .map-area { position: relative; }

  /* Sidebar becomes a bottom sheet */
  .sidebar {
    position: fixed; bottom: 0; left: 0; right: 0;
    z-index: 15; border-right: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    max-height: 70vh;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    padding-bottom: calc(14px + var(--safe-bottom));
    box-shadow: 0 -8px 32px rgba(0,0,0,0.5);
  }
  .sidebar.open { transform: translateY(0); }
  .sidebar-handle {
    display: block; width: 40px; height: 4px; border-radius: 2px;
    background: var(--border); margin: 0 auto 10px;
    flex-shrink: 0;
  }
  .sidebar-toggle { display: block; }

  /* Status bar above toggle */
  .status-bar { bottom: 56px; font-size: 0.68rem; padding: 5px 12px; }

  /* Zoom controls — left side on mobile for thumb reach */
  .zoom-controls { top: auto; bottom: 64px; right: 12px; }
  .zoom-controls .btn { width: 44px; height: 44px; }
}
