/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ===== LIGHT THEME ===== */
:root, [data-theme="light"] {
  --bg: #f5f3ef;
  --bg2: #ffffff;
  --bg3: #f0ede8;
  --border: rgba(0,0,0,0.08);
  --border2: rgba(0,0,0,0.14);
  --text: #1a1916;
  --text2: #5a5754;
  --text3: #9a9794;
  --accent: #c42b21;
  --accent2: #e63329;
  --green: #1a7a4a;
  --orange: #c47a10;
  --blue: #1a5fa0;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

/* ===== DARK THEME ===== */
[data-theme="dark"] {
  --bg: #0e0e0f;
  --bg2: #161618;
  --bg3: #1e1e21;
  --border: rgba(255,255,255,0.07);
  --border2: rgba(255,255,255,0.12);
  --text: #f0ede8;
  --text2: #8a8880;
  --text3: #5a5855;
  --accent: #e63329;
  --accent2: #ff5a52;
  --green: #22c97a;
  --orange: #f5a623;
  --blue: #4a9eff;
  --shadow: none;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'DM Sans', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  overflow: hidden;
  transition: background 0.25s, color 0.25s;
}

/* ===== LAYOUT ===== */
body {
  display: grid;
  grid-template-columns: 220px 1fr 280px;
  grid-template-rows: 100vh;
  grid-template-areas: "sidebar main ai";
}

/* ===== SIDEBAR ===== */
.sidebar {
  grid-area: sidebar;
  background: var(--bg2);
  border-right: 0.5px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0;
  overflow-y: auto;
  transition: background 0.25s;
}

.logo {
  font-family: 'DM Serif Display', serif;
  font-size: 1.4rem;
  padding: 0 1.25rem 1.5rem;
  border-bottom: 0.5px solid var(--border);
  letter-spacing: -0.02em;
  display: flex;
  align-items: baseline;
  gap: 0;
}
/* Logo: "ei" normale, "o" rossa → spelling visivo: e-io-pago */
.logo-ei { color: var(--text); order: 1; }
.logo-io { color: var(--accent); order: 0; }
.logo-pago { color: var(--text); order: 2; }
.logo-dot { font-family: 'DM Mono', monospace; font-size: 0.62rem; color: var(--text3); vertical-align: super; margin-left: 2px; order: 3; }

/* Reorder: show "ei" + "io" + "pago" = "eiiopago"? 
   Actually we want: e[io]pago where "io" is red.
   So span order in HTML: logo-ei="e", logo-io="io", logo-pago="pago"
   But user wants logo to read "eiopago" with "io" in red.
   HTML already does this correctly with the spans in order. */

.sidebar-nav { flex: 1; padding: 1rem 0; }
.nav-section-label { font-size: 10px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text3); padding: 0 1.25rem; margin-bottom: 0.4rem; }
.nav-item { display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 1.25rem; border: none; background: none; color: var(--text2); font-family: 'DM Sans', sans-serif; font-size: 13px; cursor: pointer; text-align: left; transition: all 0.15s; border-left: 2px solid transparent; }
.nav-item:hover { color: var(--text); background: rgba(128,128,128,0.06); }
.nav-item.active { color: var(--text); background: rgba(230,51,41,0.07); border-left-color: var(--accent); }
.nav-icon { font-size: 14px; opacity: 0.7; }
.fascicoli { padding: 0.5rem 1.25rem; }
.fascicolo-empty { font-size: 11px; color: var(--text3); line-height: 1.6; font-style: italic; }
.fascicolo-item { padding: 6px 8px; border-radius: 6px; font-size: 12px; color: var(--text2); cursor: pointer; border: 0.5px solid var(--border); margin-bottom: 4px; transition: all 0.15s; }
.fascicolo-item:hover { color: var(--text); border-color: var(--border2); }
.fascicolo-item strong { display: block; color: var(--text); font-size: 12px; }
.btn-salva { margin: 0.5rem 1.25rem; padding: 6px 12px; border: 0.5px solid var(--border2); border-radius: 6px; background: none; color: var(--text2); font-family: 'DM Sans', sans-serif; font-size: 12px; cursor: pointer; transition: all 0.15s; width: calc(100% - 2.5rem); }
.btn-salva:hover { color: var(--text); border-color: var(--accent); }

