/* ═══════════════════════════════════════════════════════════════
   SWD Theme — Storytelling with Data Design System
   CAE Dashboard V4
   Princípios: clareza, hierarquia, cor como sinal
   ═══════════════════════════════════════════════════════════════ */

/* ── Fonts ─────────────────────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');

/* Noto Color Emoji — flags for country pickers and legends */
@font-face {
  font-family: 'Noto Color Emoji';
  font-style: normal;
  font-weight: 400;
  src: url('../fonts/NotoColorEmoji.woff2') format('woff2');
  unicode-range: U+1f1e6-1f1ff;
}

/* ── Tokens ─────────────────────────────────────────────────────── */
:root {
  /* Brand colours */
  --c-pt:       #CC0000;   /* Portugal — vermelho */
  --c-eu:       #4A90D9;   /* EU27 — azul discreto */
  --c-other:    #999999;   /* outros países */
  --c-muted:    #666666;   /* texto secundário */
  --c-base100:  #E5E5E5;   /* linha de índice 100 */

  /* Sentiment */
  --c-positive: #2E7D32;
  --c-negative: #C62828;
  --c-neutral:  #666666;

  /* UI */
  --c-bg:       #F6F4F1;   /* fundo quente — off-white */
  --c-surface:  #FFFFFF;   /* cartões brancos sobre fundo quente */
  --c-surface-2: #F2F0ED; /* ligeiramente mais escuro que fundo para theads */
  --c-border:   #E2DED9;
  --c-text:     #1A1A1A;
  --c-text-sub: #5A5550;
  --c-nav-bg:   #FFFFFF;   /* nav clara */
  --c-nav-text: #5A5550;
  --c-nav-active: #1A1A1A;
  --c-accent:   #CC0000;

  /* Spacing */
  --sp-xs:  4px;
  --sp-sm:  8px;
  --sp-md:  16px;
  --sp-lg:  24px;
  --sp-xl:  40px;
  --sp-2xl: 64px;

  /* Typography scale */
  --fs-xs:   15px;
  --fs-sm:   17px;
  --fs-base: 19px;
  --fs-md:   22px;
  --fs-lg:   26px;
  --fs-xl:   32px;
  --fs-2xl:  48px;
  --fs-3xl:  64px;

  /* Layout */
  --content-max: 1280px;
  --nav-height: 88px;
  --radius: 8px;
  --shadow-sm: 0 1px 4px rgba(0,0,0,0.06);
  --shadow-md: 0 2px 12px rgba(0,0,0,0.09);

  /* Callout / Banner tokens */
  --c-callout-warning-bg:     #FFF8E1;
  --c-callout-warning-border: #FFD54F;
  --c-callout-warning-text:   #5D4037;
  --c-callout-info-bg:        #E8F4FD;
  --c-callout-info-border:    #90CAF9;
  --c-callout-info-text:      #1565C0;
  --c-callout-amber-bg:       #FFFBF0;
  --c-callout-amber-border:   #F0E0A0;
  --c-callout-amber-text:     #7A5500;

  /* Badge tokens */
  --c-badge-positive-bg:      #E8F5E9;
  --c-badge-negative-bg:      #FFEBEE;
  --c-badge-neutral-bg:       #F5F5F5;

  /* Skeleton / loading tokens */
  --c-skeleton-base:          #f0f0f0;
  --c-skeleton-shine:         #e8e8e8;
  --c-chart-loading-bg:       #fafafa;

  /* Search bar tokens */
  --c-kbd-bg:                 #FFFFFF;
  --c-kbd-border:             #D5D0CB;
  --c-search-focus-bg:        #FFFFFF;

  /* Haiku card tokens */
  --c-haiku-bg-start:         #FFF9F5;
  --c-haiku-bg-end:           #FFF5F5;
  --c-haiku-border:           #F5C0C0;
  --c-haiku-badge-bg:         #FFE8E8;
  --c-haiku-badge-border:     #FFCDD2;
}

