:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface2: #f0f2f5;
  --border: #dde3eb;
  --text: #1a2332;
  --muted: #5c6b7f;
  --accent: #2563eb;
  --promoter: #16a34a;
  --passive: #ca8a04;
  --detractor: #dc2626;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.08);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

a {
  color: var(--accent);
}

/* Header */
.header {
  background: linear-gradient(135deg, #ffffff 0%, #eef2f7 100%);
  border-bottom: 1px solid var(--border);
  padding: 1.25rem 1.5rem 1rem;
}

.header h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
  font-weight: 700;
}

.header .subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0;
}

/* Filters bar */
.filters-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  align-items: flex-end;
  box-shadow: var(--shadow);
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.filter-group label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.filter-group input,
.filter-group select {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  background: var(--surface);
  color: var(--text);
}

.filter-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-left: auto;
}

.btn {
  padding: 0.45rem 0.9rem;
  border: none;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
}

.btn:hover {
  filter: brightness(1.05);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}

/* Main sections */
main {
  padding: 0 1.5rem 2rem;
}

section {
  margin-top: 1.75rem;
}

section h2 {
  font-size: 1.15rem;
  margin: 0 0 0.5rem;
}

.section-note {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 1rem;
}

/* KPI grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
}

.kpi-card .label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.kpi-card .value {
  font-size: 1.35rem;
  font-weight: 700;
}

.kpi-card .sub {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.kpi-card.positive .value {
  color: var(--promoter);
}

.kpi-card.negative .value {
  color: var(--detractor);
}

.kpi-card.neutral .value {
  color: var(--passive);
}

/* Charts */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.chart-card.full-width {
  grid-column: 1 / -1;
}

.chart-card h3 {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
}

.chart-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0 0 0.5rem;
}

.chart-wrap {
  position: relative;
  height: 280px;
}

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

.chart-wrap.short {
  height: 220px;
}

/* Accordion */
.accordion-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.accordion-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface2);
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
  text-align: left;
  color: var(--text);
}

.accordion-header-main {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}

.accordion-header-code {
  flex-shrink: 0;
  padding-top: 0.1rem;
}

.accordion-header-title {
  font-weight: 500;
  line-height: 1.35;
}

.accordion-header-count {
  flex-shrink: 0;
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  padding-top: 0.15rem;
}

.accordion-body {
  display: none;
  padding: 0 1rem 1rem;
}

.accordion-item.open .accordion-body {
  display: block;
}

.freq-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  margin-top: 0.75rem;
}

.freq-table th,
.freq-table td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  text-align: left;
}

.freq-table th {
  background: var(--surface2);
}

/* Comments */
.comments-toolbar input {
  width: 100%;
  max-width: 400px;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.word-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.word-tag {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  font-size: 0.8rem;
}

.word-tag strong {
  margin-right: 0.25rem;
  color: var(--accent);
}

.comments-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.comments-table th,
.comments-table td {
  border: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
  text-align: left;
  vertical-align: top;
}

.comments-table th {
  background: var(--surface2);
  position: sticky;
  top: 0;
}

/* Loading / error */
.loading,
.error-box {
  padding: 2rem 1.5rem;
  text-align: center;
}

.error-box {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  margin: 1rem 1.5rem;
  color: #991b1b;
}

/* Quality panel */
.quality-panel .stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 2rem;
  margin-bottom: 0.5rem;
}

.quality-panel .stat {
  font-size: 0.85rem;
}

/* Question code legend (P1, P2, …) — floating (screen only) */
.question-legend--float {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  top: auto;
  z-index: 90;
  max-width: min(300px, calc(100vw - 1.5rem));
  display: flex;
  flex-direction: column-reverse;
  align-items: stretch;
  gap: 0.35rem;
}

.question-legend-toggle {
  display: block;
  width: 100%;
  padding: 0.35rem 0.55rem;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: center;
}

.question-legend-toggle:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.question-legend-panel {
  display: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: min(45vh, 360px);
  overflow: auto;
}

.question-legend-panel.open {
  display: block;
}

.question-legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem 0.35rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.question-legend-title {
  margin: 0;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.question-legend-close {
  flex-shrink: 0;
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}

.question-legend-close:hover {
  background: var(--surface2);
  color: var(--text);
}

.question-legend-list {
  margin: 0;
  padding: 0.35rem 0.5rem 0.45rem;
  list-style: none;
}

.question-legend-item {
  display: grid;
  grid-template-columns: 1.75rem 3.25rem 1fr;
  gap: 0.2rem 0.35rem;
  align-items: start;
  padding: 0.3rem 0;
  border-top: 1px solid var(--border);
  font-size: 0.68rem;
  line-height: 1.3;
}

.question-legend-item:first-child {
  border-top: none;
}

.question-legend-code {
  font-weight: 700;
  color: var(--accent);
}

.question-legend-type {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  white-space: nowrap;
}

.question-legend-text {
  grid-column: 1 / -1;
  color: var(--text);
}

@media (min-width: 641px) {
  .question-legend-item {
    grid-template-columns: 1.75rem 3.25rem 1fr;
  }

  .question-legend-text {
    grid-column: 3;
  }
}

/* Inline legend for print / PDF (hidden on screen) */
.question-legend-print {
  display: none;
  margin: 0 0 1rem;
  padding: 0.65rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.75rem;
}

.question-legend-print-heading {
  margin: 0 0 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.question-legend-list--print .question-legend-item {
  font-size: 0.72rem;
  padding: 0.35rem 0;
}

.accordion-header-code strong {
  color: var(--accent);
}

/* Footer */
.footer {
  text-align: center;
  padding: 1.5rem;
  color: var(--muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

@media (max-width: 640px) {
  .filters-bar {
    position: static;
    top: auto;
    flex-direction: column;
    align-items: stretch;
  }

  .filter-actions {
    margin-left: 0;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .charts-grid {
    grid-template-columns: 1fr;
  }

  .question-legend--float {
    bottom: 0.75rem;
    right: 0.75rem;
    max-width: calc(100vw - 1.5rem);
  }
}
