/* ===== Token di tema ===== */
:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #efeeea;
  --border: #e0dfd9;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7d7c76;
  --grid: rgba(0, 0, 0, .07);
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, .10);
  --critical: #c93636;
  --critical-soft: rgba(201, 54, 54, .11);
  --good: #1b7f4a;
  --good-soft: rgba(27, 127, 74, .10);
  --warn: #9a6200;
  --warn-soft: rgba(237, 161, 0, .14);
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 1px 8px rgba(0, 0, 0, .04);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --grid: rgba(255, 255, 255, .08);
    --accent: #5598e7;
    --accent-soft: rgba(85, 152, 231, .16);
    --critical: #ec6b6b;
    --critical-soft: rgba(236, 107, 107, .15);
    --good: #4cc488;
    --good-soft: rgba(76, 196, 136, .14);
    --warn: #f0b440;
    --warn-soft: rgba(240, 180, 64, .14);
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 18px; }
h2 { font-size: 15px; }
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.wrap { max-width: 1240px; margin: 0 auto; padding: 0 20px; }

/* ===== Barra superiore ===== */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar .wrap { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding-top: 10px; padding-bottom: 10px; }
.topbar .title { display: flex; align-items: baseline; gap: 10px; margin-right: auto; }
.topbar .saved { font-size: 12px; color: var(--text-3); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; font-size: 13px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--critical); }
.pill {
  border: 1px solid color-mix(in srgb, var(--warn) 45%, var(--border)); background: var(--warn-soft); color: var(--warn);
  border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer; white-space: nowrap;
}
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 30; min-width: 210px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px rgba(0, 0, 0, .18);
}
.menu-list button {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px;
  border-radius: 6px; cursor: pointer; font-size: 13px;
}
.menu-list button:hover { background: var(--surface-2); }
.menu-list .danger { color: var(--critical); }
.imp-list { margin: 6px 0 0; padding-left: 18px; font-size: 13px; color: var(--text-2); max-height: 160px; overflow: auto; }
.dlg-body.one { grid-template-columns: 1fr; }
.dlg-body.one p { margin: 0; }

main { padding: 18px 0 40px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

/* ===== Avvisi ===== */
.notice {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 14px; margin-bottom: 14px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface);
}
.notice.success { border-color: color-mix(in srgb, var(--good) 40%, var(--border)); background: var(--good-soft); }
.notice.error { border-color: color-mix(in srgb, var(--critical) 45%, var(--border)); background: var(--critical-soft); }
.notice .body { flex: 1; min-width: 0; }
.notice strong { display: block; }
.notice ul { margin: 6px 0 0; padding-left: 18px; max-height: 220px; overflow: auto; }
.notice li { margin: 2px 0; }
.notice .close { background: none; border: 0; cursor: pointer; font-size: 18px; line-height: 1; color: var(--text-2); padding: 0 2px; }

/* ===== Stato vuoto ===== */
.empty { padding: 56px 24px; text-align: center; border-style: dashed; border-width: 2px; }
.empty h2 { font-size: 20px; margin-bottom: 8px; }
.empty p { color: var(--text-2); max-width: 560px; margin: 8px auto; }
.empty code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.empty .actions { justify-content: center; margin-top: 18px; }
.empty .icon { font-size: 34px; color: var(--text-3); }

/* ===== Metriche ===== */
.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 14px; }
.metric { padding: 12px 16px; }
.metric .label { font-size: 12px; color: var(--text-2); }
.metric .value { font-size: 26px; font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.metric .value small { font-size: 14px; font-weight: 500; color: var(--text-2); margin-left: 2px; }
.metric .sub { font-size: 12px; color: var(--text-3); }
.metric .sub.over { color: var(--critical); }

/* ===== Grafico ===== */
.chart-card { padding: 12px 16px 8px; margin-bottom: 14px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; margin-bottom: 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 12px; cursor: pointer; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); cursor: pointer; user-select: none; }
.check input { accent-color: var(--accent); margin: 0; }
.capacity { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); }
.capacity input[type="number"] { width: 64px; }
.capacity .over { color: var(--critical); font-weight: 500; }
.toolbar .hint { margin-left: auto; font-size: 12px; color: var(--text-3); }
.chart-wrap { position: relative; height: clamp(280px, 44vh, 440px); }
.chart-msg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--text-3); text-align: center; padding: 20px;
}

input[type="number"], input[type="date"], input[type="text"], input[type="search"] {
  border: 1px solid var(--border); background: var(--surface); border-radius: 6px;
  padding: 3px 6px; font-size: 13px; font-variant-numeric: tabular-nums;
}
input[type="date"].warn { border-color: var(--warn); background: var(--warn-soft); }
input:disabled { opacity: .5; }

/* ===== Tabella ===== */
.table-card { overflow: hidden; }
.table-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.table-head .count { font-size: 12px; color: var(--text-3); margin-right: auto; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--text-2);
  padding: 8px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; background: var(--surface);
}
td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
tbody tr.selected { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
tbody tr.excluded > td:not(:last-child) { opacity: .55; }
.hide-excluded tbody tr.excluded { display: none; }
.course-cell { display: flex; gap: 8px; align-items: center; min-width: 150px; max-width: 210px; }
.dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.code { font-weight: 600; font-variant-numeric: tabular-nums; }
.sub { font-size: 11.5px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.course-cell > div { min-width: 0; }
td.c-name { min-width: 180px; }
.badge {
  display: inline-block; margin-top: 2px; font-size: 11px; padding: 0 6px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
td.c-deadline { white-space: nowrap; font-variant-numeric: tabular-nums; }
td.c-hours input { width: 64px; }
td.c-peak { white-space: nowrap; }
td.c-peak input[type="range"] { width: 96px; vertical-align: middle; accent-color: var(--accent); }
.peak-label { display: inline-block; min-width: 92px; font-size: 12px; color: var(--text-2); margin-left: 6px; }
td.c-center { text-align: center; }
td.c-center input { accent-color: var(--accent); }
.done { color: var(--good); font-weight: 600; }
canvas.spark { display: block; }
.table-head .search { width: 200px; }
td.row-actions { white-space: nowrap; text-align: right; }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--text-3); font-size: 14px; padding: 2px 6px; border-radius: 6px; }
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.icon-btn.danger:hover { color: var(--critical); background: var(--critical-soft); }
.notice .action { margin-left: 8px; }

/* ===== Dialog task ===== */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px)); box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dlg-head { padding: 16px 18px 0; }
.dlg-body { padding: 14px 18px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); min-width: 0; }
.field.full { grid-column: 1 / -1; }
.field input[type="text"], .field input[type="date"], .field input[type="number"] { font-size: 14px; padding: 6px 8px; width: 100%; }
.field .row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; min-height: 32px; }
.field input[type="range"] { flex: 1; min-width: 140px; accent-color: var(--accent); }
.dlg-error { grid-column: 1 / -1; margin: 0; font-size: 13px; color: var(--critical); }
.dlg-foot { display: flex; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.dlg-foot .spacer { margin-right: auto; }
@media (max-width: 520px) { .dlg-body { grid-template-columns: 1fr; } }

/* ===== Drag & drop ===== */
.drop-overlay {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  border: 3px dashed var(--accent); font-size: 20px; font-weight: 600; color: var(--accent);
  pointer-events: none;
}

@media (max-width: 760px) {
  .wrap { padding: 0 16px; }
  .metrics { grid-template-columns: 1fr; }
  .toolbar .hint { margin-left: 0; }
}
