/* Muttley - stile di base: colori, tipografia, controlli e struttura dell'app. */

:root {
  color-scheme: light;
  --bg: #f6f6f3;
  --surface: #ffffff;
  --surface-2: #f1f1ed;
  --surface-3: #e8e8e3;
  --border: #e3e3dd;
  --border-strong: #cfcfc8;
  --text: #1b1c1f;
  --text-2: #5a5d64;
  --text-3: #8a8d95;
  --accent: #2f5fd0;
  --accent-hover: #2752b8;
  --accent-ink: #ffffff;
  --accent-soft: #e8eefc;
  --accent-text: #244cab;
  --ok: #1f8a5b;
  --ok-soft: #e3f4ec;
  --warn: #a8690a;
  --warn-soft: #fbf0d9;
  --danger: #c43c3c;
  --danger-soft: #fbe6e6;
  --ai: #7347cc;
  --ai-soft: #f1ebfd;
  --overlay: rgba(22, 24, 30, 0.42);
  --shadow-1: 0 1px 2px rgba(20, 20, 30, 0.06), 0 1px 1px rgba(20, 20, 30, 0.04);
  --shadow-2: 0 10px 32px rgba(20, 20, 30, 0.16), 0 2px 6px rgba(20, 20, 30, 0.08);
  --radius-sm: 7px;
  --radius: 10px;
  --radius-lg: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  --nav-w: 212px;
  --tabbar-h: 56px;
  --hour-h: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="chiaro"]) {
    color-scheme: dark;
    --bg: #15171a;
    --surface: #1d2024;
    --surface-2: #262a2f;
    --surface-3: #31363c;
    --border: #2d3136;
    --border-strong: #42474e;
    --text: #e9eaec;
    --text-2: #a7abb3;
    --text-3: #787d86;
    --accent: #7fa2f5;
    --accent-hover: #96b3f8;
    --accent-ink: #0f1420;
    --accent-soft: #222e49;
    --accent-text: #a9c0f8;
    --ok: #5cc496;
    --ok-soft: #1b3228;
    --warn: #e0a84a;
    --warn-soft: #392d16;
    --danger: #ef7f7f;
    --danger-soft: #3c2121;
    --ai: #b49af2;
    --ai-soft: #2b2440;
    --overlay: rgba(0, 0, 0, 0.55);
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-2: 0 12px 36px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3);
  }
}

