/* Mobile first. Base rules target phones; min-width queries add tablet/desktop layout. */
:root {
  --bg: #0f1216; --panel: #171c22; --panel2: #1f2630; --line: #2a323c; --text: #e6e9ee; --muted: #8b96a3;
  --accent: #3b82f6; --accent2: #2563eb; --ok: #22c55e; --warn: #f59e0b; --bad: #ef4444;
  --rec-timing: #3b82f6; --rec-motion: #f59e0b; --rec-event: #ef4444;
  --pad: 12px; --radius: 10px;
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html { color-scheme: dark; -webkit-text-size-adjust: 100%; }
html, body {
  margin: 0; background: var(--bg); color: var(--text); overflow-x: hidden;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
img, video, canvas { max-width: 100%; }

/* ---------------------------------------------------------------- header */
header {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
  padding: 10px calc(var(--pad) + var(--safe-r)) 0 calc(var(--pad) + var(--safe-l));
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 30;
}
.brand { font-weight: 650; font-size: 16px; letter-spacing: .2px; white-space: nowrap; min-width: 0; }
.brand small { color: var(--muted); font-weight: 400; margin-left: 5px; }
#userbox {
  margin-left: auto; color: var(--muted); font-size: 13px;
  display: flex; gap: 10px; align-items: center; min-width: 0;
}
#userbox > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
nav {
  order: 3; width: 100%; display: flex; gap: 18px;
  overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
nav::-webkit-scrollbar { display: none; }
nav a {
  color: var(--muted); text-decoration: none; white-space: nowrap;
  padding: 10px 0 8px; border-bottom: 2px solid transparent;
}
nav a.active { color: var(--text); border-bottom-color: var(--accent); }

main { padding: var(--pad) calc(var(--pad) + var(--safe-r)) 40px calc(var(--pad) + var(--safe-l)); margin: 0 auto; max-width: 1600px; }
h1, h2, h3 { margin: 0 0 12px; font-weight: 600; line-height: 1.25; }
h2 { font-size: 19px; } h3 { font-size: 16px; }
p.muted, .muted { color: var(--muted); }
.err { color: #f87171; min-height: 1.3em; margin-top: 8px; overflow-wrap: anywhere; }
.okmsg { color: var(--ok); min-height: 1.3em; margin-top: 8px; overflow-wrap: anywhere; }

/* ---------------------------------------------------------------- controls */
button, select, input, textarea { font: inherit; color: var(--text); }
.btn {
  background: var(--panel2); border: 1px solid var(--line); color: var(--text); border-radius: 8px;
  padding: 9px 13px; cursor: pointer; line-height: 1.2; min-height: 40px; white-space: nowrap;
  touch-action: manipulation;
}
.btn:hover { border-color: #3b4654; background: #26303c; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.danger { border-color: #7f1d1d; color: #fca5a5; }
.btn.danger:hover { background: #3b1111; }
.btn.small { padding: 6px 10px; font-size: 13px; min-height: 36px; }
button.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px 0; min-height: 32px; }

input:not([type]), input[type=text], input[type=password], input[type=email], input[type=tel], input[type=search],
input[type=datetime-local], input[type=number], input[type=url], select, textarea {
  width: 100%; padding: 10px; background: #0b0e12; border: 1px solid var(--line); border-radius: 8px;
  outline: none; color: var(--text); min-height: 42px; font-size: 16px; /* 16px stops iOS zoom-on-focus */
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus, input:-webkit-autofill:active {
  -webkit-text-fill-color: var(--text);
  -webkit-box-shadow: 0 0 0 1000px #0b0e12 inset;
  box-shadow: 0 0 0 1000px #0b0e12 inset;
  caret-color: var(--text);
  transition: background-color 9999s ease-out 0s;
}
input::placeholder, textarea::placeholder { color: #5b6673; }
label.field { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
label.field > input, label.field > select, label.field > textarea { margin-top: 4px; }
label.chk { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; cursor: pointer; min-height: 36px; }
label.chk input { width: auto; min-height: 0; width: 18px; height: 18px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > * { flex: 1 1 100%; }
.row > .btn { flex: 0 0 auto; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.cards { display: grid; grid-template-columns: 1fr; gap: 10px; margin-bottom: 14px; }
.stat { font-size: 24px; font-weight: 600; }
.stat small { font-size: 13px; color: var(--muted); font-weight: 400; margin-left: 6px; }
.login-card { max-width: 400px; margin: 32px auto; }
.login-card .btn { width: 100%; margin-top: 4px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1 1 100%; }
.toolbar .spacer { display: none; }

/* ---------------------------------------------------------------- tables */
.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; white-space: nowrap; }
th { color: var(--muted); font-weight: 500; background: var(--panel); position: sticky; top: 0; }
tr:hover td { background: #161b22; }
td.wrap { white-space: normal; max-width: 320px; overflow-wrap: anywhere; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; background: var(--panel2); color: var(--muted); }
.tag.ok { background: #14532d; color: #bbf7d0; } .tag.bad { background: #7f1d1d; color: #fecaca; } .tag.warn { background: #78350f; color: #fde68a; }
.tag.admin { background: #312e81; color: #c7d2fe; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); display: inline-block; flex: none; }
.dot.off { background: var(--bad); } .dot.unknown { background: var(--muted); }
.actions { display: flex; gap: 4px; flex-wrap: wrap; }
.pager { display: flex; gap: 8px; align-items: center; margin-top: 10px; color: var(--muted); flex-wrap: wrap; }
details.json { cursor: pointer; }
details.json summary { list-style: none; color: var(--muted); max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
details.json pre { white-space: pre-wrap; font-size: 12px; max-width: 320px; margin: 6px 0 0; overflow-wrap: anywhere; }

.toast {
  position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: #111827; border: 1px solid var(--line); padding: 11px 16px; border-radius: 10px; z-index: 100;
  box-shadow: 0 8px 30px rgba(0,0,0,.5); max-width: calc(100vw - 24px); text-align: center;
}
.toast.bad { border-color: var(--bad); }
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px;
  padding: 18px; width: calc(100vw - 20px); max-width: 560px; max-height: 90dvh; overflow-y: auto;
}
dialog::backdrop { background: rgba(0,0,0,.7); }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
.cambox { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 4px 0 12px; }
pre.secret {
  background: #0b0e12; border: 1px dashed var(--line); padding: 10px; border-radius: 8px;
  font-size: 14px; user-select: all; margin: 6px 0; white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------------------------------------------------------------- live grid */
.grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.tile {
  background: #000; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  position: relative; aspect-ratio: 16/9; cursor: pointer;
}
/* the camera burns its own clock top-left and its name bottom-right, so the label sits bottom-left */
.tile-head {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 2; padding: 16px 10px 7px;
  background: linear-gradient(transparent, rgba(0,0,0,.75));
  font-size: 14px; display: flex; gap: 8px; align-items: center; pointer-events: none;
}
/* the camera also burns its own name bottom-right, so the hint goes top-right */
.tile .hint {
  position: absolute; top: 7px; right: 9px; z-index: 2; pointer-events: none;
  font-size: 11px; color: #e2e8f0; background: rgba(0,0,0,.45); padding: 3px 8px; border-radius: 20px;
}
/* the sound button is the only interactive thing in the head; the head itself lets clicks reach the tile */
.tile-head .snd { pointer-events: auto; margin-left: auto; border: 0; border-radius: 20px; min-width: 38px; min-height: 34px;
  padding: 0 9px; font-size: 16px; line-height: 1; color: #fff; background: rgba(0,0,0,.5); cursor: pointer; }
.tile.sound .snd { background: var(--accent); }
video-rtc { display: block; width: 100%; height: 100%; background: #000; }
video-rtc video { width: 100%; height: 100%; object-fit: contain; background: #000; }

#modal {
  position: fixed; inset: 0; background: #000; z-index: 50; display: flex; flex-direction: column;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 8px calc(8px + env(safe-area-inset-bottom, 0px));
}
.modal-body { width: 100%; margin: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; color: #fff; margin-bottom: 8px; }
.modal-head strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* fits both ways: full width in portrait, limited by the viewport height in landscape (head + hint ~110px) */
#modal-holder { position: relative; aspect-ratio: 16/9; width: min(100%, calc((100dvh - 110px) * 16 / 9)); margin: 0 auto;
  background: #000; border-radius: 8px; overflow: hidden; touch-action: pan-y; }
#modal-holder .slide { position: absolute; inset: 0; transition: transform .28s ease; will-change: transform; }
#modal-holder .slide video-rtc { position: absolute; inset: 0; }
#modal-holder .peek { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; z-index: 1;
  pointer-events: none; transition: opacity .35s; }
#modal-holder .peek.gone { opacity: 0; }

/* ---------------------------------------------------------------- recordings */
.rec-layout { display: grid; grid-template-columns: 1fr; gap: 12px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 10px 0 8px; }
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.calendar .wd { color: var(--muted); font-size: 11px; text-align: center; padding-bottom: 2px; }
.calendar .day { padding: 9px 0; text-align: center; border-radius: 8px; color: #4b5563; border: 1px solid transparent; font-size: 14px; }
.calendar .day.has { color: var(--text); background: var(--panel2); cursor: pointer; }
.calendar .day.sel { background: var(--accent); color: #fff; }
.calendar .day.today { outline: 1px dashed var(--muted); }
.legend { display: flex; gap: 12px; align-items: center; margin-top: 12px; color: var(--muted); font-size: 12px; flex-wrap: wrap; }
.sw { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.sw.timing { background: var(--rec-timing); } .sw.motion { background: var(--rec-motion); } .sw.alarm { background: var(--rec-event); }
.day-title { margin-bottom: 8px; color: var(--muted); font-size: 14px; }
.timeline-wrap { overflow-x: hidden; }
.timeline { position: relative; height: 44px; background: #0b0e12; border: 1px solid var(--line); border-radius: 8px; cursor: crosshair; touch-action: manipulation; }
.seg { position: absolute; top: 6px; bottom: 6px; background: var(--rec-timing); opacity: .85; border-radius: 2px; }
.seg.motion { background: var(--rec-motion); } .seg.alarm, .seg.allevent, .seg.event, .seg.smart { background: var(--rec-event); }
.playhead { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; z-index: 3; box-shadow: 0 0 4px #000; }
.hours { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); padding: 3px 0; }
.hours span:nth-child(even) { display: none; }
.seglist { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; max-height: 96px; overflow-y: auto; }
.segbtn { background: var(--panel2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 7px 10px; font-size: 13px; cursor: pointer; min-height: 36px; }
.segbtn.motion { border-color: var(--rec-motion); } .segbtn.alarm, .segbtn.allevent, .segbtn.event { border-color: var(--rec-event); }
.player video { width: 100%; aspect-ratio: 16/9; max-height: 70dvh; background: #000; border-radius: 8px; object-fit: contain; display: block; }
.pbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; color: var(--muted); font-size: 14px; }
#pinfo { flex: 1 1 100%; overflow-wrap: anywhere; }
.ptime { font-variant-numeric: tabular-nums; color: var(--text); font-size: 15px; }
.pbtns { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; width: 100%; }
.pbtns form.inline { display: inline-flex; gap: 4px; flex: 1 1 160px; }
.pbtns form.inline input { min-width: 0; }
.pbtns select { width: auto; flex: 0 0 auto; }

/* ---------------------------------------------------------------- tablet */
@media (min-width: 700px) {
  :root { --pad: 18px; }
  header { gap: 22px; padding-bottom: 0; }
  .brand { font-size: 17px; }
  nav { order: 0; width: auto; flex: 1; overflow: visible; }
  nav a { padding: 14px 0 12px; }
  #userbox { font-size: 14px; order: 1; }
  .cards { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; }
  .grid2 { grid-template-columns: 1fr 1fr; }
  .row > * { flex: 1 1 200px; }
  .toolbar input[type=search] { flex: 0 1 280px; }
  .toolbar .spacer { display: block; flex: 1; }
  /* fixed two-column quad view from tablet up; phones keep one camera under the other */
  .grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .hours span:nth-child(even) { display: block; }
  #pinfo { flex: 1 1 auto; }
  .pbtns { width: auto; margin-left: auto; }
  .pbtns form.inline { flex: 0 0 auto; }
  .pbtns form.inline input { width: 110px; }
  .modal-body { width: min(1500px, 100%); }
  #modal { background: rgba(0,0,0,.9); justify-content: center; padding: 16px; }
  td.wrap { max-width: 420px; }
  details.json summary { max-width: 320px; }
  details.json pre { max-width: 480px; }
  .card { padding: 16px; }
}

/* ---------------------------------------------------------------- desktop */
@media (min-width: 1000px) {
  /* quad view fits the window: two rows of half the viewport, video letterboxed inside */
  .grid { grid-auto-rows: calc((100dvh - 140px) / 2); }
  .tile { aspect-ratio: auto; height: 100%; min-height: 180px; }
  .rec-layout { grid-template-columns: 300px 1fr; gap: 16px; }
  .calendar .day { padding: 7px 0; }
  .segbtn { padding: 4px 9px; font-size: 12px; min-height: 32px; }
}

@media (hover: none) {
  tr:hover td { background: transparent; }
}

/* live video is not seekable: hide the native transport that go2rtc's MP4 fallback turns on */
video-rtc video::-webkit-media-controls,
video-rtc video::-webkit-media-controls-enclosure { display: none !important; }
.modal-btns { display: flex; gap: 8px; flex: none; }
#modal-holder:fullscreen { aspect-ratio: auto; width: 100%; height: 100%; border-radius: 0; }
#modal-holder:fullscreen video-rtc, #modal-holder:fullscreen video { width: 100%; height: 100%; }

/* ---- own scrub bar: the fragmented MP4 has no duration, so the native transport
       only ever shows the buffered chunk. This one spans the whole recording. ---- */
.scrub { margin-top: 10px; touch-action: none; }
.scrub-track { position: relative; height: 26px; display: flex; align-items: center; cursor: pointer; }
.scrub-track::before { content: ""; position: absolute; left: 0; right: 0; height: 6px; border-radius: 3px; background: #0b0e12; border: 1px solid var(--line); }
.scrub-fill { position: absolute; left: 0; height: 6px; border-radius: 3px; background: var(--accent); max-width: 100%; }
.scrub-knob { position: absolute; top: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.7); pointer-events: none; }
.scrub-ends { display: flex; justify-content: space-between; align-items: baseline; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pinfo { margin-top: 6px; }
.modal-hint { font-size: 12px; text-align: center; padding: 8px 0 2px; }
@media (min-width: 700px) {
  .scrub-track { height: 22px; }
  .modal-hint { font-size: 13px; }
}
