/* =========================================================
   1. MUUTTUJAT JA PERUSASETUKSET
   ========================================================= */
:root {
  --bg:#0f1115;
  --panel:#151924;
  --muted:#8b93a7;
  --border:#232836;
  --text:#e6e9f2;
  --accent:#3b82f6;
  --accent-2:#2f6fe0;
  --green:#00a050;
  --yellow:#ffc107;
  --red:#dc3545;

  --radius:10px;
  --shadow:0 8px 24px rgba(0,0,0,.25);
}

/* --- PERUS TYPOGRAFIA JA TAUSTA --- */
html, body {
  height:100%;
  margin:0;
  color:var(--text);
  background:var(--bg);
  font:14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  overflow:hidden;
  -webkit-font-smoothing:antialiased;
  -moz-osx-font-smoothing:grayscale;
}


/* =========================================================
   2. LAYOUT JA SIVUPALKKI
   ========================================================= */
.app {
  position:fixed;
  inset:0;
  display:grid;
  grid-template-columns:1fr 380px;
}
#map { height:100%; width:100%; }

/* --- SIVUPALKKI --- */
.sidebar {
  background:var(--panel);
  border-left:1px solid var(--border);
  display:flex;
  flex-direction:column;
  min-width:320px;
}

/* Sticky header yläreunaan */
.sidebar header {
  position:sticky;
  top:0;
  z-index:3;
  padding:12px 14px;
  border-bottom:1px solid var(--border);
  background:var(--panel);
  display:flex;
  align-items:center;
  gap:10px;
}

/* --- Scrollattava sisältöalue sivupalkissa --- */
/* Tämä ratkaisee overflow-ongelman */
.sidebar .controls {
  flex:1;
  overflow-y:auto;
  overflow-x:hidden;
  padding-bottom:12px;
  scroll-behavior:smooth;
}
.sidebar .controls::-webkit-scrollbar {
  width:8px;
}
.sidebar .controls::-webkit-scrollbar-thumb {
  background:#2c3347;
  border-radius:4px;
}
.sidebar .controls::-webkit-scrollbar-thumb:hover {
  background:#3b4259;
}


/* =========================================================
   3. KOMPONENTIT JA UI-ELEMENTIT
   ========================================================= */

