:root {
    --bg: #0a0d12;
    --surface: #11161d;
    --surface-2: #171e28;
    --line: #232c38;
    --text: #e7ecf3;
    --muted: #8793a6;
    --accent: #3ddc97;
    --accent-ink: #062316;
    --motion: #f5b942;
    --people: #ff6363;
    --vehicle: #5aa9ff;
    --animal: #c38bff;
    --danger: #ff6363;
    --radius: 10px;
    --topbar: 56px;
    --gap: 10px;
    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, monospace;
    color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 var(--font);
    -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: .45; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* --- Barra superiore ---------------------------------------------------- */

.topbar {
    position: sticky; top: 0; z-index: 10;
    height: var(--topbar);
    display: flex; align-items: center; gap: 24px;
    padding: 0 16px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: .01em; }
.brand svg { color: var(--accent); width: 22px; height: 22px; }
.brand-lg { font-size: 22px; }
.brand-lg svg { width: 30px; height: 30px; }

.tabs { display: flex; gap: 4px; }
.tab {
    background: none; border: 0; padding: 8px 12px; border-radius: 8px;
    color: var(--muted); font-weight: 550;
}
.tab:hover { color: var(--text); background: var(--surface); }
.tab.is-active { color: var(--text); background: var(--surface-2); }
.tab-count {
    display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px;
    border-radius: 9px; background: var(--people); color: #fff;
    font-size: 11px; line-height: 18px; text-align: center;
}

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 18px; }
.clock { font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }
.user { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.btn-ghost {
    display: inline-flex; align-items: center; gap: 6px;
    background: none; border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px;
}
.btn-ghost svg { width: 16px; height: 16px; }
.btn-ghost:hover { border-color: var(--muted); }

/* --- Controlli generici ------------------------------------------------- */

.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--surface); border: 1px solid var(--line); }
.segmented button {
    border: 0; background: none; padding: 5px 10px; border-radius: 6px;
    color: var(--muted); display: inline-flex; align-items: center; gap: 6px;
}
.segmented button:hover:not(:disabled) { color: var(--text); }
.segmented button.is-on { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.layout-switch button { padding: 5px 7px; }
.layout-switch svg { width: 16px; height: 16px; stroke-width: 1.6; }

.btn {
    border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px;
    padding: 9px 16px; font-weight: 550;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn-primary:hover { filter: brightness(1.08); border-color: transparent; }
.btn-danger {
    width: 100%; margin-top: 4px; padding: 8px; border-radius: 8px;
    background: none; border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line)); color: var(--danger);
}
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.link { background: none; border: 0; padding: 0; color: var(--accent); font-size: 13px; }

/* --- Vista live --------------------------------------------------------- */

.view { padding: 16px; }
.view-live {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 16px;
    align-items: start;
}

