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

:root {
  --blue-900: #0d1b2a;
  --blue-800: #1a2d45;
  --blue-700: #1e3a5f;
  --blue-600: #235789;
  --blue-500: #2d74c4;
  --blue-400: #4a90d9;
  --blue-300: #7ab3e8;
  --accent:   #00b4d8;
  --accent-h: #0096b4;
  --white:    #f0f4f8;
  --white-dim:#c8d6e5;
  --text:     #e8f1f8;
  --text-dim: #8ba5c0;
  --panel-bg: #0f2035;
  --panel-border: rgba(255,255,255,0.08);
  --shadow:   0 4px 24px rgba(0,0,0,0.45);
  --radius:   10px;
  --topbar-h: 62px;
  --transition: 0.22s cubic-bezier(0.4,0,0.2,1);
}

@media (max-width: 600px) {
  :root { --topbar-h: 150px; }
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--blue-900);
  color: var(--text);
}

/* ─── Top bar ─────────────────────────────────────────────────── */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  z-index: 1000;
  background: rgba(13, 27, 42, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.4);
}

@media (max-width: 600px) {
  .topbar {
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0;
    padding: 0;
    height: var(--topbar-h);
  }
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  margin-right: 4px;
}

@media (max-width: 600px) {
  .topbar-brand {
    height: 52px;
    padding: 0 14px;
    flex: 0 0 auto;
    margin-right: 0;
    border-bottom: 1px solid var(--panel-border);
    width: 100%;
  }
}

.brand-icon {
  width: 32px; height: 32px;
  color: var(--accent);
}

.brand-name {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--white);
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.72rem;
  color: var(--text-dim);
  display: none;
}
@media (min-width: 700px) { .brand-sub { display: block; } }

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

@media (max-width: 600px) {
  .topbar-controls {
    width: 100%;
    flex: 0 0 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    height: auto;
    padding: 0;
    gap: 0;
  }

  /* Invisible line-break between laji (order 2) and year filters (order 20) */
  .topbar-controls::after {
    content: '';
    flex: 0 0 100%;
    order: 10;
    height: 0;
    border-bottom: 1px solid var(--panel-border);
  }

  /* ── Row 1: search + laji ── */
  .search-wrap {
    order: 1;
    flex: 1 1 0;
    min-width: 0;
    max-width: none;
    height: 48px;
    padding: 0 10px;
    display: flex;
    align-items: center;
  }

  .topbar-controls > .filter-group:nth-child(2) { /* laji */
    order: 2;
    flex: 0 0 120px;
    height: 48px;
    display: flex;
    align-items: center;
    padding: 0 6px 0 8px;
    gap: 2px;
    border-left: 1px solid var(--panel-border);
  }

  /* ── Row 2: vuodesta + vuoteen + reset ── */
  .topbar-controls > .filter-group:nth-child(3),
  .topbar-controls > .filter-group:nth-child(4) {
    order: 20;
    flex: 1 1 0;
    height: 48px;
    display: flex;
    align-items: center;
    padding: 0 6px 0 10px;
    gap: 2px;
    border-right: 1px solid var(--panel-border);
  }

  .reset-btn {
    order: 20;
    flex: 0 0 44px;
    width: 44px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0;
    border: none;
    border-radius: 0;
    border-left: 1px solid var(--panel-border);
  }
}

/* Search */
.search-wrap {
  position: relative;
  flex: 1;
  max-width: 280px;
  min-width: 120px;
}


.search-icon {
  position: absolute;
  left: 10px; top: 50%;
  transform: translateY(-50%);
  width: 15px; height: 15px;
  color: var(--text-dim);
  pointer-events: none;
}

.search-input {
  width: 100%;
  height: 36px;
  padding: 0 34px 0 34px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.875rem;
  outline: none;
  transition: border-color var(--transition), background var(--transition);
}
.search-clear {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.1);
  border: none;
  border-radius: 50%;
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.search-clear:hover { background: rgba(255,80,80,0.2); color: #ff6b6b; }
.search-clear.hidden { display: none; }

.search-input::placeholder { color: var(--text-dim); }
.search-input:focus {
  background: rgba(255,255,255,0.11);
  border-color: var(--accent);
}

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  background: var(--blue-800);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  max-height: 280px;
  overflow-y: auto;
  z-index: 2000;
  box-shadow: var(--shadow);
}