/* --- Väritäplä --- */
.color-pill {
  width:18px; height:18px; border-radius:50%;
  border:1px solid #0003; display:inline-block; vertical-align:middle; margin-right:6px;
}
.pill-green  { background:rgba(0,160,80,.35);  outline:1px solid #0d5; }
.pill-yellow { background:rgba(255,193,7,.35); outline:1px solid #fb3; }
.pill-red    { background:rgba(220,53,69,.35); outline:1px solid #f66; }

/* --- Napit --- */
.btn {
  appearance:none; border:1px solid var(--border);
  background:#1b2030; color:var(--text);
  padding:8px 12px; border-radius:8px; cursor:pointer;
  transition:background .12s ease, border-color .12s ease, transform .05s ease;
  line-height:1;
}
.btn:hover { border-color:#2c3347; }
.btn:active { transform:translateY(1px); }
.btn:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }
.btn.primary { background:var(--accent); border-color:var(--accent-2); color:#fff; }
.btn.danger  { background:#a61e2b; border-color:#7e1822; }
.btn.ghost   { background:transparent; }

/* --- Kontrollialueet --- */
.controls .row {
  display:block; padding-bottom:18px;
}
.controls label { color:var(--muted); }
.controls h5 {
  margin:8px 0 4px;
  font-size:13px;
  color:var(--muted);
  font-weight:600;
}


/* =========================================================
   4. HAITARIRAKENNE (ACCORDION)
   ========================================================= */
.accordion {
  border-bottom:1px solid var(--border);
  overflow:hidden;
  transition:background .2s ease;
}
.accordion-title {
  cursor:pointer;
  padding:10px 14px;
  margin:0;
  font-weight:600;
  background:#181c29;
  color:var(--text);
  font-size:15px;
  border-top:1px solid #1f2330;
  display:block;
  justify-content:space-between;
  align-items:center;
  user-select:none;
  transition:background .15s ease, color .15s ease;
}
.accordion-title:hover {
  background:#1d2130;
  color:#fff;
}
.accordion-content {
  display:none;
  padding:12px 14px;
  background:#151924;
  animation:fadeIn .25s ease;
}
.accordion.open .accordion-content {
  display:block;
}
.accordion.open .accordion-title {
  background:#1e2334;
  color:var(--accent);
}
.accordion-title::after {
  content:"▸";
  font-size:12px;
  opacity:.6;
  margin-left:8px;
  transition:transform .2s ease, opacity .2s ease;
}
.accordion.open .accordion-title::after {
  transform:rotate(90deg);
  opacity:1;
}
.accordion.open {
  box-shadow:inset 0 1px 0 rgba(255,255,255,.04), 0 1px 4px rgba(0,0,0,.25);
}

@keyframes fadeIn {
  from { opacity:0; transform:translateY(-4px); }
  to   { opacity:1; transform:translateY(0); }
}

/* Tyhjä lista näyttää ohjeen */
.list:empty::before {
  content:"Ei vielä alueita – lisää uusi tai tuo JSON-tiedosto.";
  display:block;
  padding:16px;
  color:var(--muted);
  font-size:13px;
  line-height:1.5;
  text-align:center;
}


/* =========================================================
   5. LISTA / ALUEKOHTAISET KORTIT
   ========================================================= */
.list {
  flex:1; overflow:auto; padding:10px 8px;
  scroll-behavior:smooth;
}
.item {
  border:1px solid var(--border);
  border-radius:var(--radius);
  padding:10px; margin:8px;
  background:#121621;
  transition:background .2s ease, border-color .2s ease, box-shadow .2s ease, max-height .25s ease;
  overflow:hidden;
}
.item:hover { border-color:#2c3347; }
.item.expanded { background:#181c2a; box-shadow:var(--shadow); }
.item.selected { border-color:var(--accent); box-shadow:0 0 0 1px var(--accent) inset; }
.item h4 { margin:0 0 6px; font-size:14px; }
.meta { font-size:12px; color:var(--muted); margin-bottom:8px; }
.item .badge {
  font-size:11px; border:1px solid var(--border);
  padding:2px 6px; border-radius:6px; color:var(--muted);
}
.item .actions { display:none!important; gap:8px; flex-wrap:wrap; }
.item.expanded .actions { display:flex!important; }
.item:not(.expanded) .meta:first-of-type { display:none; }


/* =========================================================
   6. FOOTER
   ========================================================= */
.footer {
  border-top:1px solid var(--border);
  padding:10px 12px;
  background:#141822;
  position:sticky;
  bottom:0;
  z-index:4;
}
textarea#importJson {
  width:100%; min-height:90px; resize:vertical;
  background:#0e111a; color:var(--text);
  border:1px solid var(--border); border-radius:8px; padding:8px;
}


/* =========================================================
   7. MODALIT
   ========================================================= */
.modal {
  position:fixed; inset:0; background:#0008;
  display:none; align-items:center; justify-content:center; z-index:9999;
}
.modal .card {
  background:#121621; border:1px solid var(--border);
  border-radius:12px; width:min(520px, 92vw); padding:16px;
}
.modal .card h3 { margin:0 0 10px; }
.field { display:grid; gap:6px; margin:8px 0; }
.field input, .field textarea, .field select {
  width:100%; padding:8px 10px; border-radius:8px;
  border:1px solid var(--border); background:#0e111a; color:var(--text);
}
.field input:focus-visible, .field textarea:focus-visible, .field select:focus-visible {
  outline:2px solid var(--accent); outline-offset:2px;
}
.row-end { display:flex; gap:8px; justify-content:flex-end; margin-top:8px; }


/* =========================================================
   8. KARTTA / LEAFLET TYYLIT
   ========================================================= */
.leaflet-container { background:#0b0d12; }
.leaflet-bar a { background:#1b2030; color:#e6e9f2; border-color:#2c3347; }
.leaflet-bar a:hover { background:#22283a; }
.leaflet-draw-toolbar a { background:#1b2030; }
.leaflet-control-attribution { background:#1b2030aa; color:#99a2b7; }
.leaflet-popup-content-wrapper { background:#121621; color:var(--text); }
.leaflet-popup-tip { background:#121621; }

/* --- Karttatoggle vasempaan alakulmaan --- */
.map-toggle {
  position:absolute;
  bottom:18px;
  left:18px;
  background:#151924dd;
  color:var(--text);
  padding:8px 12px;
  border-radius:8px;
  border:1px solid var(--border);
  font-size:13px;
  z-index:999;
  box-shadow:0 2px 8px rgba(0,0,0,.35);
  backdrop-filter:blur(4px);
  user-select:none;
}
.map-toggle input[type="checkbox"] {
  margin-right:6px;
  accent-color:var(--accent);
}
.map-toggle:hover { background:#1b2030ee; }


/* =========================================================
   9. PIIRTOTILAN VISUAALISET VIHJEET
   ========================================================= */
.drawing-active .sidebar { opacity:.5; }
.drawing-active #map { outline:1px dashed rgba(255,255,255,.15); outline-offset:-1px; }


/* =========================================================
   10. INFOIKKUNAT
   ========================================================= */
.map-info {
  position:absolute; bottom:20px; left:20px; z-index:9999;
  background:#121621dd; padding:12px 16px; border-radius:10px;
  border:1px solid #2c3347; min-width:240px; backdrop-filter:blur(4px);
  box-shadow:var(--shadow); max-height:50vh; overflow-y:auto;
}
.map-info h4 { margin:0 0 4px; }
.map-info p  { margin:0 0 8px; color:var(--muted); }
.map-info button { margin-right:6px; }



/* =========================================================
   LEAFLET TOOLTIP (hover-seuraaja)
   ========================================================= */
.map-tooltip {
  position:absolute;
  z-index:99999;
  pointer-events:none;
  background:#121621dd;
  color:#e6e9f2;
  font-size:13px;
  line-height:1.4;
  border:1px solid #2c3347;
  border-radius:8px;
  padding:8px 10px;
  box-shadow:0 4px 12px rgba(0,0,0,.35);
  max-width:260px;
  backdrop-filter:blur(3px);
  transform:translate(-50%, -50%);
}
.map-tooltip strong {
  color:var(--accent);
  display:block;
  margin-bottom:2px;
}


.edit-controls {
  border-top:1px dashed var(--border);
  margin-top:10px;
  padding-top:10px;
}


/* =========================================================
   SIDEBARIN SCROLLAUSKORJAUS (HTML: header + .controls + footer)
   ========================================================= */
.sidebar {
  display:flex;
  flex-direction:column;
  height:100%;
  overflow:hidden; /* estää ulomman scrollauksen */
}

/* Varsinainen sisältö, jossa haitarit ja listat */
.sidebar .controls {
  flex:1;
  overflow-y:auto;
  overflow-x:hidden;
  padding:0;
  min-height:0; /* kriittinen fix flex-yhteensopivuuteen */
  scroll-behavior:smooth;
}

/* Scrollbar yhtenäiseksi */
.sidebar .controls::-webkit-scrollbar {
  width:8px;
}
.sidebar .controls::-webkit-scrollbar-thumb {
  background:#2c3347;
  border-radius:4px;
}
.sidebar .controls::-webkit-scrollbar-thumb:hover {
  background:#3b4259;
}

/* Footer pidetään näkyvissä alhaalla */
.footer {
  flex-shrink:0;
  border-top:1px solid var(--border);
  padding:10px 12px;
  background:#141822;
  position:sticky;
  bottom:0;
  z-index:3;
}





/* =========================================================
   11. A11Y JA RESPONSIVIISUUS
   ========================================================= */
@media (prefers-reduced-motion: reduce) {
  * { animation:none!important; transition:none!important; }
}

@media (max-width: 1040px) {
  .app { grid-template-columns: 1fr 340px; }
}
@media (max-width: 920px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position:absolute; right:0; top:0; bottom:0;
    width:min(92vw, 380px);
    border-left:1px solid var(--border);
    background:linear-gradient(180deg, rgba(21,25,36,.98), rgba(21,25,36,.96));
    box-shadow:-16px 0 24px rgba(0,0,0,.35);
  }
  .accordion-title { padding:12px 16px; }
  .accordion-content { padding:14px 16px; }
  .map-toggle {
    bottom:14px; left:14px;
    font-size:12px; padding:6px 10px;
  }
}
