:root {
  color-scheme: dark;
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #10b981;
  --danger: #ef4444;
  --btn-bg: #334155;
  --btn-hover: #475569;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans TC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: pan-y;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.player {
  position: fixed;
  top: -10px;
  left: -10px;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 1320px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  gap: 12px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 10px 14px;
}

.title-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.title-group h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.status-badge {
  font-size: 0.8rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(59, 130, 246, 0.18);
  color: #93c5fd;
  border: 1px solid rgba(59, 130, 246, 0.35);
}

.status-badge.recording {
  background: rgba(239, 68, 68, 0.22);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.5);
}

.status-badge.warn {
  background: rgba(245, 158, 11, 0.18);
  color: #fcd34d;
  border-color: rgba(245, 158, 11, 0.35);
}

.source-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

select {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #0f172a;
  color: var(--text);
  font-size: 0.9rem;
  color-scheme: dark;
}

select option {
  background: #0f172a;
  color: var(--text);
}

select#videoSource {
  max-width: 220px;
}

.full-select {
  width: 100%;
}

.main-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(290px, 1fr);
  gap: 12px;
  flex: 1;
  align-items: start;
}

.stage-card,
.settings-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  font-size: 0.82rem;
  color: var(--muted);
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid var(--panel-border);
  padding: 4px 10px;
  border-radius: 999px;
}

.canvas-box {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #020617;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--panel-border);
  touch-action: none;
}

.canvas-box canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  touch-action: none;
}

.countdown-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 5rem;
  font-weight: 800;
  color: #f8fafc;
  background: rgba(2, 6, 23, 0.55);
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.7);
  pointer-events: none;
}

.countdown-overlay[hidden] {
  display: none;
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, background-color 0.15s ease, opacity 0.15s ease;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  flex: 1 1 150px;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-primary.stop {
  background: var(--danger);
}

.btn-accent {
  background: var(--accent);
  color: #022c22;
}

.btn-secondary {
  background: var(--btn-bg);
  border-color: var(--panel-border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--btn-hover);
}

.btn-secondary.active {
  background: rgba(59, 130, 246, 0.25);
  border-color: var(--primary);
  color: #bfdbfe;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

.btn-danger:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.35);
}

.settings-card h2 {
  margin: 0;
  font-size: 1rem;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(51, 65, 85, 0.6);
}

.setting-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  color: #e2e8f0;
}

.setting-label strong {
  color: #93c5fd;
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  min-height: 28px;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn-chip {
  min-height: 36px;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: #0f172a;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-chip.active {
  background: rgba(59, 130, 246, 0.25);
  border-color: var(--primary);
  color: #bfdbfe;
}

.hint-text {
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--muted);
}

.tip-box {
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.82rem;
  line-height: 1.5;
  color: #cbd5e1;
}

.tip-box ul {
  margin: 6px 0 0 18px;
  padding: 0;
}

@media (max-width: 900px) {
  .main-layout {
    grid-template-columns: 1fr;
  }
}
