/* ============ Colori e temi ============ */
:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #000000;
  --text2: #6c6c70;
  --sep: rgba(60, 60, 67, 0.2);
  --fill: rgba(120, 120, 128, 0.12);
  --fill-strong: rgba(120, 120, 128, 0.2);
  --accent: #007aff;
  --danger: #ff3b30;
  --green: #34c759;
  --tabbar: rgba(249, 249, 249, 0.92);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --text2: #98989f;
    --sep: rgba(84, 84, 88, 0.6);
    --fill: rgba(118, 118, 128, 0.24);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --accent: #0a84ff;
    --danger: #ff453a;
    --green: #30d158;
    --tabbar: rgba(22, 22, 22, 0.92);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
  }
}

/* ============ Base ============ */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
small { color: var(--text2); font-size: 13px; }

/* ============ Intestazione ============ */
.topbar {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 14px) 20px 2px;
}
.topbar h1 { margin: 0; font-size: 34px; line-height: 1.15; font-weight: 700; letter-spacing: 0.01em; }
.subtitle { margin: 2px 0 0; color: var(--text2); font-size: 15px; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 130px);
}

.section-title {
  margin: 24px 16px 6px;
  color: var(--text2);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.spacer { height: 16px; }

/* ============ Liste di eventi ============ */
.list { list-style: none; margin: 0; padding: 0; background: var(--card); border-radius: 12px; overflow: hidden; }
.item { position: relative; display: flex; align-items: flex-start; gap: 10px; padding: 11px 14px 11px 8px; cursor: pointer; }
.item + .item::before { content: ""; position: absolute; top: 0; left: 50px; right: 0; border-top: 0.5px solid var(--sep); }
.item:active { background: var(--fill); }

.check { flex: none; display: grid; place-items: center; width: 40px; height: 40px; margin: -9px -4px -9px 0; }
.ring { display: grid; place-items: center; width: 23px; height: 23px; border-radius: 50%; border: 2px solid var(--c); transition: background 0.15s; }
.ring svg { width: 14px; height: 14px; opacity: 0; transition: opacity 0.15s; }
.item.done .ring { background: var(--c); }
.item.done .ring svg { opacity: 1; }

.body { flex: 1; min-width: 0; }
.title { overflow-wrap: anywhere; }
.item.done .title { color: var(--text2); text-decoration: line-through; }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 10px; margin-top: 2px; color: var(--text2); font-size: 13px; }
.meta span { display: inline-flex; align-items: center; gap: 3px; }
.meta svg { width: 12px; height: 12px; }
.meta .late { color: var(--danger); font-weight: 500; }
.notes { margin-top: 2px; overflow: hidden; color: var(--text2); font-size: 14px; white-space: nowrap; text-overflow: ellipsis; }

.empty { padding: 28px 20px; border-radius: 12px; background: var(--card); color: var(--text2); font-size: 15px; text-align: center; }
.empty strong { display: block; margin-bottom: 4px; color: var(--text); font-size: 17px; font-weight: 600; }

.banner { display: flex; align-items: flex-start; gap: 12px; margin-top: 16px; padding: 12px 8px 12px 14px; border-radius: 12px; background: var(--card); font-size: 15px; }
.banner p { flex: 1; margin: 0; }
.banner .icon-btn { color: var(--text2); width: 28px; height: 28px; }

/* ============ Pulsanti ============ */
.fab {
  position: fixed;
  right: max(20px, calc(50% - 340px));
  bottom: calc(env(safe-area-inset-bottom) + 70px);
  z-index: 20;
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  transition: transform 0.15s;
}
.fab:active { transform: scale(0.92); }
.fab svg { width: 26px; height: 26px; }

.icon-btn { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; color: var(--accent); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:active { background: var(--fill); }
.text-btn { padding: 6px 4px; color: var(--accent); font-size: 17px; }
.text-btn.strong { font-weight: 600; }

/* ============ Barra delle schede ============ */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 6px 0 calc(env(safe-area-inset-bottom) + 4px);
  background: var(--tabbar);
  border-top: 0.5px solid var(--sep);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 2px 0; color: var(--text2); font-size: 10px; font-weight: 500; }
.tab svg { width: 26px; height: 26px; }
.tab[aria-selected="true"] { color: var(--accent); }

