:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: #101d2d;
  --border: #26384d;
  --blue: #1976d2;
  --text: #f3f7fb;
  --muted: #9db0c5;
  font-family: system-ui, -apple-system, "Noto Sans Thai", sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
header, nav { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .8rem 1.2rem; background: #0b1726; border-bottom: 1px solid var(--border); }
h1, h2, p { margin: 0; }
header h1 { font-size: 1.35rem; }
header p { color: var(--muted); font-size: .85rem; }
button { border: 0; border-radius: .55rem; padding: .7rem 1rem; background: var(--blue); color: white; font: inherit; font-weight: 650; cursor: pointer; }
button.secondary { background: #27384c; }
button.danger { background: #b3261e; }
a { color: #9fd0ff; text-decoration: none; font-weight: 600; }
.header-actions, .layouts, .manage-actions, .form-actions { display: flex; gap: .55rem; flex-wrap: wrap; }
.icon-only { width: 3rem; min-width: 3rem; height: 3rem; padding: 0; display: inline-grid; place-items: center; font-size: 1.05rem; }
.layout.active { outline: 2px solid #73b5ff; }
#statusText { color: var(--muted); }
.media-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .8rem 1.2rem; background: #091523; border-bottom: 1px solid var(--border); }
.media-toolbar-info strong { display: block; }
.media-toolbar-info small { display: block; color: var(--muted); margin-top: .2rem; }
.media-toolbar-actions { display: flex; gap: .55rem; flex-wrap: wrap; }
.status-button { position: relative; width: 3.1rem; min-width: 3.1rem; height: 3.1rem; padding: 0; display: inline-grid; place-items: center; font-size: 1.2rem; }
.status-button::before { content: ''; position: absolute; right: .45rem; top: .45rem; width: .55rem; height: .55rem; border-radius: 999px; }
.snapshot-button { background: #23476a; }
.snapshot-button::before { background: #8bd3ff; box-shadow: 0 0 0 4px #8bd3ff22; }
.recording-button.idle { background: #5b431d; }
.recording-button.idle::before { background: #ffc85a; box-shadow: 0 0 0 4px #ffc85a22; }
.recording-button.active { background: #9c1f1f; }
.recording-button.active::before { background: #ff6969; box-shadow: 0 0 0 4px #ff696922; animation: recordPulse 1.2s infinite; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.grid { display: grid; gap: 4px; min-height: calc(100dvh - 135px); padding: 4px; }
.grid-1 { grid-template-columns: 1fr; }
.grid-4 { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr); }
.grid-9 { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.grid-focus { grid-template-columns: minmax(0, 1fr) 280px; align-items: stretch; }
.camera-tile { position: relative; overflow: hidden; min-height: 0; background: #000; border: 1px solid #1b2b40; }
.camera-media { width: 100%; height: 100%; overflow: hidden; background: #000; }
.camera-media iframe { width: 100%; height: 100%; border: 0; display: block; }
.camera-media.zoomable iframe { transform: translate(var(--pan-x, 0), var(--pan-y, 0)) scale(var(--zoom-scale, 1)); transform-origin: center center; transition: transform .18s ease-out; }
.camera-hitbox { position: absolute; inset: 0; z-index: 1; border: 0; padding: 0; background: transparent; color: transparent; cursor: pointer; }
.camera-hitbox.dragging { cursor: grabbing; }
.focus-tile { min-height: calc(100vh - 143px); }
.camera-label { position: absolute; left: .5rem; bottom: .5rem; z-index: 2; padding: .25rem .55rem; border-radius: .35rem; background: #000b; font-size: .9rem; pointer-events: none; }
.zoom-controls { position: absolute; top: .65rem; right: .65rem; z-index: 2; display: flex; align-items: center; gap: .4rem; padding: .35rem; border-radius: .55rem; background: #000b; backdrop-filter: blur(6px); }
.zoom-controls button { min-width: 3rem; }
.zoom-label { min-width: 3.3rem; text-align: center; font-size: .9rem; color: var(--text); }
.focus-menu { display: flex; flex-direction: column; gap: .8rem; min-height: 0; padding: .9rem; background: linear-gradient(180deg, #0d1c2f, #08131f); border: 1px solid #1b2b40; border-radius: .55rem; overflow: hidden; }
.focus-menu-header { display: flex; align-items: flex-start; justify-content: space-between; gap: .8rem; }
.focus-menu-header strong { display: block; font-size: 1rem; }
.focus-menu-header small { display: block; color: var(--muted); margin-top: .25rem; }
.focus-camera-list { display: flex; flex-direction: column; gap: .55rem; overflow: auto; padding-right: .2rem; }
.focus-camera { width: 100%; text-align: left; background: #162638; border: 1px solid transparent; }
.focus-camera.active { background: #1f4f86; border-color: #73b5ff; }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; padding: 2rem; }
dialog { width: min(900px, 94vw); max-height: 90vh; overflow: auto; border: 1px solid var(--border); border-radius: .8rem; background: var(--panel); color: var(--text); }
dialog::backdrop { background: #000a; }
.dialog-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.icon-button { font-size: 1.05rem; background: transparent; }
.manage-actions { margin-bottom: 1rem; }
.camera-row, .discover-item { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .8rem; border: 1px solid var(--border); border-radius: .55rem; margin-bottom: .55rem; }
.camera-row small, .discover-item small { display: block; color: var(--muted); }
.row-actions { display: flex; gap: .4rem; }
.library-tabs { display: flex; gap: .55rem; flex-wrap: wrap; margin-bottom: 1rem; }
.library-tabs .active { outline: 2px solid #73b5ff; }
.library-filters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) auto auto; gap: .7rem; align-items: end; margin-bottom: 1rem; }
.library-status { color: var(--muted); margin-bottom: 1rem; }
.library-panel.hidden { display: none; }
.library-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: .9rem; }
.library-card, .recording-card { border: 1px solid var(--border); border-radius: .7rem; overflow: hidden; background: #0a1522; }
.library-card img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; display: block; background: #000; }
.recording-card video { width: 100%; aspect-ratio: 16 / 9; background: #000; display: block; }
.library-meta { display: grid; gap: .3rem; padding: .8rem; }
.library-meta strong { overflow-wrap: anywhere; }
.library-meta small { color: var(--muted); }
.library-card-actions { display: flex; justify-content: flex-end; padding: 0 .8rem .8rem; }
.recording-list { display: grid; gap: .9rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
label { display: grid; gap: .35rem; color: #c9d6e4; }
label.wide { grid-column: 1 / -1; }
label.check { display: flex; align-items: center; }
input { width: 100%; padding: .75rem; border: 1px solid #38506b; border-radius: .45rem; background: #081523; color: white; font: inherit; }
select { width: 100%; padding: .75rem; border: 1px solid #38506b; border-radius: .45rem; background: #081523; color: white; font: inherit; }
input[type="checkbox"] { width: 1.1rem; height: 1.1rem; }
.form-actions { justify-content: flex-end; margin-top: 1rem; }
.focus-actions { display: grid; grid-template-columns: 1fr; gap: .55rem; }
.recording-info { color: var(--muted); font-size: .9rem; line-height: 1.45; padding: .75rem; background: #0a1522; border: 1px solid var(--border); border-radius: .55rem; }
#toast { position: fixed; right: 1rem; bottom: 1rem; z-index: 10; transform: translateY(120px); opacity: 0; transition: .25s; padding: .8rem 1rem; background: #24405d; border-radius: .55rem; box-shadow: 0 8px 30px #0008; }
#toast.show { transform: none; opacity: 1; }
@keyframes recordPulse {
  0% { transform: translateY(-50%) scale(1); opacity: 1; }
  50% { transform: translateY(-50%) scale(1.2); opacity: .7; }
  100% { transform: translateY(-50%) scale(1); opacity: 1; }
}
@media (max-width: 1024px) {
  .grid-9 { grid-template-columns: repeat(2, 1fr); grid-template-rows: none; }
  .camera-tile { aspect-ratio: 16 / 9; }
}
@media (max-width: 680px) {
  header, nav { align-items: stretch; flex-direction: column; }
  .header-actions button, .layouts button { flex: 1; }
  .media-toolbar { align-items: stretch; flex-direction: column; }
  .media-toolbar-actions button { flex: 1; }
  .library-grid { grid-template-columns: 1fr; }
  .library-filters { grid-template-columns: 1fr 1fr; }
  nav { align-items: stretch; }
  #statusText { text-align: right; }
  .grid { min-height: calc(100dvh - 220px); }
  .grid-4, .grid-9 { grid-template-columns: 1fr; grid-template-rows: none; }
  .grid-focus { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .focus-tile { min-height: min(62dvh, 520px); aspect-ratio: auto; }
  .zoom-controls { top: auto; right: .55rem; bottom: 3rem; left: .55rem; justify-content: center; flex-wrap: wrap; }
  .focus-menu { padding: .8rem; }
  .focus-menu-header { align-items: stretch; flex-direction: column; }
  .focus-camera-list { flex-direction: row; overflow: auto hidden; padding-right: 0; padding-bottom: .2rem; }
  .focus-camera { min-width: 180px; white-space: nowrap; }
  .form-grid { grid-template-columns: 1fr; }
  label.wide { grid-column: auto; }
  .camera-row { align-items: stretch; flex-direction: column; }
  #toast { left: 1rem; right: 1rem; bottom: 1rem; }
}