/* Theme switch */
.sidebar-footer { padding: 1rem 1.25rem 0; border-top: 0.5px solid var(--border); font-size: 11px; color: var(--text3); }
.theme-switch-row { display: flex; align-items: center; justify-content: space-between; margin-top: 0.75rem; font-size: 11px; color: var(--text3); }
.switch { position: relative; display: inline-block; width: 34px; height: 18px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; inset: 0; background: var(--border2); border-radius: 18px; transition: 0.2s; }
.slider:before { content: ''; position: absolute; height: 12px; width: 12px; left: 3px; bottom: 3px; background: white; border-radius: 50%; transition: 0.2s; }
input:checked + .slider { background: var(--accent); }
input:checked + .slider:before { transform: translateX(16px); }

/* ===== MAIN ===== */
.main { grid-area: main; display: flex; flex-direction: column; overflow: hidden; }
.topbar { display: flex; align-items: flex-end; justify-content: space-between; padding: 1.25rem 2rem 1rem; border-bottom: 0.5px solid var(--border); background: var(--bg2); flex-shrink: 0; gap: 1rem; transition: background 0.25s; }
.topbar-left { display: flex; flex-direction: column; gap: 4px; }
.topbar-right { display: flex; gap: 8px; align-items: center; }
.topbar-pill { display: inline-block; font-size: 9px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent); border: 0.5px solid var(--accent); border-radius: 3px; padding: 2px 6px; width: fit-content; }
.topbar h1 { font-family: 'DM Serif Display', serif; font-size: 1.4rem; font-weight: 400; color: var(--text); letter-spacing: -0.02em; }
.btn-export, .btn-upload { padding: 6px 14px; border: 0.5px solid var(--border2); border-radius: 6px; background: none; color: var(--text2); font-family: 'DM Mono', monospace; font-size: 11px; cursor: pointer; transition: all 0.15s; white-space: nowrap; }
.btn-export:hover, .btn-upload:hover { color: var(--text); border-color: var(--accent); }

.pdf-banner { display: flex; justify-content: space-between; align-items: center; padding: 8px 2rem; background: rgba(34,201,122,0.08); border-bottom: 0.5px solid rgba(34,201,122,0.2); font-size: 12px; color: var(--green); flex-shrink: 0; }
.pdf-banner button { background: none; border: none; color: var(--text3); cursor: pointer; font-size: 14px; }

.tab-content { display: none; flex: 1; overflow-y: auto; padding: 1.5rem 2rem 1rem; }
.tab-content.active { display: block; }

/* ===== WIZARD ===== */
.wizard { display: flex; flex-direction: column; gap: 1.5rem; }
.wizard-step { background: var(--bg2); border: 0.5px solid var(--border); border-radius: 8px; padding: 1.25rem; transition: background 0.25s; }
.step-header { display: flex; align-items: flex-start; gap: 1rem; margin-bottom: 1.25rem; }
.step-num { font-family: 'DM Mono', monospace; font-size: 11px; color: var(--accent); border: 0.5px solid var(--accent); border-radius: 3px; padding: 2px 6px; flex-shrink: 0; margin-top: 2px; }
.step-title { font-size: 14px; font-weight: 500; color: var(--text); }
.step-sub { font-size: 11px; color: var(--text3); margin-top: 2px; }
.fields-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.field-block { display: flex; flex-direction: column; gap: 5px; }
.field-block.wide { grid-column: span 2; }
.field-block label { font-size: 11px; font-weight: 500; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text3); }
input[type="number"], input[type="text"], select { background: var(--bg3); border: 0.5px solid var(--border2); border-radius: 6px; padding: 8px 12px; color: var(--text); font-family: 'DM Sans', sans-serif; font-size: 14px; outline: none; transition: border-color 0.15s, background 0.25s; width: 100%; -webkit-appearance: none; }
input:focus, select:focus { border-color: var(--accent); }
select option, select optgroup { background: var(--bg2); }
.field-hint { font-size: 11px; color: var(--text3); line-height: 1.5; }
.coeff-display { font-family: 'DM Mono', monospace; font-size: 2rem; font-weight: 500; color: var(--accent); letter-spacing: -0.02em; padding: 4px 0; }
.limit-bar { margin-top: 8px; }
.limit-track { height: 3px; background: var(--bg3); border-radius: 2px; overflow: hidden; margin-bottom: 4px; }
.limit-fill { height: 100%; background: var(--accent); border-radius: 2px; transition: width 0.3s; width: 0%; }
#limit-label { font-size: 10px; color: var(--text3); font-family: 'DM Mono', monospace; }

