:root {
  --bg: #f4f5f8;
  --card: #ffffff;
  --text: #15171c;
  --muted: #7b808c;
  --line: #e3e5ea;
  --line-strong: #cfd3da;
  --soft: #eceef2;
  --accent: #f29a1f;
  --ok: #2fb36b;
  --ok-bg: #dcf5e6;
  --warn: #e8a317;
  --warn-bg: #fdf0cf;
  --bad: #e5484d;
  --bad-bg: #fde0e0;
  --sun-bg: #fff6e3;
  --V: #ef6a5b;
  --R: #3d86f2;
  --radius: 14px;
  --sa-top: env(safe-area-inset-top, 0px);
  --sa-bot: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f14;
    --card: #181b22;
    --text: #f1f2f5;
    --muted: #8a909c;
    --line: #262a33;
    --line-strong: #353a45;
    --soft: #20242c;
    --ok-bg: #143224;
    --warn-bg: #3a2e10;
    --bad-bg: #3d1719;
    --sun-bg: #2a2414;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.25 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
input, select { font: inherit; font-size: 16px; color: var(--text); -webkit-user-select: text; user-select: text; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column;
  padding: var(--sa-top) 0 var(--sa-bot); }

/* ---------- Login ---------- */
#login { justify-content: center; align-items: center; }
.login-box { width: 100%; max-width: 380px; padding: 24px; text-align: center; }
.logo { width: 104px; height: 104px; border-radius: 24px; box-shadow: 0 10px 24px -10px rgba(216,27,122,.6); }
.login-box h1 { font-size: 26px; margin: 8px 0 4px; }
.login-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 22px; }
.login-grid .grp { grid-column: 1 / -1; text-align: left; font-size: 13px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em; margin-top: 6px; }
.login-grid button { height: 76px; border-radius: 18px; color: #fff; font-size: 20px; font-weight: 600;
  box-shadow: 0 6px 16px -8px rgba(0,0,0,.4); }

/* ---------- Topbar ---------- */
.topbar { display: flex; align-items: center; gap: 4px; padding: 6px 10px 4px; position: relative; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; font-size: 28px; line-height: 1;
  display: grid; place-items: center; color: var(--text); }
