* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: #0d1b26;
  color: #e8eef2;
}

#app {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

#map-wrap {
  position: relative;
  flex: 1 1 auto;
  overflow: hidden;
  background: #0d1b26;
  cursor: grab;
  touch-action: none;
}
#map-wrap.dragging { cursor: grabbing; }

#map-stage {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

#map-stage img {
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

#map-stage svg {
  position: absolute;
  top: 0;
  left: 0;
}

.mark {
  cursor: pointer;
}
.mark .hit {
  fill: transparent;
}
.mark:hover .body, .mark.active .body {
  filter: drop-shadow(0 0 4px #fff);
}

/* Sidebar - algab kohe ülaservast, ainult kategooriapuu, ei nihku kunagi */
#sidebar {
  width: 320px;
  flex: 0 0 320px;
  background: #10222f;
  border-left: 1px solid #24404f;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#legend {
  padding: 12px 16px 16px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

#sidebar-footer {
  flex: 0 0 auto;
  border-top: 1px solid #24404f;
  padding: 8px 10px;
}
#legal-link {
  width: 100%;
  background: none;
  border: none;
  color: #7fa0b3;
  font-size: 11.5px;
  text-align: left;
  padding: 6px 6px;
  border-radius: 4px;
  cursor: pointer;
}
#legal-link:hover { background: #17303f; color: #cfe3ee; }

/* Kasutustingimuste popup */
#legal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: #000a;
  z-index: 60;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
#legal-backdrop.open { display: flex; }
#legal-modal {
  position: relative;
  background: #10222f;
  border: 1px solid #2c4a5a;
  border-radius: 10px;
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 24px 24px 20px;
  box-shadow: 0 12px 32px #000c;
}
#legal-modal h2 {
  font-size: 16px;
  margin: 0 0 14px;
  padding-right: 24px;
}
#legal-modal p {
  font-size: 13px;
  line-height: 1.55;
  color: #cfe3ee;
  margin: 0 0 12px;
}
#legal-modal ul {
  margin: 0 0 12px;
  padding-left: 20px;
  font-size: 13px;
  line-height: 1.55;
  color: #cfe3ee;
}
#legal-modal a { color: #9fd6ff; }
#legal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid #2c4a5a;
  background: #17303f;
  color: #e8eef2;
  font-size: 14px;
  cursor: pointer;
}
#legal-close:hover { background: #1f3d4d; }
#legend h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #7fa0b3;
  margin: 0;
  flex: 1 1 auto;
}

.legend-group { margin-top: 10px; }
.legend-group:first-child { margin-top: 0; }
.legend-group-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 4px;
  border-radius: 4px;
  cursor: pointer;
}
.legend-group-header:hover { background: #17303f; }
.legend-group.all-off .legend-group-header h2 { opacity: 0.4; }
.legend-group-header .expand-arrow { transition: transform 0.15s ease; }
.legend-group.expanded .legend-group-header .expand-arrow { transform: rotate(90deg); }
.legend-group-body { display: none; }
.legend-group.expanded .legend-group-body { display: block; }

.cat-group { margin-bottom: 1px; }
.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  border-radius: 4px;
  font-size: 12.5px;
  cursor: pointer;
}
.legend-row:hover { background: #17303f; }
.legend-row.off { opacity: 0.35; }
.legend-row svg { flex: 0 0 auto; overflow: visible; }
.legend-label { flex: 1 1 auto; }
.legend-count {
  color: #6c8695;
  font-size: 11px;
}
.expand-arrow {
  flex: 0 0 auto;
  width: 10px;
  font-size: 9px;
  color: #6c8695;
  transition: transform 0.15s ease;
}
.cat-group.expanded .expand-arrow { transform: rotate(90deg); }
.leading-line-icon {
  flex: 0 0 auto;
  width: 26px;
  text-align: center;
  color: #ff9c1a;
  font-size: 15px;
}
.legend-eye {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: #9fd6ff;
  cursor: pointer;
  padding: 3px 5px;
  border-radius: 3px;
  opacity: 0.9;
}
.legend-eye:hover { opacity: 1; background: #1f3d4d; }
.legend-eye.eye-off { color: #56707e; opacity: 0.7; }
.legend-eye.eye-off:hover { opacity: 1; }
.group-eye svg { width: 17px; height: 12px; }

.mark-list {
  display: none;
  flex-direction: column;
  padding: 2px 0 4px 44px;
}
.cat-group.expanded .mark-list { display: flex; }
.mark-row {
  padding: 3px 8px;
  font-size: 12px;
  color: #c3d6e0;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mark-row:hover { background: #17303f; color: #fff; }
.mark-row.active {
  background: #1d4a63;
  color: #fff;
  font-weight: 600;
}

#controls {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 6px;
  z-index: 5;
}
#controls button {
  background: #10222fcc;
  border: 1px solid #2c4a5a;
  color: #e8eef2;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
}
#controls button:hover { background: #1a3546; }
#controls button.active {
  background: #22c55e33;
  border-color: #22c55e;
}

#tooltip {
  position: absolute;
  display: none;
  pointer-events: none;
  max-width: 240px;
  background: #10222fee;
  border: 1px solid #2c4a5a;
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 12px;
  line-height: 1.45;
  box-shadow: 0 6px 16px #000a;
  z-index: 15;
}
#tooltip .t-name {
  font-weight: 600;
  color: #fff;
  font-size: 12.5px;
}
#tooltip .t-type { color: #9fd6ff; }
#tooltip .t-extra { color: #a9c2cf; margin-top: 2px; }

