/* =============================================================================
   b00ks — design system (basato su STR4TUM)
   ============================================================================= */

/* ---------- TOKENS ----------
   Palette: Morphous Lavender (override locale di STR4TUM per il progetto b00ks).
   Light: warm cream + lavender + verde muted. Dark: deep purple + green primary.
*/
:root {
  --bg-base:           oklch(0.977 0.010 87.5);   /* warm cream */
  --bg-panel:          oklch(0.937 0.021 304.0);  /* card lavender pale */
  --bg-panel-elevated: oklch(0.954 0.008 319.4);  /* secondary */
  --bg-input:          oklch(0.977 0.010 87.5);

  --border-subtle:     oklch(0.933 0.018 303.4 / 0.5);
  --border-default:    oklch(0.933 0.018 303.4);
  --border-strong:     oklch(0.330 0.056 296.1);  /* dark purple */

  --text-primary:      oklch(0.330 0.056 296.1);  /* dark purple */
  --text-secondary:    oklch(0.249 0.039 289.8);
  --text-muted:        oklch(0.481 0.114 301.8 / 0.75);
  --text-dim:          oklch(0.481 0.114 301.8 / 0.45);

  --accent-info:       oklch(0.650 0.054 143.2);  /* muted green (chart-2) */
  --accent-success:    oklch(0.650 0.054 143.2);  /* green */
  --accent-danger:     oklch(0.679 0.116 299.8);  /* lavender purple (theme destructive) */
  --accent-warning:    oklch(0.825 0.060 82.9);   /* chart-4 gold */
  --accent-header:     oklch(0.679 0.116 299.8);  /* lavender purple (primary, identità b00ks) */
  --accent-secondary:  oklch(0.481 0.114 301.8);  /* deeper purple (accent) */

  --accent-info-dim:      oklch(0.650 0.054 143.2 / 0.14);
  --accent-success-dim:   oklch(0.650 0.054 143.2 / 0.16);
  --accent-danger-dim:    oklch(0.679 0.116 299.8 / 0.14);
  --accent-warning-dim:   oklch(0.825 0.060 82.9 / 0.22);
  --accent-header-dim:    oklch(0.679 0.116 299.8 / 0.14);
  --accent-secondary-dim: oklch(0.481 0.114 301.8 / 0.20);

  --font-mono: 'JetBrains Mono', 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  --fs-xs:   0.6875rem;
  --fs-sm:   0.75rem;
  --fs-base: 0.875rem;
  --fs-md:   1rem;
  --fs-lg:   1.25rem;
  --fs-xl:   1.75rem;
  --fs-2xl:  2.25rem;

  --lh-tight: 1.2;
  --lh-snug:  1.4;
  --lh-normal:1.6;

  --fw-regular:  400;
  --fw-medium:   500;
  --fw-semibold: 600;
  --fw-bold:     700;

  --tracking-tight:  -0.01em;
  --tracking-wider:  0.1em;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10:2.5rem;
  --space-12:3rem;
  --space-16:4rem;

  --radius-sm: 0;
  --radius-md: 0;
  --radius-lg: 8px;
  --radius-xl: 16px;

  --ease-out:        cubic-bezier(0.22, 1, 0.36, 1);
  --duration-fast:   120ms;
  --duration-normal: 200ms;

  --container-max: 1400px;
  --z-toast: 3000;
}