:root[data-theme="scuro"] {
  color-scheme: dark;
  --bg: #15171a;
  --surface: #1d2024;
  --surface-2: #262a2f;
  --surface-3: #31363c;
  --border: #2d3136;
  --border-strong: #42474e;
  --text: #e9eaec;
  --text-2: #a7abb3;
  --text-3: #787d86;
  --accent: #7fa2f5;
  --accent-hover: #96b3f8;
  --accent-ink: #0f1420;
  --accent-soft: #222e49;
  --accent-text: #a9c0f8;
  --ok: #5cc496;
  --ok-soft: #1b3228;
  --warn: #e0a84a;
  --warn-soft: #392d16;
  --danger: #ef7f7f;
  --danger-soft: #3c2121;
  --ai: #b49af2;
  --ai-soft: #2b2440;
  --overlay: rgba(0, 0, 0, 0.55);
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-2: 0 12px 36px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

h1, h2, h3, h4, p { margin: 0; }
h1 { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; text-wrap: balance; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 13px; font-weight: 650; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.05em; }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--accent-soft); }
.icon { flex: none; display: block; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.nowrap { white-space: nowrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 8px; }
.spacer { flex: 1; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tnum { font-variant-numeric: tabular-nums; }

.boot {
  height: 100%;
  display: grid;
  place-items: center;
  color: var(--text-3);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

/* ---- Pulsanti ---------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  font-size: 14px;
  white-space: nowrap;
  user-select: none;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { background: var(--surface-3); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.ai { color: var(--ai); border-color: color-mix(in srgb, var(--ai) 35%, var(--border)); }
.btn.ai:hover { background: var(--ai-soft); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: none;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-2);
  transition: background 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:active { background: var(--surface-3); }
.icon-btn.on { background: var(--accent-soft); color: var(--accent-text); }
.icon-btn:disabled { opacity: 0.4; cursor: default; background: transparent; }
.icon-btn.sm { width: 28px; height: 28px; }

/* Selettore a segmenti */
.seg {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.seg button {
  border: 0;
  background: transparent;
  height: 28px;
  padding: 0 11px;
  border-radius: 5px;
  color: var(--text-2);
  font-size: 13px;
  font-weight: 550;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }

/* ---- Campi ------------------------------------------------------------- */

.input, .textarea, .select {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  padding: 7px 10px;
  min-height: 36px;
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.input:focus, .textarea:focus, .select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.textarea { resize: vertical; min-height: 80px; line-height: 1.5; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > label, .label { font-size: 13px; font-weight: 600; color: var(--text-2); }
.field .hint { font-size: 12px; color: var(--text-3); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.check input { width: 17px; height: 17px; accent-color: var(--accent); margin: 0; }

/* ---- Elementi comuni --------------------------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
}
.card.pad { padding: 14px 16px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 550;
  white-space: nowrap;
  text-decoration: none;
}
a.chip:hover, button.chip:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.chip.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); }
.chip.ai { background: var(--ai-soft); border-color: transparent; color: var(--ai); }
.chip.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.chip.danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.chip.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.chip > span { overflow: hidden; text-overflow: ellipsis; }

.avatar {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: hsl(var(--h, 220) 45% 32%);
  background: hsl(var(--h, 220) 60% 90%);
}
.avatar.lg { width: 44px; height: 44px; font-size: 16px; }
:root[data-theme="scuro"] .avatar { color: hsl(var(--h, 220) 70% 84%); background: hsl(var(--h, 220) 30% 28%); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="chiaro"]) .avatar { color: hsl(var(--h, 220) 70% 84%); background: hsl(var(--h, 220) 30% 28%); }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 36px 20px;
  text-align: center;
  color: var(--text-3);
}
.empty .icon { opacity: 0.6; }
.empty p { max-width: 42ch; }

.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: inherit;
  text-decoration: none;
  min-width: 0;
}
a.list-row:hover, button.list-row:hover { background: var(--surface-2); text-decoration: none; }
button.list-row { border: 0; background: transparent; width: 100%; text-align: left; }
.list-row .title { font-weight: 550; }
.divider { height: 1px; background: var(--border); margin: 4px 0; }

.kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text-2);
  background: var(--surface);
}

/* ---- Finestre di dialogo ---------------------------------------------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--overlay);
  display: grid;
  place-items: center;
  padding: 16px;
  animation: fade 0.12s ease-out;
}
.dialog {
  width: min(560px, 100%);
  max-height: min(92dvh, 100%);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  animation: pop 0.14s ease-out;
}
.dialog.wide { width: min(760px, 100%); }
.dialog-head { display: flex; align-items: center; gap: 8px; padding: 14px 14px 10px 18px; }
.dialog-head h2 { flex: 1; font-size: 17px; }
.dialog-body { padding: 4px 18px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.dialog-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px; border-top: 1px solid var(--border); }

@media (max-width: 640px) {
  .overlay { padding: 0; place-items: end stretch; }
  .dialog, .dialog.wide {
    width: 100%;
    max-height: 94dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: 0;
    padding-bottom: var(--safe-bottom);
    animation: slide-up 0.18s ease-out;
  }
}

.menu {
  position: absolute;
  z-index: 60;
  min-width: 190px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
}
.menu button, .menu a {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-size: 14px;
  text-decoration: none;
}
.menu button:hover, .menu a:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }

.toasts {
  position: fixed;
  z-index: 200;
  left: 50%;
  bottom: calc(18px + var(--safe-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  width: min(520px, calc(100% - 24px));
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: var(--shadow-2);
  animation: pop 0.16s ease-out;
}
.toast.errore { background: var(--danger); color: #fff; }
.toast button { border: 0; background: transparent; color: inherit; font-weight: 700; text-decoration: underline; padding: 0; }

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: scale(0.97); } }
@keyframes slide-up { from { transform: translateY(24px); opacity: 0; } }
@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 0.9s linear infinite; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---- Struttura: barra laterale (desktop/tablet) e barra in basso (telefono) ---- */

.shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: var(--nav-w) minmax(0, 1fr);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: calc(12px + var(--safe-top)) 10px 12px;
  border-right: 1px solid var(--border);
  background: var(--bg);
  min-height: 0;
  overflow-y: auto;
}
.nav-brand { display: flex; align-items: center; gap: 9px; padding: 4px 8px 12px; font-weight: 700; font-size: 16px; letter-spacing: -0.01em; }
.nav-brand .logo { width: 24px; height: 24px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 36px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-2);
  font-weight: 550;
  font-size: 14px;
  text-decoration: none;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }
.nav-item.on .icon { color: var(--accent); }
.nav-item .badge {
  margin-left: auto;
  min-width: 20px;
  height: 18px;
  padding: 0 6px;
  border-radius: 9px;
  background: var(--surface-3);
  color: var(--text-2);
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.nav-item .badge.ai { background: var(--ai-soft); color: var(--ai); }
.nav-search { color: var(--text-3); border: 1px solid var(--border); background: var(--surface); margin-bottom: 8px; }
.nav-search .kbd { margin-left: auto; }
.nav-foot { margin-top: auto; padding-top: 10px; display: flex; flex-direction: column; gap: 2px; }

.sync-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 0;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-3);
  font-size: 12.5px;
  text-align: left;
  width: 100%;
}
.sync-pill:hover { background: var(--surface-2); color: var(--text-2); }
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); flex: none; }
.sync-pill.ok .dot { background: var(--ok); }
.sync-pill.sincronizzo .dot { background: var(--accent); }
.sync-pill.offline .dot, .sync-pill.nessuno .dot { background: var(--warn); }
.sync-pill.errore .dot, .sync-pill.accesso .dot { background: var(--danger); }

.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; position: relative; }

.tabbar { display: none; }

/* Pagina standard: intestazione fissa + contenuto che scorre */
.page { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + var(--safe-top)) 20px 12px;
  min-height: 58px;
  flex: none;
}
.page-head h1 { flex: 1; min-width: 0; }
.page-head .sub { color: var(--text-2); font-size: 13px; }
.page-body { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 20px 32px; -webkit-overflow-scrolling: touch; }
.page-body.narrow > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.section { display: flex; flex-direction: column; gap: 8px; margin-bottom: 22px; }
.section-head { display: flex; align-items: center; gap: 8px; min-height: 28px; }
.section-head h3 { flex: 1; }