@media (max-width: 600px) {
  .search-results {
    position: fixed;
    top: var(--topbar-h);
    left: 8px;
    right: 8px;
    max-height: 55vh;
  }
}
.search-results.hidden { display: none; }
.search-result-item {
  padding: 9px 14px;
  cursor: pointer;
  font-size: 0.85rem;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  transition: background var(--transition);
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: rgba(0,180,216,0.12); }
.search-result-item strong { color: var(--accent); }
.search-result-item small { color: var(--text-dim); display: block; margin-top: 2px; }

/* Filters */
.filter-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.filter-label {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  line-height: 1;
}
.filter-select {
  height: 34px;
  padding: 0 28px 0 10px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.82rem;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' stroke='%238ba5c0' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  transition: border-color var(--transition);
  min-width: 130px;
  flex-shrink: 0;
}
.filter-select:focus { border-color: var(--accent); }
.filter-select option { background: var(--blue-800); color: var(--text); }

@media (max-width: 600px) {
  .filter-select { min-width: 0; font-size: 0.78rem; height: 32px; width: 100%; }
  .filter-label  { font-size: 0.56rem; }
}

.filter-select-sm {
  min-width: 110px;
  font-size: 0.78rem;
  height: 30px;
}

.reset-btn {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  color: var(--text-dim);
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}
.reset-btn:hover { background: rgba(255,80,80,0.15); color: #ff6b6b; border-color: rgba(255,80,80,0.3); }

.topbar-stats {
  font-size: 0.75rem;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
  display: none;
}
@media (min-width: 900px) { .topbar-stats { display: block; } }

/* ─── Map ──────────────────────────────────────────────────────── */
#map {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0; bottom: 0;
  z-index: 1;
}

/* ─── Side panel ───────────────────────────────────────────────── */
.panel {
  position: fixed;
  top: var(--topbar-h);
  right: 0;
  width: 380px;
  height: calc(100vh - var(--topbar-h));
  background: var(--panel-bg);
  border-left: 1px solid var(--panel-border);
  z-index: 500;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
  overflow: hidden;
}
.panel-open { transform: translateX(0); }
.panel-handle { display: none; }

@media (max-width: 600px) {
  .panel {
    width: 100%;
    top: auto;
    bottom: 0;
    height: 88vh;
    transform: translateY(100%);
    border-left: none;
    border-top: 1px solid var(--panel-border);
    border-radius: 16px 16px 0 0;
  }
  .panel-open { transform: translateY(0); }

  /* Drag handle — now a real element for tap */
  .panel-handle {
    display: block;
    width: 100%;
    height: 28px;
    flex-shrink: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .panel-handle::after {
    content: '';
    width: 36px; height: 4px;
    background: rgba(255,255,255,0.25);
    border-radius: 2px;
  }
  .panel-handle:active::after { background: rgba(255,255,255,0.5); }

  /* Minimized state — just shows title bar */
  .panel-minimized {
    height: 72px !important;
    overflow: hidden;
  }

  /* Compact header */
  .panel-header {
    padding: 8px 44px 8px 14px;
  }
  .panel-title { font-size: 1rem; margin-bottom: 4px; }
  .panel-meta  { gap: 4px; }
  .meta-tag    { font-size: 0.67rem; padding: 1px 6px; }

  /* Summary cards: 2×2 grid */
  .panel-summary {
    padding: 8px 12px;
    gap: 8px;
    flex-wrap: wrap;
  }
  .summary-card {
    flex: 1 1 calc(50% - 4px);
    min-width: calc(50% - 4px);
    padding: 8px 10px;
  }
  .summary-card-value {
    font-size: 1rem;
    word-break: break-word;
    overflow-wrap: anywhere;
  }
  .summary-card-label { font-size: 0.6rem; margin-top: 2px; }

  /* Compact filter row */
  .panel-filter-row { padding: 8px 12px; gap: 6px; }
  .filter-select-sm  { min-width: 0; flex: 1; font-size: 0.75rem; }

  /* Panel close button */
  .panel-close { top: 10px; right: 10px; }
}

.panel-close {
  position: absolute;
  top: 14px; right: 14px;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  color: var(--text-dim);
  cursor: pointer;
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
  z-index: 10;
}
.panel-close:hover { background: rgba(255,255,255,0.12); color: var(--white); }

.panel-header {
  padding: 20px 48px 0 20px;
  border-bottom: 1px solid var(--panel-border);
  padding-bottom: 14px;
  flex-shrink: 0;
}

.panel-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--white);
  line-height: 1.3;
  margin-bottom: 6px;
}

.panel-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.meta-tag {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 100px;
  background: rgba(0,180,216,0.12);
  color: var(--accent);
  border: 1px solid rgba(0,180,216,0.2);
}