:root[data-theme="dark"] {
  --bg-base:           oklch(0.330 0.056 296.1);  /* deep purple */
  --bg-panel:          oklch(0.278 0.046 292.3);  /* darker card */
  --bg-panel-elevated: oklch(0.417 0.032 294.6);  /* sidebar-accent */
  --bg-input:          oklch(0.330 0.056 296.1);
  --border-subtle:     oklch(1 0 0 / 0.10);
  --border-default:    oklch(1 0 0 / 0.18);
  --border-strong:     oklch(1 0 0 / 0.32);
  --text-primary:      oklch(0.977 0.010 87.5);   /* cream */
  --text-secondary:    oklch(0.977 0.010 87.5 / 0.85);
  --text-muted:        oklch(0.952 0.003 354.7 / 0.75);
  --text-dim:          oklch(0.952 0.003 354.7 / 0.40);
  --accent-info:       oklch(0.679 0.116 299.8);  /* lavender (chart-2 in dark) */
  --accent-success:    oklch(0.650 0.054 143.2);  /* green primary */
  --accent-danger:     oklch(0.679 0.116 299.8);  /* lavender */
  --accent-warning:    oklch(0.825 0.060 82.9);   /* gold */
  --accent-header:     oklch(0.650 0.054 143.2);  /* green primary (identità dark) */
  --accent-secondary:  oklch(0.481 0.114 301.8);
  --accent-info-dim:      oklch(0.679 0.116 299.8 / 0.18);
  --accent-success-dim:   oklch(0.650 0.054 143.2 / 0.20);
  --accent-danger-dim:    oklch(0.679 0.116 299.8 / 0.18);
  --accent-warning-dim:   oklch(0.825 0.060 82.9 / 0.22);
  --accent-header-dim:    oklch(0.650 0.054 143.2 / 0.18);
  --accent-secondary-dim: oklch(0.481 0.114 301.8 / 0.20);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-base:           oklch(0.330 0.056 296.1);
    --bg-panel:          oklch(0.278 0.046 292.3);
    --bg-panel-elevated: oklch(0.417 0.032 294.6);
    --bg-input:          oklch(0.330 0.056 296.1);
    --border-subtle:     oklch(1 0 0 / 0.10);
    --border-default:    oklch(1 0 0 / 0.18);
    --border-strong:     oklch(1 0 0 / 0.32);
    --text-primary:      oklch(0.977 0.010 87.5);
    --text-secondary:    oklch(0.977 0.010 87.5 / 0.85);
    --text-muted:        oklch(0.952 0.003 354.7 / 0.75);
    --text-dim:          oklch(0.952 0.003 354.7 / 0.40);
    --accent-info:       oklch(0.679 0.116 299.8);
    --accent-success:    oklch(0.650 0.054 143.2);
    --accent-danger:     oklch(0.679 0.116 299.8);
    --accent-warning:    oklch(0.825 0.060 82.9);
    --accent-header:     oklch(0.650 0.054 143.2);
    --accent-secondary:  oklch(0.481 0.114 301.8);
    --accent-info-dim:      oklch(0.679 0.116 299.8 / 0.18);
    --accent-success-dim:   oklch(0.650 0.054 143.2 / 0.20);
    --accent-danger-dim:    oklch(0.679 0.116 299.8 / 0.18);
    --accent-warning-dim:   oklch(0.825 0.060 82.9 / 0.22);
    --accent-header-dim:    oklch(0.650 0.054 143.2 / 0.18);
    --accent-secondary-dim: oklch(0.481 0.114 301.8 / 0.20);
  }
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

body {
  margin: 0;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  font-feature-settings: 'ss01', 'cv01';
}

h1, h2, h3, h4 {
  margin: 0;
  font-weight: var(--fw-semibold);
  line-height: var(--lh-tight);
  letter-spacing: var(--tracking-tight);
}

p { margin: 0; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }

