:root {
  --bg: #0b0f14;
  --bg-elevated: #121821;
  --surface: #1a2230;
  --border: rgba(255, 255, 255, 0.08);
  --text: #f2f5f9;
  --text-muted: #8b97a8;
  --accent: #3ddea8;
  --accent-dim: rgba(61, 222, 168, 0.15);
  --accent-strong: #2bc48f;
  --danger: #ff6b7a;
  --guide: #3ddea8;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --font: "IBM Plex Sans Thai", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --touch: 48px;
  --transition: 180ms ease;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
}

.hidden {
  display: none !important;
}

.app {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  max-width: 1200px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 16px calc(12px + var(--safe-bottom));
  gap: 12px;
}

/* —— Top bar —— */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background:
    radial-gradient(circle at 30% 30%, #7dffd0, transparent 50%),
    linear-gradient(135deg, var(--accent), #1a8f6a);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08), 0 8px 20px rgba(61, 222, 168, 0.25);
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--mono);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--text-muted);
  max-width: 50%;
}

.status-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status-pill.ready .dot {
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.status-pill.busy .dot {
  background: #f5c542;
  animation: pulse 1s ease infinite;
}

.status-pill.error .dot {
  background: var(--danger);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
}

/* —— Stage —— */
.stage {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  min-height: 0;
}

/* After capture only the result card is shown (camera hidden) */

.panel[hidden],
.viewport[hidden],
.controls[hidden] {
  display: none !important;
}

/* Result-only layout: center the result card */
.stage.has-result {
  justify-items: center;
}

.stage.has-result .panel {
  width: 100%;
  max-width: 420px;
}

.viewport {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  aspect-ratio: 3 / 4;
  max-height: min(68dvh, 720px);
  min-height: 280px;
}

@media (min-width: 900px) {
  .viewport {
    aspect-ratio: 4 / 3;
    max-height: none;
    height: 100%;
    min-height: 420px;
  }
}

#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
  transform: scaleX(-1); /* mirror selfie-style; points mapped accordingly */
}

#video.rear {
  transform: none;
}

/* Guide overlay — dim outside the square via box-shadow cutout */
.overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.guide {
  position: absolute;
  /* left/top/width/height set by JS so square matches viewfinder exactly */
  left: 15%;
  top: 15%;
  width: 70%;
  height: 70%;
  border: 1.5px solid rgba(61, 222, 168, 0.55);
  border-radius: 12px;
  /* Darken everything outside the guide box (true rectangular mask) */
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.55);
  background: transparent;
}

.corner {
  position: absolute;
  width: 28px;
  height: 28px;
  border-color: var(--guide);
  border-style: solid;
  border-width: 0;
}

.corner.tl {
  top: -1px;
  left: -1px;
  border-top-width: 3px;
  border-left-width: 3px;
  border-top-left-radius: 10px;
}

.corner.tr {
  top: -1px;
  right: -1px;
  border-top-width: 3px;
  border-right-width: 3px;
  border-top-right-radius: 10px;
}

.corner.bl {
  bottom: -1px;
  left: -1px;
  border-bottom-width: 3px;
  border-left-width: 3px;
  border-bottom-left-radius: 10px;
}

.corner.br {
  bottom: -1px;
  right: -1px;
  border-bottom-width: 3px;
  border-right-width: 3px;
  border-bottom-right-radius: 10px;
}

.crosshair {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  transform: translate(-50%, -50%);
}

.cross-h,
.cross-v {
  position: absolute;
  background: rgba(61, 222, 168, 0.85);
}

.cross-h {
  left: 0;
  right: 0;
  top: 50%;
  height: 1.5px;
  transform: translateY(-50%);
}

.cross-v {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1.5px;
  transform: translateX(-50%);
}

.cross-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--guide);
  border: 2px solid #0b0f14;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 3px rgba(61, 222, 168, 0.25);
}

.guide-label {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--guide);
  letter-spacing: 0.02em;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
  pointer-events: none;
}

.flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.flash.on {
  opacity: 0.75;
}

.processing {
  position: absolute;
  inset: 0;
  display: none; /* only while segmenting — see .processing.is-on */
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(11, 15, 20, 0.72);
  backdrop-filter: blur(6px);
  z-index: 5;
  pointer-events: none;
}

.processing.is-on {
  display: flex;
  pointer-events: auto;
}

/* Ensure [hidden] always wins over display:flex */
.processing[hidden] {
  display: none !important;
}

.processing p {
  margin: 0;
  font-size: 0.9375rem;
  color: var(--text);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .spinner,
  .status-pill.busy .dot {
    animation: none;
  }
  .flash {
    transition: none;
  }
}

/* —— Result panel —— */
.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.badge {
  font-family: var(--mono);
  font-size: 0.6875rem;
  color: var(--accent);
  background: var(--accent-dim);
  padding: 4px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.result-frame {
  position: relative;
  aspect-ratio: 1;
  max-width: 320px;
  width: 100%;
  margin: 0 auto;
  border-radius: var(--radius-sm);
  background:
    linear-gradient(45deg, #e8e8e8 25%, transparent 25%),
    linear-gradient(-45deg, #e8e8e8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e8e8e8 75%),
    linear-gradient(-45deg, transparent 75%, #e8e8e8 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #fff;
  border: 1px solid var(--border);
  overflow: hidden;
  display: grid;
  place-items: center;
}

#resultImage {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  image-rendering: auto;
}

#resultImage[hidden] {
  display: none !important;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

/* —— Controls —— */
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 8px 0 4px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--touch);
  padding: 0 18px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition:
    background var(--transition),
    border-color var(--transition),
    opacity var(--transition),
    transform 100ms ease;
  color: var(--text);
  background: var(--surface);
}

.btn:hover {
  border-color: rgba(255, 255, 255, 0.12);
}

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

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn.secondary {
  background: var(--accent);
  color: #062018;
}

.btn.secondary:hover {
  background: var(--accent-strong);
  border-color: transparent;
}

.btn.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}

.btn.icon {
  width: var(--touch);
  height: var(--touch);
  padding: 0;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.shutter {
  position: relative;
  width: 76px;
  height: 76px;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  padding: 0;
}

.shutter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
}

.shutter:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.shutter-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid #fff;
}

.shutter-core {
  position: absolute;
  inset: 8px;
  border-radius: 50%;
  background: #fff;
  transition:
    transform 100ms ease,
    background var(--transition);
}

.shutter:not(:disabled):active .shutter-core {
  transform: scale(0.9);
  background: var(--accent);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 100;
  max-width: min(92vw, 400px);
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.875rem;
  text-align: center;
}

.toast.error {
  border-color: rgba(255, 107, 122, 0.4);
  color: #ffc9cf;
}