.wall {
    display: grid;
    gap: var(--gap);
    width: 100%;
    /* larghezza massima perché righe 4:3 + spazio tra righe stiano in altezza */
    max-width: calc((100dvh - var(--topbar) - 36px - var(--gap)) * 4 / 3 + var(--gap));
    margin: 0 auto;
}
.wall[data-layout="4"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.tile {
    position: relative;
    aspect-ratio: 4 / 3;
    background: #000;
    border-radius: var(--radius);
    overflow: hidden;
    outline: 1px solid var(--line);
    transition: box-shadow .2s;
}
.tile.is-selected { outline-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.tile.is-alert { box-shadow: 0 0 0 2px var(--motion), 0 0 24px -6px var(--motion); }
.tile:fullscreen { border-radius: 0; outline: 0; aspect-ratio: auto; }

cam-player { position: absolute; inset: 0; display: block; }
cam-player video { width: 100%; height: 100%; object-fit: contain; background: #000; }

.tile-top, .tile-bar {
    position: absolute; left: 0; right: 0;
    display: flex; align-items: center; gap: 8px;
    padding: 10px 12px;
    pointer-events: none;
}
.tile-top { top: 0; background: linear-gradient(rgba(0,0,0,.65), transparent); }
.tile-bar {
    bottom: 0; justify-content: space-between;
    background: linear-gradient(transparent, rgba(0,0,0,.7));
    opacity: 0; transition: opacity .2s;
}
.tile:hover .tile-bar, .tile:focus-within .tile-bar, .tile.is-loading .tile-bar { opacity: 1; }
@media (hover: none) { .tile-bar { opacity: 1; } }

.tile-name { font-weight: 600; text-shadow: 0 1px 2px #000; }
.live-dot {
    display: inline-flex; align-items: center; gap: 5px;
    font: 600 10.5px/1 var(--font); letter-spacing: .08em;
    padding: 4px 6px; border-radius: 4px; background: rgba(0,0,0,.45);
}
.live-dot::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.tile.is-loading .live-dot::before, .tile.is-offline .live-dot::before, .tile.is-error .live-dot::before { background: var(--muted); box-shadow: none; }
.tile-dets { margin-left: auto; display: flex; gap: 6px; }

.tile-mode { font: 12px var(--mono); color: rgba(255,255,255,.75); text-shadow: 0 1px 2px #000; }
.tile-tools { display: flex; gap: 4px; pointer-events: auto; }
.tool {
    width: 34px; height: 34px; display: grid; place-items: center;
    border: 0; border-radius: 8px; background: rgba(0,0,0,.45); color: #fff;
}
.tool:hover { background: rgba(255,255,255,.18); }
.tool.is-on { color: var(--accent); }

.tile-status {
    position: absolute; inset: 0; display: none; place-items: center;
    color: var(--muted); font-size: 13px; pointer-events: none;
}
.tile.is-loading .tile-status, .tile.is-error .tile-status { display: grid; }
.tile.is-loading .tile-status::before {
    content: ""; width: 26px; height: 26px; margin-bottom: 44px; position: absolute;
    border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
    animation: spin .9s linear infinite;
}
.tile.is-offline .tile-status { display: grid; }
.tile.is-offline .tile-status::after { content: "Telecamera offline"; position: absolute; margin-top: 44px; color: var(--danger); }
@keyframes spin { to { transform: rotate(360deg); } }

.tile-empty {
    display: grid; place-content: center; text-align: center; gap: 4px;
    background: repeating-linear-gradient(135deg, #0d1117 0 12px, #0b0f14 12px 24px);
    color: var(--muted);
}
.tile-empty span { font: 600 13px var(--mono); letter-spacing: .1em; }
.tile-empty small { font-size: 12px; opacity: .7; }

.det {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 3px 8px; border-radius: 999px;
    font-size: 11.5px; font-weight: 600;
    background: color-mix(in srgb, var(--c) 22%, rgba(0,0,0,.55));
    color: color-mix(in srgb, var(--c) 70%, #fff);
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 55%, transparent);
}
.det-motion { --c: var(--motion); }
.det-people { --c: var(--people); }
.det-vehicle { --c: var(--vehicle); }
.det-animal { --c: var(--animal); }

/* --- Pannelli laterali -------------------------------------------------- */

.side { display: grid; gap: 16px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 16px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 650; }

.status-pill { font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.status-pill.is-ok { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.status-pill.is-bad { color: var(--danger); background: color-mix(in srgb, var(--danger) 14%, transparent); }

.events-state {
    display: flex; align-items: center; gap: 8px;
    margin: -4px 0 12px; font-size: 12.5px; color: var(--muted);
}
.events-state::before { content: ""; width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--accent); }
.events-state.is-paused::before { background: var(--muted); }

.panel-text { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.push-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.push-actions .btn { padding: 7px 12px; font-size: 13px; }

.specs { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 14px; font-size: 13px; }
.specs dt { color: var(--muted); }
.specs dd { margin: 0; text-align: right; font-family: var(--mono); font-size: 12px; overflow-wrap: anywhere; }

.controls { display: grid; gap: 10px; padding-top: 12px; border-top: 1px solid var(--line); }
.control { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.mini-events { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.mini-event {
    width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
    background: none; border: 0; padding: 4px; border-radius: 8px;
}
.mini-event:hover { background: var(--surface-2); }
.mini-event img, .mini-event .no-thumb {
    width: 64px; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 6px; background: var(--surface-2); flex: none;
    border-left: 3px solid var(--c, var(--motion));
}
.mini-event > span:last-child { display: grid; font-size: 13px; }
.mini-event .muted { font-size: 12px; }
.empty { color: var(--muted); font-size: 13px; }

.type-motion { --c: var(--motion); }
.type-people { --c: var(--people); }
.type-vehicle { --c: var(--vehicle); }
.type-animal { --c: var(--animal); }

/* --- Vista eventi ------------------------------------------------------- */

.view-events { max-width: 1400px; margin: 0 auto; }
.events-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }
.day h3 { margin: 20px 0 10px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.event-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.event-card {
    display: grid; padding: 0; text-align: left; overflow: hidden;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    border-top: 3px solid var(--c);
}
.event-card:hover { border-color: var(--muted); border-top-color: var(--c); }
.event-card img, .event-card .no-thumb { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: var(--surface-2); }
.event-card .no-thumb { display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.event-meta { display: grid; gap: 2px; padding: 10px 12px 12px; }
.event-meta .muted { font-size: 12.5px; }
.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.events-more { display: grid; place-items: center; padding: 24px; }

/* --- Lightbox e toast --------------------------------------------------- */

.lightbox {
    padding: 0; border: 1px solid var(--line); border-radius: var(--radius);
    background: var(--surface); color: var(--text); max-width: min(96vw, 1100px);
}
.lightbox::backdrop { background: rgba(0,0,0,.75); }
.lightbox img { display: block; width: 100%; max-height: 80vh; object-fit: contain; background: #000; }
.lightbox p { margin: 0; padding: 12px 16px; color: var(--muted); }
.lightbox-close {
    position: absolute; top: 8px; right: 8px; width: 34px; height: 34px;
    border: 0; border-radius: 8px; background: rgba(0,0,0,.55); color: #fff; font-size: 22px; line-height: 1;
}

.modal {
    width: min(400px, calc(100vw - 32px)); padding: 24px;
    background: var(--surface); color: var(--text);
    border: 1px solid var(--line); border-radius: 14px;
    box-shadow: 0 30px 80px rgba(0,0,0,.5);
}
.modal::backdrop { background: rgba(0,0,0,.65); }
.modal h2 { margin: 0 0 4px; font-size: 17px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
    padding: 10px 16px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line);
    box-shadow: 0 10px 30px rgba(0,0,0,.45);
}
.toast.error { border-color: color-mix(in srgb, var(--danger) 60%, var(--line)); color: #ffd0d0; }

/* --- Login -------------------------------------------------------------- */

body.login { display: grid; place-items: center; padding: 16px; min-height: 100%;
    background: radial-gradient(1200px 600px at 50% -10%, #12241c 0%, var(--bg) 60%); }
.login-card {
    width: min(380px, 100%); padding: 32px 28px;
    background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
    box-shadow: 0 30px 80px rgba(0,0,0,.5);
}
.login-card > .muted { margin: 8px 0 22px; }
.login-card form, .modal form { display: grid; gap: 14px; }
.login-card label, .modal label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input, .modal input {
    font: inherit; color: var(--text); background: var(--bg);
    border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
.login-card input:focus, .modal input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.login-card .btn-primary { margin-top: 6px; padding: 11px; }
.form-error { margin: 0; color: var(--danger); font-size: 13px; }

/* --- Schermi stretti ---------------------------------------------------- */

@media (max-width: 1000px) {
    .view-live { grid-template-columns: 1fr; }
    .wall { max-width: none; }
    .side { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
}
@media (max-width: 640px) {
    .topbar { gap: 12px; padding: 0 12px; }
    .brand span, .clock, .user-name, .btn-ghost .lbl, .layout-switch { display: none; }
    .btn-ghost { padding: 6px 8px; }
    .topbar-right { gap: 8px; }
    .view { padding: 12px; }
    .wall[data-layout="4"] { gap: 6px; }
    .tile-mode { display: none; }
    .tile-bar { justify-content: flex-end; }
}
