:root {
  --bg: #0c0912;
  --surface: #17111f;
  --surface-2: #211a2c;
  --surface-3: #2b2238;
  --line: rgba(243, 236, 224, 0.09);
  --line-strong: rgba(243, 236, 224, 0.18);
  --text: #f3ece0;
  --muted: #a69cb3;
  --pumpkin: #ff7a1a;
  --pumpkin-hi: #ffb347;
  --pumpkin-ink: #2a1203;
  --toxic: #9be15d;
  --blood: #e0243f;
  --violet: #9b6bff;
  --radius: 20px;
  --font-display: "Creepster", "Mitr", system-ui, sans-serif;
  --font-head: "Mitr", "IBM Plex Sans Thai", system-ui, sans-serif;
  --font-body: "IBM Plex Sans Thai", "Mitr", system-ui, sans-serif;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  background:
    radial-gradient(1200px 700px at 8% -10%, rgba(155, 107, 255, 0.16), transparent 60%),
    radial-gradient(900px 600px at 100% 110%, rgba(255, 122, 26, 0.12), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 400 15px/1.55 var(--font-body);
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible {
  outline: 2px solid var(--pumpkin-hi);
  outline-offset: 3px;
}
[hidden] { display: none !important; }

svg { display: block; }

.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 24px 32px;
}

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 0 20px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 44px; height: 44px; filter: drop-shadow(0 0 12px rgba(255, 122, 26, 0.45)); }
.brand h1 {
  margin: 0;
  font: 400 32px/1 var(--font-display);
  letter-spacing: 0.02em;
  color: var(--pumpkin);
  text-shadow: 0 0 18px rgba(255, 122, 26, 0.45);
}
.brand p { margin: 2px 0 0; font: 400 13px/1.2 var(--font-head); color: var(--muted); }

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px 7px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(23, 17, 31, 0.7);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}
.status .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--pumpkin-hi);
  box-shadow: 0 0 0 0 rgba(255, 179, 71, 0.6);
  animation: pulse 1.4s infinite;
}
.status[data-state="ready"] .dot { background: var(--toxic); animation: none; box-shadow: 0 0 10px rgba(155, 225, 93, 0.7); }
.status[data-state="failed"] .dot { background: var(--blood); animation: none; }
@keyframes pulse {
  70% { box-shadow: 0 0 0 8px rgba(255, 179, 71, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 179, 71, 0); }
}

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 24px;
  align-items: start;
}

/* ---------- stage ---------- */
.stage {
  position: relative;
  height: min(calc(100vh - 220px), 780px);
  min-height: 420px;
  border-radius: var(--radius);
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 35%, #1d1528, #0a070e 75%);
  border: 1px solid var(--line);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.4),
    0 30px 80px -30px rgba(255, 122, 26, 0.25),
    inset 0 0 120px rgba(0, 0, 0, 0.5);
  isolation: isolate;
}
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
[data-mode="idle"] .stage canvas { object-fit: cover; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 24px;
  z-index: 2;
}
[data-mode="live"] .empty, [data-mode="photo"] .empty { display: none; }
.eyebrow {
  margin: 0 0 12px;
  font: 500 13px/1 var(--font-head);
  letter-spacing: 0.08em;
  color: var(--pumpkin-hi);
}
.empty h2 {
  margin: 0;
  font: 600 clamp(28px, 4vw, 44px)/1.2 var(--font-head);
  text-wrap: balance;
  text-shadow: 0 4px 30px rgba(0, 0, 0, 0.8);
}
.lead {
  margin: 14px auto 0;
  max-width: 46ch;
  color: #cfc6da;
  text-wrap: pretty;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.9);
}
.cta { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-top: 28px; }
.hint { margin: 16px 0 0; font-size: 13px; color: var(--muted); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 50px;
  padding: 0 24px;
  border-radius: 999px;
  border: 1px solid transparent;
  font: 500 16px/1 var(--font-head);
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
}
.btn svg, .ctl svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: linear-gradient(180deg, var(--pumpkin-hi), var(--pumpkin));
  color: var(--pumpkin-ink);
  box-shadow: 0 10px 30px -10px rgba(255, 122, 26, 0.8), inset 0 1px 0 rgba(255, 255, 255, 0.4);
}
.btn.primary:hover { box-shadow: 0 14px 40px -10px rgba(255, 122, 26, 1), inset 0 1px 0 rgba(255, 255, 255, 0.4); }
.btn.ghost {
  background: rgba(23, 17, 31, 0.75);
  border-color: var(--line-strong);
  backdrop-filter: blur(8px);
}
.btn.ghost:hover { background: rgba(43, 34, 56, 0.9); }