/* Kinnistatud (klõpsatud) popup - jääb märgi juurde püsima, on
   kerimisega ja suletav, erinevalt kursorit järgivast hõljutusversioonist. */
#tooltip.pinned {
  pointer-events: auto;
  max-width: 290px;
  max-height: 65vh;
  overflow-y: auto;
  padding-right: 26px;
}
#tooltip .t-close {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border: none;
  background: none;
  color: #8fa9b8;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  border-radius: 4px;
}
#tooltip .t-close:hover { background: #1f3d4d; color: #fff; }
#tooltip dl {
  margin: 8px 0 0;
  display: grid;
  grid-template-columns: 82px 1fr;
  gap: 3px 8px;
  font-size: 11.5px;
}
#tooltip dt { color: #7fa0b3; }
#tooltip dd { margin: 0; }

#attribution {
  position: absolute;
  bottom: 6px;
  left: 12px;
  font-size: 10.5px;
  color: #cfe3ee;
  background: #0d1b26aa;
  padding: 2px 6px;
  border-radius: 4px;
  z-index: 5;
}

#toggle-sidebar { display: none; }
#sidebar-close { display: none; }
#sidebar-backdrop { display: none; }

/* --- Responsive: kitsad ekraanid (telefonid/tahvlid) --- */
@media (max-width: 768px) {
  #app { position: relative; }

  #sidebar {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: min(340px, 88vw);
    flex: 0 0 auto;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 30;
    box-shadow: -6px 0 18px #000a;
  }
  #sidebar.open { transform: translateX(0); }

  #sidebar-close {
    display: block;
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 2;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    border: 1px solid #2c4a5a;
    background: #10222fcc;
    color: #e8eef2;
    font-size: 16px;
    cursor: pointer;
  }
  #sidebar-close:hover { background: #1a3546; }
  #legend { padding-top: 44px; }

  /* Tagaplaan, millele puudutades sahtel sulgub - standardne
     mobiili-sahtli muster, et sulgemiseks ei peaks nimekirjast
     valima ega ☰-nuppu (mis jääb sahtli alla peidus) otsima. */
  #sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: #000a;
    z-index: 25;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }
  #sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

  /* ☰-nupp jääb alati klõpsatavaks ka avatud sahtli peal (varem
     kattis kõrgema z-indexiga sahtel selle ära). */
  #toggle-sidebar { display: block; z-index: 40; }

  #controls {
    top: env(safe-area-inset-top, 12px);
    left: env(safe-area-inset-left, 12px);
    z-index: 40;
  }
  #controls button {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  #attribution { max-width: 60vw; }
}
