:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1b1f24;
  --muted: #5d6673;
  --line: #dde1e7;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --good: #1a7f37;
  --warn: #9a6700;
  --bad: #cf222e;
  --good-bg: #dafbe1;
  --warn-bg: #fff8c5;
  --bad-bg: #ffebe9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 10px;
  --stage: #0d1117;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --panel-2: #1c222b;
    --text: #e6edf3;
    --muted: #8d96a0;
    --line: #2d333b;
    --accent: #4493f8;
    --accent-text: #0d1117;
    --good: #3fb950;
    --warn: #d29922;
    --bad: #f85149;
    --good-bg: rgba(46, 160, 67, .15);
    --warn-bg: rgba(187, 128, 9, .15);
    --bad-bg: rgba(248, 81, 73, .15);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --stage: #010409;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.35rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
h3 { font-size: .95rem; margin: 0 0 .4rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; }
[hidden] { display: none !important; }

/* header */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1.2rem;
  padding: .6rem 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .45rem; color: var(--text); font-weight: 650; }
.brand:hover { text-decoration: none; }
.brand svg { width: 24px; height: 24px; color: var(--accent); }
.top nav { display: flex; gap: .2rem; flex: 1; }
.top nav a { color: var(--muted); padding: .35rem .65rem; border-radius: 6px; }
.top nav a:hover { background: var(--panel-2); text-decoration: none; }
.top nav a.on { color: var(--text); background: var(--panel-2); font-weight: 600; }
.me { position: relative; margin-left: auto; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 200px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); padding: .3rem; display: flex; flex-direction: column;
}
.menu button { text-align: left; background: none; border: 0; color: var(--text); }
.menu button:hover { background: var(--panel-2); }

main { max-width: 1280px; margin: 0 auto; padding: 18px 16px 60px; }