/* ---------- PAGE ---------- */
.page {
  max-width: var(--container-max);
  margin: 0 auto;
  padding: var(--space-4);
  padding-bottom: var(--space-16);
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding-bottom: var(--space-3);
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.page-title {
  font-size: var(--fs-xl);
  font-weight: var(--fw-bold);
  color: var(--accent-header);
}

.page-subtitle {
  margin-top: var(--space-1);
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.toolbar {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}

/* ---------- PANEL ---------- */
.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.panel-title {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-secondary);
}

/* ---------- BUTTONS ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: var(--space-2) var(--space-4);
  background: var(--bg-panel-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  transition: background-color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out),
              opacity var(--duration-fast) var(--ease-out);
}

.btn:hover { border-color: var(--border-strong); }
.btn:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: 2px;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn[data-variant="primary"] {
  background: var(--text-primary);
  color: var(--bg-base);
  border-color: var(--text-primary);
}
.btn[data-variant="primary"]:hover { opacity: 0.85; }

.btn[data-variant="ghost"] {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}
.btn[data-variant="ghost"]:hover {
  background: var(--bg-panel-elevated);
  border-color: var(--border-subtle);
}

.btn[data-variant="danger"] {
  background: var(--accent-danger);
  color: #faf9f5;
  border-color: var(--accent-danger);
}
.btn[data-variant="danger"]:hover { opacity: 0.85; }

/* ---------- FORM COLLAPSE / EXPAND ---------- */
.form-panel { margin-bottom: var(--space-4); }

/* Quando collapsed: pannello rimpicciolito a 1 riga (header), content height 0 */
.form-panel[data-expanded="false"] { display: none; }
.form-panel[data-expanded="true"]  { display: block; }

/* Animazione slide-down del contenuto del form */
.form-content {
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows var(--duration-normal) var(--ease-out);
}
.form-content > * { overflow: hidden; min-height: 0; }
.form-panel[data-expanded="false"] .form-content {
  grid-template-rows: 0fr;
}

/* X di chiusura compatto (override del .btn default) */
#form-close {
  min-height: 32px;
  min-width: 32px;
  padding: 0 var(--space-2);
  font-size: var(--fs-lg);
  line-height: 1;
}

/* ---------- FORM ---------- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin-bottom: var(--space-3);
}
.field:last-child { margin-bottom: 0; }

.field label {
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  font-weight: var(--fw-medium);
}

.field input[type="text"],
.field input[type="number"],
.field input[type="search"],
.field textarea,
.field select {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: var(--fs-base);
  min-height: 40px;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: 2px solid var(--accent-info);
  outline-offset: -1px;
  border-color: var(--accent-info);
}

.field textarea {
  resize: vertical;
  min-height: 80px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.req { color: var(--accent-danger); }

.form-actions {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

.form-actions .btn[data-variant="primary"] { flex: 1; }

.form-error {
  margin-top: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--accent-danger-dim);
  color: var(--accent-danger);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
}

/* ---------- RATING STARS ---------- */
.rating-input {
  display: flex;
  gap: var(--space-1);
  align-items: center;
}

.star {
  background: transparent;
  border: none;
  padding: var(--space-1);
  font-size: var(--fs-xl);
  color: var(--text-dim);
  min-width: 36px;
  min-height: 36px;
  transition: color var(--duration-fast) var(--ease-out),
              transform var(--duration-fast) var(--ease-out);
}

.star:hover { transform: scale(1.15); }
.star:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: 2px;
}

.star.active,
.star.preview { color: var(--accent-warning); }
.star.preview { opacity: 0.6; }

.star-clear {
  background: transparent;
  border: none;
  padding: var(--space-1);
  margin-left: var(--space-2);
  color: var(--text-muted);
  font-size: var(--fs-lg);
  min-width: 32px;
  min-height: 32px;
}
.star-clear:hover { color: var(--accent-danger); }

/* ---------- TABS ---------- */
.nav-tabs {
  display: flex;
  gap: 0;
  margin-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-default);
  overflow-x: auto;
}

.nav-tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: var(--space-3) var(--space-4);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  margin-bottom: -1px;
  white-space: nowrap;
  transition: color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}

.nav-tab:hover { color: var(--text-primary); }
.nav-tab[aria-selected="true"] {
  color: var(--text-primary);
  border-bottom-color: var(--accent-header);
}
.nav-tab:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: -2px;
}

