:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222631;
  --text: #e8eaf0;
  --muted: #8a90a0;
  --accent: #3d8bfd;
  --ok: #2fbf71;
  --warn: #f0a020;
  --danger: #e5484d;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f8; --surface: #ffffff; --surface-2: #eceef3;
    --text: #15171c; --muted: #646b7a;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(68px + var(--safe-b));
}

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.top h1 { font-size: 20px; margin: 0; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.pill.ok { color: var(--ok); }
.pill.err { color: var(--danger); }

main { max-width: 900px; margin: 0 auto; padding: 0 16px; }
.tab { display: none; }
.tab.active { display: block; }

.player {
  position: relative; aspect-ratio: 16/9; width: 100%;
  background: #000; border-radius: var(--radius); overflow: hidden;
}
#video-host, #video-host > * { position: absolute; inset: 0; width: 100%; height: 100%; }
#video-host video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.overlay {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: #ccc; font-size: 14px; background: rgba(0,0,0,.45); pointer-events: none; text-align: center; padding: 16px;
}
.overlay[hidden] { display: none; }
.badge {
  position: absolute; top: 8px; left: 8px; font-size: 11px; padding: 2px 8px;
  border-radius: 6px; background: rgba(0,0,0,.55); color: #fff; text-transform: uppercase;
}
.badge:empty { display: none; }
.clock {
  position: absolute; top: 8px; right: 8px; z-index: 3; pointer-events: none;
  font: 600 12px/1.2 ui-monospace, "SF Mono", Menlo, monospace; font-variant-numeric: tabular-nums;
  padding: 3px 8px; border-radius: 6px; background: rgba(0,0,0,.55); color: #fff;
}
/* nasconde l'etichetta modalità (RTC/MSE) che go2rtc sovrappone al video */
#video-host video-stream > div { display: none !important; }

.controls { display: flex; gap: 8px; margin: 12px 0; }
.controls > button, .seg button {
  min-height: 44px; min-width: 44px; border: 0; border-radius: 12px;
  background: var(--surface); color: var(--text); font-size: 18px; flex: 1;
}
.seg { display: flex; flex: 2; background: var(--surface); border-radius: 12px; padding: 3px; }
.seg button { font-size: 14px; font-weight: 600; background: transparent; min-height: 38px; border-radius: 9px; }
.seg button.on { background: var(--accent); color: #fff; }
button:active { opacity: .7; }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.card img, .card video { width: 100%; border-radius: 10px; display: block; background: #000; }
.btn {
  display: block; text-align: center; margin-top: 10px; padding: 12px; border-radius: 12px; border: 0;
  background: var(--accent); color: #fff; font-size: 16px; text-decoration: none; width: 100%;
}
.btn.danger { background: var(--danger); }

.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 48px; border-bottom: 1px solid var(--surface-2);
}
.row:last-child { border-bottom: 0; }
.row select { background: var(--surface-2); color: var(--text); border: 0; border-radius: 8px; padding: 8px; font-size: 15px; }
input[type=checkbox] {
  appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 99px;
  background: var(--surface-2); position: relative; flex: none; transition: background .2s;
}
input[type=checkbox]::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s;
}
input[type=checkbox]:checked { background: var(--ok); }
input[type=checkbox]:checked::after { transform: translateX(20px); }
input:disabled, select:disabled { opacity: .4; }

.muted { color: var(--muted); }
.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 6px; font-size: 14px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; text-align: right; word-break: break-all; }

.list details { background: var(--surface); border-radius: var(--radius); margin-bottom: 8px; overflow: hidden; }
.list summary { padding: 14px; cursor: pointer; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
.list summary::-webkit-details-marker { display: none; }
.list summary .muted { font-weight: 400; font-size: 14px; }
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; padding: 0 14px 14px; }
.files button {
  min-height: 44px; border: 0; border-radius: 10px; background: var(--surface-2); color: var(--text); font-size: 14px;
}
.files button.playing { background: var(--accent); color: #fff; }
.rec-title { margin-top: 8px; font-size: 14px; color: var(--muted); }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; padding: 6px 8px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--surface-2);
}
.tabs button {
  flex: 1; border: 0; background: none; color: var(--muted); font-size: 11px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 48px; justify-content: center;
}
.tabs button span { font-size: 20px; line-height: 1; }
.tabs button.on { color: var(--accent); }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(80px + var(--safe-b)); z-index: 20;
  background: var(--text); color: var(--bg); padding: 12px 14px; border-radius: 12px; text-align: center; font-size: 14px;
}

@media (orientation: landscape) and (max-height: 500px) {
  .top { display: none; }
  #tab-live .player { aspect-ratio: auto; height: calc(100dvh - 90px); }
}

/* ---------- login / setup ---------- */
.view[hidden] { display: none !important; }
.view.center { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth { width: min(380px, 100%); display: grid; gap: 12px; padding: 24px; }
.auth h1 { margin: 0; font-size: 22px; text-align: center; }
.auth .logo { width: 64px; height: 64px; margin: 0 auto; border-radius: 16px; }
.auth label, .stack label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.auth input, .stack input:not([type=checkbox]) {
  font-size: 16px; padding: 12px; border-radius: 10px; border: 1px solid var(--surface-2);
  background: var(--bg); color: var(--text); width: 100%;
}
.err { color: var(--danger); font-size: 14px; min-height: 1em; margin: 0; }
.btn.secondary { background: var(--surface-2); color: var(--text); }

/* ---------- selettore camere ---------- */
.cams { display: flex; gap: 8px; overflow-x: auto; padding: 0 16px 10px; max-width: 900px; margin: 0 auto; scrollbar-width: none; }
.cams::-webkit-scrollbar { display: none; }
.cams[hidden] { display: none; }
.cams button {
  flex: none; border: 0; border-radius: 99px; padding: 8px 14px; min-height: 36px;
  background: var(--surface); color: var(--text); font-size: 14px; font-weight: 600;
}
.cams button.on { background: var(--accent); color: #fff; }

/* ---------- PTZ ---------- */
.ptz .pad { display: grid; grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(3, 56px); gap: 6px; justify-content: center; }
.ptz .pad button { border: 0; border-radius: 12px; background: var(--surface-2); color: var(--text); font-size: 20px; touch-action: none; user-select: none; -webkit-user-select: none; }
.ptz .pad [data-dir=up] { grid-column: 2; grid-row: 1; }
.ptz .pad [data-dir=left] { grid-column: 1; grid-row: 2; }
.ptz .pad [data-dir=right] { grid-column: 3; grid-row: 2; }
.ptz .pad [data-dir=down] { grid-column: 2; grid-row: 3; }
.ptz .pad button:active { background: var(--accent); color: #fff; }

/* ---------- account / admin ---------- */
.stack { display: grid; gap: 10px; margin: 8px 0; }
.items { display: grid; gap: 6px; margin-bottom: 8px; }
.item {
  display: grid; gap: 2px; text-align: left; border: 0; border-radius: 10px; padding: 12px;
  background: var(--surface-2); color: var(--text); font-size: 15px;
}
.item .muted { font-size: 13px; }
dialog {
  border: 0; border-radius: 16px; padding: 18px; width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px); background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h2 { margin: 0 0 4px; font-size: 18px; }
fieldset { border: 1px solid var(--surface-2); border-radius: 10px; padding: 4px 12px; margin: 0; }
legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.actions .btn { width: auto; margin-top: 4px; padding: 10px 16px; }
.actions .danger { margin-right: auto; }
.actions [hidden] { display: none; }
