:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #8b93a5;
  --accent: #4f8cff;
  --add: #2fbf71;
  --update: #f2b233;
  --remove: #ef5b5b;
  --flash-add: rgba(47, 191, 113, 0.28);
  --flash-update: rgba(242, 178, 51, 0.28);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #f0f2f6;
    --border: #dde1e8;
    --text: #1b1f27;
    --muted: #677084;
    --flash-add: rgba(47, 191, 113, 0.22);
    --flash-update: rgba(242, 178, 51, 0.3);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Segoe UI", sans-serif;
}

h1, h2 { margin: 0; font-weight: 600; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 18px; }
.topbar h1 span { color: var(--muted); font-weight: 400; font-size: 14px; margin-left: 6px; }
.topbar { flex-wrap: wrap; }
.topbar-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px 12px; min-width: 0; }
.status { color: var(--muted); font-size: 13px; }
.camera-select {
  max-width: 220px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 13px;
}
.camera-select[hidden] { display: none; }

.btn {
  border: 0;
  border-radius: 8px;
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: progress; }
.btn[hidden] { display: none; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.is-stop { background: var(--remove); }

/* ---------- layout ---------- */
.layout {
  max-width: 1120px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  gap: 16px;
}

.stage { display: flex; justify-content: center; }
.frame {
  /* --ar 由 JS 依影片/照片比例設定；高度上限 56vh，直式照片也不會把下方文字擠出畫面 */
  --ar: 16 / 9;
  position: relative;
  width: min(100%, calc(56vh * var(--ar)));
  aspect-ratio: var(--ar);
  background: #000;
  border-radius: 12px;
  overflow: hidden;
}
.frame video,
.frame img,
.frame canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
/* 前鏡頭鏡像：影片翻轉；canvas 不翻轉，繪圖時自行換算座標，文字才不會反過來 */
.frame.is-mirrored video { transform: scaleX(-1); }
.placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #9aa3b5;
}
.frame.is-live .placeholder { display: none; }
.frame img { display: none; }
.frame.is-photo img { display: block; }
.frame.is-photo video { display: none; }
.frame.is-dragover { outline: 3px dashed var(--accent); outline-offset: -3px; }
.placeholder { text-align: center; padding: 16px; }

/* ---------- panel ---------- */
.panel { display: grid; gap: 12px; }

.settings {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 24px;
  align-items: center;
  font-size: 13px;
  color: var(--muted);
}
.settings label { display: flex; align-items: center; gap: 8px; }
.settings output { color: var(--text); font-variant-numeric: tabular-nums; }
.settings input[type="range"] { width: 140px; }

.groups {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}
@media (max-width: 760px) {
  .groups { grid-template-columns: 1fr; }
}

.group, .log-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  min-width: 0;
}
.group h2, .log-box h2 { font-size: 14px; margin-bottom: 8px; }
.group h2 small { color: var(--muted); font-weight: 400; margin-left: 4px; }

.rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.rows:empty::before { content: "—"; color: var(--muted); padding: 6px 8px; }

.row {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: baseline;
  gap: 2px 12px;
  padding: 6px 8px;
  border-radius: 8px;
  background: var(--surface-2);
  transition: opacity 0.5s ease;
}
.row-label { color: var(--muted); font-size: 13px; white-space: nowrap; }
.row-value { font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.row-prev {
  grid-column: 2;
  text-align: right;
  font-size: 12px;
  color: var(--muted);
  text-decoration: line-through;
  opacity: 0;
  height: 0;
  overflow: hidden;
}
.row-prev.show { animation: prev-fade 2.4s ease forwards; }
.row.is-leaving { opacity: 0.25; text-decoration: line-through; }

.row.flash-add { animation: flash-add 1.4s ease; }
.row.flash-update { animation: flash-update 1.4s ease; }

@keyframes flash-add { from { background: var(--flash-add); } to { background: var(--surface-2); } }
@keyframes flash-update { from { background: var(--flash-update); } to { background: var(--surface-2); } }
@keyframes prev-fade {
  0%, 70% { opacity: 1; height: 1.4em; }
  100% { opacity: 0; height: 0; }
}

/* ---------- change log ---------- */
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.log:empty::before { content: "尚無變化"; color: var(--muted); }
.log li { display: flex; gap: 8px; padding: 2px 0; border-bottom: 1px dashed var(--border); }
.log time { color: var(--muted); flex: none; }
.log .op { flex: none; width: 1em; text-align: center; font-weight: 700; }
.log .op-add { color: var(--add); }
.log .op-update { color: var(--update); }
.log .op-remove { color: var(--remove); }

.stats { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