/* ── Dark Mode ──────────────────────────────────────────────────── */
[data-theme="dark"] {
  --c-pt:       #e74c3c;
  --c-eu:       #5ba3e8;
  --c-other:    #888888;
  --c-muted:    #a8a8c0;
  --c-base100:  #444455;

  --c-positive: #4CAF50;
  --c-negative: #EF5350;
  --c-neutral:  #888888;

  --c-bg:       #0f0f13;
  --c-surface:  #1e1e2a;
  --c-surface-2: #1a1a24;
  --c-border:   #2a2a3a;
  --c-text:     #e8e8f0;
  --c-text-sub: #a0a0b8;
  --c-nav-bg:   #13131a;
  --c-nav-text: #a0a0b8;
  --c-nav-active: #e8e8f0;
  --c-accent:   #e74c3c;

  --shadow-sm: 0 1px 4px rgba(0,0,0,0.3);
  --shadow-md: 0 2px 12px rgba(0,0,0,0.4);

  /* Callout / Banner tokens — dark overrides */
  --c-callout-warning-bg:     #2a2510;
  --c-callout-warning-border: #6b5a00;
  --c-callout-warning-text:   #e8c96a;
  --c-callout-info-bg:        #101e2a;
  --c-callout-info-border:    #1a4a7a;
  --c-callout-info-text:      #7ab8f0;
  --c-callout-amber-bg:       #1e1a0a;
  --c-callout-amber-border:   #5a4400;
  --c-callout-amber-text:     #c8a840;

  /* Badge tokens — dark overrides */
  --c-badge-positive-bg:      rgba(76, 175, 80, 0.15);
  --c-badge-negative-bg:      rgba(239, 83, 80, 0.15);
  --c-badge-neutral-bg:       rgba(136, 136, 136, 0.15);

  /* Skeleton / loading tokens — dark overrides */
  --c-skeleton-base:          #252535;
  --c-skeleton-shine:         #2e2e42;
  --c-chart-loading-bg:       #1e1e2a;

  /* Search bar tokens — dark overrides */
  --c-kbd-bg:                 #252535;
  --c-kbd-border:             #3a3a4a;
  --c-search-focus-bg:        #1e1e2a;

  /* Haiku card tokens — dark overrides */
  --c-haiku-bg-start:         #1e1822;
  --c-haiku-bg-end:           #1e1a1a;
  --c-haiku-border:           #4a2a2a;
  --c-haiku-badge-bg:         rgba(231, 76, 60, 0.12);
  --c-haiku-badge-border:     #5a2020;
}

/* Dark mode: suppress body bg-pattern and render via pseudo-element at low opacity */
[data-theme="dark"] body {
  background-image: none;
}
[data-theme="dark"] body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: url("../images/prumo/bg-pattern.svg");
  background-repeat: repeat;
  background-size: 60px 60px;
  opacity: 0.03;
  pointer-events: none;
  z-index: -1;
}

/* Theme toggle button */
.nav-controls {
  position: fixed;
  bottom: 20px;
  right: 16px;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--c-surface, #fff);
  border: 1px solid var(--c-border, #e5e5e5);
  border-radius: 24px;
  padding: 4px 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
}

.theme-toggle-btn {
  background: none;
  border: none;
  border-radius: 6px;
  padding: 4px 6px;
  cursor: pointer;
  font-size: 18px;
  color: var(--c-nav-text);
  transition: background 0.15s;
  display: flex;
  align-items: center;
  line-height: 1;
  flex-shrink: 0;
  white-space: nowrap;
  opacity: 0.7;
}
.theme-toggle-btn:hover {
  background: var(--c-surface-2);
  opacity: 1;
}

/* Smooth theme transitions */
body,
.nav,
.kpi-card,
.chart-card,
.search-bar,
.search-bar-inner,
.search-bar .search-results,
#ai-panel,
#painel-ia-panel {
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', 'Noto Color Emoji', system-ui, -apple-system, sans-serif;
  background-color: var(--c-bg);
  background-image: url("../images/prumo/bg-pattern.svg");
  background-repeat: repeat;
  background-size: 60px 60px;
  color: var(--c-text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── Navigation ─────────────────────────────────────────────────── */
.nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--c-nav-bg);
  border-bottom: 1px solid var(--c-border);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  display: flex;
  align-items: stretch;
  gap: 0;
  height: var(--nav-height);
  padding: 8px 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }

