/* ========================================
   CHNB Open House #17 — Photobooth
   ✨ Magic / Wizard Theme ✨
   ======================================== */

/* --- Reset & Variables --- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    /* Deep mystical palette */
    --bg-deep: #0a0a1a;
    --bg-mid: #120e24;
    --bg-card: rgba(30, 20, 60, 0.55);
    --border: rgba(180, 160, 255, 0.15);
    --border-glow: rgba(180, 160, 255, 0.3);

    /* Magic colors */
    --gold: #f0c040;
    --gold-dim: #c49a2a;
    --gold-glow: rgba(240, 192, 64, 0.4);
    --purple: #9b59b6;
    --purple-deep: #6c3483;
    --purple-glow: rgba(155, 89, 182, 0.35);
    --blue-magic: #5dade2;
    --blue-glow: rgba(93, 173, 226, 0.3);
    --emerald: #2ecc71;
    --emerald-glow: rgba(46, 204, 113, 0.3);
    --crimson: #c0392b;

    --text: #ede8f5;
    --text-dim: rgba(220, 210, 240, 0.5);
    --glass: blur(18px);
    --radius: 16px;
    --radius-sm: 10px;
    --font: 'Outfit', system-ui, sans-serif;
    --shadow-magic: 0 8px 40px rgba(100, 60, 180, 0.25);
}

html {
    font-size: 16px;
}

body {
    background: var(--bg-deep);
    color: var(--text);
    font-family: var(--font);
    min-height: 100vh;
    overflow-x: hidden;
    /* Magical gradient background */
    background:
        radial-gradient(ellipse at 20% 0%, rgba(100, 50, 180, 0.2) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 100%, rgba(40, 20, 120, 0.25) 0%, transparent 50%),
        radial-gradient(ellipse at 50% 50%, rgba(20, 10, 50, 1) 0%, #0a0a1a 100%);
    background-attachment: fixed;
}

.hidden {
    display: none !important;
}

/* ========================================
   FLOATING SPARKLES (canvas overlay)
   ======================================== */
#sparkle-canvas {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.page {
    position: relative;
    z-index: 1;
}

/* ========================================
   SITE HEADER — Magical Banner
   ======================================== */
.site-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 28px;
    background: linear-gradient(180deg, rgba(18, 12, 40, 0.95) 0%, rgba(18, 12, 40, 0.8) 100%);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    box-shadow:
        0 4px 30px rgba(100, 60, 180, 0.15),
        inset 0 -1px 0 rgba(240, 192, 64, 0.1);
}

.header-logo {
    width: 52px;
    height: 52px;
    object-fit: contain;
    flex-shrink: 0;
    filter: drop-shadow(0 0 8px rgba(240, 192, 64, 0.4));
    animation: logo-glow 3s ease-in-out infinite;
}

@keyframes logo-glow {
    0%, 100% { filter: drop-shadow(0 0 6px rgba(240, 192, 64, 0.3)); }
    50% { filter: drop-shadow(0 0 14px rgba(240, 192, 64, 0.6)); }
}

