:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #eef1f7;
    --border: #dce1ea;
    --text: #1c2430;
    --muted: #6b7280;
    --primary: #2f6bff;
    --primary-hover: #1f57e6;
    --danger: #e5484d;
    --radius: 12px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.brand {
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: .3px;
}
.brand span { color: var(--primary); }
.brand.center { text-align: center; margin-bottom: 1.5rem; }
/* Logo da marca (imagem). Na sidebar ocupa a largura disponível; nas telas
   de auth fica centralizado com um limite de largura. */
.brand-logo {
    display: block;
    width: 100%;
    max-width: 180px;
    height: auto;
}
.brand.center .brand-logo {
    margin: 0 auto;
    max-width: 200px;
}

.muted { color: var(--muted); }

/* ---------- Auth ---------- */
.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}
.auth-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: 0 10px 30px rgba(28,36,48,.10);
}
.auth-card h1 { font-size: 1.4rem; margin: 0 0 1.25rem; }
.auth-links { margin-top: 1.25rem; text-align: center; font-size: .9rem; }

.field { margin-bottom: 1rem; }
.field label {
    display: block;
    font-size: .85rem;
    color: var(--muted);
    margin-bottom: .35rem;
}
.form-input,
.field input {
    width: 100%;
    padding: .7rem .85rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: .95rem;
}
.form-input:focus,
.field input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(91,140,255,.15);
}
.field-error { color: var(--danger); font-size: .8rem; margin-top: .35rem; }

.btn {
    display: inline-block;
    border: none;
    border-radius: 8px;
    padding: .7rem 1rem;
    font-size: .95rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s ease;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; }
.btn-block { width: 100%; }
.btn-ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    width: 100%;
}
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }

.alert {
    padding: .7rem .85rem;
    border-radius: 8px;
    font-size: .9rem;
    margin-bottom: 1rem;
}
.alert-error {
    background: rgba(229,72,77,.10);
    border: 1px solid rgba(229,72,77,.35);
    color: #b42318;
}

/* ---------- App layout ---------- */
.app-shell { min-height: 100vh; }
.layout { display: flex; min-height: 100vh; align-items: flex-start; }

.sidebar {
    width: 240px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.5rem 1rem;
    display: flex;
    flex-direction: column;
    /* Fica fixa do lado esquerdo mesmo com scroll na página */
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.nav { margin-top: 1.5rem; display: flex; flex-direction: column; gap: .25rem; flex: 1; }
.nav-item {
    padding: .6rem .75rem;
    border-radius: 8px;
    color: var(--muted);
    font-size: .95rem;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item.active { background: var(--surface-2); color: var(--text); }
.nav-item.disabled { opacity: .45; cursor: not-allowed; }
.nav-item.disabled:hover { background: transparent; color: var(--muted); }
.logout { margin-top: 1rem; }

.content { flex: 1; padding: 1.75rem 2rem; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
}
.topbar h1 { font-size: 1.35rem; margin: 0; }
.user { color: var(--muted); font-size: .9rem; }

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
}
.card-title { color: var(--muted); font-size: .85rem; }
.card-value { font-size: 1.8rem; font-weight: 700; margin: .35rem 0; }
.card-hint { color: var(--muted); font-size: .8rem; }

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
}
.panel h2 { margin-top: 0; }