.nav-brand {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 0 var(--sp-xl) 0 var(--sp-lg);
  font-family: 'Space Grotesk', 'Inter', system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.45rem;
  color: var(--c-text);
  white-space: nowrap;
  letter-spacing: -0.3px;
}

.nav-brand-link {
  display: flex;
  align-items: stretch;
  text-decoration: none;
  color: inherit;
}

.nav-brand span {
  color: var(--c-pt);   /* #CC0000 — WCAG AA (5.5:1) sobre nav branca ✅ */
}

.nav-tab {
  display: flex;
  align-items: center;
  padding: 8px var(--sp-lg);
  min-height: 44px;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 11.5px;
  font-weight: 400;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--c-nav-text);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
  transition: color 0.18s, border-color 0.18s, opacity 0.18s, background 0.18s;
  opacity: 0.7;
}

.nav-tab:hover {
  color: var(--c-text);
  opacity: 1;
  background: rgba(0, 0, 0, 0.04);
}

.nav-tab.active {
  color: var(--c-nav-active);
  border-bottom-color: var(--c-pt);
  opacity: 1;
  font-weight: 600;
}

.nav-tab-icon {
  margin-right: 6px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  opacity: 0.75;
  transition: opacity 0.18s;
}
.nav-tab:hover .nav-tab-icon,
.nav-tab.active .nav-tab-icon {
  opacity: 1;
}

.nav-tab-icon-svg img {
  vertical-align: middle;
  margin-bottom: 1px;
  opacity: 0.85;
}

/* ── Sections ───────────────────────────────────────────────────── */
.section {
  display: none;
  padding: var(--sp-xl) var(--sp-xl);
  max-width: var(--content-max);
  margin: 0 auto;
  min-height: calc(100vh - var(--nav-height));
}

.section.active {
  display: block;
}

/* ── Section Header ─────────────────────────────────────────────── */
.section-header {
  margin-top: var(--sp-md);
  margin-bottom: var(--sp-md);
}

.section-eyebrow {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--c-muted);
  margin-bottom: var(--sp-xs);
}

.section-title {
  font-size: var(--fs-lg);
  font-weight: 700;
  color: var(--c-text);
  line-height: 1.3;
  margin-bottom: var(--sp-sm);
}

.section-subtitle {
  font-size: var(--fs-base);
  color: var(--c-text-sub);
  font-weight: 400;
}

/* ── KPI Cards ──────────────────────────────────────────────────── */
/* Bug 4: use CSS Grid for proper 3-column layout */
.kpi-grid {
  display: grid;
  /* FIX 4: auto-fill with minmax — wraps to 2 cols ~900px, 1 col ~600px automatically */
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  margin-bottom: var(--sp-2xl);
}

.kpi-card {
  flex: 1 1 280px;
  background: var(--c-surface);
  border-radius: var(--radius);
  padding: var(--sp-lg) var(--sp-xl);
  display: flex;
  flex-direction: column;
  gap: var(--sp-sm);
  position: relative;
  border: 1px solid var(--c-border);
  transition: box-shadow 0.18s ease, transform 0.15s ease;
}
.kpi-card:hover {
  box-shadow: 0 2px 10px rgba(0,0,0,0.07);
  transform: translateY(-1px);
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--c-border);
}

.kpi-card.positive::before { background: var(--c-positive); }
.kpi-card.negative::before { background: var(--c-negative); }
.kpi-card.neutral::before  { background: var(--c-neutral); }

.kpi-label {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--c-muted);
}

.kpi-value-row {
  display: flex;
  align-items: baseline;
  gap: var(--sp-sm);
}

.kpi-value {
  font-size: var(--fs-2xl);
  font-weight: 800;
  line-height: 1;
  color: var(--c-text);
  letter-spacing: -1px;
}