/* ===== RESULTS ===== */
.results-panel { background: var(--bg2); border: 0.5px solid var(--border); border-radius: 8px; padding: 1.25rem; margin-top: 1.5rem; transition: background 0.25s; }
.results-header { font-family: 'DM Serif Display', serif; font-size: 1.1rem; font-weight: 400; margin-bottom: 1rem; color: var(--text); display: flex; gap: 8px; align-items: baseline; }
.results-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 10px; margin-bottom: 1.25rem; }
.res-card { background: var(--bg3); border-radius: 6px; padding: 0.875rem; border: 0.5px solid var(--border); transition: background 0.25s; }
.res-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text3); margin-bottom: 4px; }
.res-value { font-family: 'DM Mono', monospace; font-size: 16px; font-weight: 500; color: var(--text); }
.res-value.big { font-size: 20px; }
.res-sub { font-size: 10px; color: var(--text3); margin-top: 3px; }
.res-card.red .res-value { color: var(--accent2); }
.res-card.green .res-value { color: var(--green); }
.piano-title { font-size: 11px; font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text3); margin-bottom: 0.75rem; padding-top: 1rem; border-top: 0.5px solid var(--border); }
.piano-row { display: flex; align-items: center; justify-content: space-between; padding: 9px 0; border-bottom: 0.5px solid var(--border); gap: 12px; }
.piano-row:last-child { border-bottom: none; }
.piano-row-left { flex: 1; }
.piano-row-left strong { font-size: 13px; font-weight: 500; color: var(--text); display: block; }
.piano-row-left small { font-size: 11px; color: var(--text3); }
.piano-row-right { text-align: right; flex-shrink: 0; }
.piano-date { font-size: 11px; margin-bottom: 3px; }
.piano-amount { font-family: 'DM Mono', monospace; font-size: 14px; font-weight: 500; color: var(--text); }
.date-green { color: var(--green); }
.date-orange { color: var(--orange); }
.date-red { color: var(--accent2); }
.date-blue { color: var(--blue); }

/* ===== F24 FAC SIMILE ===== */
.f24-facsimile { border: 1px solid var(--border2); border-radius: 8px; overflow: hidden; margin-bottom: 1.5rem; font-family: 'DM Mono', monospace; }
.f24-header { background: var(--accent); color: white; padding: 10px 16px; display: flex; justify-content: space-between; align-items: center; }
.f24-header-title { font-size: 13px; font-weight: 500; letter-spacing: 0.05em; }
.f24-header-sub { font-size: 10px; opacity: 0.8; }
.f24-section { border-bottom: 0.5px solid var(--border); }
.f24-section:last-child { border-bottom: none; }
.f24-section-label { font-size: 9px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text3); padding: 6px 12px; background: var(--bg3); border-bottom: 0.5px solid var(--border); }
.f24-row { display: grid; grid-template-columns: 2fr 1fr 1fr 1.5fr; gap: 0; }
.f24-cell { padding: 8px 12px; border-right: 0.5px solid var(--border); font-size: 11px; color: var(--text); }
.f24-cell:last-child { border-right: none; }
.f24-cell-label { font-size: 9px; color: var(--text3); display: block; margin-bottom: 2px; text-transform: uppercase; letter-spacing: 0.05em; }
.f24-cell-value { font-size: 13px; font-weight: 500; color: var(--text); }
.f24-cell-value.accent { color: var(--accent2); }
.f24-total { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; background: var(--bg3); border-top: 1px solid var(--border2); }
.f24-total-label { font-size: 11px; color: var(--text2); text-transform: uppercase; letter-spacing: 0.06em; }
.f24-total-value { font-size: 16px; font-weight: 500; color: var(--accent2); }

