:root {
  --bg: #0f1216;
  --panel: #171b21;
  --panel-2: #1f252d;
  --line: #2a313b;
  --text: #e6e9ee;
  --muted: #8b95a3;
  --accent: #3b82f6;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

a { color: var(--accent); text-decoration: none; }
h1, h2 { margin: 0 0 16px; font-weight: 600; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; }

/* --- Top bar -------------------------------------------------------------- */

header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 16px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
header .brand { font-weight: 700; margin-right: 12px; white-space: nowrap; }
header nav { display: flex; gap: 4px; flex: 1; }
header nav a, header button.link {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
  white-space: nowrap;
}
header nav a.active, header nav a:hover { background: var(--panel-2); color: var(--text); }
header .user { color: var(--muted); white-space: nowrap; font-size: 13px; }

main { padding: 16px; max-width: 1600px; margin: 0 auto; }

/* --- Controls ------------------------------------------------------------- */

button, .button {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button.link { background: none; border: none; }
button:disabled { opacity: .5; cursor: default; }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  max-width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

label { display: block; margin-bottom: 12px; }
label > span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 4px; }
label.check { display: flex; gap: 8px; align-items: center; }
label.check > span { display: inline; margin: 0; color: var(--text); font-size: inherit; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px; }
.chip { padding: 5px 12px; border-radius: 999px; font-size: 13px; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* --- Live grid ------------------------------------------------------------ */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--tile, 360px)), 1fr));
  gap: 10px;
}
.tile {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  aspect-ratio: 16 / 9;
  cursor: pointer;
}
cam-live { display: block; position: relative; width: 100%; height: 100%; background: #000; }
cam-live video { width: 100%; height: 100%; display: block; object-fit: contain; }
/* In the grid every image fills the same 16:9 box (4:3 cameras are cropped a little;
   the full image is in the single camera view) */
.tile cam-live video { object-fit: cover; }
.cam-status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
}
.tile .label {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 18px 10px 6px;
  font-size: 13px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .75));
  pointer-events: none;
}
.tile .label small { color: #bbb; margin-left: 6px; }
.site-title { margin: 20px 0 10px; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .05em; }

.single { aspect-ratio: 16 / 9; max-height: calc(100vh - 180px); background: #000; border-radius: var(--radius); overflow: hidden; }


/* --- Recordings ----------------------------------------------------------- */

.player { background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 16 / 9; max-height: 65vh; width: 100%; }
.player video { width: 100%; height: 100%; display: block; }

.timeline {
  position: relative;
  height: 44px;
  margin: 12px 0 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  overflow: hidden;
  touch-action: none;
}
.timeline .seg { position: absolute; top: 8px; bottom: 8px; background: var(--accent); opacity: .55; }
.timeline .cursor { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; }
.timeline .sel { position: absolute; top: 0; bottom: 0; background: rgba(34, 197, 94, .25); border: 1px solid var(--ok); }
.hours { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }
.now-playing { margin: 8px 0 16px; font-variant-numeric: tabular-nums; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
}

/* --- Tables --------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
td .tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--panel-2); margin: 0 4px 4px 0; }
tr.inactive td { opacity: .5; }

/* --- Dialog --------------------------------------------------------------- */

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  width: min(560px, calc(100vw - 32px));
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
.sites-box { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 4px 12px; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

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

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login form {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
}
.login img { width: 56px; height: 56px; display: block; margin-bottom: 12px; }
.login input, .login button { width: 100%; }

@media (max-width: 700px) {
  main { padding: 10px; padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)); }
  .grid { gap: 6px; }
  header { overflow: visible; }
  header .user { display: none; }
  header .brand { flex: 1; }
  /* Mobile: menu as a bar at the bottom, like an app */
  header nav {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 20;
    display: flex;
    gap: 0;
    background: var(--panel);
    border-top: 1px solid var(--line);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom, 0px));
  }
  header nav a {
    flex: 1;
    text-align: center;
    padding: 10px 2px;
    font-size: 12px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