.tab-content {
  animation: fade-in var(--duration-normal) var(--ease-out);
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- DASHBOARD SECTION HEADINGS ---------- */
.dashboard-section { margin-bottom: var(--space-6); }
.dashboard-section-title {
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--border-subtle);
}

/* ---------- YEAR FILTER PILLS (in cima alla dashboard) ---------- */
.year-filter {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
}
.year-filter-pill {
  padding: var(--space-1) var(--space-3);
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-secondary);
  cursor: pointer;
  min-height: 32px;
  transition: background-color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.year-filter-pill:hover { border-color: var(--border-strong); }
.year-filter-pill[aria-pressed="true"] {
  background: var(--accent-header);
  color: #faf9f5;
  border-color: var(--accent-header);
}
.year-filter-pill:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: 2px;
}

/* ---------- KPI ---------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-3);
}

.kpi {
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.kpi-label {
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  font-weight: var(--fw-medium);
}

.kpi-value {
  font-size: var(--fs-xl);
  font-weight: var(--fw-bold);
  color: var(--text-primary);
  line-height: var(--lh-tight);
}

.kpi-value[data-tone="info"]    { color: var(--accent-info); }
.kpi-value[data-tone="success"] { color: var(--accent-success); }
.kpi-value[data-tone="danger"]  { color: var(--accent-danger); }
.kpi-value[data-tone="warning"] { color: var(--accent-warning); }
.kpi-value[data-tone="header"]  { color: var(--accent-header); }

.kpi-value-text {
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  line-height: var(--lh-snug);
  word-break: break-word;
}

.kpi-stars {
  display: inline-flex;
  gap: 2px;
  color: var(--accent-warning);
  font-size: var(--fs-md);
  letter-spacing: 0;
}
.kpi-stars .empty { color: var(--text-dim); }

.kpi-delta {
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
}
.kpi-delta[data-tone="up"]   { color: var(--accent-success); }
.kpi-delta[data-tone="down"] { color: var(--accent-danger); }

/* ---------- CHARTS ---------- */
.charts-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.chart-panel { margin-bottom: 0; }

.chart-wide { grid-column: 1 / -1; }

.chart-container {
  position: relative;
  width: 100%;
  height: 260px;
}
.chart-container.tall { height: 320px; }

/* Wrapper scrollabile orizzontale (mobile-friendly per molti dati) */
.chart-scroll {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  /* Touch-action: panoramica solo orizzontale, lascia la scroll verticale
     della pagina al body. Importante su iOS Safari per non bloccare lo scroll. */
  touch-action: pan-x;
  overscroll-behavior-x: contain;
}
.chart-scroll > .chart-container {
  height: 340px;
  /* min-width settato da JS in base al numero di mesi */
}
/* Il canvas dentro lo scroll DEVE permettere il pan orizzontale al touch.
   Senza questo Chart.js cattura touchmove per i tooltip e blocca lo scroll
   del wrapper (regressione classica su Chrome/Brave Android). */
.chart-scroll canvas {
  touch-action: pan-x;
  display: block;
}

/* Wrapper composito: asse Y sticky a sinistra + scroll del canvas */
.chart-scroll-wrap {
  position: relative;
  padding-left: 38px;  /* spazio per la y-axis HTML sticky */
  /* Hint scroll: gradient fade a destra quando scrollable */
}
.chart-scroll-wrap[data-scrollable="true"]::after {
  content: 'scorri →';
  position: absolute;
  bottom: 0;
  right: 8px;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  background: linear-gradient(90deg, transparent, var(--bg-panel) 30%);
  padding: 2px 4px 2px 28px;
  pointer-events: none;
  animation: hint-pulse 2s ease-out 1;
}
@keyframes hint-pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}
.chart-y-axis {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 38px;
  z-index: 2;
  pointer-events: none;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-subtle);
}
.chart-y-axis .tick {
  position: absolute;
  right: 6px;
  transform: translateY(-50%);
  font-size: var(--fs-xs);
  color: var(--text-muted);
  font-family: var(--font-mono);
  white-space: nowrap;
}

