:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #16181d;
  --muted: #646b78;
  --accent: #3b5bdb;
  --accent-soft: #e8edff;
  --accent-text: #ffffff;
  --ok: #1f8a4c;
  --ok-soft: #e3f5ea;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --bad: #c92a2a;
  --bad-soft: #ffe8e8;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --aisle: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --border: #2a2f3a;
    --text: #e8eaef;
    --muted: #98a0ae;
    --accent: #7b93ff;
    --accent-soft: #232c4d;
    --accent-text: #0f1115;
    --ok: #51cf87;
    --ok-soft: #173325;
    --warn: #ffb44d;
    --warn-soft: #3a2a12;
    --bad: #ff7b7b;
    --bad-soft: #3d1c1c;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0 0 .5rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 .9rem; }
h3 { font-size: 1rem; margin: 1.2rem 0 .6rem; }
p { margin: .4rem 0; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem 1rem;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 650; color: var(--text); }
.brand:hover { text-decoration: none; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 50%, #22b8cf)); }
.nav { display: flex; align-items: center; gap: .3rem; }
.nav a { padding: .35rem .7rem; border-radius: var(--radius-sm); color: var(--muted); }
.nav a.on, .nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav form { margin: 0 0 0 .3rem; }
.who { font-weight: 600; margin-right: .3rem; }
.wrap { max-width: 1080px; margin: 0 auto; padding: 1.4rem 1rem 4rem; }
body.center .wrap { min-height: calc(100vh - 60px); display: flex; flex-direction: column; align-items: center; justify-content: center; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.card.link { display: block; color: inherit; transition: border-color .15s, transform .15s; }
.card.link:hover { border-color: var(--accent); text-decoration: none; transform: translateY(-1px); }
.card.highlight { border-color: var(--accent); background: var(--accent-soft); }
.card h2 { margin-bottom: .5rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.cards .card { margin: 0; }
.card-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: .4rem; }
.empty-state { text-align: center; color: var(--muted); padding: 2.5rem 1rem; }
.auth { width: 100%; max-width: 400px; }
.page-h { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-h h1 { margin: 0; }
.type { display: block; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.back { display: inline-block; margin-bottom: .6rem; font-size: .9rem; }
.back::before { content: "\2039  "; }
.desc { color: var(--muted); white-space: pre-line; margin-bottom: 1rem; }
.big { font-size: 1.35rem; margin: .2rem 0; }

/* Forms */
label { display: flex; flex-direction: column; gap: .3rem; font-size: .88rem; font-weight: 550; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: .55rem .7rem; width: 100%;
}
textarea { resize: vertical; font-family: inherit; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
label.check { flex-direction: row; align-items: flex-start; gap: .55rem; color: var(--text); font-weight: 400; }
label.check input { width: auto; margin-top: .25rem; }
.stack { display: flex; flex-direction: column; gap: .9rem; }
.row2, .row3 { display: grid; gap: .8rem; }
.row2 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.row3 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); align-items: end; }
.new-project .row3 { grid-template-columns: 1fr 200px auto; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.actions form { margin: 0; }
.actions.sticky { position: sticky; bottom: .8rem; justify-content: flex-end; }
fieldset.wd { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .4rem; }
fieldset.wd legend { font-size: .88rem; font-weight: 550; color: var(--muted); margin-bottom: .4rem; padding: 0; }
label.chk { flex-direction: row; align-items: center; gap: .35rem; padding: .35rem .7rem; border: 1px solid var(--border); border-radius: 999px; color: var(--text); cursor: pointer; font-weight: 500; }
label.chk input { width: auto; margin: 0; }
label.chk:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); }
details.block { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .2rem .9rem; }
details.block[open] { padding-bottom: .9rem; }
details.block summary { cursor: pointer; font-weight: 600; padding: .6rem 0; }
.mt { margin-top: 1rem; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; cursor: pointer;
  padding: .55rem 1rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); background: transparent; }
.btn.danger:hover { background: var(--bad-soft); }
.btn.sm { padding: .3rem .65rem; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* Tabs / segmented */
.tabs, .seg { display: flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; gap: 3px; margin: .8rem 0 1rem; }
.tabs a, .seg button { flex: 1; text-align: center; padding: .45rem .8rem; border-radius: 6px; color: var(--muted); font: inherit; font-weight: 600; border: 0; background: transparent; cursor: pointer; }
.tabs a.on, .seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); text-decoration: none; }
.seg { margin: 0; }
.seg button.on.st-aperto { color: var(--ok); }
.seg button.on.st-chiuso { color: var(--bad); }
.subnav { display: flex; gap: .3rem; margin-bottom: 1rem; overflow-x: auto; }
.subnav a { padding: .4rem .8rem; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); color: var(--text); white-space: nowrap; font-size: .9rem; }

