/* ── Reset & variables ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #f4f5f7;
  --surface:   #ffffff;
  --border:    #e2e5ea;
  --text:      #1a1d23;
  --text-muted:#6b7280;
  --accent:    #2a7d4f;
  --accent-dk: #1f6040;
  --danger:    #dc2626;
  --danger-dk: #b91c1c;
  --original:  #2563eb;
  --converted: #d97706;
  --arrow:     #dc2626;
  --radius:    8px;
  --shadow:    0 1px 4px rgba(0,0,0,.08), 0 0 0 1px var(--border);
  --sidebar-w: 272px;
  --header-h:  48px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  -webkit-text-size-adjust: 100%;
}

/* ── Layout ────────────────────────────────────────────── */
#app { display: flex; flex-direction: column; height: 100dvh; }

header {
  height: var(--header-h);
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 0 14px;
  flex-shrink: 0;
  gap: 10px;
}
.header-title {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 15px; letter-spacing: .01em;
  min-width: 0;
}
.header-right {
  display: flex; align-items: center; gap: 6px; flex-shrink: 0;
}
.header-sub { font-size: 11px; opacity: .75; white-space: nowrap; }

/* Header icon buttons */
.header-icon-btn {
  display: flex; align-items: center; gap: 5px;
  background: rgba(255,255,255,.15);
  border: none; border-radius: 6px;
  color: #fff; cursor: pointer; padding: 5px 9px;
  font-size: 12px; font-weight: 500;
  transition: background .15s;
  white-space: nowrap;
}
.header-icon-btn:hover { background: rgba(255,255,255,.25); }

/* Hamburger only visible on mobile */
#btn-sidebar-toggle { display: none; }

#main {
  flex: 1;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 0;
}

/* ── Sidebar ───────────────────────────────────────────── */
#sidebar {
  background: var(--bg);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 8px;
  transition: max-height .3s ease;
}

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  flex-shrink: 0;
}

.panel-label {
  font-size: 10px; font-weight: 700; letter-spacing: .08em;
  color: var(--text-muted); text-transform: uppercase;
  margin-bottom: 10px;
  display: flex; align-items: center; gap: 5px;
}
.help-tip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--border); color: var(--text-muted);
  font-size: 9px; font-weight: 700; cursor: help;
}

/* Drop zone */
#drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 18px 10px;
  text-align: center; color: var(--text-muted);
  transition: border-color .2s, background .2s;
  cursor: pointer;
}
#drop-zone.drag-over { border-color: var(--accent); background: #f0faf5; }
#drop-zone svg { display: block; margin: 0 auto 10px; opacity: .5; }
#drop-zone p   { font-size: 12px; line-height: 1.5; margin-bottom: 10px; }
#drop-zone input { display: none; }

.btn-browse {
  display: inline-block; padding: 5px 14px;
  background: var(--accent); color: #fff;
  border-radius: 5px; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: background .15s;
}
.btn-browse:hover { background: var(--accent-dk); }

#file-info {
  margin-top: 10px; padding: 8px 10px;
  background: #f0faf5; border-radius: 6px;
  border: 1px solid #c6e8d5;
  font-size: 11.5px; color: var(--accent-dk);
  word-break: break-word; line-height: 1.6;
}
#file-info.hidden { display: none; }


/* Object counter */
.ct-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.ct-row:last-of-type { border-bottom: none; }
.ct-label { color: var(--text-muted); }
.ct-num   { font-weight: 700; font-size: 14px; color: var(--text); }

.ct-total-row { margin-top: 4px; padding-top: 8px; border-top: 2px solid var(--border); border-bottom: none; }
.ct-total-row .ct-label { color: var(--text); font-weight: 600; }
.ct-total-num { color: var(--accent); font-size: 17px; }