.facehint {
  position: absolute;
  top: 16px; left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(10, 7, 14, 0.7);
  border: 1px solid var(--line);
  font-size: 13px;
  color: #ddd3e8;
  white-space: nowrap;
  backdrop-filter: blur(6px);
}

.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 4;
  pointer-events: none;
  font: 400 clamp(120px, 22vw, 220px)/1 var(--font-display);
  color: var(--pumpkin);
  text-shadow: 0 0 40px rgba(255, 122, 26, 0.8);
}
.countdown:not(:empty) { animation: tick 1s ease-out infinite; }
@keyframes tick {
  0% { transform: scale(1.4); opacity: 0; }
  20% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.9); opacity: 0.2; }
}

.flash {
  position: absolute; inset: 0; z-index: 5;
  background: #fff; opacity: 0; pointer-events: none;
}
.flash.on { animation: flash 0.5s ease-out; }
@keyframes flash { 0% { opacity: 0.9; } 100% { opacity: 0; } }

.drop {
  position: absolute; inset: 12px; z-index: 6;
  display: grid; place-items: center;
  border: 2px dashed var(--pumpkin);
  border-radius: calc(var(--radius) - 6px);
  background: rgba(12, 9, 18, 0.75);
  font: 500 20px var(--font-head);
  color: var(--pumpkin-hi);
  opacity: 0; pointer-events: none;
  transition: opacity 0.15s;
}
.stage.dragging .drop { opacity: 1; }

.toast {
  position: absolute;
  left: 50%; bottom: 18px;
  z-index: 7;
  max-width: calc(100% - 32px);
  transform: translate(-50%, 12px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(23, 17, 31, 0.92);
  border: 1px solid var(--line-strong);
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  backdrop-filter: blur(8px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- controls ---------- */
.controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  min-height: 96px;
  padding: 12px 4px 0;
}
[data-mode="idle"] .controls { visibility: hidden; }
.side { display: flex; gap: 6px; }
.side.right { justify-content: flex-end; }
.center { display: flex; justify-content: center; }

.ctl {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 72px;
  padding: 8px 10px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--muted);
  font: 400 12px/1.2 var(--font-head);
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.ctl:hover { background: var(--surface-2); color: var(--text); }
.ctl[aria-pressed="true"] { color: var(--pumpkin-hi); background: rgba(255, 122, 26, 0.1); }

.app [data-show] { display: none; }
[data-mode="live"] [data-show~="live"],
[data-mode="photo"] [data-show~="photo"] { display: flex; }
[data-mode="live"] .shutter[data-show~="live"] { display: grid; }

.shutter {
  position: relative;
  display: grid;
  place-items: center;
  width: 78px; height: 78px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
.shutter .ring {
  position: absolute; inset: 0;
  border-radius: 50%;
  border: 4px solid var(--text);
  box-shadow: 0 0 24px rgba(255, 122, 26, 0.35);
}
.shutter .core {
  width: 60px; height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, var(--pumpkin-hi), var(--pumpkin) 55%, #b84a06);
  transition: transform 0.15s ease;
}
.shutter:hover .core { transform: scale(0.94); }
.shutter:active .core { transform: scale(0.84); }
.shutter:disabled { opacity: 0.5; cursor: progress; }

.save { min-width: 180px; }

/* ---------- panel ---------- */
.panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.group {
  padding: 18px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--surface), rgba(23, 17, 31, 0.6));
  border: 1px solid var(--line);
}
.group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}
.group h2 { margin: 0; font: 500 16px/1.2 var(--font-head); }
.tag {
  font: 500 12px/1 var(--font-head);
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 122, 26, 0.12);
  color: var(--pumpkin-hi);
}

