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

:root {
  --bg: #0b0b14;
  --surface: #151525;
  --border: #252545;
  --primary: #e94560;
  --primary-glow: rgba(233, 69, 96, 0.4);
  --text: #f0f0f0;
  --muted: #777;
  --radius: 14px;
}

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

/* ---- Badge (top-left) ---- */

.badge {
  position: fixed;
  top: 0.8rem;
  left: 0.8rem;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: rgba(11, 11, 20, 0.8);
  backdrop-filter: blur(16px);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.badge-title {
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: 2px;
  color: var(--primary);
}

.badge nav { display: flex; gap: 0.25rem; }

.nav-btn {
  padding: 0.3rem 0.75rem;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: 0.15s;
}
.nav-btn:hover { border-color: var(--primary); color: var(--text); }
.nav-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }

/* ---- AI Picker (top-right) ---- */

.ai-picker {
  position: fixed;
  top: 0.8rem;
  right: 0.8rem;
  z-index: 50;
}

.ai-picker-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.75rem;
  background: rgba(11, 11, 20, 0.8);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.15s;
  white-space: nowrap;
}

.ai-picker-btn:hover { border-color: var(--primary); color: var(--text); }

.ai-picker-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--primary);
  flex-shrink: 0;
}

.ai-picker-chevron {
  transition: transform 0.2s;
}

.ai-picker.open .ai-picker-chevron {
  transform: rotate(180deg);
}

.ai-picker-menu {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  min-width: 160px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.3rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  animation: ai-menu-in 0.15s ease-out;
}

.ai-picker-menu[hidden] { display: none; }

@keyframes ai-menu-in {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.ai-opt {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.45rem 0.65rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  border-radius: 10px;
  cursor: pointer;
  transition: 0.12s;
  text-align: left;
}

.ai-opt:hover { background: rgba(233, 69, 96, 0.1); color: var(--text); }
.ai-opt.active { color: var(--primary); font-weight: 600; }
.ai-opt.disabled { opacity: 0.35; cursor: not-allowed; }
.ai-opt.disabled:hover { background: transparent; color: var(--muted); }

.ai-opt-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}

.ai-opt.active .ai-opt-dot { background: var(--primary); }

/* ---- Layout ---- */

main { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.section { display: none; }
.section.active { display: flex; flex-direction: column; flex: 1; min-height: 0; }

/* ---- Camera area ---- */

.cam-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0.5rem;
}

.cam-frame {
  position: relative;
  max-height: 100%;
  max-width: 100%;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  border: 2px solid var(--border);
  flex-shrink: 1;
  min-height: 0;
}

.cam-frame video, .cam-frame > img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cam-frame video {
  transform: scaleX(-1);
}

.cam-frame video.rear {
  transform: none;
}

.cam-frame video[hidden], .cam-frame > img[hidden] {
  display: none;
}

.btn-flip {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 10;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: 0.15s;
}

.btn-flip:hover { background: rgba(233, 69, 96, 0.6); border-color: var(--primary); }
.btn-flip[hidden] { display: none; }

.cam-buttons {
  display: flex;
  justify-content: center;
  gap: 0.6rem;
  flex-shrink: 0;
  padding-top: 0.5rem;
}

.btn-capture {
  padding: 0.6rem 1.8rem;
  border-radius: 999px;
  border: 2px solid #fff;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.2s;
}
.btn-capture:hover { background: rgba(255, 255, 255, 0.25); }
.btn-capture[hidden] { display: none; }

.btn-retake {
  padding: 0.6rem 1.4rem;
  border-radius: 999px;
  border: 2px solid var(--primary);
  background: rgba(233, 69, 96, 0.15);
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.2s;
}
.btn-retake:hover { background: rgba(233, 69, 96, 0.3); }

.btn-retake[hidden] { display: none; }

/* ---- Bottom panel (patricks + go button) ---- */

.bottom-panel {
  flex-shrink: 0;
  position: relative;
  display: flex;
  justify-content: center;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.bottom-panel .btn-go {
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
}

.patricks-strip {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  padding: 0.2rem 0;
}

.patricks-strip::-webkit-scrollbar { height: 0; }

.p-card {
  flex-shrink: 0;
  width: 90px;
  height: 110px;
  border-radius: 10px;
  overflow: hidden;
  border: 3px solid transparent;
  cursor: pointer;
  transition: 0.2s;
  background: var(--bg);
  scroll-snap-align: start;
  position: relative;
}
.p-card img { width: 100%; height: 100%; display: block; object-fit: cover; }
.p-card:hover { border-color: rgba(233, 69, 96, 0.5); }
.p-card.sel { border-color: var(--primary); box-shadow: 0 0 14px var(--primary-glow); }

.p-card .p-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 2px 4px;
  background: rgba(0,0,0,0.75);
  font-size: 0.6rem;
  text-align: center;
  text-transform: capitalize;
}

/* ---- Go button ---- */

.btn-go {
  flex-shrink: 0;
  padding: 0.9rem 2rem;
  border: none;
  border-radius: 999px;
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), #ff6348);
  box-shadow: 0 4px 20px var(--primary-glow);
  cursor: pointer;
  transition: 0.2s;
  white-space: nowrap;
}
.btn-go:disabled {
  opacity: 0.3;
  filter: grayscale(1);
  box-shadow: none;
  cursor: not-allowed;
}
.btn-go:hover:not(:disabled) {
  box-shadow: 0 0 30px var(--primary-glow), 0 0 60px rgba(233, 69, 96, 0.2);
}