.kpi-unit {
  font-size: var(--fs-md);
  font-weight: 400;
  color: var(--c-text-sub);
}

.kpi-trend-row {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
}

.kpi-yoy {
  font-size: var(--fs-md);
  font-weight: 600;
}
.kpi-yoy.positive { color: var(--c-positive); }
.kpi-yoy.negative { color: var(--c-negative); }
.kpi-yoy.neutral  { color: var(--c-neutral); }

.kpi-arrow {
  font-size: var(--fs-lg);
}
/* Bug 8: arrow colour follows sentiment (not raw YoY sign) */
.kpi-arrow.positive { color: var(--c-positive); }
.kpi-arrow.negative { color: var(--c-negative); }
.kpi-arrow.neutral  { color: var(--c-neutral); }

.kpi-context {
  font-size: var(--fs-sm);
  color: var(--c-text-sub);
  line-height: 1.4;
  margin-top: var(--sp-xs);
}

/* ── Chart Cards ────────────────────────────────────────────────── */
.chart-card {
  background: var(--c-bg);
  border-radius: var(--radius);
  border: 1px solid var(--c-border);
  padding: var(--sp-lg);
  margin-bottom: var(--sp-lg);
}

.chart-title {
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--c-text);
  margin-bottom: var(--sp-xs);
  line-height: 1.3;
}

.chart-subtitle {
  font-size: var(--fs-sm);
  color: var(--c-text-sub);
  margin-bottom: var(--sp-md);
}

.chart-container {
  width: 100%;
  height: 260px;
}

.chart-container.tall {
  height: 360px;
}

.chart-container.medium {
  height: 300px;
}

/* ── Small Multiples Grid ───────────────────────────────────────── */
.small-multiples {
  display: grid;
  gap: var(--sp-md);
}

.small-multiples.cols-2 { grid-template-columns: repeat(2, 1fr); }
.small-multiples.cols-3 { grid-template-columns: repeat(3, 1fr); }

/* ── Selectors ──────────────────────────────────────────────────── */
.controls-row {
  display: flex;
  align-items: center;
  gap: var(--sp-md);
  margin-bottom: var(--sp-lg);
  flex-wrap: wrap;
}

.control-label {
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--c-text-sub);
}

.swd-select {
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--c-text);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23666' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

.swd-select:focus {
  outline: 2px solid var(--c-pt);
  outline-offset: 1px;
}

.swd-input {
  font-family: inherit;
  font-size: var(--fs-sm);
  color: var(--c-text);
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: 6px;
  padding: 8px 12px;
  transition: border-color 0.15s;
}
.swd-input:focus {
  outline: none;
  border-color: var(--c-pt);
  box-shadow: 0 0 0 2px rgba(204, 0, 0, 0.1);
}
.swd-input::placeholder {
  color: var(--c-muted);
}

/* ── Tables ─────────────────────────────────────────────────────── */
.swd-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}

.swd-table th {
  font-weight: 600;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--c-muted);
  text-align: left;
  padding: var(--sp-sm) var(--sp-md);
  border-bottom: 2px solid var(--c-border);
}

.swd-table td {
  padding: var(--sp-sm) var(--sp-md);
  border-bottom: 1px solid var(--c-border);
  color: var(--c-text);
  vertical-align: middle;
}

.swd-table tr:last-child td { border-bottom: none; }

.swd-table tr:hover td { background: var(--c-surface); }

/* ── Value colours ──────────────────────────────────────────────── */
.val-positive { color: var(--c-positive); font-weight: 600; }
.val-negative { color: var(--c-negative); font-weight: 600; }
.val-neutral  { color: var(--c-neutral); }
.val-nd       { color: var(--c-muted); font-style: italic; }

/* ── Badge ──────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 2px 8px;
  border-radius: 20px;
}
.badge-positive { background: var(--c-badge-positive-bg); color: var(--c-positive); }
.badge-negative { background: var(--c-badge-negative-bg); color: var(--c-negative); }
.badge-neutral  { background: var(--c-badge-neutral-bg);  color: var(--c-neutral); }

/* ── Loading / Error states ─────────────────────────────────────── */
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: var(--c-muted);
  font-size: var(--fs-sm);
  gap: var(--sp-sm);
}

