:root {
  --desk: #F2E3CC;
  --sheet: #FFFAF2;
  --grid: #F1E3CD;
  --grid-hour: #E3CBA8;
  --ink: #3B2A1D;
  --pencil: #8A6C52;
  --rule: #EBD6B8;
  --red: #9E2F2F;
  --rust: #B4532A;
  --heading: #8A4220;
  --hl-ink: #3B2A1D;
  --focus: #B4532A;
  --tint: 24%;
  --serif: ui-serif, "New York", "Iowan Old Style", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  --sq: 12px;          /* lato del quadretto = 15 minuti */
  --hour: 48px;        /* 4 quadretti */
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(214, 120, 52, .16), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(140, 47, 57, .10), transparent 60%),
    var(--desk);
  background-attachment: fixed;
  min-height: 100vh;
  color: var(--ink);
  font: 15px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; }

.page {
  max-width: 1480px;
  margin: 0 auto;
  padding: 28px 32px 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 372px;
  grid-template-areas: "hero hero" "week side" "lower side";
  gap: 22px 26px;
  align-items: start;
}

/* ---------- Hero ---------- */
.hero { grid-area: hero; display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; padding: 4px 2px 0; }
.hero h1 { font-size: 46px; line-height: 1.05; font-weight: 500; letter-spacing: -0.01em; }
.hero-line { font-family: var(--serif); font-size: 20px; line-height: 1.4; margin: 12px 0 0; max-width: 62ch; }
.hero-line .hl { padding: 0 5px; box-shadow: inset 0 -2px 0 var(--c); border-radius: 2px 2px 0 0; }
.hero-line .when { color: var(--pencil); }
.sync-line { margin: 8px 0 0; color: var(--pencil); font-size: 13px; }
.sync-line.err { color: var(--red); }
.sync-line button { border: 0; background: none; padding: 0; color: inherit; text-decoration: underline; cursor: pointer; }
.hero-tools { display: flex; align-items: center; gap: 14px; }
.clock { font-family: var(--serif); font-size: 30px; font-variant-numeric: tabular-nums; color: var(--heading); }