/* ---- Lightbox ---- */

.lightbox[hidden] { display: none; }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  backdrop-filter: blur(10px);
}

.lightbox-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

#lightbox-img {
  max-width: 90vw;
  max-height: 85vh;
  border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}

#lightbox-img[hidden] { display: none; }

.btn-close {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 2;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(11, 11, 20, 0.7);
  backdrop-filter: blur(8px);
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: 0.2s;
}
.btn-close:hover { background: var(--primary); border-color: var(--primary); }

/* ---- Lightbox error ---- */

.lb-error[hidden] { display: none; }

.lb-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8rem;
  max-width: 340px;
  padding: 2rem 2.2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
  animation: lb-err-in 0.25s ease-out;
}

@keyframes lb-err-in {
  from { opacity: 0; transform: scale(0.92) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.lb-error-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(233, 69, 96, 0.15);
  border: 2px solid var(--primary);
  color: var(--primary);
  font-size: 1.5rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.lb-error-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
}

.lb-error-msg {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  word-break: break-word;
}

.lb-error-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.4rem;
  width: 100%;
}

.lb-err-btn {
  flex: 1;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.15s;
  border: none;
}

.lb-err-btn--retry {
  background: var(--primary);
  color: #fff;
}
.lb-err-btn--retry:hover {
  box-shadow: 0 0 16px var(--primary-glow);
}

.lb-err-btn--switch {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}
.lb-err-btn--switch:hover {
  border-color: var(--primary);
  color: var(--text);
}

/* ---- Loader ---- */

.ldr {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.2rem;
}
.ldr[hidden] { display: none; }

.ldr-anim {
  position: relative;
  width: 72px;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ldr-ring {
  position: absolute;
  inset: 0;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-right-color: var(--primary);
  border-radius: 50%;
  animation: ldr-spin 1s cubic-bezier(0.6, 0, 0.4, 1) infinite;
}

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

.ldr-coin {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--primary);
  animation: ldr-pulse 1.5s ease-in-out infinite;
  z-index: 1;
}

@keyframes ldr-pulse {
  0%, 100% { transform: scale(1); opacity: 0.7; }
  50% { transform: scale(1.2); opacity: 1; }
}

.ldr-text {
  color: var(--muted);
  font-style: italic;
  font-size: 0.9rem;
  text-align: center;
  min-height: 1.4em;
  animation: ldr-fade 0.4s ease-out;
}

@keyframes ldr-fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- History ---- */

#section-history { padding: 0; }

.history-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 3.5rem 1.5rem 1.5rem;
}

.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}

.h-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: 0.2s;
}
.h-card:hover { transform: translateY(-4px); box-shadow: 0 8px 24px rgba(0,0,0,0.4); }

.h-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  display: block;
}

.h-card .h-info { padding: 0.6rem 0.8rem; }
.h-card .h-name { font-weight: 600; text-transform: capitalize; font-size: 0.9rem; }
.h-card .h-date { color: var(--muted); font-size: 0.7rem; margin-top: 0.1rem; }

.empty-msg {
  text-align: center;
  color: var(--muted);
  padding: 4rem 0;
  font-style: italic;
}

/* ---- Responsive ---- */

@media (max-width: 768px) {
  .cam-area { padding: 0.4rem; }
  .cam-frame { aspect-ratio: 3 / 4; }

  .p-card { width: 75px; height: 92px; border-radius: 8px; }
  .p-card .p-label { font-size: 0.5rem; }

  .btn-go { font-size: 0.9rem; padding: 0.7rem 1.4rem; }

  .history-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }

  .lb-error { max-width: 90vw; padding: 1.5rem 1.2rem; }
}

@media (max-width: 640px) {
  .badge { top: 0.5rem; left: 0.5rem; padding: 0.35rem 0.7rem; }
  .badge-title { font-size: 0.75rem; letter-spacing: 1.5px; }
  .nav-btn { padding: 0.25rem 0.55rem; font-size: 0.68rem; }

  .ai-picker { top: 0.5rem; right: 0.5rem; }
  .ai-picker-btn { padding: 0.35rem 0.6rem; font-size: 0.68rem; }

  .bottom-panel {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    padding: 0.5rem 0;
    overflow: hidden;
  }

  .bottom-panel .btn-go {
    position: static;
    transform: none;
    margin: 0 0.6rem;
  }

  .patricks-strip {
    justify-content: flex-start;
    overflow-x: scroll;
    scroll-padding: 0 0.6rem;
    padding: 0.2rem 0.6rem;
    touch-action: pan-x;
  }

  .p-card { width: 64px; height: 78px; }

  .btn-capture { padding: 0.5rem 1.2rem; font-size: 0.8rem; }
  .btn-retake { padding: 0.5rem 1rem; font-size: 0.78rem; }

  .history-scroll { padding: 3rem 0.8rem 1rem; }
  .history-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.6rem; }
  .h-card .h-info { padding: 0.4rem 0.6rem; }
  .h-card .h-name { font-size: 0.8rem; }
}

@media (max-width: 380px) {
  .badge-title { display: none; }

  .p-card { width: 56px; height: 68px; border-width: 2px; }

  .btn-go { font-size: 0.8rem; padding: 0.6rem 1rem; }

  .ldr-text { font-size: 0.78rem; padding: 0 0.5rem; }

  .history-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
}