/* ===== DOC BODY ===== */
.doc-body { max-width: 760px; }
.doc-section-title { font-family: 'DM Serif Display', serif; font-size: 1.3rem; font-weight: 400; color: var(--text); margin-bottom: 1.5rem; padding-bottom: 0.75rem; border-bottom: 0.5px solid var(--border); }
.doc-subsection { font-size: 13px; font-weight: 500; color: var(--text2); margin: 1.5rem 0 0.75rem; letter-spacing: 0.03em; }
.timeline { display: flex; flex-direction: column; }
.tl-item { display: flex; gap: 1rem; padding: 1rem 0; border-bottom: 0.5px solid var(--border); }
.tl-item:last-child { border-bottom: none; }
.tl-date { font-family: 'DM Mono', monospace; font-size: 11px; font-weight: 500; min-width: 80px; padding-top: 2px; flex-shrink: 0; }
.tl-date.green { color: var(--green); }
.tl-date.orange { color: var(--orange); }
.tl-date.red { color: var(--accent2); }
.tl-date.blue { color: var(--blue); }
.tl-content strong { font-size: 13px; font-weight: 500; color: var(--text); display: block; margin-bottom: 3px; }
.tl-content p { font-size: 12px; color: var(--text2); line-height: 1.6; }
.tl-content code, .gs-body code { font-family: 'DM Mono', monospace; font-size: 11px; background: var(--bg3); border: 0.5px solid var(--border2); padding: 1px 5px; border-radius: 3px; color: var(--accent2); }
.guide-steps { display: flex; flex-direction: column; }
.guide-step { display: flex; gap: 1.25rem; padding: 1.25rem 0; border-bottom: 0.5px solid var(--border); }
.guide-step:last-child { border-bottom: none; }
.gs-num { font-family: 'DM Mono', monospace; font-size: 11px; color: var(--accent); border: 0.5px solid var(--accent); border-radius: 3px; padding: 2px 5px; height: fit-content; flex-shrink: 0; margin-top: 2px; }
.gs-body h3 { font-size: 14px; font-weight: 500; color: var(--text); margin-bottom: 6px; }
.gs-body p { font-size: 12px; color: var(--text2); line-height: 1.7; margin-bottom: 6px; }
.gs-body ul { padding-left: 1rem; font-size: 12px; color: var(--text2); line-height: 1.8; }
.gs-tip { font-size: 11px; color: var(--orange); background: rgba(245,166,35,0.06); border: 0.5px solid rgba(245,166,35,0.2); border-radius: 6px; padding: 6px 10px; margin-top: 6px; }
.formula { font-family: 'DM Mono', monospace; font-size: 12px; background: var(--bg3); border: 0.5px solid var(--border2); border-radius: 6px; padding: 10px 14px; color: var(--green); margin-top: 8px; }
.ref-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.ref-table thead tr { border-bottom: 0.5px solid var(--border2); }
.ref-table th { padding: 8px 12px; text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text3); font-weight: 500; }
.ref-table td { padding: 10px 12px; border-bottom: 0.5px solid var(--border); color: var(--text2); }
.ref-table tr:last-child td { border-bottom: none; }
.ref-table tr:hover td { background: rgba(128,128,128,0.03); }
.ref-table code { font-family: 'DM Mono', monospace; font-size: 11px; background: var(--bg3); border: 0.5px solid var(--border2); padding: 1px 5px; border-radius: 3px; color: var(--accent2); }
.coeff-badge { font-family: 'DM Mono', monospace; font-size: 11px; font-weight: 500; padding: 2px 8px; border-radius: 3px; }
.c40 { background: rgba(74,158,255,0.1); color: var(--blue); }
.c62 { background: rgba(245,166,35,0.1); color: var(--orange); }
.c67 { background: rgba(34,201,122,0.1); color: var(--green); }
.c78 { background: rgba(255,90,82,0.1); color: var(--accent2); }
.c86 { background: rgba(230,51,41,0.12); color: var(--accent); }
.info-box { display: flex; gap: 10px; background: rgba(74,158,255,0.05); border: 0.5px solid rgba(74,158,255,0.2); border-radius: 6px; padding: 12px 14px; font-size: 12px; color: var(--text2); line-height: 1.7; margin-top: 1.5rem; }
.info-icon { color: var(--blue); flex-shrink: 0; font-size: 14px; }