/* controls */
button, .btn {
  font: inherit; cursor: pointer; border-radius: 7px; padding: .45rem .85rem;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
  display: inline-flex; align-items: center; gap: .4rem; white-space: nowrap;
}
button:hover:not(:disabled), .btn:hover { background: var(--panel-2); text-decoration: none; }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--bad); }
button.ghost { border-color: transparent; background: transparent; }
button.small, .btn.small { padding: .2rem .55rem; font-size: .85rem; }
button.big { padding: .7rem 1.4rem; font-size: 1.05rem; }
input, select {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 7px; padding: .42rem .6rem; min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: flex; flex-direction: column; gap: .2rem; font-size: .85rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: .45rem; color: var(--text); font-size: .9rem; }
.row { display: flex; gap: .6rem; align-items: flex-end; flex-wrap: wrap; }
.spacer { flex: 1; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.stack > * + * { margin-top: 14px; }
.error { color: var(--bad); }

/* chips */
.chip {
  display: inline-flex; align-items: center; gap: .3rem; font-size: .78rem; font-weight: 600;
  padding: .12rem .5rem; border-radius: 99px; background: var(--panel-2); color: var(--muted);
}
.chip.good { background: var(--good-bg); color: var(--good); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }

/* login */
.login { max-width: 360px; margin: 10vh auto 0; }
.login form { display: flex; flex-direction: column; gap: .8rem; }
.login button { justify-content: center; }

/* camera grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 14px; }
.card { display: flex; flex-direction: column; gap: .5rem; color: var(--text); }
.card:hover { text-decoration: none; border-color: var(--accent); }
.card .title { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.card .title strong { font-size: 1.05rem; }
.account-head { margin: 22px 0 10px; color: var(--muted); font-size: .85rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* camera page */
.cam-head { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; margin-bottom: 12px; }
.tabs { display: flex; gap: .2rem; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; color: var(--muted); padding: .55rem .9rem; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 16px; align-items: start;
  grid-template-areas: "stage side" "below side"; grid-template-rows: auto 1fr;
}
.stage-col { grid-area: stage; min-width: 0; }
.below-col { grid-area: below; min-width: 0; }
.layout > .side { grid-area: side; }
/* narrow: the controls (Save, Record) come right under the picture */
@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stage" "side" "below"; grid-template-rows: auto; }
}
@media (max-width: 600px) {
  .top { gap: .4rem; padding: .5rem 12px; }
  .brand span { display: none; }
  .top nav a { padding: .35rem .5rem; }
  #me-button { max-width: 34vw; overflow: hidden; text-overflow: ellipsis; padding: .4rem .3rem; }
  main { padding: 14px 12px 60px; }
  .board-inputs { grid-template-columns: 1fr 1fr 1fr; }
  .board-inputs label { font-size: .78rem; }
}
.stage {
  position: relative; background: var(--stage); border-radius: var(--radius); overflow: hidden;
  min-height: 200px; display: flex; align-items: center; justify-content: center;
}
.stage img { display: block; width: 100%; height: auto; }
.stage.tall img { width: auto; max-width: 100%; max-height: 82vh; margin: 0 auto; }
.stage canvas { position: absolute; pointer-events: none; }
.stage .wait {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: .5rem;
  align-items: center; justify-content: center; color: #c9d1d9; font-size: .95rem; text-align: center; padding: 1rem;
}
.spinner { width: 26px; height: 26px; border: 3px solid rgba(255,255,255,.25); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.side { display: flex; flex-direction: column; gap: 14px; }
.views { display: flex; flex-direction: column; gap: .35rem; }
.view-line { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.board-inputs { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .5rem; }
.board-inputs input { width: 100%; }
.save-row { display: flex; gap: .6rem; align-items: center; }
.save-row button { flex: 1; justify-content: center; }
.kbd { font: .75rem ui-monospace, monospace; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 .3rem; color: var(--muted); }

/* gallery */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.shot { position: relative; background: var(--panel-2); border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.shot.flash { outline: 3px solid var(--accent); }
.shot img { width: 100%; display: block; cursor: zoom-in; aspect-ratio: 16 / 9; object-fit: cover; background: var(--stage); }
.shot.tall img { aspect-ratio: 9 / 15; }
.shot .meta { display: flex; align-items: center; gap: .25rem; padding: .3rem .4rem; font-size: .75rem; flex-wrap: wrap; }
.shot .del { position: absolute; top: 4px; right: 4px; padding: .05rem .4rem; background: rgba(0,0,0,.55); color: #fff; border: 0; font-size: .8rem; }
.shot .del:hover { background: var(--bad); }

/* calibration results */
.cal-view { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.cal-view:first-of-type { border-top: 0; padding-top: 0; margin-top: 0; }
.coverage { width: 100%; border-radius: 6px; background: var(--stage); display: block; margin: .3rem 0 .5rem; }
.result { border: 1px solid var(--line); border-radius: 8px; padding: .55rem .7rem; margin-top: .5rem; }
.result.accepted { border-color: var(--good); }
.result .nums { display: grid; grid-template-columns: repeat(4, auto); gap: .1rem .8rem; margin: .35rem 0; }
.result .actions { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .3rem; }

/* clips */
.clips { display: flex; flex-direction: column; gap: 8px; }
.clip { display: grid; grid-template-columns: 128px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 8px; }
.clip img, .clip .noimg { width: 128px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 6px; background: var(--stage); display: block; }
.clip > div { min-width: 0; overflow-wrap: anywhere; }
.clip .actions { display: flex; gap: .35rem; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 640px) {
  .clip { grid-template-columns: 96px minmax(0, 1fr); }
  .clip img, .clip .noimg { width: 96px; }
  .clip .actions { grid-column: 1 / -1; justify-content: flex-start; }
}
.bar { height: 8px; border-radius: 99px; background: var(--panel-2); overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); }
.bar.over > div { background: var(--bad); }

/* tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .5rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.table-wrap { overflow-x: auto; }

/* dialogs */
dialog {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 0;
  background: var(--panel); color: var(--text); max-width: min(96vw, 1200px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog .dlg-head { display: flex; align-items: center; gap: .6rem; padding: .6rem .8rem; border-bottom: 1px solid var(--line); }
dialog .dlg-head .spacer { flex: 1; }
dialog .dlg-body { padding: .8rem; overflow: auto; max-height: calc(92vh - 60px); }
dialog form { display: flex; flex-direction: column; gap: .7rem; min-width: min(360px, 86vw); }
.viewer { position: relative; display: inline-block; }
.viewer img, .viewer video { display: block; max-width: 100%; max-height: 78vh; }
.viewer canvas { position: absolute; left: 0; top: 0; pointer-events: none; }

/* toasts */
#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 50; width: min(92vw, 460px); }
.toast { background: var(--text); color: var(--bg); padding: .6rem .9rem; border-radius: 8px; box-shadow: var(--shadow); font-size: .92rem; }
.toast.bad { background: var(--bad); color: #fff; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }

/* pan & tilt */
.ptz-row { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.dpad { display: grid; grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); gap: 4px; flex: none; }
.dpad button { justify-content: center; padding: 0; font-size: 1rem; touch-action: manipulation; }
.dpad .ptz-up { grid-column: 2; grid-row: 1; }
.dpad .ptz-left { grid-column: 1; grid-row: 2; }
.dpad .ptz-mid { grid-column: 2; grid-row: 2; border-radius: 50%; margin: 14px; background: var(--panel-2); }
.dpad .ptz-right { grid-column: 3; grid-row: 2; }
.dpad .ptz-down { grid-column: 2; grid-row: 3; }
.ptz-row > .stack { flex: 1; min-width: 150px; }
.seg { display: inline-flex; margin-top: .2rem; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 7px 0 0 7px; margin-left: 0; }
.seg button:last-child { border-radius: 0 7px 7px 0; }
.seg button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.presets { display: flex; gap: .35rem; flex-wrap: wrap; }
.presets button { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.ptz-note:empty { display: none; }
details.panel > summary { cursor: pointer; list-style-position: inside; }
details.panel[open] > summary { margin-bottom: 12px; }
