/* Tema scuro = default (:root). Tema chiaro = [data-theme="light"]. */
:root {
  --bg: #0f1216;
  --bg2: #10151b;
  --card: #171c22;
  --fg: #e6e9ee;
  --muted: #9aa4b2;
  --accent: #4ea1ff;
  --on-accent: #06121f;
  --line: #222a33;
  --chip: #232b34;
  --danger: #ff6b6b;
  --warn: #ffb454;
  --ok: #57c98a;
  --ev-bg: #283445;
  --ev-fg: #dce8f7;
}
:root[data-theme="light"] {
  --bg: #eef1f5;
  --bg2: #e7ecf2;
  --card: #ffffff;
  --fg: #1b2330;
  --muted: #5c6776;
  --accent: #1f6fd6;
  --on-accent: #ffffff;
  --line: #d6dce4;
  --chip: #eaeef3;
  --danger: #cf4040;
  --warn: #9c6a12;
  --ok: #2c8f5a;
  --ev-bg: #dceaff;
  --ev-fg: #163a66;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, "Segoe UI", sans-serif; background: var(--bg); color: var(--fg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar { display: flex; flex-direction: column; gap: .45rem; padding: .55rem 1rem; border-bottom: 1px solid var(--line); }
.topbar-main { display: flex; align-items: center; gap: .55rem; }
.brand a { color: var(--fg); font-weight: 600; }
.ver { color: var(--muted); font-size: .72rem; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: .55rem; }
.bell { position: relative; font-size: 1.15rem; line-height: 1; text-decoration: none; }
.bell:hover { text-decoration: none; }
.bell .nbadge { position: absolute; top: -7px; right: -9px; }
.nav { display: flex; gap: 1.1rem; flex-wrap: wrap; }
.nav a { color: var(--muted); }
.icon-btn { padding: .2rem .45rem; line-height: 1; }

main { padding: 1.4rem; max-width: 1000px; margin: 0 auto; }
h1 { margin: .2rem 0 1rem; font-size: 1.5rem; }
h2 { font-size: 1.1rem; margin: .2rem 0; }
h3 { font-size: .95rem; margin: 1.1rem 0 .5rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

.muted { color: var(--muted); }
.sm { font-size: .8rem; }
.danger-t { color: var(--danger); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1.2rem; margin-bottom: 1rem; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 720px){ .cols { grid-template-columns: 1fr; } }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: .8rem; margin-bottom: 1rem; }
@media (max-width: 720px){ .kpis { grid-template-columns: repeat(2,1fr); } }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; text-align: center; color: var(--fg); }
.kpi:hover { border-color: var(--accent); text-decoration: none; }
.kpi-num { font-size: 1.9rem; font-weight: 700; }
.kpi-lbl { color: var(--muted); font-size: .8rem; }
.kpi.danger .kpi-num { color: var(--danger); }
.kpi.warn .kpi-num { color: var(--warn); }

