:root {
  --bg: #0f0f1a;
  --panel: #1b1a3a;
  --navy: #3C3B6E;
  --red: #B22234;
  --accent: #B22234;
  --accent2: #3C3B6E;
  --text: #f5f5f7;
  --muted: #a0a0b8;
  --ok: #2ecc71;
  --danger: #c0392b;
  --radius: 16px;
  --ctrl-h: 96px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* —— Brand header —— */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: calc(56px + var(--safe-t));
  padding: var(--safe-t) 14px 0;
  background: var(--navy);
  border-bottom: 3px solid var(--red);
  flex-shrink: 0;
  z-index: 25;
}
#topbar .brand-link { display: flex; align-items: center; height: 100%; min-width: 0; }
#brand-logo { height: 40px; width: auto; max-width: 100%; display: block; }
.topbar-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.8);
  white-space: nowrap;
}

/* —— Get a CamStand® CTA —— */
.btn.btn-buy {
  display: flex;
  width: 100%;
  min-height: 60px;
  font-size: 19px;
  text-decoration: none;
  background: linear-gradient(135deg, var(--red), #d23a4e);
  color: #fff;
  border: 2px solid rgba(255,255,255,0.9);
  box-shadow: 0 6px 18px rgba(178, 34, 52, 0.4);
}
.result-body .btn-buy { max-width: 520px; }
.hint.center { text-align: center; }

/* —— Stage —— */
#stage {
  position: relative;
  flex: 1;
  background: #000;
  overflow: hidden;
  min-height: 0;
}

#video, #preview-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#preview-canvas { pointer-events: none; z-index: 2; }

#flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  z-index: 10;
  transition: opacity 80ms linear;
}
#flash.on { opacity: 0.95; transition: none; }

#countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(96px, 28vw, 200px);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 24px rgba(0,0,0,0.6);
  z-index: 9;
  pointer-events: none;
}
#countdown.hidden { display: none; }

#rec-timer {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0,0,0,0.72);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: clamp(22px, 6vw, 36px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  color: #fff;
  pointer-events: none;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
#rec-timer.hidden { display: none; }
.rec-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ff2d2d;
  box-shadow: 0 0 0 0 rgba(255,45,45,0.6);
  animation: rec-pulse 1.1s ease-out infinite;
}
@keyframes rec-pulse {
  0% { box-shadow: 0 0 0 0 rgba(255,45,45,0.55); }
  70% { box-shadow: 0 0 0 10px rgba(255,45,45,0); }
  100% { box-shadow: 0 0 0 0 rgba(255,45,45,0); }
}

#watermark {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: rgba(255,255,255,0.85);
  background: rgba(0,0,0,0.35);
  padding: 5px 8px 4px;
  border-radius: 6px;
  pointer-events: none;
}
#watermark img { height: 16px; width: auto; display: block; opacity: 0.92; }
#watermark.wm-off { display: none; }

#status {
  position: absolute;
  top: calc(12px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  z-index: 8;
  background: rgba(0,0,0,0.65);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 14px;
  max-width: 90%;
  text-align: center;
}
#status.hidden { display: none; }
body.recording #status { top: 68px; }

/* —— Mode switch —— */
#mode-bar {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px 0;
  background: var(--panel);
  flex-shrink: 0;
  z-index: 20;
}
.mode-btn {
  border: 1px solid rgba(255,255,255,0.12);
  background: #12122a;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  padding: 8px 18px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 40px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.mode-btn.active {
  background: var(--navy);
  color: #fff;
  border-color: rgba(255,255,255,0.28);
}
.mode-btn[data-mode="video"].active {
  background: var(--red);
  border-color: rgba(255,255,255,0.35);
}
body.recording #mode-bar { opacity: 0.45; pointer-events: none; }

/* —— Controls —— */
#controls {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 8px;
  height: calc(var(--ctrl-h) + var(--safe-b));
  padding: 12px 12px calc(12px + var(--safe-b));
  background: var(--panel);
  z-index: 20;
  flex-shrink: 0;
}