.chart-insight {
  margin-top: var(--space-3);
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
}

.chart-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  padding: var(--space-4);
  color: var(--text-muted);
  font-size: var(--fs-sm);
  text-align: center;
  font-style: italic;
}

/* ---------- HEATMAP CALENDARIO ---------- */
.heatmap-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.heatmap {
  display: grid;
  grid-template-columns: 48px repeat(12, minmax(28px, 1fr));
  gap: 2px;
  font-family: var(--font-mono);
  min-width: 420px;
}

.heatmap-header-cell {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  text-align: center;
  padding: var(--space-1) 0;
}

.heatmap-row-label {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  font-weight: var(--fw-medium);
  align-self: center;
  padding-right: var(--space-2);
}

.heatmap-cell {
  aspect-ratio: 1 / 1;
  min-height: 28px;
  border-radius: 2px;
  cursor: pointer;
  border: none;
  padding: 0;
  transition: outline var(--duration-fast) var(--ease-out),
              transform var(--duration-fast) var(--ease-out);
}
.heatmap-cell:hover { outline: 2px solid var(--text-primary); outline-offset: -1px; }
.heatmap-cell:focus-visible { outline: 2px solid var(--accent-info); outline-offset: 1px; }
.heatmap-cell[data-level="0"] { background: var(--bg-panel-elevated); }
/* livelli 1-5 vengono settati inline via JS (beige → fig) */

/* ---------- DRILLDOWN PANEL ---------- */
.drilldown-panel {
  margin-top: var(--space-6);
  padding: var(--space-4);
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  animation: fade-in var(--duration-normal) var(--ease-out);
}

.drilldown-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
}
.drilldown-title {
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  line-height: var(--lh-snug);
}
.drilldown-count {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  margin-top: var(--space-1);
}
.drilldown-close {
  min-height: 32px;
  min-width: 32px;
  padding: 0 var(--space-2);
  font-size: var(--fs-lg);
  line-height: 1;
  flex-shrink: 0;
}

.drilldown-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}
.drilldown-table th {
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-default);
}
.drilldown-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-primary);
  vertical-align: top;
}
.drilldown-table tbody tr {
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-out);
}
.drilldown-table tbody tr:hover { background: var(--bg-panel-elevated); }
.drilldown-table .col-stars {
  color: var(--accent-warning);
  white-space: nowrap;
  letter-spacing: 0;
}
.drilldown-table .col-stars .empty { color: var(--text-dim); }
.drilldown-table .placeholder { color: var(--text-dim); font-style: italic; }

/* ---------- LIST CONTROLS ---------- */
.list-controls {
  display: flex;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
  flex-wrap: wrap;
}

.list-controls input[type="search"] {
  flex: 1;
  min-width: 180px;
  padding: var(--space-2) var(--space-3);
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: var(--fs-base);
  min-height: 40px;
}

.list-controls input[type="search"]:focus,
.list-controls select:focus {
  outline: 2px solid var(--accent-info);
  outline-offset: -1px;
  border-color: var(--accent-info);
}

.list-controls select {
  padding: var(--space-2) var(--space-3);
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: var(--fs-base);
  min-height: 40px;
}

/* ---------- FILTER PILLS (genere) ---------- */
.filter-pills {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-2);
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.filter-pills:empty { display: none; }

.filter-pill {
  flex-shrink: 0;
  white-space: nowrap;
  padding: var(--space-1) var(--space-3);
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-secondary);
  cursor: pointer;
  min-height: 28px;
  transition: background-color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.filter-pill:hover { border-color: var(--border-strong); }
