:root {
  --bg: #000;
  --fg: #f4f4f5;
  --muted: #a1a1aa;
  --panel: rgba(20, 20, 24, 0.92);
  --line: rgba(255, 255, 255, 0.14);
  --accent: #4f8cff;
  --danger: #ff6b6b;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button, select, input { font: inherit; color: inherit; }

.stage { position: fixed; inset: 0; }

#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* Start screen */
.start {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px 16px; text-align: center; background: var(--bg);
}
.start h1 { margin: 0; font-size: 28px; letter-spacing: -0.02em; }
.start p { margin: 0; max-width: 34ch; color: var(--muted); }
.start-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 8px; }
.start-actions button, .file-btn {
  min-height: 44px; padding: 10px 18px; border-radius: 999px;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06); cursor: pointer;
}
.start-actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.error { color: var(--danger); min-height: 1.4em; }

/* HUD */
.hud {
  position: absolute; z-index: 2; left: 0; right: 0; top: 0;
  display: flex; gap: 12px; justify-content: center;
  padding: calc(8px + var(--safe-t)) 16px 8px;
  font-size: 12px; font-variant-numeric: tabular-nums; color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9); pointer-events: none;
}
.hud span { background: rgba(0, 0, 0, 0.45); padding: 2px 8px; border-radius: 999px; }

/* Toolbar */
.toolbar {
  position: absolute; z-index: 3; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center; gap: 18px;
  padding: 14px 16px calc(14px + var(--safe-b));
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.55));
}
.icon {
  width: 46px; height: 46px; display: grid; place-items: center;
  border-radius: 50%; border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.45); cursor: pointer;
}
.icon svg { width: 24px; height: 24px; }
.icon.toggle[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); }
.shutter {
  width: 70px; height: 70px; border-radius: 50%; cursor: pointer;
  border: 4px solid #fff; background: rgba(255, 255, 255, 0.25);
  box-shadow: inset 0 0 0 4px rgba(0, 0, 0, 0.35);
}
.shutter:active { transform: scale(0.94); background: #fff; }
button:focus-visible, input:focus-visible + span, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Settings panel */
.panel {
  position: absolute; z-index: 4; left: 0; right: 0;
  bottom: calc(98px + var(--safe-b));
  margin: 0 auto; width: min(560px, calc(100% - 16px));
  max-height: min(62vh, 640px); overflow-y: auto; overscroll-behavior: contain;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 6px 16px 14px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
fieldset { border: 0; border-bottom: 1px solid var(--line); margin: 0; padding: 10px 0 12px; }
legend { float: left; width: 100%; padding: 0 0 6px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.row { display: flex; flex-direction: column; gap: 6px; padding: 6px 0; clear: both; }
.row .label { display: flex; justify-content: space-between; font-size: 14px; }
.row output { color: var(--muted); font-variant-numeric: tabular-nums; }
.row.check { flex-direction: row; align-items: center; gap: 10px; cursor: pointer; }
.row.check input { width: 20px; height: 20px; accent-color: var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; }
select { min-height: 40px; padding: 6px 10px; background: rgba(255, 255, 255, 0.06); border: 1px solid var(--line); border-radius: 10px; }
select option { background: #18181b; }
input[type="text"] { min-height: 40px; padding: 6px 10px; background: rgba(255, 255, 255, 0.06); border: 1px solid var(--line); border-radius: 10px; }
.hint { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { position: relative; width: 36px; height: 36px; border-radius: 50%; cursor: pointer; background: var(--c); border: 2px solid var(--line); }
.swatch input[type="radio"] { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.swatch span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.swatch:has(input:checked) { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--fg); }
.swatch:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 4px; }
.swatch.custom { background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); overflow: hidden; }
.swatch.custom input { position: absolute; inset: -8px; width: 52px; height: 52px; opacity: 0; cursor: pointer; border: 0; padding: 0; }
.swatch.custom.active { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--fg); }

.segmented { display: flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented label { flex: 1 1 auto; position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span { display: block; text-align: center; padding: 8px 10px; font-size: 14px; }
.segmented input:checked + span { background: var(--accent); color: #fff; }

.install-hint { font-size: 14px; }
.text-btn { margin-top: 12px; background: none; border: 0; color: var(--accent); padding: 8px 0; cursor: pointer; }

/* Feedback */
.toast {
  position: absolute; z-index: 6; left: 50%; transform: translateX(-50%);
  top: calc(44px + var(--safe-t)); max-width: calc(100% - 32px);
  background: rgba(0, 0, 0, 0.8); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 14px; font-size: 14px; text-align: center;
}
.flash { position: absolute; inset: 0; z-index: 7; background: #fff; opacity: 0; pointer-events: none; transition: opacity 0.25s ease-out; }
.flash.on { opacity: 0.8; transition: none; }

@media (prefers-reduced-motion: reduce) { .flash { transition: none; } }
