/* =============================================
   VAISTO – Alueanalyysi  |  style.css
   Layout: Vasen 28% | Keski 47% | Oikea 25%
   ============================================= */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --border: #e2e8f0;
  --bg: #f1f5f9;
  --bg-white: #ffffff;
  --bg-subtle: #f8fafc;
  --text: #1e293b;
  --text-muted: #64748b;
  --text-faint: #94a3b8;
  --blue: #1d4ed8;
  --blue-light: #eff6ff;
  --blue-border: #bfdbfe;
  --green: #16a34a;
  --red: #dc2626;
  --topbar-h: 52px;
}

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

/* ---- TOPBAR ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  height: var(--topbar-h);
  background: #1e293b;
  color: #f8fafc;
  flex-shrink: 0;
  z-index: 100;
}

.brand { font-weight: 700; font-size: 15px; white-space: nowrap; letter-spacing: 0.02em; }

.searchWrap { display: flex; gap: 6px; }

.searchWrap input {
  width: 180px;
  padding: 5px 10px;
  border: 1px solid #475569;
  border-radius: 6px;
  background: #334155;
  color: #f8fafc;
  font-size: 13px;
}

.searchWrap input::placeholder { color: #94a3b8; }

.searchWrap button {
  padding: 5px 14px;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}

.searchWrap button:hover { background: #2563eb; }

.topbarInfo {
  font-size: 13px;
  color: #a6b1c0;
  flex: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.statusWrap { font-size: 12px; color: #94a3b8; white-space: nowrap; }

.topbarActions { display: flex; gap: 6px; flex-shrink: 0; }

.topbarBtn {
  padding: 5px 12px;
  background: #334155;
  color: #f8fafc;
  border: 1px solid #475569;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.15s;
}

.topbarBtn:hover { background: #475569; }

/* ---- MAIN LAYOUT ---- */
.layout {
  display: flex;
  flex-direction: row;
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
}

/* ---- VASEN PANEELI ---- */
.leftPanel {
  width: 28%;
  min-width: 450px;
  max-width: 450px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
  flex-shrink: 0;
  background: var(--bg);
}

/* Kartta */
.mapSection {
  flex: 0 0 45%;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

#map { width: 100%; height: 100%; }

/* Kaksi palstaa kartan alla */
.leftStats {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 6px;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  background: var(--bg);
}

.leftCol {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ---- KESKI: 3 palstaa ---- */
.statsPanel {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg);
  padding: 6px;
}

.threeColGrid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  align-items: start;
}