.header-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.header-title-en {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: 2px;
    font-family: 'Cinzel', serif;
    background: linear-gradient(135deg, var(--gold), #ffe082, var(--gold-dim));
    background-size: 200% 100%;
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: shimmer 4s ease-in-out infinite;
    line-height: 1.2;
    text-transform: uppercase;
}

@keyframes shimmer {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.header-title-th {
    font-size: 0.82rem;
    font-weight: 400;
    color: rgba(200, 180, 240, 0.6);
    letter-spacing: 0.3px;
    line-height: 1.3;
}

/* Magic star decorations for header */
.site-header::after {
    content: '✦';
    position: absolute;
    right: 28px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.4rem;
    color: var(--gold);
    opacity: 0.5;
    animation: star-twinkle 2s ease-in-out infinite;
}

@keyframes star-twinkle {
    0%, 100% { opacity: 0.3; transform: translateY(-50%) scale(0.9); }
    50% { opacity: 0.7; transform: translateY(-50%) scale(1.1); }
}

/* ========================================
   PAGES
   ======================================== */
.page {
    width: 100%;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    padding-top: 90px;
}

/* ========================================
   CAMERA VIEW
   ======================================== */
.camera-layout {
    display: flex;
    gap: 28px;
    max-width: 900px;
    width: 100%;
    align-items: stretch;
    justify-content: center;
}

.camera-side {
    flex: 0 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

/* --- Camera Frame with Magic Border --- */
.cam-wrapper {
    position: relative;
    width: 100%;
    max-width: 420px;
    min-width: 280px;
    aspect-ratio: 412.7 / 460.2;
    border-radius: var(--radius);
    overflow: hidden;
    background: #000;
    box-shadow: var(--shadow-magic);
    /* Animated magic border */
    border: 2px solid transparent;
    background-clip: padding-box;
}

.cam-wrapper::before {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: calc(var(--radius) + 3px);
    background: linear-gradient(135deg, var(--gold), var(--purple), var(--blue-magic), var(--gold));
    background-size: 300% 300%;
    animation: magic-border 6s ease-in-out infinite;
    z-index: -1;
    opacity: 0.7;
}

@keyframes magic-border {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

#webcam {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
    display: block;
    position: relative;
    z-index: 1;
    border-radius: calc(var(--radius) - 2px);
}

/* --- Photo Badge --- */
.photo-badge {
    position: absolute;
    top: 14px;
    left: 14px;
    background: rgba(10, 10, 30, 0.7);
    backdrop-filter: blur(10px);
    border: 1px solid var(--border-glow);
    padding: 6px 16px;
    border-radius: 30px;
    font-size: 0.95rem;
    font-weight: 600;
    color: #fff;
    z-index: 5;
    letter-spacing: 0.5px;
}

#photo-count {
    color: var(--gold);
    font-weight: 700;
    font-size: 1.1rem;
}

/* --- Countdown Overlay --- */
.countdown {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(6rem, 20vw, 12rem);
    font-weight: 700;
    color: var(--gold);
    background: rgba(10, 5, 25, 0.55);
    z-index: 10;
    animation: countdown-pop 0.4s ease-out;
    text-shadow:
        0 0 30px var(--gold-glow),
        0 0 60px var(--purple-glow),
        0 0 100px rgba(240, 192, 64, 0.15);
}

@keyframes countdown-pop {
    0% { transform: scale(1.8); opacity: 0; }
    60% { transform: scale(0.95); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

/* --- Controls --- */
.cam-controls {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
}

/* ========================================
   BUTTONS — Enchanted Style
   ======================================== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    border: none;
    border-radius: 50px;
    padding: 14px 32px;
    font-size: 1.05rem;
    font-weight: 600;
    font-family: var(--font);
    color: #fff;
    cursor: pointer;
    transition: all 0.3s ease;
    outline: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    overflow: hidden;
}

.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255,255,255,0.15) 0%, transparent 50%);
    opacity: 0;
    transition: opacity 0.3s;
}

.btn:hover::after {
    opacity: 1;
}

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

.btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    transform: none !important;
}

/* Capture Button — Golden Magic */
.btn-capture {
    background: linear-gradient(135deg, var(--gold-dim), var(--gold), #ffe082);
    color: #1a0e30;
    box-shadow: 0 4px 25px var(--gold-glow);
    font-size: 1.15rem;
    padding: 16px 40px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.btn-capture:hover:not(:disabled) {
    box-shadow:
        0 6px 35px var(--gold-glow),
        0 0 60px rgba(240, 192, 64, 0.15);
    transform: translateY(-3px);
}

/* Download — Purple Spell */
.btn-download {
    background: linear-gradient(135deg, var(--purple-deep), var(--purple), #bb8fce);
    box-shadow: 0 4px 22px var(--purple-glow);
}

.btn-download:hover {
    transform: translateY(-3px);
    box-shadow:
        0 6px 30px var(--purple-glow),
        0 0 50px rgba(155, 89, 182, 0.15);
}

/* Restart */
.btn-restart {
    background: rgba(180, 160, 255, 0.08);
    border: 1px solid var(--border-glow);
    box-shadow: none;
}

.btn-restart:hover {
    background: rgba(180, 160, 255, 0.15);
    transform: translateY(-2px);
    border-color: rgba(180, 160, 255, 0.4);
}

/* Finish — Emerald Enchantment */
.btn-finish {
    background: linear-gradient(135deg, #1a7a42, var(--emerald), #58d68d);
    color: #0a2e1a;
    box-shadow: 0 4px 22px var(--emerald-glow);
    width: 100%;
    justify-content: center;
    margin-top: 8px;
    font-weight: 700;
}

.btn-finish:hover {
    transform: translateY(-2px);
    box-shadow:
        0 6px 30px var(--emerald-glow),
        0 0 50px rgba(46, 204, 113, 0.12);
}

/* ========================================
   THUMBNAIL SIDE — Spell Book
   ======================================== */
.thumb-side {
    width: 260px;
    flex-shrink: 0;
    align-self: stretch;
    background: var(--bg-card);
    backdrop-filter: var(--glass);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow-magic);
    position: relative;
    display: flex;
    flex-direction: column;
}

/* Top decorative line */
.thumb-side::before {
    content: '⚝ ─── ✦ ─── ⚝';
    display: block;
    text-align: center;
    font-size: 0.7rem;
    letter-spacing: 4px;
    color: var(--gold);
    opacity: 0.35;
    margin-bottom: 10px;
}

.thumb-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 14px;
    color: rgba(200, 180, 240, 0.65);
    text-align: center;
    letter-spacing: 0.5px;
}

.thumb-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.thumb-slot {
    aspect-ratio: 412.7 / 460.2;
    background: rgba(100, 60, 180, 0.06);
    border: 2px dashed rgba(180, 160, 255, 0.18);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    font-weight: 700;
    color: rgba(180, 160, 255, 0.15);
    overflow: hidden;
    transition: all 0.3s ease;
    position: relative;
}

.thumb-slot.filled {
    border: 2px solid var(--gold);
    background: transparent;
    box-shadow: 0 0 10px rgba(240, 192, 64, 0.15);
}

.thumb-slot.filled span {
    display: none;
}

.thumb-slot.active {
    border-color: var(--purple);
    border-style: solid;
    box-shadow: 0 0 15px var(--purple-glow);
    animation: magic-pulse 1.8s ease-in-out infinite;
}

@keyframes magic-pulse {
    0%, 100% {
        box-shadow: 0 0 8px var(--purple-glow);
        border-color: var(--purple);
    }
    50% {
        box-shadow: 0 0 22px var(--purple-glow), 0 0 40px rgba(155, 89, 182, 0.1);
        border-color: #bb8fce;
    }
}

.thumb-slot img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* --- Retake Area --- */
.retake-area {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.retake-label {
    font-size: 0.85rem;
    color: var(--text-dim);
    margin-bottom: 10px;
    text-align: center;
}

.retake-buttons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.btn-retake-single {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: rgba(192, 57, 43, 0.12);
    border: 1px solid rgba(192, 57, 43, 0.25);
    color: #e88a82;
    border-radius: 8px;
    padding: 8px 4px;
    font-size: 0.8rem;
    font-weight: 600;
    font-family: var(--font);
    cursor: pointer;
    transition: all 0.2s;
}

.btn-retake-single:hover {
    background: rgba(192, 57, 43, 0.22);
    transform: translateY(-1px);
    border-color: rgba(192, 57, 43, 0.4);
}

.btn-retake-single:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: none;
}

/* ========================================
   RESULT VIEW — Centered QR Focus
   ======================================== */
.result-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
    max-width: 520px;
    width: 100%;
    text-align: center;
}

.result-title {
    font-size: 1.7rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--gold), #ffe082, var(--gold-dim));
    background-size: 200% 100%;
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: shimmer 4s ease-in-out infinite;
}

/* --- QR Card --- */
.qr-card {
    width: 100%;
    background: var(--bg-card);
    backdrop-filter: var(--glass);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 28px;
    box-shadow: var(--shadow-magic);
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

/* Animated magical border on the card */
.qr-card::before {
    content: '';
    position: absolute;
    inset: -2px;
    border-radius: calc(var(--radius) + 2px);
    background: linear-gradient(135deg, var(--gold), var(--purple), var(--blue-magic), var(--gold));
    background-size: 300% 300%;
    animation: magic-border 6s ease-in-out infinite;
    z-index: -1;
    opacity: 0.5;
}

/* --- Upload Progress --- */
.upload-progress {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 20px 0;
    color: var(--text-dim);
    font-size: 1rem;
}

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

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

/* --- QR Area --- */
.qr-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    width: 100%;
}

.qr-subtitle {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    letter-spacing: 0.3px;
}

.qr-frame {
    background: #fff;
    border-radius: 14px;
    padding: 16px;
    box-shadow:
        0 0 20px var(--gold-glow),
        0 0 50px rgba(240, 192, 64, 0.1);
    display: inline-block;
}

#qrcode {
    display: block;
    line-height: 0;
}

#qrcode img {
    display: block !important;
}

#qrcode canvas {
    display: none !important;
}

.qr-status {
    font-size: 0.8rem;
    color: var(--text-dim);
    line-height: 1.4;
}

/* --- Mini Preview --- */
.result-mini-preview {
    width: 160px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.result-mini-preview canvas {
    width: 100%;
    height: auto;
    border-radius: 6px;
    display: block;
}

/* --- Action Buttons --- */
.result-buttons {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    justify-content: center;
}

/* ========================================
   FLASH EFFECT — Golden Burst
   ======================================== */
.flash-overlay {
    position: fixed;
    inset: 0;
    background: radial-gradient(circle at center, rgba(240, 192, 64, 0.6), rgba(255, 255, 255, 0.8));
    opacity: 0.85;
    z-index: 9999;
    pointer-events: none;
    animation: flash-magic 0.4s ease-out forwards;
}

@keyframes flash-magic {
    0% { opacity: 0.85; }
    100% { opacity: 0; }
}

/* ========================================
   RESPONSIVE
   ======================================== */

/* Tablet */
@media (max-width: 960px) {
    .camera-layout {
        flex-direction: column;
        align-items: center;
        max-width: 480px;
    }

    .cam-wrapper {
        max-width: 400px;
    }

    .thumb-side {
        width: 100%;
        max-width: 400px;
    }

    .thumb-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .retake-buttons {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Mobile */
@media (max-width: 600px) {
    .site-header {
        padding: 10px 16px;
        gap: 12px;
    }

    .site-header::after {
        display: none;
    }

    .header-logo {
        width: 40px;
        height: 40px;
    }

    .header-title-en {
        font-size: 1.05rem;
    }

    .header-title-th {
        font-size: 0.75rem;
    }

    .page {
        padding: 12px;
        align-items: flex-start;
        padding-top: 76px;
    }

    .cam-wrapper {
        border-radius: 12px;
    }

    .thumb-side {
        padding: 14px;
    }

    .thumb-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
    }

    .retake-buttons {
        grid-template-columns: repeat(3, 1fr);
    }

    .btn {
        padding: 12px 24px;
        font-size: 0.95rem;
    }

    .btn-capture {
        padding: 14px 32px;
        font-size: 1.05rem;
    }

    .result-title {
        font-size: 1.3rem;
    }

    .qr-card {
        padding: 20px;
    }

    .result-mini-preview {
        width: 120px;
    }

    .result-buttons {
        flex-direction: column;
        width: 100%;
    }

    .result-buttons .btn {
        width: 100%;
        justify-content: center;
    }
}

/* Large Screens */
@media (min-width: 1400px) {
    .cam-wrapper {
        max-width: 500px;
    }

    .thumb-side {
        width: 300px;
    }

    .result-center {
        max-width: 580px;
    }
}