.filter-pill[aria-pressed="true"] {
  background: var(--text-primary);
  color: var(--bg-base);
  border-color: var(--text-primary);
}
.filter-pill .count {
  margin-left: var(--space-1);
  opacity: 0.6;
}
.filter-pill:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: 2px;
}

.list-meta {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  margin-bottom: var(--space-3);
}

/* ---------- BOOK LIST ---------- */
.book-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.book-card {
  position: relative;
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-4);
  transition: border-color var(--duration-fast) var(--ease-out);
}
.book-card:hover { border-color: var(--border-strong); }

/* Header card: titolo a sinistra, badge + menu a destra */
.book-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.book-card-title-block { min-width: 0; flex: 1; }

/* ---------- COPERTINA card list ---------- */
.book-card[data-has-cover="true"] .book-card-head {
  display: grid;
  grid-template-columns: 60px 1fr auto;
  gap: var(--space-3);
  align-items: start;
}
.book-cover {
  width: 60px;
  height: 88px;
  object-fit: cover;
  background: var(--bg-panel-elevated);
  border-radius: var(--radius-sm);
  display: block;
  flex-shrink: 0;
}
.book-cover[data-error="true"] {
  visibility: hidden;
}

/* ---------- COPERTINA form preview ---------- */
.cover-input-row {
  display: flex;
  gap: var(--space-2);
  align-items: stretch;
}
.cover-input-row input { flex: 1; }
.cover-preview {
  width: 48px;
  height: 64px;
  object-fit: cover;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  background: var(--bg-panel-elevated);
}

.book-title {
  font-size: var(--fs-md);
  font-weight: var(--fw-bold);
  color: var(--text-primary);
  line-height: var(--lh-snug);
  margin-bottom: var(--space-1);
  /* mai troncato — wrap su parole, anche su mobile */
  overflow-wrap: anywhere;
  word-break: normal;
  white-space: normal;
}

.book-author {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}
.book-author.placeholder {
  color: var(--text-dim);
  font-style: italic;
}

/* Badge anno (in alto a destra) */
.book-year-badge {
  flex-shrink: 0;
  padding: 2px var(--space-2);
  background: var(--bg-panel-elevated);
  color: var(--text-secondary);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  font-weight: var(--fw-medium);
  border-radius: var(--radius-sm);
  white-space: nowrap;
}

/* Riga inferiore: genere pill + stars */
.book-card-info {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  align-items: center;
  margin-top: var(--space-3);
}
.book-card-info:empty { display: none; }

.book-stars {
  display: inline-flex;
  gap: 1px;
  color: var(--accent-warning);
  font-size: var(--fs-base);
  letter-spacing: 0;
}
.book-stars .empty { color: var(--text-dim); }
.book-stars.placeholder {
  color: var(--text-dim);
  font-style: italic;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
}

.book-genre {
  display: inline-block;
  padding: 1px var(--space-2);
  background: var(--accent-info-dim);
  color: var(--accent-info);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  font-weight: var(--fw-medium);
  border-radius: 999px;
}

.book-note-toggle {
  background: transparent;
  border: none;
  padding: 0;
  color: var(--accent-info);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  margin-top: var(--space-3);
  text-align: left;
}
.book-note-toggle:hover { text-decoration: underline; }

/* ---------- SOMMARIO (card) ---------- */
.book-summary {
  margin-top: var(--space-3);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  line-height: var(--lh-snug);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-summary.expanded {
  display: block;
  -webkit-line-clamp: unset;
  overflow: visible;
}
.book-summary-toggle {
  background: transparent;
  border: none;
  padding: 0;
  margin-top: var(--space-1);
  color: var(--accent-info);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  cursor: pointer;
}
.book-summary-toggle:hover { text-decoration: underline; }

/* ---------- TRAMA (card) — descrizione editoriale, auto-popolata ---------- */
.book-plot {
  margin-top: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--bg-panel-elevated);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  line-height: var(--lh-snug);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-plot.expanded {
  display: block;
  -webkit-line-clamp: unset;
  overflow: visible;
}
.book-plot-toggle {
  background: transparent;
  border: none;
  padding: 0;
  margin-top: var(--space-1);
  color: var(--accent-info);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  cursor: pointer;
}
.book-plot-toggle:hover { text-decoration: underline; }

.book-note {
  margin-top: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--bg-panel-elevated);
  border-left: 2px solid var(--accent-info);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  white-space: pre-wrap;
}

