:root {
  --bg: #0f1216; --panel: #181c22; --panel2: #20252d; --line: #2b313b;
  --text: #e6e9ee; --muted: #8b93a1; --accent: #3b82f6; --ok: #22c55e; --warn: #f59e0b; --bad: #ef4444;
  font-family: "Microsoft JhengHei", "Noto Sans TC", system-ui, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
.hidden { display: none !important; }
button, .btn, select, input {
  font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 10px; text-decoration: none; display: inline-block;
}
button, .btn { cursor: pointer; }
button:hover, .btn:hover { border-color: #4b5563; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost, .btn.ghost { background: transparent; }
input[type=checkbox] { padding: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--bad); min-height: 1em; }
.spacer { flex: 1; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; }
.login h1 { font-size: 20px; margin: 0 0 8px; }
.login label { display: flex; flex-direction: column; gap: 4px; }

header { display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 16px; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav button { background: transparent; border-color: transparent; }
nav button.active { background: var(--panel2); border-color: var(--line); }
header .right { margin-left: auto; display: flex; gap: 12px; align-items: center; }
.badge { background: var(--bad); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; margin-left: 4px; }

.page { padding: 12px 16px 24px; }
.toolbar, .row { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.row.wrap label { display: flex; flex-direction: column; gap: 4px; }
.row label { display: flex; flex-direction: column; gap: 4px; }
.check { display: flex; align-items: center; gap: 6px; flex-direction: row !important; }
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.summary { display: flex; gap: 16px; color: var(--muted); }
.summary b { color: var(--text); font-size: 16px; }

.grid { display: grid; gap: 6px; grid-template-columns: repeat(var(--cols, 3), 1fr); }
.tile { position: relative; background: #000; aspect-ratio: 16 / 9; border-radius: 6px; overflow: hidden; cursor: zoom-in; }
.tile img { width: 100%; height: 100%; object-fit: contain; display: block; }
.tile .label { position: absolute; left: 6px; top: 6px; background: rgba(0,0,0,.6); padding: 2px 8px; border-radius: 4px; font-size: 12px; }
.tile .count { position: absolute; right: 6px; top: 6px; background: rgba(0,0,0,.6); padding: 2px 8px; border-radius: 4px; font-size: 12px; }
.tile .offline { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); background: #0b0d10; text-align: center; padding: 8px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 4px; }
.dot.off { background: var(--bad); }
.site-title { grid-column: 1 / -1; margin: 8px 0 2px; color: var(--muted); font-weight: 700; }

.viewer { position: fixed; inset: 0; background: #000; z-index: 20; display: flex; flex-direction: column; }
.viewer-bar { display: flex; padding: 8px 16px; align-items: center; }
.viewer img { flex: 1; min-height: 0; object-fit: contain; width: 100%; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.card h3 { margin: 0 0 10px; font-size: 14px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.kpi .v { font-size: 26px; font-weight: 700; }
.kpi .k { color: var(--muted); font-size: 12px; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 12px; }
.charts canvas { width: 100% !important; max-height: 280px; }
.heat { display: grid; grid-template-columns: 34px repeat(24, 1fr); gap: 2px; font-size: 10px; }
.heat div { aspect-ratio: 1; display: grid; place-items: center; border-radius: 2px; }
.heat .h { color: var(--muted); aspect-ratio: auto; }

.events { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 10px; }
.ev { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.ev.new { border-color: var(--bad); }
.ev img { width: 100%; display: block; aspect-ratio: 16/9; object-fit: cover; background: #000; cursor: zoom-in; }
.ev .body { padding: 10px 12px; display: flex; flex-direction: column; gap: 4px; }
.ev .type { font-weight: 700; }
.ev .type.after_hours { color: var(--bad); }
.ev .type.hogging { color: var(--warn); }
.empty { color: var(--muted); padding: 40px; text-align: center; grid-column: 1 / -1; }

.settings { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(520px, 1fr)); align-items: start; }
.site-block { border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 10px; }
.site-block .row { margin-bottom: 8px; }
.table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.table td, .table th { border-bottom: 1px solid var(--line); padding: 6px; text-align: left; }
.cam-row input { width: 100%; }
.editor { position: relative; background: #000; border-radius: 6px; overflow: hidden; margin-bottom: 8px; }
.editor img { width: 100%; display: block; min-height: 200px; }
.editor canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }
.zone-item { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.zone-item input.name { width: 140px; }
.zone-item input.min { width: 70px; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 30; }
.toast { background: var(--panel); border: 1px solid var(--bad); border-left: 4px solid var(--bad); border-radius: 8px;
  padding: 10px 14px; max-width: 340px; box-shadow: 0 6px 20px rgba(0,0,0,.4); cursor: pointer; }

@media (max-width: 700px) {
  .grid { grid-template-columns: repeat(min(var(--cols, 3), 2), 1fr); }
  .charts, .settings { grid-template-columns: 1fr; }
  header .right { margin-left: 0; }
}