/* Per-layer breakdown (multi-layer zip) */
.ct-breakdown {
  margin: 4px 0;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.ct-layer-row {
  display: flex;
  justify-content: space-between;
  padding: 2px 0 2px 10px;
  font-size: 11px;
}
.ct-layer-name { color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
.ct-layer-val  { color: var(--text); font-weight: 500; flex-shrink: 0; margin-left: 8px; }

.counter-note {
  margin-top: 8px; padding: 6px 8px;
  background: var(--bg); border-radius: 5px;
  font-size: 10.5px; color: var(--text-muted); line-height: 1.5;
}

/* Stats */
.hint { color: var(--text-muted); font-size: 12px; }

.stat-grid   { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 8px; }
.stat-grid-1 { grid-template-columns: 1fr; }   /* single centred box when avg = max */
.stat-box { background: var(--bg); border-radius: 6px; padding: 8px; text-align: center; }
.stat-val  { font-size: 15px; font-weight: 700; color: var(--text); }
.stat-unit { font-size: 10px; color: var(--text-muted); }
.stat-lbl  { font-size: 10px; color: var(--text-muted); margin-top: 2px; }

.stat-row {
  display: flex; justify-content: space-between;
  padding: 4px 0; border-bottom: 1px solid var(--border); font-size: 12px;
}
.stat-row:last-child { border-bottom: none; }
.stat-row .key { color: var(--text-muted); }
.stat-row .val { font-weight: 600; }
/* De-emphasise the arrow-scale note row */
.stat-row-note .key, .stat-row-note .val { font-size: 10.5px; color: var(--text-muted); font-weight: 400; font-style: italic; }


/* Layer toggles */
.toggle-row {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 0; cursor: pointer;
  font-size: 12px; color: var(--text);
  border-bottom: 1px solid var(--border);
}
.toggle-row:last-child { border-bottom: none; }
.toggle-row input[type=checkbox] { accent-color: var(--accent); width: 14px; height: 14px; cursor: pointer; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0; }
/* Original: split blue/green gradient to hint at multi-color scheme */
.original-dot  { background: linear-gradient(135deg, #0ea5e9 50%, #22c55e 50%); }
/* Converted: split orange/purple */
.converted-dot { background: linear-gradient(135deg, #f97316 50%, #7c3aed 50%); }
.arrow-dot     { background: #22d3ee; border: 1.5px solid #0e7490; }

/* Action buttons */
#panel-actions { display: flex; flex-direction: column; gap: 7px; }

.btn-primary, .btn-secondary, .btn-danger {
  width: 100%; display: flex; align-items: center; justify-content: center;
  gap: 7px; padding: 9px 12px;
  border: none; border-radius: 7px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn-primary   { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled)   { background: var(--accent-dk); }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }
.btn-danger    { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.btn-danger:hover:not(:disabled)    { background: #fee2e2; }
.btn-primary:disabled, .btn-secondary:disabled, .btn-danger:disabled {
  opacity: .35; cursor: not-allowed;
}

.sidebar-footer {
  margin-top: auto; padding: 10px 4px 2px;
  font-size: 10px; color: var(--text-muted);
  text-align: center; line-height: 1.6;
}

/* ── Map ────────────────────────────────────────────────── */
#map-wrap { position: relative; min-height: 0; }
#map { width: 100%; height: 100%; }

#map-hint {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255,255,255,.88); backdrop-filter: blur(4px);
  padding: 10px 18px; border-radius: 8px;
  font-size: 13px; color: var(--text-muted);
  pointer-events: none; box-shadow: var(--shadow);
}

#map-legend {
  position: absolute; bottom: 28px; right: 10px;
  background: rgba(255,255,255,.94); backdrop-filter: blur(4px);
  padding: 9px 12px; border-radius: 8px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 4px; z-index: 800;
  min-width: 150px;
}
.leg-group       { display: flex; flex-direction: column; gap: 4px; }
.leg-group-label { font-size: 10px; font-weight: 700; letter-spacing: .06em; color: var(--text-muted); text-transform: uppercase; margin-bottom: 2px; }
.leg-divider     { height: 1px; background: var(--border); margin: 4px 0; }
.leg-item        { display: flex; align-items: center; gap: 8px; font-size: 11.5px; color: var(--text); }

/* Solid line swatch */
.leg-line {
  display: inline-block; width: 22px; height: 3px;
  border-radius: 2px; flex-shrink: 0;
}
/* Dashed line swatch — drawn as repeating gradient in the element's color */
.leg-dashed {
  background: repeating-linear-gradient(
    90deg,
    currentColor 0px, currentColor 5px,
    transparent  5px, transparent  9px
  ) !important;
  height: 3px;
}
/* Filled square swatch for polygons */
.leg-swatch {
  display: inline-block; width: 12px; height: 12px;
  border-radius: 2px; flex-shrink: 0; opacity: .85;
}
/* Dashed-border swatch for converted polygons */
.leg-swatch-dashed {
  opacity: .55;
  outline: 2px dashed currentColor;
  outline-offset: -2px;
}
/* Circle for points */
.leg-dot {
  display: inline-block; width: 10px; height: 10px;
  border-radius: 50%; flex-shrink: 0;
}

#base-toggle {
  position: absolute; top: 10px; right: 10px;
  display: flex;
  background: rgba(255,255,255,.92); backdrop-filter: blur(4px);
  border-radius: 7px; box-shadow: var(--shadow); overflow: hidden; z-index: 800;
}
.base-btn {
  padding: 5px 12px; border: none; background: none;
  font-size: 12px; font-weight: 500; cursor: pointer;
  color: var(--text-muted); transition: background .15s, color .15s;
}
.base-btn.active { background: var(--accent); color: #fff; }

/* ── Help Modal ─────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 10000;
  background: rgba(0,0,0,.5); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
  width: 100%; max-width: 600px;
  max-height: 90vh; overflow-y: auto;
  padding: 24px 24px 20px;
  position: relative;
}
.modal-close {
  position: absolute; top: 14px; right: 14px;
  background: var(--bg); border: none; border-radius: 6px;
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-muted);
  transition: background .15s, color .15s;
}
.modal-close:hover { background: var(--border); color: var(--text); }

.modal-title {
  display: flex; align-items: center; gap: 10px;
  font-size: 17px; font-weight: 700; margin-bottom: 18px;
  color: var(--accent);
}
.modal-section { margin-bottom: 16px; }
.modal-section h3 {
  font-size: 13px; font-weight: 700; color: var(--text);
  margin-bottom: 6px; padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.modal-section p, .modal-section li {
  font-size: 12.5px; color: var(--text-muted); line-height: 1.65;
}
.modal-section ul, .modal-section ol {
  padding-left: 18px; margin-top: 6px; display: flex; flex-direction: column; gap: 4px;
}
.modal-section strong { color: var(--text); }
.modal-highlight {
  background: #f0faf5; border: 1px solid #c6e8d5;
  border-radius: 8px; padding: 10px 14px;
}
.modal-highlight h3 { border-bottom-color: #c6e8d5; color: var(--accent-dk); }
.modal-highlight p  { color: #1f4d38; }

/* ── Toast ─────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 20px; left: 50%;
  transform: translateX(-50%) translateY(60px);
  background: #1a1d23; color: #fff;
  padding: 8px 20px; border-radius: 20px;
  font-size: 13px; pointer-events: none;
  opacity: 0; transition: opacity .25s, transform .25s;
  z-index: 9999; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }

/* ── Scrollbars ─────────────────────────────────────────── */
#sidebar::-webkit-scrollbar,
.modal-box::-webkit-scrollbar { width: 4px; }
#sidebar::-webkit-scrollbar-track,
.modal-box::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb,
.modal-box::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Leaflet overrides ─────────────────────────────────── */
.leaflet-control-attribution { font-size: 9px !important; }

/* ── Mobile responsive ─────────────────────────────────── */
@media (max-width: 768px) {
  :root { --header-h: 50px; }

  .title-text   { font-size: 14px; }
  .header-sub   { display: none; }          /* hide subtitle on small screens */
  #btn-sidebar-toggle { display: flex; }    /* show hamburger */

  #main {
    display: flex;
    flex-direction: column;
  }

  /* Sidebar: full width, scrollable, collapsible */
  #sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-y: auto;
    max-height: 55vh;          /* show ~half screen worth of panels */
    padding: 8px 6px;
    gap: 5px;
  }
  #sidebar.sidebar-collapsed {
    max-height: 0;
    padding-top: 0; padding-bottom: 0;
    overflow: hidden;
    border-bottom: none;
  }

  /* Map takes remaining space */
  #map-wrap { flex: 1; min-height: 220px; }

  /* Compact panels on mobile */
  .panel { padding: 10px; }
  .panel-label { margin-bottom: 8px; }

  #drop-zone { padding: 14px 8px; }
  #drop-zone svg { width: 22px; height: 22px; margin-bottom: 8px; }
  #drop-zone p { font-size: 11px; margin-bottom: 8px; }

  .stat-grid { grid-template-columns: 1fr 1fr; gap: 4px; }
  .stat-val  { font-size: 14px; }

  /* Buttons slightly smaller */
  .btn-primary, .btn-secondary, .btn-danger { padding: 8px 10px; font-size: 12px; }

  /* Base layer toggle — move so it doesn't clash with zoom controls */
  #base-toggle { top: auto; bottom: 36px; right: 10px; }

  /* Legend position */
  #map-legend { bottom: 70px; }

  /* Modal: full width on mobile */
  .modal-box { padding: 18px 16px 16px; border-radius: 10px; }
  .modal-title { font-size: 15px; }
  .modal-section h3 { font-size: 12.5px; }

  /* Footer text hidden on mobile to save space */
  .sidebar-footer { display: none; }
}

@media (max-width: 400px) {
  .btn-label { display: none; }  /* hide "Help" text, show only icon */
  #btn-help { padding: 5px 7px; }
}