/* ---------- KEBAB MENU ---------- */
.book-menu { position: relative; }
.book-menu summary { list-style: none; cursor: pointer; }
.book-menu summary::-webkit-details-marker { display: none; }
.book-menu summary::marker { content: ''; }

.book-menu-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: var(--fs-lg);
  line-height: 1;
  user-select: none;
  transition: background-color var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.book-menu-trigger:hover {
  background: var(--bg-panel-elevated);
  color: var(--text-primary);
}
.book-menu-trigger:focus-visible {
  outline: 2px solid var(--accent-info);
  outline-offset: 2px;
}

.book-menu-popup {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 20;
  background: var(--bg-panel);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: var(--space-1);
  min-width: 140px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.book-menu-popup button {
  background: transparent;
  border: none;
  padding: var(--space-2) var(--space-3);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  color: var(--text-primary);
  text-align: left;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-out);
}
.book-menu-popup button:hover { background: var(--bg-panel-elevated); }
.book-menu-popup button[data-tone="danger"] { color: var(--accent-danger); }
.book-menu-popup button[data-tone="danger"]:hover { background: var(--accent-danger-dim); }

.empty-state {
  text-align: center;
  padding: var(--space-8) var(--space-4);
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

/* ---------- TOAST ---------- */
.toast {
  position: fixed;
  bottom: var(--space-4);
  left: var(--space-4);
  right: var(--space-4);
  z-index: var(--z-toast);
  padding: var(--space-3) var(--space-4);
  background: var(--text-primary);
  color: var(--bg-base);
  border-radius: var(--radius-lg);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  text-align: center;
  animation: toast-in var(--duration-normal) var(--ease-out);
  max-width: 480px;
  margin: 0 auto;
}

.toast[data-tone="danger"]  { background: var(--accent-danger);  color: #faf9f5; }
.toast[data-tone="success"] { background: var(--accent-success); color: #faf9f5; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- DIALOG ---------- */
.dialog {
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xl);
  background: var(--bg-panel);
  color: var(--text-primary);
  padding: var(--space-5);
  max-width: 400px;
  width: calc(100% - var(--space-8));
  font-family: var(--font-mono);
}

.dialog::backdrop { background: rgba(20, 20, 19, 0.5); }

.dialog-title {
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  margin-bottom: var(--space-2);
}

.dialog-body {
  font-size: var(--fs-base);
  color: var(--text-secondary);
  margin-bottom: var(--space-4);
  word-break: break-word;
}

.dialog-actions {
  display: flex;
  gap: var(--space-2);
  justify-content: flex-end;
}

/* ---------- THEME TOGGLE ---------- */
.theme-icon { font-size: var(--fs-md); }

/* ---------- RESPONSIVE ---------- */
@media (min-width: 768px) {
  .page { padding: var(--space-6); }
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .charts-grid { grid-template-columns: 1fr 1fr; }
  .chart-container { height: 300px; }
  .chart-scroll > .chart-container { height: 300px; }
}

@media (min-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(6, 1fr); }
  .heatmap { grid-template-columns: 56px repeat(12, 1fr); }
}

/* ---------- THEME TRANSITION ---------- */
html, body, .page, .panel, .btn, .nav-tab, .book-card, .kpi, .dialog {
  transition: background-color var(--duration-normal) var(--ease-out),
              border-color var(--duration-normal) var(--ease-out),
              color var(--duration-normal) var(--ease-out);
}
