:root {
  --bg: #0b1118; --panel: #121c26; --panel2: #18242f; --line: #24333f; --text: #e6edf3; --muted: #8a9aa8;
  --accent: #3fb68b; --accent2: #2a8f6c; --warn: #f5a524; --danger: #e5484d; --zone: #ffd200;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; }
body { min-height: 100vh; overscroll-behavior-y: none; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ---------- top bar & bottom nav */
.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-t)) 16px 10px; background: rgba(11,17,24,.92); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.top h1 { font-size: 17px; margin: 0; font-weight: 700; letter-spacing: .2px; display: flex; align-items: center; gap: 8px; }
.top h1 img { width: 26px; height: 26px; border-radius: 7px; }
.top .sp { flex: 1; }
.pill { font-size: 12.5px; padding: 3px 9px; border-radius: 99px; background: var(--panel2); color: var(--muted); white-space: nowrap; }
.pill.hot { background: rgba(229,72,77,.18); color: #ff8b8e; }
.pill.ok { background: rgba(63,182,139,.15); color: #6fd5ad; }
main { padding: 12px 12px calc(84px + var(--safe-b)); max-width: 1400px; margin: 0 auto; }
nav.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; background: rgba(18,28,38,.97); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
nav.tabs button { flex: 1; background: none; border: 0; padding: 9px 0 8px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; cursor: pointer; }
nav.tabs button svg { width: 24px; height: 24px; }
nav.tabs button.on { color: var(--accent); }

/* ---------- live grid */
.grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 760px) { .grid.n2 { grid-template-columns: 1fr 1fr; } }
.tile { position: relative; background: #000; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); aspect-ratio: 16/9; cursor: pointer; }
.tile.alert { border-color: var(--danger); box-shadow: 0 0 0 2px rgba(229,72,77,.45); }
.tile img.feed, .tile canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.tile canvas { pointer-events: none; }
.tile .bar { position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: linear-gradient(rgba(0,0,0,.65), transparent); font-size: 13.5px; }
.tile .bar b { font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.live { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.dot.error { background: var(--danger); } .dot.connecting, .dot.starting { background: var(--warn); }
.tile .msg { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; text-align: center; padding: 20px; color: var(--muted); font-size: 14px; }
.tile .birdtag { position: absolute; right: 10px; bottom: 10px; background: var(--danger); color: #fff; font-weight: 700; padding: 4px 10px; border-radius: 8px; font-size: 13px; animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: .6; } }
.liveopts { display: flex; gap: 8px; align-items: center; margin: 2px 0 12px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; color: var(--muted); font-size: 13px; }
.seg { display: inline-flex; flex: none; background: var(--panel); border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 6px 11px; color: var(--muted); cursor: pointer; font-size: 13px; }
.seg button.on { background: var(--panel2); color: var(--text); }

/* full-screen single camera */
.full { position: fixed; inset: 0; z-index: 50; background: #000; display: flex; flex-direction: column; }
.full .stage { position: relative; flex: 1; overflow: hidden; touch-action: none; user-select: none; }
.full .stage.zoomed { cursor: grab; }
.full .zoomer { position: absolute; inset: 0; transform-origin: 0 0; will-change: transform; }
.full .zoomer img, .full .zoomer canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; -webkit-user-drag: none; }
.full .zoomer canvas { pointer-events: none; }
.zoombar { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; background: rgba(0,0,0,.45); border-radius: 10px; padding: 4px; z-index: 2; }
.zoombar span { font-size: 12px; color: #fff; min-width: 34px; text-align: center; }
.ptzpad { position: absolute; left: 12px; bottom: 12px; z-index: 2; background: rgba(11,17,24,.82); border: 1px solid var(--line); border-radius: 14px; padding: 8px; width: 158px; touch-action: none; }
.ptzpad[hidden] { display: none; }
.ptzpad .dpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.ptzpad .dpad button { aspect-ratio: 1; border-radius: 9px; border: 1px solid var(--line); background: var(--panel2); color: var(--text); font-size: 17px; cursor: pointer; touch-action: none; }
.ptzpad .dpad button:active { background: var(--accent2); }
.ptzpad .dpad button[data-k="spd"] { font-size: 12px; color: var(--muted); }
.ptzpad .presets { display: flex; gap: 4px; align-items: center; margin-top: 8px; font-size: 12px; color: var(--muted); }
.ptzpad .presets button { flex: 1; padding: 5px 0; border-radius: 7px; border: 1px solid var(--line); background: var(--panel2); color: var(--text); cursor: pointer; }
.ptzpad .presets button.on { background: var(--warn); color: #000; }
.ptzpad small { display: block; color: var(--muted); font-size: 11px; line-height: 1.35; margin-top: 6px; }
.full .tools { display: flex; gap: 8px; padding: 10px 12px calc(10px + var(--safe-b)); background: #0b1118; flex-wrap: wrap; align-items: center; }
.full .tools .sp { flex: 1; }

/* ---------- cards / forms */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 15.5px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.card h2 .sp { flex: 1; }
.row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
.row:first-of-type { border-top: 0; }
.row > label, .row > .lbl { flex: 1; min-width: 0; }
.row small, .hint { color: var(--muted); font-size: 12.5px; display: block; line-height: 1.45; }
.hint { margin: 6px 0; }
input[type=text], input[type=password], input[type=number], input[type=time], input[type=date], input[type=url], select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 0; color-scheme: dark; }
input.wide, select.wide { width: 100%; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.field { margin: 10px 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.btn { text-decoration: none; background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 8px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.pri { background: var(--accent2); border-color: var(--accent2); color: #fff; }
.btn.danger { color: #ff8b8e; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.switch { position: relative; width: 46px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: #33434f; border-radius: 99px; transition: .15s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.camrow { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.camrow:first-of-type { border-top: 0; }
.camrow img { width: 88px; aspect-ratio: 16/9; object-fit: cover; border-radius: 6px; background: #000; flex: none; }
.camrow .lbl { flex: 1; min-width: 0; }
.camrow .lbl small { display: block; color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.found { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin: 8px 0; background: var(--bg); }
.found b { font-size: 14px; }
.found .prof { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 13px; }
.found .prof code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
pre.log { background: #05090d; border-radius: 8px; padding: 10px; font-size: 11.5px; max-height: 50vh; overflow: auto; white-space: pre-wrap; word-break: break-all; color: #b6c4cf; }

/* zone editor */
.zone-ed { position: relative; background: #000; border-radius: 10px; overflow: hidden; touch-action: none; user-select: none; }
.zone-ed img { display: block; width: 100%; }
.zone-ed canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }

/* ---------- history */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 99px; padding: 5px 12px; font-size: 13px; cursor: pointer; color: var(--muted); }
.chip.on { background: var(--accent2); border-color: var(--accent2); color: #fff; }
.daylabel { color: var(--muted); font-size: 13px; margin: 14px 2px 8px; display: flex; justify-content: space-between; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.th { position: relative; border-radius: 9px; overflow: hidden; background: #000; aspect-ratio: 16/9; cursor: pointer; border: 1px solid var(--line); }
.th img { width: 100%; height: 100%; object-fit: cover; display: block; }
.th > span { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 7px 4px; background: linear-gradient(transparent, rgba(0,0,0,.8)); font-size: 12px; display: flex; justify-content: space-between; }
.chart { display: flex; align-items: flex-end; gap: 3px; height: 90px; padding-top: 6px; }
.chart div { flex: 1; background: var(--accent2); border-radius: 3px 3px 0 0; min-height: 2px; position: relative; }
.chart div.today { background: var(--accent); }
.chart-x { display: flex; gap: 3px; font-size: 10.5px; color: var(--muted); margin-top: 3px; }
.chart-x span { flex: 1; text-align: center; overflow: hidden; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

/* viewer / sheets */
.modal { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.92); display: flex; flex-direction: column; }
.modal .mtop { display: flex; align-items: center; gap: 8px; padding: calc(10px + var(--safe-t)) 12px 10px; }
.modal .mtop .sp { flex: 1; }
.modal .mimg { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; }
.modal .mimg img { max-width: 100%; max-height: 100%; object-fit: contain; }
.modal .mbot { display: flex; gap: 8px; padding: 10px 12px calc(12px + var(--safe-b)); justify-content: center; }
.sheet { position: fixed; inset: 0; z-index: 55; background: rgba(0,0,0,.6); display: flex; align-items: flex-end; justify-content: center; }
.sheet > div { background: var(--panel); width: 100%; max-width: 640px; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 16px 16px calc(20px + var(--safe-b)); }
@media (min-width: 700px) { .sheet { align-items: center; } .sheet > div { border-radius: 16px; } }
.sheet h3 { margin: 0 0 6px; font-size: 17px; display: flex; align-items: center; }
.sheet h3 .sp { flex: 1; }
.x { background: none; border: 0; font-size: 26px; line-height: 1; color: var(--muted); cursor: pointer; padding: 0 4px; }

/* login */
.login { max-width: 360px; margin: 12vh auto 0; padding: 0 20px; text-align: center; }
.login img { width: 84px; height: 84px; border-radius: 20px; }
.login h1 { font-size: 22px; margin: 14px 0 4px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login input { width: 100%; margin-bottom: 10px; padding: 11px 12px; font-size: 16px; }
.login .btn { width: 100%; justify-content: center; padding: 11px; font-size: 16px; }
.err { color: #ff8b8e; font-size: 13.5px; min-height: 20px; margin: 4px 0; }

#toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 99; background: #27394a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }
.guide h4 { margin: 14px 0 4px; font-size: 14.5px; }
.guide p, .guide li { color: #c7d2db; line-height: 1.6; font-size: 14px; margin: 4px 0; }
.guide ol, .guide ul { padding-left: 20px; margin: 4px 0; }
.spin { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: rot .8s linear infinite; vertical-align: -3px; }
@keyframes rot { to { transform: rotate(360deg); } }
/* joystick always shown under a camera that can turn (live screen) */
.camcell { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.ptzpad.inline { position: static; width: auto; display: flex; gap: 14px; align-items: center; padding: 10px; }
.ptzpad.inline .dpad { width: 138px; flex: none; }
.ptzpad.inline .ptzside { flex: 1; min-width: 0; }
.ptzpad.inline .presets { margin-top: 0; }
.ptzpad .ptzside small { margin-top: 8px; }
/* laptop / desktop: a camera picture never taller than the window; the joystick sits beside it */
@media (min-width: 900px) {
  .camcell .tile { max-height: calc(100vh - 210px); }
  .grid.n1 { max-width: calc((100vh - 210px) * 16 / 9 + 200px); margin: 0 auto; }
  .grid.n1 .camcell { flex-direction: row; align-items: flex-start; }
  .grid.n1 .camcell .tile { flex: 1; min-width: 0; }
  .grid.n1 .ptzpad.inline { flex-direction: column; align-items: stretch; width: 180px; flex: none; }
  .grid.n1 .ptzpad.inline .dpad { width: auto; }
}