.btn {
  border: none;
  border-radius: 999px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  color: var(--text);
  background: var(--accent2);
  min-width: 56px;
  min-height: 56px;
  padding: 0 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.1s, opacity 0.15s, background 0.15s;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.btn-primary {
  background: var(--accent);
  font-size: 20px;
  min-width: 140px;
  min-height: 68px;
  box-shadow: 0 6px 20px rgba(178, 34, 52, 0.45);
}
.btn-primary.btn-record {
  background: #e11d2e;
  box-shadow: 0 6px 22px rgba(225, 29, 46, 0.55);
}
.btn-primary.btn-stop {
  background: #222;
  border: 2px solid #e11d2e;
  box-shadow: 0 0 0 3px rgba(225, 29, 46, 0.25);
}
.btn-secondary { background: #2a2a48; font-size: 22px; }
.btn-danger { background: var(--danger); }
.btn.hidden { display: none; }
.btn-close {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 22px;
  width: 44px;
  height: 44px;
  cursor: pointer;
  border-radius: 50%;
}
.btn-close:active { background: rgba(255,255,255,0.1); }

/* —— Panels —— */
.panel {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: rgba(15, 15, 26, 0.96);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel.hidden { display: none; }
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-t)) 16px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}
.panel-header h2 { font-size: 20px; }
.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(24px + var(--safe-b));
}
.panel-body h3 {
  font-size: 15px;
  margin: 8px 0 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 15px;
}
.field span { color: var(--muted); font-weight: 600; }
.field select, .field input[type="text"], .field input[type="number"] {
  background: #12122a;
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 14px;
  font-size: 16px;
  min-height: 48px;
}
.field.check {
  flex-direction: row;
  align-items: center;
  gap: 12px;
}
.field.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.field.check span { color: var(--text); }
.field select:disabled { opacity: 0.5; }
#camera-field select { text-overflow: ellipsis; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.4; margin-top: 4px; }
hr { border: none; border-top: 1px solid rgba(255,255,255,0.08); margin: 18px 0; }
.hidden { display: none !important; }

/* Result */
.result-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
#result-img, #result-video {
  max-width: 100%;
  max-height: 55vh;
  border-radius: 12px;
  object-fit: contain;
  background: #000;
}
#result-video {
  width: 100%;
  max-width: 420px;
}
.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  width: 100%;
}
.result-actions .btn { flex: 1 1 140px; min-height: 56px; }

/* Gallery */
#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.gallery-item {
  position: relative;
  aspect-ratio: 3/4;
  border-radius: 10px;
  overflow: hidden;
  background: #12122a;
}
.gallery-item img,
.gallery-item video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.gallery-item .play-badge {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  pointer-events: none;
  border: 2px solid rgba(255,255,255,0.85);
}
.gallery-item .del {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  z-index: 2;
}
.gallery-item .dl {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  z-index: 2;
}
.gallery-actions { margin-top: 16px; text-align: center; }

/* Landscape: controls as side rail on wider short screens */
@media (orientation: landscape) and (max-height: 500px) {
  #app { flex-direction: row; }
  #mode-bar {
    position: absolute;
    bottom: 8px;
    left: 8px;
    right: calc(88px + env(safe-area-inset-right, 0px));
    background: transparent;
    padding: 0;
    z-index: 22;
    justify-content: flex-start;
  }
  .mode-btn { background: rgba(18,18,42,0.75); backdrop-filter: blur(6px); font-size: 12px; padding: 6px 12px; min-height: 34px; }
  #controls {
    flex-direction: column;
    width: calc(88px + env(safe-area-inset-right, 0px));
    height: 100%;
    padding: 12px calc(8px + env(safe-area-inset-right, 0px)) 12px 8px;
  }
  .btn-primary {
    min-width: 64px;
    min-height: 64px;
    writing-mode: horizontal-tb;
    font-size: 16px;
    padding: 8px;
  }
  #watermark { bottom: 8px; right: 8px; }
  #topbar {
    position: absolute;
    top: 0; left: 0;
    right: calc(88px + env(safe-area-inset-right, 0px));
    height: 44px;
    padding: 0 12px;
    background: rgba(27, 26, 58, 0.82);
  }
  #brand-logo { height: 30px; }
}

@media (min-width: 768px) {
  .btn-primary { min-width: 180px; font-size: 22px; }
  #gallery-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
}

/* Landscape phones: keep the rail compact (overrides the min-width:768px rule) and clear the brand bar */
@media (orientation: landscape) and (max-height: 500px) {
  .btn-primary { min-width: 64px; font-size: 15px; padding: 6px; }
  #status { top: 52px; }
  body.recording #status { top: 58px; }
}