/* Badges, chips, notes */
.badge { font-size: .75rem; font-weight: 650; padding: .15rem .55rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.st-aperto { background: var(--ok-soft); color: var(--ok); }
.badge.st-chiuso { background: var(--bad-soft); color: var(--bad); }
.pill { font-size: .75rem; font-weight: 650; padding: .05rem .5rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip { display: inline-block; font-size: .78rem; font-weight: 600; padding: .1rem .5rem; border-radius: 999px; white-space: nowrap; }
.chip.r1 { background: var(--ok-soft); color: var(--ok); }
.chip.r2 { background: var(--accent-soft); color: var(--accent); }
.chip.r3, .chip.rn { background: var(--warn-soft); color: var(--warn); }
.chip.out { background: var(--bad-soft); color: var(--bad); }
.chip.mute { background: var(--surface-2); color: var(--muted); }
.flash, .note { padding: .7rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; font-size: .92rem; background: var(--surface-2); }
.flash.ok { background: var(--ok-soft); color: var(--ok); }
.flash.err, .note.warn { background: var(--warn-soft); color: var(--warn); }
.flash.err { background: var(--bad-soft); color: var(--bad); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.ok-text { color: var(--ok); font-weight: 600; }
.warn-text { color: var(--warn); font-weight: 600; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.stats > div { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .6rem .8rem; }
.stats .k { display: block; font-size: .78rem; color: var(--muted); }
.stats .v { font-size: 1.3rem; font-weight: 700; }
.stats .v.bad { color: var(--bad); }
.progress { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: .6rem 0 .3rem; }
.progress span { display: block; height: 100%; background: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: .92rem; }
.tbl th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: .5rem; border-bottom: 1px solid var(--border); }
.tbl td { padding: .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tbl tr.missing td:first-child { color: var(--muted); }
.tbl input, .tbl select { padding: .35rem .5rem; min-width: 120px; }
.tbl form { margin: 0; }
ol.inline { margin: 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: .25rem .7rem; counter-reset: p; }
ol.inline li { counter-increment: p; }
ol.inline li::before { content: counter(p) ". "; color: var(--muted); }

/* Seat grid */
.desk {
  margin: 0 auto .8rem; width: min(260px, 60%); text-align: center;
  padding: .35rem; border-radius: var(--radius-sm); background: var(--surface-2); color: var(--muted);
  font-size: .78rem; text-transform: uppercase; letter-spacing: .1em; font-weight: 600;
}
.desk-row { display: flex; justify-content: center; max-width: 760px; margin: 0 auto .8rem; }
.desk-row .desk { margin: 0; }
.desk-row.left { justify-content: flex-start; }
.desk-row.right { justify-content: flex-end; }
.layout-editor .quick { margin-top: .6rem; }
.seat-grid { display: grid; gap: 8px; max-width: 760px; margin: 0 auto; }
.seat-grid .aisle, .seat-grid .gap { min-height: 1px; }
.seat {
  position: relative; min-width: 0; min-height: 56px; padding: .4rem;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .1rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface);
  font: inherit; color: var(--text); text-align: center;
}
button.seat { cursor: pointer; transition: background .12s, border-color .12s, transform .08s; }
button.seat:active { transform: scale(.97); }
.seat .code { font-weight: 700; font-size: .85rem; }
.seat .name { font-size: .78rem; line-height: 1.2; word-break: break-word; }
.seat.free .name { color: var(--muted); font-style: italic; }
.seat.me { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.seat.me .name { color: var(--accent-text); }
.seat-grid.editor .seat { min-height: 44px; font-weight: 600; }
.seat.off { background: transparent; border-style: dashed; color: var(--muted); opacity: .55; }
.picker .seat.picked { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.picker .seat.full:not(.picked), .picker .daybtn.full:not(.picked) { opacity: .5; }
.seat .rank:empty, .daybtn .rank:empty { display: none; }
.seat .rank {
  position: absolute; top: -7px; right: -7px; min-width: 22px; height: 22px; padding: 0 5px;
  border-radius: 999px; background: var(--text); color: var(--surface); font-size: .75rem; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

/* Picker */
.picker { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 1.2rem; align-items: start; }
.picker > * { min-width: 0; }
.picker-side { position: sticky; top: 76px; background: var(--surface-2); border-radius: var(--radius); padding: 1rem; }
.side-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }
ol.picked { list-style: none; margin: 0 0 .8rem; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
ol.picked li { display: flex; align-items: center; gap: .55rem; padding: .45rem .5rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); }
ol.picked li.empty { color: var(--muted); justify-content: center; border-style: dashed; }
ol.picked .n { flex: none; width: 24px; height: 24px; border-radius: 999px; background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: .8rem; display: flex; align-items: center; justify-content: center; }
ol.picked .lbl { flex: 1; font-weight: 550; }
ol.picked .acts { display: flex; gap: 2px; }
ol.picked .acts button { width: 28px; height: 28px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer; font-size: .8rem; }
ol.picked .acts button:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
ol.picked .acts button:disabled { opacity: .3; cursor: default; }
.picker[data-readonly="1"] .seat, .picker[data-readonly="1"] .daybtn { cursor: default; }
.day-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: .5rem; }
.daybtn {
  position: relative; display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .75rem .9rem; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; text-align: left;
}
.daybtn.picked { background: var(--accent-soft); border-color: var(--accent); }
.daybtn .rank { min-width: 24px; height: 24px; border-radius: 999px; background: var(--accent); color: var(--accent-text); font-size: .8rem; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

/* Results: days */
.days { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: .7rem; }
.day { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .7rem .9rem; background: var(--surface); }
.day-h { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; margin-bottom: .3rem; }
.day ul { margin: 0; padding-left: 1.1rem; }
.day li.me { font-weight: 700; color: var(--accent); }
.result-h { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin: 1.2rem 0 .8rem; }

/* Waiting */
.wait { text-align: center; }
.spinner { width: 36px; height: 36px; margin: .5rem auto 1rem; border-radius: 50%; border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.danger-zone { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.5rem; }
.danger-zone form { margin: 0; }

/* Schede progetto */
.tabs.wide { margin: 0 0 1.2rem; }
.tabs.wide a { font-size: .92rem; }
.badge.st-ok { background: var(--ok-soft); color: var(--ok); }
.grow { flex: 1; }
.row3.align-end { align-items: end; }
.stack.tight { gap: .5rem; }

/* Inserimento preferenze */
.prefs { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 1rem; align-items: start; }
.prefs .card { margin: 0; }
.students { position: sticky; top: 76px; padding: .8rem; max-height: calc(100vh - 96px); display: flex; flex-direction: column; gap: .5rem; }
.students-h { display: flex; justify-content: space-between; align-items: center; }
.students input { padding: .4rem .6rem; }
.students ul { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.students li button {
  width: 100%; display: flex; align-items: center; gap: .5rem; text-align: left;
  padding: .45rem .55rem; border: 0; border-radius: 6px; background: transparent; color: var(--text); font: inherit; cursor: pointer;
}
.students li button:hover { background: var(--surface-2); }
.students li button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.students .dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--border); }
.students .done .dot { background: var(--ok); }
.students .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.students .tg { font-size: .72rem; color: var(--muted); }
.editor-h { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; border-bottom: 1px solid var(--border); padding-bottom: .6rem; margin-bottom: .8rem; }
.editor-h h2 { margin: 0; font-size: 1.3rem; }
.editor .picker { grid-template-columns: minmax(0, 1fr) 230px; margin-bottom: 1.2rem; }
.editor .picker h3 { margin-top: 0; }
.editor .picker-side { top: auto; position: static; margin-top: 2rem; }
.editor-actions { border-top: 1px solid var(--border); padding-top: .8rem; }
.mate-pick { display: flex; flex-wrap: wrap; gap: .4rem; }
.chipbtn {
  position: relative; display: inline-flex; align-items: center; gap: .4rem;
  padding: .4rem .75rem; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text); font: inherit; font-size: .9rem; cursor: pointer;
}
.chipbtn.picked { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chipbtn.full:not(.picked) { opacity: .5; }
.chipbtn .rank { min-width: 20px; height: 20px; border-radius: 999px; background: var(--accent); color: var(--accent-text); font-size: .72rem; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.chipbtn .rank:empty { display: none; }

/* Opzioni */
.tbl.options td.cap { width: 110px; }
.tbl.options td.cap input { min-width: 0; }
.row-acts { white-space: nowrap; text-align: right; width: 1%; }
.row-acts .btn { padding: .25rem .5rem; }

/* Risultato */
.seat.mate-ok { box-shadow: inset 0 -3px 0 var(--ok); }
.legend { display: flex; align-items: center; gap: .4rem; justify-content: center; margin-top: .6rem; }
.legend .sw { width: 18px; height: 6px; border-radius: 3px; background: var(--ok); }
details.explain { margin-top: 1rem; font-size: .9rem; }
details.explain summary { cursor: pointer; color: var(--accent); font-weight: 600; }
details.explain ul { margin: .5rem 0 0; padding-left: 1.2rem; color: var(--muted); }
details.explain li { margin: .25rem 0; }

/* Avanzamento calcolo */
.job { max-width: 640px; margin: 2rem auto; }
.job-h { display: flex; align-items: center; gap: .7rem; margin-bottom: 1rem; }
.job-h h2 { margin: 0; }
.spinner.sm { width: 22px; height: 22px; margin: 0; border-width: 3px; }
.bar { height: 12px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #22b8cf)); border-radius: 999px; transition: width .35s ease; }
.job.failed .bar span { background: var(--bad); }
.job-meta { display: flex; flex-wrap: wrap; gap: .4rem 1.2rem; margin: .7rem 0; font-size: .92rem; color: var(--muted); }
.job-meta strong { color: var(--text); }
.hint { font-weight: 400; font-size: .82rem; color: var(--muted); }
[hidden] { display: none !important; }

@media (max-width: 760px) {
  .prefs { grid-template-columns: minmax(0, 1fr); }
  .students { position: static; max-height: 230px; }
  .editor .picker { grid-template-columns: minmax(0, 1fr); }
  .picker { grid-template-columns: minmax(0, 1fr); }
  .picker-side { position: static; }
  .new-project .row3 { grid-template-columns: 1fr; }
  .seat-grid { gap: 5px; }
  :root { --aisle: 10px; }
  .seat { min-height: 48px; padding: .25rem; }
  .seat .name { font-size: .7rem; }
  h1 { font-size: 1.35rem; }
}

/* Print */
body.print { background: #fff; color: #000; }
@media print {
  .noprint, .topbar { display: none !important; }
  body { background: #fff; color: #000; }
  .seat, .day { border-color: #999; }
  .wrap { max-width: none; padding: 0; }
}