@media (max-width: 720px) {
    .layout { flex-direction: column; }
    .sidebar {
        width: 100%;
        height: auto;
        position: static;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
}

/* ---------- Dashboard da loja (cabe na tela, sem scroll) ---------- */
/* A página do dashboard ocupa exatamente a altura da janela: o conteúdo vira
   coluna flex e o grid estica para preencher o espaço abaixo do topbar. */
.content:has(.dash-fit) {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.dash-fit {
    flex: 1;
    min-height: 0;
    display: grid;
    /* acessos (40% da largura, altura toda) | ociosidade (40%) sobre espera x atendimento (60%) */
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    grid-template-rows: auto minmax(0, 2fr) minmax(0, 3fr);
    gap: 1rem;
}
.dash-toolbar { grid-column: 1 / -1; margin: 0; }
.dash-tall { grid-column: 1; grid-row: 2 / 4; }

.dash-card {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    padding: 1rem 1.25rem;
    overflow: hidden; /* gráfico nunca vaza por cima do card vizinho */
}
.dash-card h2 { font-size: 1.05rem; margin: 0 0 .5rem; }
.dash-card h2 small { font-weight: 400; font-size: .8rem; }
.chart-hint { display: block; margin: .2rem 0 .6rem; font-size: .8rem; }
.dash-card .chart-hint { margin: 0 0 .25rem; }

/* Bloco de números: total grande à esquerda, dois menores empilhados à direita */
.kpis {
    display: grid;
    grid-template-columns: 1.3fr 1fr;
    gap: .75rem;
    margin-bottom: .75rem;
}
.kpi {
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: var(--surface-2);
    border-radius: 10px;
    padding: .45rem .8rem;
}
.kpi-stack { display: grid; grid-template-rows: 1fr 1fr; gap: .5rem; }
.kpi-label { color: var(--muted); font-size: .78rem; display: flex; align-items: center; gap: .35rem; }
.kpi-fechada { color: #6b7280; font-size: .72rem; }
.kpi-label-wrap { flex-wrap: wrap; row-gap: 0; }
.kpi-value { font-size: 2.2rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi-value.sm { font-size: 1.2rem; }
/* Acessos: números mais compactos para sobrar altura aos gráficos das portas */
.kpis-acessos { flex-shrink: 0; gap: .5rem; margin-bottom: .6rem; }
.kpis-acessos .kpi { padding: .3rem .7rem; }
.kpis-acessos .kpi-value { font-size: 1.7rem; }
.kpis-acessos .kpi-value.sm { font-size: 1rem; }
.kpis-acessos .kpi-stack { gap: .35rem; }
/* Ociosidade: só os números, ocupando o card todo; hora mais ociosa (40%) sobre a linha acumulada (60%) */
.kpis-ocioso { flex: 1; min-height: 0; margin-bottom: 0; }
.kpi-stack-ocioso { grid-template-rows: minmax(0, 2fr) minmax(0, 3fr); }
.kpi-stack-ocioso .kpi { min-height: 0; overflow: hidden; }
.kpi-spark { position: relative; flex: 1; min-height: 0; }
.kpi-spark canvas { position: absolute; inset: 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-p1 { background: #2f6bff; }
.dot-p2 { background: #7c9dff; }

.tag-demo {
    font-size: .7rem;
    font-weight: 500;
    color: #b45309;
    background: #fef3c7;
    border-radius: 999px;
    padding: .1rem .5rem;
    margin-left: .35rem;
    vertical-align: middle;
}

/* Gráficos esticam para ocupar o resto do card (Chart.js com
   maintainAspectRatio: false precisa de um container com altura). */
.chart-box { position: relative; flex: 1; min-height: 0; }
.chart-box > canvas { position: absolute; inset: 0; }
.chart-stack { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: .4rem; }
.chart-porta { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chart-title { display: flex; align-items: center; gap: .4rem; margin: 0 0 .3rem; font-size: .95rem; font-weight: 700; }
.chart-title small { font-weight: 400; font-size: .78rem; }

/* Telas baixas: mantém as duas colunas, mas com altura fixa e scroll */
@media (max-height: 600px) {
    .content:has(.dash-fit) { height: auto; overflow: visible; }
    .dash-fit { grid-template-rows: auto 300px 380px; }
}
/* Telas estreitas: um card por linha */
@media (max-width: 900px) {
    .content:has(.dash-fit) { height: auto; overflow: visible; }
    .dash-fit { grid-template-columns: 1fr; grid-template-rows: none; }
    .dash-card { height: 420px; }
    .dash-tall { grid-column: auto; grid-row: auto; }
    /* Ociosidade: total em cima, hora mais ociosa e linha acumulada embaixo */
    .kpis-ocioso { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
    .kpis-ocioso .kpi-value { font-size: 1.7rem; }
    .kpis-ocioso .kpi-value.sm { font-size: 1.1rem; }
}

/* ---------- Câmeras ---------- */
.grid-toolbar {
    display: flex;
    align-items: center;
    gap: .75rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

/* Grid de até 8 câmeras: 4 colunas x 2 linhas em telas grandes */
.camera-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}
.camera-cell {
    margin: 0;
    background: #000;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.camera-cell iframe {
    width: 100%;
    aspect-ratio: 16 / 9;
    border: 0;
    background: #0a0c11;
    display: block;
}
.camera-cell figcaption {
    padding: .5rem .7rem;
    font-size: .85rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

/* Câmeras ao vivo (HLS): cabem na tela sem scroll. A página ocupa a altura
   da janela e a grade (2 colunas) divide o espaço restante em linhas iguais;
   o vídeo se ajusta dentro da célula sem cortar (object-fit: contain). */
.content:has(.live-fit) {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.live-fit {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
.live-fit .grid-toolbar { margin-bottom: .75rem; }
.live-grid {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: minmax(0, 1fr);
    gap: .75rem;
}
.live-cell {
    position: relative;
    margin: 0;
    background: #000;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    min-height: 0;
    cursor: zoom-in;
}
.live-cell video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    background: #000;
}
.live-nome {
    position: absolute;
    top: .5rem;
    left: .5rem;
    padding: .2rem .55rem;
    font-size: .85rem;
    font-weight: 600;
    color: #fff;
    background: rgba(0, 0, 0, .6);
    border-radius: 6px;
    pointer-events: none;
}
.live-status {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: .95rem;
    background: rgba(0, 0, 0, .45);
    pointer-events: none;
}
.live-status[hidden] { display: none; }
/* Celular: uma câmera por linha, com scroll (não cabem 4 na altura) */
@media (max-width: 720px) {
    .content:has(.live-fit) { height: auto; overflow: visible; }
    .live-grid { grid-template-columns: 1fr; grid-auto-rows: auto; }
    .live-cell { aspect-ratio: 4 / 3; }
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-top: 1.25rem;
}
.page-status { color: var(--muted); font-size: .9rem; }

@media (max-width: 1100px) {
    .camera-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
    .camera-grid { grid-template-columns: 1fr; }
}

/* ---------- Tabela ---------- */
.panel.no-pad { padding: 0; overflow: hidden; }
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: .9rem;
}
.table th, .table td {
    text-align: left;
    padding: .7rem .9rem;
    border-bottom: 1px solid var(--border);
}
.table th { color: var(--muted); font-weight: 600; }
.table tr:last-child td { border-bottom: none; }
.url-cell {
    max-width: 320px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
}
.actions a { margin-right: .75rem; font-size: .85rem; }
.actions a.danger { color: var(--danger); }
.actions .link-btn {
    background: none; border: none; padding: 0; margin-right: .75rem;
    color: var(--accent, #5b8cff); font-size: .85rem; cursor: pointer;
    font-family: inherit;
}
.actions .link-btn:hover { text-decoration: underline; }

.badge {
    display: inline-block;
    padding: .15rem .5rem;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
}
.badge.on { background: rgba(34,160,90,.14); color: #1a7f43; }
.badge.off { background: rgba(229,72,77,.14); color: #b42318; }

/* ---------- Formulário de câmera ---------- */
.form-panel { max-width: 620px; }
.form-actions { display: flex; gap: .75rem; margin-top: 1.25rem; }
.field small.muted { display: block; margin-top: .3rem; font-size: .78rem; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #e64a58; text-decoration: none; }

/* Checkbox "ativa" alinhado */
.field input[type="checkbox"] { width: auto; margin-right: .4rem; }

/* Campos de checkbox no formulário de usuário: label ao lado */
.field-check { display: flex; align-items: center; gap: .5rem; flex-direction: row-reverse; justify-content: flex-end; }
.field-check label { margin: 0; }
.field-check input[type="checkbox"] { margin: 0; }

.messages { margin-bottom: 1rem; }
.alert-success {
    background: rgba(34,160,90,.10);
    border: 1px solid rgba(34,160,90,.35);
    color: #1a7f43;
}
.alert-warning {
    background: rgba(245,176,32,.12);
    border: 1px solid rgba(245,176,32,.4);
    color: #8a5a00;
}
.alert-info {
    background: rgba(47,107,255,.10);
    border: 1px solid rgba(47,107,255,.3);
    color: #1f57e6;
}

/* ---------- Mapa de calor (conveniência) ---------- */
.heatmap-wrap {
    display: grid;
    gap: 2px;
    width: 100%;
    aspect-ratio: 16 / 9;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
}
.heat-cell {
    border-radius: 3px;
    min-height: 10px;
    transition: outline .1s ease;
}
.heat-cell:hover { outline: 2px solid var(--primary); }

/* Gráfico ainda sem câmera instalada */
.aguardando {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 140px;
    color: var(--muted);
    font-size: 1rem;
    border: 1px dashed var(--muted);
    border-radius: 8px;
}