/* liste */
.list { list-style: none; padding: 0; margin: .3rem 0; }
.list li { padding: .4rem 0; border-bottom: 1px solid var(--line); display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.list li:last-child { border-bottom: none; }

/* badge */
.badge { display: inline-block; padding: .1rem .45rem; border-radius: 6px; font-size: .72rem; background: var(--chip); color: var(--fg); }
.badge.big { font-size: .85rem; padding: .25rem .6rem; }
.fascia-alta { background: #4a1f24; color: #ff9ea4; }
.fascia-media { background: #4a3a1f; color: #ffd28a; }
.fascia-bassa { background: #20303a; color: #93c7e0; }
.stato-aperta { background: #1f3a4a; color: #93c7e0; }
.stato-sospesa { background: #4a3a1f; color: #ffd28a; }
.stato-chiusa { background: #2a2f36; color: var(--muted); }
.badge.tipo { background: var(--ev-bg); color: var(--ev-fg); }
.badge.done { background: #1f3a2a; color: var(--ok); }

/* override badge semantici per tema chiaro */
:root[data-theme="light"] .fascia-alta { background: #fde2e4; color: #a23440; }
:root[data-theme="light"] .fascia-media { background: #fbeecd; color: #8a6512; }
:root[data-theme="light"] .fascia-bassa { background: #dcebf5; color: #1f5e85; }
:root[data-theme="light"] .stato-aperta { background: #dcebf5; color: #1f5e85; }
:root[data-theme="light"] .stato-sospesa { background: #fbeecd; color: #8a6512; }
:root[data-theme="light"] .stato-chiusa { background: #e7ebef; color: #5c6776; }
:root[data-theme="light"] .badge.done { background: #d8f0e2; color: #1f7a4d; }

/* tabella griglia */
.grid { width: 100%; border-collapse: collapse; font-size: .85rem; }
.grid th, .grid td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--line); }
.grid th { color: var(--muted); font-weight: 600; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: var(--bg2); }

/* filtri */
.filters { display: flex; align-items: center; gap: .4rem; margin: .6rem 0 1rem; flex-wrap: wrap; }
.chip { padding: .15rem .6rem; border: 1px solid var(--line); border-radius: 20px; color: var(--muted); font-size: .8rem; }
.chip.on { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* form */
label { display: block; margin: .5rem 0; font-size: .85rem; color: var(--muted); }
input, select, textarea { width: 100%; margin-top: .2rem; padding: .45rem .6rem; background: var(--bg2); border: 1px solid var(--line); border-radius: 7px; color: var(--fg); font: inherit; }
.row { display: grid; grid-template-columns: repeat(3,1fr); gap: .6rem; }
@media (max-width: 600px){ .row { grid-template-columns: 1fr; } }
.inline { display: inline; }

.btn { display: inline-block; padding: .4rem .8rem; border: 1px solid var(--line); border-radius: 7px; background: var(--chip); color: var(--fg); cursor: pointer; font: inherit; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 600; }
.btn.sm { padding: .15rem .5rem; font-size: .75rem; }

/* key-value */
.kv { width: 100%; border-collapse: collapse; }
.kv td { padding: .35rem .2rem; border-bottom: 1px solid var(--line); }
.kv td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.kv tr.tot td { font-weight: 700; border-bottom: none; }

.brief-ai { margin: .6rem 0; padding: .8rem 1rem; background: var(--bg2); border-left: 3px solid var(--accent); border-radius: 6px; line-height: 1.5; }
.status { margin-top: 1rem; padding: .7rem 1rem; background: var(--bg2); border-radius: 8px; color: var(--muted); font-family: ui-monospace, monospace; }

/* ===== Agenda settimanale ===== */
.weeknav { display: flex; align-items: center; gap: .35rem; }
.weeknav .range { margin-left: .5rem; color: var(--muted); font-size: .85rem; }
.neweve summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.neweve[open] summary { margin-bottom: .6rem; }
.hint { margin: .1rem 0 .7rem; }

.cal { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
.cal-inner { min-width: 740px; }
.cal-head { display: flex; border-bottom: 1px solid var(--line); }
.cal-gutter-h { width: 54px; flex: none; }
.cal-dayh { flex: 1 1 0; min-width: 96px; text-align: center; padding: .5rem 0; font-size: .8rem; color: var(--muted); border-left: 1px solid var(--line); }
.cal-dayh.today { color: var(--accent); font-weight: 600; }

.cal-body { display: flex; position: relative; }
.cal-gutter { width: 54px; flex: none; }
.cal-hr { font-size: .68rem; color: var(--muted); text-align: right; padding-right: 6px; border-top: 1px solid var(--line); box-sizing: border-box; }
.cal-col { flex: 1 1 0; min-width: 96px; position: relative; border-left: 1px solid var(--line); }
.cal-col.today { background: rgba(78, 161, 255, .04); }
.cal-col.drop { background: rgba(78, 161, 255, .12); }
.cal-slot { border-top: 1px solid var(--line); box-sizing: border-box; }

.ev { position: absolute; left: 3px; right: 3px; border-radius: 6px; padding: 2px 6px; font-size: .72rem; overflow: hidden; cursor: grab; background: var(--ev-bg); color: var(--ev-fg); border-left: 3px solid var(--accent); box-shadow: 0 1px 3px rgba(0,0,0,.3); touch-action: none; user-select: none; }
.ev .ev-time { font-weight: 600; opacity: .85; }
.ev .ev-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev.dragging { cursor: grabbing; opacity: .92; box-shadow: 0 6px 18px rgba(0,0,0,.5); }
.ev.is-done { opacity: .55; }
.ev.is-done .ev-title { text-decoration: line-through; }
.ev.tipo-sopralluogo { border-left-color: var(--ok); }
.ev.tipo-appuntamento { border-left-color: var(--accent); }
.ev.tipo-scadenza { border-left-color: var(--danger); }
.ev.tipo-promemoria { border-left-color: var(--warn); }
.ev.tipo-altro { border-left-color: var(--muted); }
.ev-resize { position: absolute; left: 0; right: 0; bottom: 0; height: 9px; cursor: ns-resize; touch-action: none; }
.ev-resize::after { content: ''; position: absolute; left: 50%; bottom: 2px; width: 18px; height: 2px; margin-left: -9px; border-radius: 2px; background: rgba(255,255,255,.4); }
.ev.resizing { opacity: .92; box-shadow: 0 4px 12px rgba(0,0,0,.4); }

.cal-now { position: absolute; left: 0; right: 0; height: 0; border-top: 2px solid var(--danger); z-index: 5; pointer-events: none; }
.cal-now::before { content: ''; position: absolute; left: -3px; top: -4px; width: 7px; height: 7px; border-radius: 50%; background: var(--danger); }

.ev-tip { position: fixed; z-index: 1000; max-width: 260px; background: #0c1116; color: #e6e9ee; border: 1px solid #2a2f36; border-radius: 8px; padding: .5rem .7rem; font-size: .78rem; line-height: 1.45; box-shadow: 0 6px 20px rgba(0,0,0,.5); pointer-events: none; }

dialog.modal { background: var(--card); color: var(--fg); border: 1px solid var(--line); border-radius: 12px; padding: 1.3rem; max-width: 440px; width: 92%; }
dialog.modal::backdrop { background: rgba(0,0,0,.55); }
.modal-actions { display: flex; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; align-items: center; }

/* ===== Notifiche / PWA ===== */
.nbadge { display: inline-block; min-width: 16px; padding: 0 5px; height: 16px; line-height: 16px; font-size: .66rem; text-align: center; background: var(--danger); color: #fff; border-radius: 8px; vertical-align: middle; }
.banner { background: #13351f; border: 1px solid #1f5a33; color: #9fe0b6; padding: .5rem .8rem; border-radius: 8px; margin-bottom: .9rem; font-size: .9rem; }
:root[data-theme="light"] .banner { background: #e0f4e9; border-color: #bfe6cf; color: #1f7a4d; }
.notif-list li { display: block; padding: .6rem 0; }
.notif-list li.unread { background: var(--bg2); border-left: 3px solid var(--accent); padding-left: .6rem; border-radius: 0 6px 6px 0; }
code { background: var(--bg2); padding: .05rem .35rem; border-radius: 4px; font-size: .82em; }

/* ===== Allegati / cattura ===== */
.upl { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .5rem 0 .8rem; }
.upl input[type=file] { flex: 1 1 220px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .7rem; margin-top: .6rem; }
.att { background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: .5rem; }
.att img, .att video { width: 100%; height: 120px; object-fit: cover; border-radius: 6px; display: block; background: #000; }
.att audio { width: 100%; margin: .3rem 0; }
.att .doc { display: block; padding: 1.2rem .4rem; text-align: center; }
.att-meta { display: flex; justify-content: space-between; align-items: center; gap: .4rem; margin-top: .4rem; }
.att-meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-date { margin-top: .35rem; }

.capture-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin: .7rem 0; }
.cap-btn { position: relative; display: flex; align-items: center; justify-content: center; gap: .4rem; padding: 1.1rem .6rem; background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; text-align: center; color: var(--fg); font-size: .9rem; }
.cap-btn:hover { border-color: var(--accent); }
.cap-btn input { position: absolute; width: 1px; height: 1px; opacity: 0; }