.panel-summary {
  padding: 14px 20px;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  gap: 12px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.summary-card {
  flex: 1;
  min-width: 0;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 8px;
  text-align: center;
}
.summary-card-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.summary-card-label {
  font-size: 0.68rem;
  color: var(--text-dim);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.panel-section {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}
.panel-section::-webkit-scrollbar { width: 4px; }
.panel-section::-webkit-scrollbar-track { background: transparent; }
.panel-section::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 4px; }

.panel-section-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  padding: 12px 20px 8px;
  position: sticky;
  top: 0;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-filter-row {
  display: flex;
  gap: 8px;
  padding: 10px 20px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
}

.panel-records {
  padding: 8px 0;
}

.record-group {
  margin-bottom: 4px;
}

.record-group-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px 6px;
  cursor: pointer;
  user-select: none;
}
.record-group-header:hover { background: rgba(255,255,255,0.03); }

.species-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.record-group-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--white);
  flex: 1;
}

.record-group-count {
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 600;
}

.record-group-toggle {
  font-size: 0.65rem;
  color: var(--text-dim);
  transition: transform var(--transition);
}
.record-group-open .record-group-toggle { transform: rotate(180deg); }

.record-list { display: none; }
.record-group-open .record-list { display: block; }

.record-item {
  padding: 8px 20px 8px 38px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
}
.record-item:last-child { border-bottom: none; }

.record-date {
  font-size: 0.78rem;
  color: var(--text-dim);
  grid-column: 1 / -1;
}

.record-place {
  font-size: 0.82rem;
  color: var(--text);
}

.record-count {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  text-align: right;
}

.record-details {
  font-size: 0.72rem;
  color: var(--text-dim);
  grid-column: 1;
}

.record-size {
  font-size: 0.72rem;
  color: var(--text-dim);
  text-align: right;
}

/* ─── Legend ───────────────────────────────────────────────────── */
.legend {
  position: fixed;
  bottom: 24px;
  left: 16px;
  background: rgba(13,27,42,0.92);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  z-index: 500;
  min-width: 140px;
}

@media (max-width: 600px) {
  .legend {
    bottom: 12px;
    left: 10px;
    padding: 8px 10px;
    min-width: 0;
  }
  .legend-title { font-size: 0.6rem; margin-bottom: 5px; }
  .legend-items { gap: 3px; }
  .legend-item  { font-size: 0.67rem; gap: 5px; }
}

.legend-title {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.legend-items { display: flex; flex-direction: column; gap: 5px; }

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.legend-circle {
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.7;
  flex-shrink: 0;
}

/* ─── Geocode notice ───────────────────────────────────────────── */
.geocode-notice {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: rgba(13,27,42,0.9);
  border: 1px solid var(--panel-border);
  border-radius: 100px;
  padding: 7px 14px 7px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-dim);
  z-index: 600;
  transition: opacity var(--transition);
}
.geocode-notice.hidden { display: none; }
.legend.hidden { display: none; }

@media (max-width: 600px) {
  .geocode-notice {
    bottom: 12px;
    right: 10px;
    font-size: 0.65rem;
    padding: 5px 10px 5px 8px;
    max-width: calc(100vw - 160px);
  }
}

.geocode-notice-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

/* ─── Custom Leaflet markers ───────────────────────────────────── */
.custom-marker {
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.25);
  opacity: 0.85;
  transition: transform 0.15s, opacity 0.15s;
  cursor: pointer;
}
.custom-marker:hover { opacity: 1; transform: scale(1.15); }
.custom-marker-selected {
  border-color: #fff !important;
  opacity: 1 !important;
  transform: scale(1.2);
  box-shadow: 0 0 0 3px rgba(0,180,216,0.4);
}

/* Cluster */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background-clip: padding-box;
  border-radius: 50%;
}
.marker-cluster-small { background: rgba(0,96,150,0.6); }
.marker-cluster-medium { background: rgba(0,140,180,0.6); }
.marker-cluster-large { background: rgba(0,180,216,0.6); }
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  width: 30px; height: 30px;
  margin: 5px;
  background: rgba(0,180,216,0.75);
  text-align: center;
  border-radius: 15px;
  color: #fff;
  font-weight: 700;
  font-size: 0.75rem;
  line-height: 30px;
}

/* ─── Scrollbar global ─────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.18); border-radius: 4px; }

/* ─── Loading spinner ──────────────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--blue-900);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  transition: opacity 0.4s;
}
.loading-overlay.fade-out { opacity: 0; pointer-events: none; }

.loading-spinner {
  width: 48px; height: 48px;
  border: 3px solid rgba(255,255,255,0.1);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  font-size: 0.9rem;
  color: var(--text-dim);
}