.icon-btn:active { background: var(--soft); }
.gear { font-size: 22px; margin-left: 4px; background: var(--card); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.day-label { flex: 1; display: flex; flex-direction: column; align-items: center; }
.day-label span { font-size: 18px; font-weight: 700; text-transform: capitalize; }
.day-label small { font-size: 12px; color: var(--muted); }
.hidden-date { position: absolute; opacity: 0; pointer-events: none; left: 50%; top: 40px; width: 1px; height: 1px; }

/* ---------- Board ---------- */
.colheads, .board { display: grid; grid-template-columns: 38px 1fr 58px 1fr; column-gap: 4px; padding: 0 8px; }
.colhead { text-align: center; font-weight: 700; font-size: 15px; padding: 6px 0;
  border-radius: 10px 10px 0 0; color: #fff; }
#headV { background: var(--V); }
#headR { background: var(--R); }
.colhead.mid { color: var(--muted); font-size: 11px; font-weight: 600; line-height: 1.1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 2px 0; }
.colhead.mid b { color: var(--accent); font-size: 13px; }
.board { flex: 1; min-height: 0; position: relative; padding-bottom: 4px; }
.times, .col { display: grid; grid-template-rows: repeat(18, 1fr); position: relative; min-height: 0; }
.times div { font-size: 11px; color: var(--muted); text-align: right; padding-right: 4px;
  transform: translateY(-7px); font-variant-numeric: tabular-nums; }
.times div.half { opacity: .45; font-size: 9.5px; }
.col { background: var(--card); }
#colV { border-radius: 0 0 12px 12px; }
#colR { border-radius: 0 0 12px 12px; }
.col .slot { border-top: 1px solid var(--line); }
.col .slot.hour { border-top-color: var(--line-strong); }
.col .slot:active { background: var(--soft); }

.col.mid { background: transparent; gap: 0; }
.sum { margin: 1px 0; border-radius: 8px; display: flex; flex-direction: column; align-items: center;
  justify-content: center; line-height: 1; background: var(--sun-bg); }
.sum b { font-size: 14px; font-variant-numeric: tabular-nums; }
.sum small { font-size: 9.5px; color: var(--muted); margin-top: 1px; font-variant-numeric: tabular-nums; }
.sum.ok { background: var(--ok-bg); } .sum.ok b { color: var(--ok); }
.sum.warn { background: var(--warn-bg); } .sum.warn b { color: var(--warn); }
.sum.bad { background: var(--bad-bg); } .sum.bad b { color: var(--bad); }
.sum.empty small { font-size: 10.5px; }

.bk { position: absolute; left: 2px; right: 2px; border-radius: 9px; color: #fff; padding: 3px 6px;
  overflow: hidden; display: flex; flex-direction: column; justify-content: flex-start;
  box-shadow: 0 2px 6px -3px rgba(0,0,0,.45); border: 1.5px solid rgba(255,255,255,.55); z-index: 2; }
.bk:active { filter: brightness(.92); }
.bk .t { font-size: 13px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bk .s { font-size: 11px; opacity: .92; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bk.short { flex-direction: row; align-items: center; gap: 4px; }
.bk.short .s { display: none; }
.bk .rep { font-size: 10px; opacity: .9; }

.now-line { position: absolute; left: 44px; right: 8px; height: 2px; background: var(--bad); z-index: 5;
  pointer-events: none; }
.now-line::before { content: ''; position: absolute; left: -5px; top: -4px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--bad); }

/* ---------- Bottom ---------- */
.bottombar { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px 4px; gap: 10px; }
.legend { display: flex; gap: 10px; font-size: 11.5px; color: var(--muted); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.bad { background: var(--bad); }
.add-btn { background: var(--accent); color: #fff; font-weight: 700; font-size: 17px; padding: 13px 22px;
  border-radius: 16px; box-shadow: 0 8px 18px -8px rgba(242,154,31,.8); }
.add-btn:active { transform: scale(.97); }

/* ---------- Sheets ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; flex-direction: column;
  padding: var(--sa-top) 0 var(--sa-bot); animation: up .22s ease-out; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-bar { display: flex; align-items: center; padding: 10px 14px; }
.sheet-bar strong { font-size: 17px; flex: 0 0 auto; }
.sheet-bar .link, .sheet-bar .spacer { flex: 1; }
.link { color: var(--accent); font-size: 17px; text-align: left; }
.link.right { text-align: right; font-weight: 600; }
.sheet-body { flex: 1; min-height: 0; padding: 0 14px; display: flex; flex-direction: column; overflow: auto; }
.sheet-body.scroll { display: block; padding-bottom: 30px; }
.label { font-size: 12.5px; font-weight: 600; color: var(--muted); text-transform: uppercase;
  letter-spacing: .05em; margin: 10px 2px 6px; }
.sheet-foot { display: flex; gap: 10px; padding: 8px 14px 6px; }
.primary, .danger, .secondary { height: 52px; border-radius: 15px; font-weight: 700; font-size: 17px; flex: 1; }
.primary { background: var(--accent); color: #fff; }
.primary:disabled { opacity: .4; }
.danger { background: var(--bad-bg); color: var(--bad); flex: 0 0 34%; }
.danger.small { height: 36px; font-size: 14px; padding: 0 14px; flex: none; border-radius: 10px; }
.secondary { background: var(--card); color: var(--accent); width: 100%; margin-top: 8px; }
.card { background: var(--card); border-radius: var(--radius); padding: 10px 12px; }

/* Utenti */
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.seg button { height: 42px; border-radius: 12px; background: var(--card); font-weight: 650; font-size: 15px;
  border: 2px solid transparent; }
.seg button.on { color: #fff; }

/* Tile app-icon */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr);
  grid-auto-columns: calc((100% - 30px) / 4); gap: 8px 10px; overflow-x: auto; scroll-snap-type: x mandatory;
  padding: 2px 0 4px; scrollbar-width: none; }
.tiles.many { grid-auto-flow: column; grid-template-rows: repeat(2, auto); grid-template-columns: none; }
.tiles::-webkit-scrollbar { display: none; }
.tile { display: flex; flex-direction: column; align-items: center; gap: 3px; scroll-snap-align: start; min-width: 0; }
.tile .ic { width: 58px; height: 58px; border-radius: 15px; display: grid; place-items: center; font-size: 30px;
  background: linear-gradient(160deg, var(--card), var(--soft)); box-shadow: 0 2px 8px -3px rgba(0,0,0,.25);
  border: 2.5px solid transparent; position: relative; }
.tile.on .ic { border-color: var(--accent); }
.tile .nm { font-size: 11.5px; font-weight: 600; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .sp { font-size: 10px; color: var(--muted); white-space: nowrap; }
.tile .who { position: absolute; right: -4px; top: -4px; width: 17px; height: 17px; border-radius: 50%;
  font-size: 9.5px; font-weight: 700; color: #fff; display: grid; place-items: center; border: 1.5px solid var(--bg); }

/* Anteprima */
.preview { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.two > * { flex: 1; min-width: 0; }
.name-row input { flex: 1; height: 42px; border: 0; background: var(--soft); border-radius: 11px; padding: 0 12px; font-weight: 600; }
.emoji-btn { width: 42px; height: 42px; border-radius: 11px; background: var(--soft); font-size: 24px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.wide { flex: 1; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field > span em { font-style: normal; color: var(--text); font-weight: 500; }
.field input[type=date], .field select { height: 40px; border: 0; background: var(--soft); border-radius: 11px;
  padding: 0 10px; width: 100%; -webkit-appearance: none; appearance: none; font-weight: 600; text-align: center; }
.stepper { display: flex; align-items: center; background: var(--soft); border-radius: 11px; height: 40px; }
.stepper button { width: 38px; height: 40px; font-size: 22px; color: var(--accent); font-weight: 600; }
.stepper input, .stepper output { flex: 1; min-width: 0; text-align: center; font-weight: 700; border: 0; background: none; font-size: 16px; }
.days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.days button { height: 34px; border-radius: 10px; background: var(--soft); font-weight: 650; font-size: 14px; }
.days button.on { background: var(--accent); color: #fff; }
.impact { border-radius: 11px; padding: 8px 10px; font-size: 13.5px; font-weight: 600; background: var(--soft); }
.impact.ok { background: var(--ok-bg); color: var(--ok); }
.impact.warn { background: var(--warn-bg); color: #a77208; }
.impact.bad { background: var(--bad-bg); color: var(--bad); }
@media (prefers-color-scheme: dark) { .impact.warn { color: var(--warn); } }

/* Impostazioni */
.me { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; font-size: 16px; }
.plist { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 6px; }
.plist h4 { margin: 0; padding: 8px 12px 4px; font-size: 13px; }
.pitem { display: flex; align-items: center; gap: 10px; padding: 8px 12px; width: 100%; text-align: left;
  border-top: 1px solid var(--line); }
.pitem .ic { font-size: 24px; width: 34px; text-align: center; }
.pitem .nm { flex: 1; font-weight: 600; }
.pitem .sp { color: var(--muted); font-size: 13px; }
.pitem.empty { color: var(--muted); font-size: 13px; }
.form-grid { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.form-grid label { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 14.5px; }
.form-grid label input { width: 46%; height: 38px; border: 0; background: var(--soft); border-radius: 10px;
  padding: 0 10px; text-align: right; font-weight: 600; }
.form-grid .field { flex-direction: row; justify-content: space-between; align-items: center; }
.form-grid .field > span { font-size: 14.5px; color: var(--text); font-weight: 400; }
.form-grid .stepper { width: 46%; }
.note { font-size: 12.5px; color: var(--muted); padding: 0 4px; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.emoji-grid button { aspect-ratio: 1; border-radius: 11px; background: var(--card); font-size: 22px; border: 2px solid transparent; }
.emoji-grid button.on { border-color: var(--accent); }

/* Action sheet */
.action-sheet { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); display: flex;
  align-items: flex-end; padding: 10px 10px calc(10px + var(--sa-bot)); animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.as-box { width: 100%; display: flex; flex-direction: column; gap: 8px; }
.as-box > div, .as-cancel { background: var(--card); border-radius: 16px; overflow: hidden; }
.as-title { padding: 12px; text-align: center; color: var(--muted); font-size: 13px; border-bottom: 1px solid var(--line); }
#asButtons button { display: block; width: 100%; height: 54px; font-size: 18px; color: var(--accent); border-top: 1px solid var(--line); }
#asButtons button:first-child { border-top: 0; }
#asButtons button.red { color: var(--bad); }
#asButtons .emoji-grid { padding: 10px; }
.as-cancel { height: 56px; font-size: 18px; font-weight: 700; color: var(--accent); }

.toast { position: fixed; left: 50%; bottom: calc(90px + var(--sa-bot)); transform: translateX(-50%); z-index: 50;
  background: rgba(20,22,28,.92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: 90%; text-align: center; }

/* Notifiche */
.notif { display: flex; flex-direction: column; gap: 2px; }
.notif .note { margin: 4px 0; }
.primary.full { width: 100%; }
.switch-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); position: relative; }
.switch-row span { flex: 1; display: flex; flex-direction: column; }
.switch-row small { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch-row .sw { width: 51px; height: 31px; border-radius: 16px; background: var(--line-strong); position: relative; flex: none; transition: background .2s; }
.switch-row .sw::after { content: ''; position: absolute; left: 2px; top: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.25); transition: transform .2s; }
.switch-row input:checked + .sw { background: var(--ok); }
.switch-row input:checked + .sw::after { transform: translateX(20px); }
.notif-actions { display: flex; justify-content: space-between; padding-top: 8px; }
.notif-actions .link { flex: none; font-size: 16px; }
.link.red { color: var(--bad); }

.calib-row { display: flex; align-items: center; gap: 10px; font-size: 13px; padding: 6px 0; border-top: 1px solid var(--line); }
.calib-row span:first-child { color: var(--muted); width: 72px; flex: none; }
.calib-row span:nth-child(2) { flex: 1; }
.calib-row .link { flex: none; font-size: 15px; }