/* ============ Calendario ============ */
.cal { margin-top: 14px; padding: 8px 8px 6px; border-radius: 12px; background: var(--card); touch-action: pan-y; }
.cal-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px 6px 10px; }
.cal-head h2 { margin: 0; font-size: 19px; font-weight: 600; }
.cal-nav { display: flex; align-items: center; gap: 2px; }
.cal-nav .text-btn { margin-right: 6px; font-size: 15px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.cal-wd { padding: 4px 0; color: var(--text2); font-size: 12px; font-weight: 600; text-align: center; }
.day { display: flex; flex-direction: column; align-items: center; gap: 1px; min-height: 48px; padding: 2px 0 4px; }
.day .n { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; font-size: 17px; }
.day.other { opacity: 0.35; }
.day.today .n { color: var(--accent); font-weight: 600; }
.day.sel .n { background: var(--text); color: var(--bg); font-weight: 600; }
.day.sel.today .n { background: var(--accent); color: #fff; }
.dots { display: flex; gap: 3px; height: 6px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--c); }

/* ============ Gruppi e righe (impostazioni, modulo) ============ */
.group { overflow: hidden; border-radius: 12px; background: var(--card); }
.row { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 46px; padding: 6px 16px; }
.row + .row::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 0.5px solid var(--sep); }
.row.link { width: 100%; color: var(--accent); text-align: left; }
.row.link:active { background: var(--fill); }
.row .value { color: var(--text2); }
.danger { color: var(--danger) !important; }
.footnote { margin: 6px 16px 0; color: var(--text2); font-size: 13px; }
.sep { height: 0.5px; margin-left: 16px; background: var(--sep); }

.row input[type="date"],
.row input[type="time"],
.row select {
  -webkit-appearance: none;
  appearance: none;
  min-width: 96px;
  min-height: 34px;
  max-width: 62%;
  padding: 6px 10px;
  border: 0;
  border-radius: 8px;
  background: var(--fill);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  text-align: center;
}
.row select { text-align-last: center; }

.field { display: block; width: 100%; padding: 12px 16px; border: 0; outline: none; background: transparent; color: var(--text); font: inherit; font-size: 17px; }
.field::placeholder { color: var(--text2); opacity: 0.8; }
textarea.field { min-height: 84px; resize: none; }

.switch {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 31px;
  background: var(--fill-strong);
  transition: background 0.2s;
  cursor: pointer;
}
.switch::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s;
}
.switch:checked { background: var(--green); }
.switch:checked::before { transform: translateX(20px); }

.colors { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px 8px; padding: 14px 16px; }
.colors label { position: relative; }
.colors input { position: absolute; inset: 0; margin: 0; opacity: 0; }
.colors span { display: block; width: 30px; height: 30px; border-radius: 50%; background: var(--c); }
.colors input:checked + span { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--c); }
.colors input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 4px; }

/* ============ Foglio di modifica ============ */
dialog.sheet {
  position: fixed;
  inset: auto 0 0 0;
  width: 100%;
  max-width: 640px;
  max-height: calc(100dvh - env(safe-area-inset-top) - 10px);
  margin: 0 auto;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 14px 14px 0 0;
  background: var(--bg);
  color: var(--text);
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: sheet-up 0.3s cubic-bezier(0.2, 0.9, 0.3, 1); }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }
.sheet form { display: flex; flex: 1; flex-direction: column; min-height: 0; }
.sheet-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 12px 16px 8px; }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; outline: none; }
.sheet-head .left { justify-self: start; }
.sheet-head .right { justify-self: end; }
.sheet-body { overflow-y: auto; padding: 0 16px calc(env(safe-area-inset-bottom) + 28px); }
.sheet-body .group { margin-top: 18px; }
.sheet-body .group:first-child { margin-top: 6px; }

@keyframes sheet-up { from { transform: translateY(100%); } }

/* ============ Notifiche nell'app ============ */
#toasts {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  pointer-events: none;
}
.toast {
  width: 100%;
  max-width: 420px;
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 15px;
  pointer-events: auto;
  animation: toast-in 0.3s ease;
  transition: opacity 0.25s, transform 0.25s;
}
.toast b { display: block; margin-bottom: 1px; }
.toast.out { opacity: 0; transform: translateY(-10px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-14px); } }