/* ---------- Sheets ---------- */
.sheet {
  background: var(--sheet);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 16px 18px 18px;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.sheet-head h2 { font-size: 19px; color: var(--heading); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-head .count { white-space: nowrap; }
.count { color: var(--pencil); font-size: 13px; font-variant-numeric: tabular-nums; }
.side { grid-area: side; display: grid; gap: 22px; }
.lower { grid-area: lower; display: grid; grid-template-columns: 1.6fr 1fr; gap: 22px; align-items: start; }

/* ---------- Buttons & inputs ---------- */
.btn {
  border: 1px solid var(--rule); background: transparent; border-radius: 6px;
  padding: 6px 12px; cursor: pointer; line-height: 1.2; white-space: nowrap;
}
.btn:hover { border-color: var(--grid-hour); background: color-mix(in srgb, var(--grid) 45%, transparent); }
.btn.primary { background: var(--rust); color: #FFF8EE; border-color: var(--rust); }
.btn.primary:hover { background: #9C4522; border-color: #9C4522; }
.btn.ghost { border-color: transparent; }
.btn.small { padding: 3px 8px; font-size: 13px; }
.icon-btn { border: 0; background: none; color: var(--pencil); padding: 6px; border-radius: 6px; cursor: pointer; line-height: 0; font-size: 16px; }
.icon-btn:hover { color: var(--ink); background: color-mix(in srgb, var(--grid) 60%, transparent); }

input[type=text], input[type=url], input[type=date], select, textarea {
  border: 1px solid var(--rule); border-radius: 6px; background: var(--sheet);
  padding: 6px 9px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: -1px; }
.add-form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.add-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.add-row > input[type=text], .add-row > input[type=url] { flex: 1 1 160px; }
.add-row > select { flex: 1 1 120px; }

/* ---------- Highlighter ---------- */
.hl {
  --c: #C08A2B;
  background: color-mix(in srgb, var(--c) var(--tint), var(--sheet));
  color: var(--hl-ink);
  border-radius: 3px;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
}

/* ---------- Week (the squared sheet) ---------- */
.week-sheet { grid-area: week; padding-bottom: 12px; }
.week-nav { display: flex; gap: 2px; }
.week-nav .btn { min-width: 34px; }
.week { display: grid; grid-template-columns: 46px repeat(var(--days, 5), minmax(0, 1fr)); }
.week .dayhead {
  padding: 4px 6px 8px; font-size: 13px; color: var(--pencil);
  display: flex; align-items: baseline; gap: 6px;
}
.week .dayhead b { font-family: var(--serif); font-size: 20px; font-weight: 500; color: var(--ink); font-variant-numeric: tabular-nums; }
.week .dayhead.today b { color: #FFF8EE; background: var(--rust); border-radius: 50%; width: 30px; height: 30px; display: inline-grid; place-items: center; font-size: 16px; }
.week .gutter { position: relative; }
.week .gutter span { position: absolute; right: 8px; transform: translateY(-50%); font-size: 11px; color: var(--pencil); font-variant-numeric: tabular-nums; }
.week .col {
  position: relative;
  border-left: 1px solid var(--grid-hour);
  background-color: var(--sheet);
  background-image:
    linear-gradient(var(--grid-hour) 1px, transparent 1px),
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 100% var(--hour), var(--sq) var(--sq), var(--sq) var(--sq);
  border-bottom: 1px solid var(--grid-hour);
}
.week .col:last-child { border-right: 1px solid var(--grid-hour); }
.week .col.today { background-color: color-mix(in srgb, var(--rust) 6%, var(--sheet)); }
.ev {
  position: absolute; left: 3px; right: 3px; overflow: hidden;
  padding: 3px 6px 3px 7px; font-size: 12.5px; line-height: 1.25;
  border-left: 3px solid var(--c); border-radius: 2px 4px 4px 2px;
  cursor: default;
}
.ev strong { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.ev small { display: block; font-size: 11.5px; opacity: .82; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.ev.cancelled { opacity: .55; }
.ev.cancelled strong { text-decoration: line-through; text-decoration-color: var(--red); text-decoration-thickness: 2px; }
.ev.cancelled::after { content: "Annullata"; position: absolute; right: 5px; bottom: 3px; font-size: 10.5px; color: var(--red); font-weight: 600; }
.nowline { position: absolute; left: -1px; right: 0; height: 2px; background: var(--red); z-index: 3; pointer-events: none; }
.nowline::before { content: ""; position: absolute; left: -4px; top: -3px; width: 8px; height: 8px; border-radius: 50%; background: var(--red); }
.week-empty { grid-column: 1 / -1; color: var(--pencil); padding: 6px 2px 0; font-size: 14px; }

.agenda { display: none; }
.agenda h3 { font-size: 16px; margin: 14px 0 6px; }
.agenda ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.agenda li { padding: 6px 9px; border-left: 3px solid var(--c); }
.agenda li small { display: block; font-size: 12.5px; opacity: .85; }

/* ---------- Timer ---------- */
.timer-face {
  font-family: var(--serif); font-size: 64px; line-height: 1; font-weight: 400;
  font-variant-numeric: tabular-nums; letter-spacing: -0.02em; margin: 2px 0 14px;
}
.timer-sheet.running .timer-face { color: var(--ink); }
.timer-sheet.break .timer-face { color: var(--pencil); }
.timer-row { display: flex; gap: 8px; }
.timer-row select { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 6px; overflow: hidden; }
.seg-btn { border: 0; background: transparent; padding: 3px 9px; font-size: 13px; cursor: pointer; color: var(--pencil); }
.seg-btn + .seg-btn { border-left: 1px solid var(--rule); }
.seg-btn[aria-pressed=true] { background: var(--rust); color: #FFF8EE; }

/* ---------- To-do ---------- */
.todo-list { list-style: none; margin: 12px 0 0; padding: 0; }
.todo-list li { display: grid; grid-template-columns: 22px 1fr auto; gap: 10px; align-items: start; padding: 7px 0; border-top: 1px dashed var(--rule); }
.todo-list li:first-child { border-top: 0; }
.check {
  appearance: none; -webkit-appearance: none; margin: 1px 0 0; width: 20px; height: 20px;
  border: 1.5px solid var(--pencil); border-radius: 3px; cursor: pointer; display: grid; place-items: center; background: var(--sheet);
}
.check:checked { border-color: var(--rust); background: var(--rust); }
.check:checked::after { content: ""; width: 5px; height: 10px; border: solid var(--sheet); border-width: 0 2px 2px 0; transform: translateY(-1px) rotate(45deg); }
.todo-text { overflow-wrap: anywhere; }
.todo-text[contenteditable=true] { outline: 2px solid var(--focus); border-radius: 3px; }
.todo-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 3px; font-size: 12.5px; color: var(--pencil); }
.todo-meta .tag { padding: 0 6px; font-size: 12px; border-left: 2px solid var(--c); }
.todo-meta .late { color: var(--red); font-weight: 600; }
.todo-meta .soon { color: var(--ink); font-weight: 600; }
.del { opacity: 0; transition: opacity .12s; }
li:hover .del, li:focus-within .del { opacity: 1; }
.done-box { margin-top: 10px; }
.done-box summary { cursor: pointer; color: var(--pencil); font-size: 13px; }
.done-box .todo-text { text-decoration: line-through; color: var(--pencil); }
.done-box .btn { margin-top: 6px; }
.empty { color: var(--pencil); font-size: 14px; margin: 10px 0 0; }

/* ---------- Exams ---------- */
.exam-list { list-style: none; margin: 0 0 12px; padding: 0; }
.exam-list li { display: grid; grid-template-columns: 58px 1fr auto; gap: 12px; align-items: center; padding: 7px 0; border-top: 1px dashed var(--rule); }
.exam-list li:first-child { border-top: 0; }
.exam-days { font-family: var(--serif); font-size: 26px; line-height: 1; text-align: right; font-variant-numeric: tabular-nums; }
.exam-days small { display: block; font-family: var(--sans); font-size: 11px; color: var(--pencil); margin-top: 3px; }
.exam-list li.near .exam-days { color: var(--rust); }
.exam-list li.past { opacity: .5; }
.exam-name { overflow-wrap: anywhere; }
.exam-name small { display: block; color: var(--pencil); font-size: 12.5px; }

/* ---------- Notes ---------- */
.notes-sheet textarea {
  width: 100%; min-height: 210px; resize: vertical; border: 0; padding: 0 2px; background: transparent;
  font-family: var(--serif); font-size: 16px; line-height: 24px;
  background-image: linear-gradient(transparent 23px, var(--grid) 23px);
  background-size: 100% 24px; background-attachment: local;
}
.notes-sheet textarea:focus { outline: none; }

/* ---------- Stats ---------- */
.stats { list-style: none; margin: 0; padding: 0; display: grid; gap: 9px; }
.stats li { display: grid; gap: 3px; font-size: 13.5px; }
.stats .row { display: flex; justify-content: space-between; gap: 8px; }
.stats .row span:last-child { color: var(--pencil); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stats .bar { height: 10px; }

/* ---------- Links ---------- */
.links { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.links li { display: flex; align-items: center; gap: 8px; }
.links a { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; padding: 5px 4px; border-radius: 6px; flex: 1; min-width: 0; }
.links a:hover { background: color-mix(in srgb, var(--grid) 55%, transparent); }
.links .ini { width: 26px; height: 26px; border-radius: 6px; border: 1px solid var(--rule); display: grid; place-items: center; font-family: var(--serif); font-weight: 600; font-size: 14px; flex: none; }
.links a span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.links .del { opacity: 1; }
#link-form { margin-top: 10px; }

/* ---------- Settings ---------- */
.settings { border: 1px solid var(--rule); border-radius: 10px; padding: 0; width: min(760px, 94vw); max-height: 88vh; background: var(--sheet); color: var(--ink); }
.settings::backdrop { background: rgba(30, 24, 18, .45); }
.settings-inner { padding: 18px 22px 22px; }
.settings-head { display: flex; justify-content: space-between; align-items: center; }
.settings-head h2 { font-size: 24px; font-weight: 500; color: var(--heading); }
.tabs { display: flex; gap: 4px; margin: 14px 0 6px; border-bottom: 1px solid var(--rule); }
.tab { border: 0; background: none; padding: 8px 12px; cursor: pointer; color: var(--pencil); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab[aria-selected=true] { color: var(--heading); border-bottom-color: var(--rust); }
.tab-panel h3 { color: var(--heading); font-size: 17px; margin: 18px 0 4px; }
.hint { color: var(--pencil); font-size: 13.5px; margin: 0 0 10px; max-width: 70ch; }
.hint code { font-size: 12.5px; }
#cliente-id { width: 100%; margin-bottom: 10px; font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.course-list, .name-list, .cal-list { list-style: none; margin: 0 0 10px; padding: 0; }
.course-list li { display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; align-items: start; padding: 8px 0; border-top: 1px dashed var(--rule); }
.swatch { width: 22px; height: 22px; border: 0; border-radius: 4px; cursor: pointer; padding: 0; margin-top: 1px; background: var(--c) !important; }
.course-list .found { font-size: 12.5px; color: var(--pencil); }
.course-list .missing { font-size: 12.5px; color: var(--red); }
.name-list li { display: grid; grid-template-columns: 1fr minmax(180px, 280px); gap: 12px; align-items: center; padding: 6px 0; border-top: 1px dashed var(--rule); font-size: 14px; }
.name-list .n small { color: var(--pencil); margin-left: 6px; }
.name-list .amb { color: var(--red); font-size: 12px; display: block; }
.cal-list li { display: flex; gap: 8px; align-items: center; padding: 4px 0; font-size: 13.5px; }
.cal-list code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .page { grid-template-columns: 1fr; grid-template-areas: "hero" "week" "side" "lower"; }
  .side { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
  .lower { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
}
@media (max-width: 720px) {
  .page { padding: 18px 14px 32px; }
  .hero h1 { font-size: 34px; }
  .clock { display: none; }
  .week { display: none; }
  .agenda { display: block; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
.page > *, .side > *, .lower > * { min-width: 0; }
.add-form input, .add-form select { max-width: 100%; }
@media (max-width: 720px) { .side, .lower { grid-template-columns: 1fr; } }