/* Tablet: barra laterale stretta con sole icone */
@media (max-width: 1080px) and (min-width: 721px) {
  :root { --nav-w: 64px; }
  .nav { padding-left: 8px; padding-right: 8px; align-items: center; }
  .nav-brand span, .nav-item span, .nav-item .kbd, .sync-pill span { display: none; }
  .nav-brand { padding: 4px 0 12px; }
  .nav-item { width: 44px; height: 44px; justify-content: center; padding: 0; position: relative; }
  .nav-item .badge { position: absolute; top: 3px; right: 1px; min-width: 16px; height: 16px; font-size: 10px; padding: 0 4px; }
  .sync-pill { justify-content: center; width: 44px; }
}

/* Telefono: barra in basso */
@media (max-width: 720px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .nav { display: none; }
  .tabbar {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    border-top: 1px solid var(--border);
    background: var(--surface);
  }
  .tabbar a, .tabbar button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    border: 0;
    background: transparent;
    color: var(--text-3);
    font-size: 10.5px;
    font-weight: 600;
    text-decoration: none;
    position: relative;
  }
  .tabbar a.on { color: var(--accent); }
  .tabbar .badge {
    position: absolute;
    top: 6px;
    left: calc(50% + 6px);
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    background: var(--ai);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: grid;
    place-items: center;
  }
  .page-head { padding: calc(10px + var(--safe-top)) 14px 10px; min-height: 52px; }
  .page-body { padding: 2px 14px 28px; }
  h1 { font-size: 20px; }
  .hide-phone { display: none !important; }
}
@media (min-width: 721px) {
  .only-phone { display: none !important; }
}

/* ---- Testo formattato (Markdown) -------------------------------------- */

.md { line-height: 1.6; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md h1, .md h2, .md h3, .md h4 { margin: 18px 0 8px; text-transform: none; letter-spacing: -0.005em; color: var(--text); }
.md h1 { font-size: 20px; }
.md h2 { font-size: 17px; }
.md h3 { font-size: 15px; }
.md h4 { font-size: 14px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md li.task { list-style: none; margin-left: -22px; display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px; }
.md li.task > input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); flex: none; transform: translateY(2px); }
.md li.task > span { flex: 1; min-width: 0; }
.md li.task > ul, .md li.task > ol { flex-basis: 100%; margin: 0; padding-left: 46px; }
.md li.task.done > span { color: var(--text-3); text-decoration: line-through; }
.md blockquote { margin: 0 0 10px; padding: 2px 0 2px 14px; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.md code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.md pre { margin: 0 0 10px; padding: 10px 12px; background: var(--surface-2); border-radius: var(--radius-sm); overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.md img.md-img { display: block; max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); margin: 6px 0; min-height: 24px; background: var(--surface-2); }
.md mark { background: color-mix(in srgb, #f5d547 55%, transparent); color: inherit; border-radius: 2px; padding: 0 2px; }
.md .wikilink { color: var(--accent-text); background: var(--accent-soft); padding: 0 5px; border-radius: 4px; font-weight: 550; }
.md .wikilink.missing { color: var(--text-2); background: var(--surface-2); }
.md-table { overflow-x: auto; margin: 0 0 10px; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.md th { background: var(--surface-2); font-weight: 600; }