/* ===== AI PANEL ===== */
.ai-panel { grid-area: ai; background: var(--bg2); border-left: 0.5px solid var(--border); display: flex; flex-direction: column; overflow: hidden; transition: background 0.25s; }
.ai-header { padding: 1.25rem; border-bottom: 0.5px solid var(--border); font-size: 12px; font-weight: 500; color: var(--text2); display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.ai-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); animation: pulse 2s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }
.ai-chat { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 10px; }
.ai-msg { font-size: 12px; line-height: 1.7; padding: 10px 12px; border-radius: 6px; }
.ai-msg.ai { background: var(--bg3); border: 0.5px solid var(--border); color: var(--text2); }
.ai-msg.user { background: rgba(230,51,41,0.08); border: 0.5px solid rgba(230,51,41,0.2); color: var(--text); align-self: flex-end; max-width: 90%; }
.ai-msg.loading { color: var(--text3); font-style: italic; }
.ai-chip { display: inline-block; font-size: 11px; padding: 2px 8px; border: 0.5px solid var(--border2); border-radius: 3px; color: var(--text2); cursor: pointer; margin: 2px 2px 0 0; transition: all 0.15s; }
.ai-chip:hover { border-color: var(--accent); color: var(--text); }
.ai-input-row { display: flex; gap: 6px; padding: 0.875rem 1rem; border-top: 0.5px solid var(--border); flex-shrink: 0; }
.ai-input-row input { flex: 1; padding: 7px 10px; font-size: 12px; }
.ai-input-row button { padding: 7px 12px; background: var(--accent); border: none; border-radius: 6px; color: white; font-size: 14px; cursor: pointer; transition: background 0.15s; }
.ai-input-row button:hover { background: var(--accent2); }

/* ===== FOOTER ===== */
.app-footer { padding: 8px 2rem; border-top: 0.5px solid var(--border); font-size: 10px; color: var(--text3); font-family: 'DM Mono', monospace; flex-shrink: 0; background: var(--bg2); transition: background 0.25s; }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text3); }

/* ===== PRINT ===== */
@media print {
  body { display: block; overflow: auto; background: white; color: black; }
  .sidebar, .ai-panel, .topbar, .app-footer { display: none; }
  .main { display: block; overflow: visible; }
  .tab-content { display: none !important; }
  .tab-content.print-active { display: block !important; padding: 0; }
}

/* ===== ACCESSIBILITÀ — EAA / WCAG 2.1 AA ===== */

/* Focus visibile su tutti gli elementi interattivi */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 3px;
}

/* Rimuovi outline di default solo quando focus non è da tastiera */
:focus:not(:focus-visible) { outline: none; }

/* Skip link per screen reader e navigazione tastiera */
.skip-link {
  position: absolute;
  top: -100px;
  left: 1rem;
  background: var(--accent);
  color: white;
  padding: 8px 16px;
  border-radius: 0 0 4px 4px;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  z-index: 99999;
  transition: top 0.2s;
}
.skip-link:focus { top: 0; }

/* Screen reader only utility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* Contrasto testo migliorato — WCAG AA 4.5:1 */
.nav-item { color: var(--text2); }
.nav-item.active { color: var(--text); }
.res-label { color: var(--text2); } /* era text3 — contrasto insufficiente */
.piano-row-left small { color: var(--text2); }
.field-hint { color: var(--text2); }
.step-sub { color: var(--text2); }
.ai-msg.ai { color: var(--text); }

/* Focus su input e select — più visibile */
input[type="number"]:focus,
input[type="text"]:focus,
select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}

/* Bottoni — area clic minima 44x44px (WCAG 2.5.5) */
.nav-item { min-height: 44px; }
.btn-export, .btn-upload { min-height: 36px; padding-top: 8px; padding-bottom: 8px; }
.ai-input-row button { min-width: 44px; min-height: 44px; }
.btn-salva { min-height: 36px; }

/* Link — underline visibile per accessibilità */
.tl-content a,
.gs-body a,
.info-box a,
.app-footer a {
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Animazioni ridotte per utenti con prefers-reduced-motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .ai-dot { animation: none; }
  .limit-fill { transition: none; }
}

/* Testo non selezionabile su elementi UI puri */
.nav-item, .tab, .step-num, .gs-num,
.res-label, .piano-date, .badge { user-select: none; }

/* Contrasto bordi form in light mode */
[data-theme="light"] input,
[data-theme="light"] select {
  border-color: rgba(0,0,0,0.25);
}
[data-theme="light"] input:focus,
[data-theme="light"] select:focus {
  border-color: var(--accent);
}

/* Dimensione testo minima 16px per input (evita zoom iOS) */
input, select, textarea { font-size: max(16px, 1rem); }

/* ===== FINE ACCESSIBILITÀ ===== */