.loading-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--c-border);
  border-top-color: var(--c-pt);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* Skeleton shimmer */
.skeleton {
  background: linear-gradient(90deg, var(--c-skeleton-base) 25%, var(--c-skeleton-shine) 50%, var(--c-skeleton-base) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  border-radius: 4px;
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.error-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 120px;
  color: var(--c-negative);
  font-size: var(--fs-sm);
  gap: var(--sp-sm);
}

/* ── Divider ────────────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--c-border);
  margin: var(--sp-xl) 0;
}

/* ── Sub-section ────────────────────────────────────────────────── */
.subsection {
  margin-bottom: var(--sp-2xl);
}

.subsection-title {
  font-size: var(--fs-md);
  font-weight: 700;
  color: var(--c-text);
  margin-bottom: var(--sp-xs);
  padding-bottom: var(--sp-sm);
  border-bottom: 2px solid var(--c-border);
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
}

/* ── Two-col layout ─────────────────────────────────────────────── */
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-lg);
}

.three-col {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--sp-lg);
}

/* ── Responsive ─────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .section { padding: var(--sp-lg) var(--sp-md); }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col, .three-col { grid-template-columns: 1fr; }
  .small-multiples.cols-2, .small-multiples.cols-3 { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .kpi-value { font-size: var(--fs-xl); }
  .chart-container { height: 220px; }
  /* Análise: chart ocupa toda a largura disponível */
  .section { padding: var(--sp-md) var(--sp-sm); }
  #exp-chart, .chart-wrap, [id="exp-chart"] { overflow: hidden !important; }

  /* Mobile nav: gerido por nav-brand.css */
  .nav-tab {
    flex: 1;
    justify-content: center;
    padding: 0 2px;
    font-size: 10px;
    letter-spacing: 0;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 0;
  }
  .nav-tab-icon {
    margin-right: 0;
    font-size: 18px;
  }
  .nav-tab-icon-svg img {
    width: 18px;
    height: 18px;
  }
  .nav-tab-label {
    display: block;
    font-size: 8px;
    letter-spacing: 0.3px;
    line-height: 1;
    opacity: 0.75;
  }
  .nav-tab.active .nav-tab-label {
    opacity: 1;
  }
}

/* ── Footnote ───────────────────────────────────────────────────── */
.footnote {
  font-size: var(--fs-xs);
  color: var(--c-muted);
  margin-top: var(--sp-md);
  line-height: 1.6;
}

/* ── Inline legend (label directo) ─────────────────────────────── */
.inline-legend {
  display: flex;
  align-items: center;
  gap: var(--sp-lg);
  margin-bottom: var(--sp-md);
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-sm);
  color: var(--c-text-sub);
}

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

.legend-line {
  width: 20px;
  height: 2px;
  flex-shrink: 0;
}

/* ── Download button ─────────────────────────────────────────────── */
.btn-download {
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--c-text);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: 6px;
  padding: 6px 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
}

.btn-download:hover {
  background: var(--c-border);
  border-color: #CCCCCC;
}

.btn-primary {
  background: #B30000;
  color: #FFFFFF;
  border-color: #B30000;
}

.btn-primary:hover {
  background: #990000;
  border-color: #990000;
  color: #FFFFFF;
}

/* ── Site Footer ─────────────────────────────────────────────────── */
.site-footer {
  text-align: center;
  padding: 1.5rem 1rem;
  font-size: var(--fs-sm);
  color: var(--c-muted);
  border-top: 1px solid var(--c-border);
  margin-top: auto;
}
.site-footer a {
  color: var(--c-pt);
  text-decoration: none;
}
.site-footer a:hover {
  text-decoration: underline;
}

/* ── Footer dev credit ───────────────────────────────────────────── */
.footer-dev {
  font-size: var(--fs-xs);
  color: var(--c-muted);
}
.footer-dev strong {
  color: var(--c-pt);
  font-weight: 600;
}