.filters {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.tile {
  --accent: var(--pumpkin);
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid transparent;
  background:
    radial-gradient(circle at 50% 38%, color-mix(in srgb, var(--accent) 40%, transparent), transparent 70%),
    var(--surface-2);
  box-shadow: inset 0 0 0 1px var(--line);
  transition: transform 0.15s ease, border-color 0.15s, box-shadow 0.2s;
}
.tile:hover { transform: translateY(-2px); }
.tile canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0;
  transition: opacity 0.3s;
}
.tile.has-preview canvas { opacity: 1; }
.tile .glyph {
  position: absolute; inset: 0 0 18px;
  display: grid; place-items: center;
  font-size: 34px;
  transition: opacity 0.3s;
}
.tile.has-preview .glyph { opacity: 0; }
.tile .name {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 18px 4px 7px;
  background: linear-gradient(transparent, rgba(8, 5, 12, 0.88));
  font: 500 13px/1.1 var(--font-head);
  text-align: center;
}
.tile[aria-pressed="true"] {
  border-color: var(--pumpkin);
  box-shadow: 0 0 0 3px rgba(255, 122, 26, 0.22), 0 10px 26px -10px rgba(255, 122, 26, 0.7);
}

.range {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px;
  background: transparent;
  --fill: 85%;
}
.range::-webkit-slider-runnable-track {
  height: 6px; border-radius: 99px;
  background: linear-gradient(90deg, var(--pumpkin) var(--fill), var(--surface-3) var(--fill));
}
.range::-moz-range-track { height: 6px; border-radius: 99px; background: var(--surface-3); }
.range::-moz-range-progress { height: 6px; border-radius: 99px; background: var(--pumpkin); }
.range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px; height: 22px; margin-top: -8px;
  border-radius: 50%;
  background: var(--text);
  border: 4px solid var(--pumpkin);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}
.range::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--text);
  border: 4px solid var(--pumpkin);
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 14px 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: transparent;
  color: var(--muted);
  font: 400 14px/1 var(--font-head);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.chip .ico { font-size: 16px; filter: grayscale(1) opacity(0.6); transition: filter 0.15s; }
.chip:hover { color: var(--text); }
.chip[aria-pressed="true"] {
  background: rgba(255, 122, 26, 0.12);
  border-color: rgba(255, 122, 26, 0.55);
  color: var(--text);
}
.chip[aria-pressed="true"] .ico { filter: none; }

.privacy {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin: 2px 4px 0;
  font-size: 12.5px;
  color: var(--muted);
}
.privacy svg {
  flex: none; width: 16px; height: 16px; margin-top: 2px;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round;
}

#video {
  position: fixed;
  width: 2px; height: 2px;
  left: 0; top: 0;
  opacity: 0;
  pointer-events: none;
}

/* ---------- responsive ---------- */
@media (max-width: 960px) {
  .app { padding: max(12px, env(safe-area-inset-top)) 16px 24px; }
  .top { padding-bottom: 14px; }
  .brand h1 { font-size: 26px; }
  .brand-mark { width: 36px; height: 36px; }
  .layout { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .stage { height: auto; min-height: 0; aspect-ratio: var(--ar, 3 / 4); max-height: 68vh; width: 100%; }
  [data-mode="idle"] .stage { aspect-ratio: 3 / 4; }
  .group { padding: 14px; }
  .filters {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    margin: 0 -14px;
    padding: 2px 14px 6px;
    scrollbar-width: none;
  }
  .filters::-webkit-scrollbar { display: none; }
  .tile { flex: 0 0 84px; scroll-snap-align: start; }
  .tile .name { font-size: 12px; }
}

@media (max-width: 480px) {
  .status { padding: 6px 10px; font-size: 12px; }
  .status #statusText { max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
  .brand p { display: none; }
  .brand h1 { font-size: 23px; white-space: nowrap; }
  .brand-mark { width: 30px; height: 30px; }
  .ctl { min-width: 60px; padding: 8px 6px; }
  .save { min-width: 0; padding: 0 18px; }
  .controls { gap: 6px; }
  .empty h2 br { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