.statsCol {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ---- OIKEA: AI-ANALYYSIT ---- */
.aiPanel {
  width: 35%;
  min-width: 340px;
  max-width: 340px;
  background: var(--bg-white);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.aiPanelHeader {
  padding: 10px 14px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  flex-shrink: 0;
}

.aiPanelHeader h1 { font-size: 16px; font-weight: 700; color: var(--text); }
#panelSubtitle { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.aiPanelBody {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

/* ---- TILASTORYHMÄ ---- */
.statGroup {
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: visible;
  background: var(--bg-white);
}

.statGroup.coming-soon { opacity: 0.55; }

.statGroupTitle {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 8px;
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  font-weight: 700;
  color: #334155;
  cursor: pointer;
  user-select: none;
  border-radius: 6px 6px 0 0;
}

.statGroupTitle:hover { background: #f1f5f9; }
.statGroupIcon { font-size: 13px; }

.statGroupSource {
  margin-left: auto;
  font-size: 10px;
  font-weight: 400;
  color: var(--text-faint);
  white-space: nowrap;
}

.comingBadge {
  background: #fef3c7;
  color: #92400e;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 10px;
}

/* ---- ACCORDION ---- */
.statGroup.collapsed .statTable,
.statGroup.collapsed .statGroupSubtitle { display: none; }

/* ---- TILASTORIVI ---- */
.statTable { padding: 2px 0; }

.statGroupSubtitle {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  padding: 5px 8px 2px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-top: 1px solid var(--border);
  background: #fafbfc;
}

.statRow {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-bottom: 1px solid #f8fafc;
  transition: background 0.1s;
}

.statRow:last-child { border-bottom: none; }
.statRow:hover { background: #f8fafc; }
.statRow.placeholder { opacity: 0.45; }

.statLabel { font-size: 11px; color: #475569; line-height: 1.3; }

.statValue {
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
  text-align: right;
  min-width: 55px;
  white-space: nowrap;
}

.statTrend { font-size: 12px; width: 16px; text-align: center; }
.trend-up   { color: var(--green); }
.trend-down { color: var(--red); }
.trend-flat { color: var(--text-faint); }

/* ---- VÄLILEHDET ---- */
.tabBar {
  display: flex;
  gap: 2px;
  padding: 6px 6px 0;
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.tabBtn {
  padding: 5px 8px;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}

.tabBtn:hover { background: var(--bg-white); color: var(--text); }

.tabBtn.active {
  background: var(--bg-white);
  color: var(--blue);
  border-color: var(--border);
  font-weight: 700;
}

/* ---- ANALYYSITEKSTI ---- */
.tabContent {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  min-height: 0;
}

.tabPanel { display: none; }
.tabPanel.active { display: block; }

.analysisPlaceholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  color: var(--text-faint);
  font-size: 12px;
  gap: 8px;
  padding: 20px;
  text-align: center;
}

.analysisPlaceholderIcon { font-size: 28px; }

.analysisBody {
  font-size: 12px;
  line-height: 1.75;
  color: #334155;
}

.analysisBody p { margin-bottom: 10px; }
.analysisBody p:last-child { margin-bottom: 0; }

.analysisActions {
  display: flex;
  gap: 6px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.btnCopy {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-subtle);
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s;
}

.btnCopy:hover { background: var(--border); color: var(--text); }
.btnCopy.copied { background: #dcfce7; color: #166534; border-color: #bbf7d0; }

.btnUrl {
  padding: 5px 12px;
  border: 1px solid var(--blue-border);
  border-radius: 6px;
  background: var(--blue-light);
  color: var(--blue);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.btnUrl:hover { background: #dbeafe; }

/* ---- SPINNER ---- */
#loadingSpinner {
  display: none;
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255,255,255,0.78);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.spinnerBox {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  background: white;
  padding: 24px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid #e2e8f0;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

.spinnerText { font-size: 13px; color: #475569; font-weight: 500; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- VIERITYSPALKKI ---- */
.leftStats::-webkit-scrollbar,
.statsPanel::-webkit-scrollbar,
.tabContent::-webkit-scrollbar { width: 4px; }
.leftStats::-webkit-scrollbar-track,
.statsPanel::-webkit-scrollbar-track,
.tabContent::-webkit-scrollbar-track { background: transparent; }
.leftStats::-webkit-scrollbar-thumb,
.statsPanel::-webkit-scrollbar-thumb,
.tabContent::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

/* ---- PDF / TULOSTUS ---- */
@media print {
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
  body { background: white; font-size: 10px; }

  .topbar, .mapSection, .tabBar,
  .analysisActions, #loadingSpinner { display: none !important; }

  .layout { display: block !important; height: auto !important; overflow: visible !important; }

  .leftPanel {
    width: 100% !important;
    max-width: none !important;
    overflow: visible !important;
  }

  .leftStats {
    grid-template-columns: 1fr 1fr !important;
    overflow: visible !important;
  }

  .statsPanel {
    width: 100% !important;
    overflow: visible !important;
    padding: 0 !important;
    page-break-before: always;
  }

  .threeColGrid {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  .aiPanel {
    width: 100% !important;
    max-width: none !important;
    border: none !important;
    overflow: visible !important;
    page-break-before: always;
  }

  .tabPanel {
    display: block !important;
    page-break-inside: avoid;
    margin-bottom: 12px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 10px;
  }

  .tabPanel::before {
    content: attr(data-title);
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 6px;
    padding-bottom: 4px;
    border-bottom: 1px solid #e2e8f0;
  }

  .statGroup { page-break-inside: avoid; }
  .statRow { padding: 2px 6px !important; }
}