/* ==========================================================================
   style.css — วันปัดฉิม ม.6 · v2.0
   Complete rewrite: modern CSS, smooth animations, responsive, accessible
   ========================================================================== */

/* ==========================================================================
   SECTION 1 — CUSTOM PROPERTIES & THEMES
   ========================================================================== */

:root {
    /* ── Color Palette ───────────────────────────────────────── */
    --color-bg:            #0a0e1a;
    --color-bg-alt:        #111827;
    --color-gold:          #f5c842;
    --color-gold-dim:      #d4a72c;
    --color-purple:        #9b7fe8;
    --color-purple-dim:    #7c5fd0;
    --color-glass:         rgba(255, 255, 255, 0.06);
    --color-glass-hover:   rgba(255, 255, 255, 0.1);
    --color-text:          #e8e8f0;
    --color-text-muted:    rgba(232, 232, 240, 0.55);
    --color-accent:        #7eb8f7;
    --color-glass-border:  rgba(255, 255, 255, 0.12);
    --color-glass-shadow:  rgba(0, 0, 0, 0.4);
    --color-success:       #34d399;
    --color-danger:        #f87171;
    --color-warning:       #fbbf24;

    /* ── Typography ──────────────────────────────────────────── */
    --font-thai:   "Kanit", sans-serif;
    --font-serif:  "Noto Serif Thai", "Cormorant Garamond", serif;

    /* ── Spacing Scale (fluid) ───────────────────────────────── */
    --space-xs:  clamp(0.25rem, 0.5vw, 0.5rem);
    --space-sm:  clamp(0.5rem, 1vw, 0.75rem);
    --space-md:  clamp(0.75rem, 1.5vw, 1.25rem);
    --space-lg:  clamp(1rem, 2vw, 2rem);
    --space-xl:  clamp(1.5rem, 3vw, 3rem);
    --space-2xl: clamp(2rem, 4vw, 4rem);

    /* ── Border Radius ───────────────────────────────────────── */
    --radius-sm:   8px;
    --radius-md:   12px;
    --radius-lg:   16px;
    --radius-xl:   20px;
    --radius-2xl:  24px;
    --radius-full: 9999px;

    /* ── Easing (spring-like) ────────────────────────────────── */
    --ease-out:      cubic-bezier(0.22, 1, 0.36, 1);
    --ease-in:       cubic-bezier(0.55, 0, 1, 0.45);
    --ease-in-out:   cubic-bezier(0.65, 0, 0.35, 1);
    --ease-spring:   cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-bounce:   cubic-bezier(0.68, -0.55, 0.265, 1.55);
    --ease-smooth:   cubic-bezier(0.25, 1, 0.5, 1);

    /* ── Durations ───────────────────────────────────────────── */
    --dur-fast:    150ms;
    --dur-normal:  250ms;
    --dur-slow:    400ms;
    --dur-slower:  600ms;

    /* ── Shadows ─────────────────────────────────────────────── */
    --shadow-sm:   0 2px 8px rgba(0, 0, 0, 0.15);
    --shadow-md:   0 4px 20px rgba(0, 0, 0, 0.25);
    --shadow-lg:   0 8px 40px rgba(0, 0, 0, 0.35);
    --shadow-xl:   0 16px 60px rgba(0, 0, 0, 0.45);
    --shadow-glow: 0 0 20px rgba(245, 200, 66, 0.25);
    --shadow-glow-purple: 0 0 20px rgba(155, 127, 232, 0.25);
}

/* ── Light Mode ──────────────────────────────────────────────── */
@media (prefers-color-scheme: light) {
    :root {
        --color-bg:           #f0f2f5;
        --color-bg-alt:       #ffffff;
        --color-gold:         #c49a1a;
        --color-gold-dim:     #a67c00;
        --color-purple:       #6d4cc9;
        --color-purple-dim:   #5a3db0;
        --color-glass:        rgba(255, 255, 255, 0.7);
        --color-glass-hover:  rgba(255, 255, 255, 0.85);
        --color-text:         #1a1a2e;
        --color-text-muted:   rgba(26, 26, 46, 0.5);
        --color-accent:       #3b82f6;
        --color-glass-border: rgba(0, 0, 0, 0.08);
        --color-glass-shadow: rgba(0, 0, 0, 0.08);
        --color-success:      #059669;
        --color-danger:       #dc2626;
        --color-warning:      #d97706;

        --shadow-sm:   0 2px 8px rgba(0, 0, 0, 0.06);
        --shadow-md:   0 4px 20px rgba(0, 0, 0, 0.08);
        --shadow-lg:   0 8px 40px rgba(0, 0, 0, 0.1);
        --shadow-xl:   0 16px 60px rgba(0, 0, 0, 0.12);
        --shadow-glow: 0 0 20px rgba(196, 154, 26, 0.15);
        --shadow-glow-purple: 0 0 20px rgba(109, 76, 201, 0.15);
    }
}


/* ==========================================================================
   SECTION 2 — BASE & RESETS
   ========================================================================== */

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    margin: 0;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-thai);
    line-height: 1.6;
    min-height: 100vh;
    min-height: 100dvh;
    transition: background-color var(--dur-slow) var(--ease-out),
                color var(--dur-slow) var(--ease-out);
}

/* Page load transition */
body {
    opacity: 0;
}
body.loaded {
    opacity: 1;
    transition: opacity var(--dur-slow) var(--ease-out);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}


/* ==========================================================================
   SECTION 3 — TYPOGRAPHY
   ========================================================================== */

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-serif);
    line-height: 1.2;
    letter-spacing: -0.01em;
}

.gold { color: var(--color-gold); }
.purple { color: var(--color-purple); }
.accent { color: var(--color-accent); }

/* Gold shimmer text */
@keyframes shimmer {
    0%, 100% { background-position: -200% center; }
    50%      { background-position: 200% center; }
}

.text-shimmer {
    background: linear-gradient(
        90deg,
        var(--color-gold) 0%,
        #ffe082 25%,
        var(--color-gold) 50%,
        #ffe082 75%,
        var(--color-gold) 100%
    );
    background-size: 200% auto;
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: shimmer 4s ease-in-out infinite;
}


/* ==========================================================================
   SECTION 4 — INTERACTIVE ELEMENTS
   ========================================================================== */

button, a {
    transition: transform var(--dur-fast) var(--ease-out),
                opacity var(--dur-fast) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
    button:hover, a:hover {
        transform: translateY(-1px);
    }
}

button:active, a:active {
    transform: scale(0.97);
}

:focus-visible {
    outline: 2px solid var(--color-gold);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
}

/* Form inputs */
input[type="text"],
input[type="password"],
input[type="number"],
input[type="email"],
input[type="search"],
input[type="tel"],
input[type="url"],
input[type="date"],
input[type="datetime-local"],
textarea,
select {
    color: var(--color-text);
    background-color: var(--color-glass);
    border: 1.5px solid var(--color-glass-border);
    border-radius: var(--radius-md);
    transition: border-color var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-normal) var(--ease-out);
    -webkit-appearance: none;
    appearance: none;
    font-family: inherit;
    font-size: 1rem;
}

input::placeholder,
textarea::placeholder {
    color: var(--color-text-muted);
}

input:focus,
textarea:focus,
select:focus {
    border-color: var(--color-gold);
    box-shadow: 0 0 0 3px rgba(245, 200, 66, 0.15);
    outline: none;
}

/* Contact form input — high-visibility fallback */
.contact-row input[type="text"] {
    background-color: rgba(255, 255, 255, 0.18) !important;
    border: 2px solid rgba(255, 255, 255, 0.5) !important;
    color: #ffffff !important;
    opacity: 1 !important;
    border-radius: 0.75rem;
    padding: 0.75rem 1rem;
    font-size: 16px;
}

.contact-row input[type="text"]::placeholder {
    color: rgba(255, 255, 255, 0.45);
}

.contact-row input[type="text"]:focus {
    border-color: rgba(245, 200, 66, 0.7) !important;
    box-shadow: 0 0 0 3px rgba(245, 200, 66, 0.2) !important;
}

/* Autofill fix */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
select:-webkit-autofill {
    -webkit-text-fill-color: var(--color-text);
    -webkit-box-shadow: 0 0 0 1000px var(--color-bg-alt) inset;
    transition: background-color 5000s ease-in-out 0s;
}

select option {
    background-color: var(--color-bg-alt);
    color: var(--color-text);
}


/* ==========================================================================
   SECTION 5 — GLASSMORPHISM
   ========================================================================== */

.glass {
    background: var(--color-glass);
    backdrop-filter: blur(16px) saturate(1.2);
    -webkit-backdrop-filter: blur(16px) saturate(1.2);
    border: 1px solid var(--color-glass-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

.glass-card {
    background: var(--color-glass);
    backdrop-filter: blur(20px) saturate(1.3);
    -webkit-backdrop-filter: blur(20px) saturate(1.3);
    border: 1px solid var(--color-glass-border);
    border-radius: var(--radius-xl);
    padding: var(--space-lg);
    box-shadow: var(--shadow-md);
    transition: transform var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-normal) var(--ease-out);
}

@supports not (backdrop-filter: blur(1px)) {
    .glass, .glass-card {
        background: rgba(15, 20, 35, 0.92);
    }
    @media (prefers-color-scheme: light) {
        .glass, .glass-card {
            background: rgba(255, 255, 255, 0.92);
        }
    }
}

@media (hover: hover) and (pointer: fine) {
    .glass-hover:hover {
        transform: translateY(-4px);
        box-shadow: var(--shadow-lg);
    }
}


/* ==========================================================================
   SECTION 6 — STARFIELD
   ========================================================================== */

#starfield {
    position: fixed;
    inset: 0;
    z-index: -2;
    overflow: hidden;
    pointer-events: none;
}

span.star {
    position: absolute;
    border-radius: 50%;
    background: var(--color-text);
    will-change: opacity;
}

@keyframes twinkle {
    0%, 100% { opacity: 0.15; }
    50%      { opacity: 0.75; }
}

@keyframes float {
    0%, 100% { transform: translate3d(0, 0, 0); }
    33%      { transform: translate3d(3px, -6px, 0); }
    66%      { transform: translate3d(-4px, 4px, 0); }
}

.star {
    animation: twinkle 3s infinite alternate;
}

@media (hover: hover) and (pointer: fine) {
    .star {
        animation: twinkle 3s infinite alternate, float 8s infinite ease-in-out;
    }
}


/* ==========================================================================
   SECTION 7 — ANIMATED BACKGROUND
   ========================================================================== */

.bg-orb {
    position: fixed;
    border-radius: 50%;
    pointer-events: none;
    filter: blur(40px);
    opacity: 0.08;
    animation: orb-drift linear infinite;
    will-change: transform, opacity;
    contain: layout style;
}

/* Reduce blur for low-end mobile devices */
@media (max-width: 640px) {
    .bg-orb {
        filter: blur(20px);
        opacity: 0.05;
    }
    .glass-card {
        backdrop-filter: blur(12px) saturate(1.1);
        -webkit-backdrop-filter: blur(12px) saturate(1.1);
    }
    .glass {
        backdrop-filter: blur(10px) saturate(1.1);
        -webkit-backdrop-filter: blur(10px) saturate(1.1);
    }
}

@keyframes orb-drift {
    0%   { transform: translate3d(0, 0, 0) scale(1);    opacity: 0; }
    15%  { opacity: 0.1; }
    50%  { transform: translate3d(30px, -60px, 0) scale(1.12); opacity: 0.12; }
    85%  { opacity: 0.1; }
    100% { transform: translate3d(-20px, -120px, 0) scale(0.92); opacity: 0; }
}

.constellation-bg {
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
        radial-gradient(circle, rgba(245, 200, 66, 0.12) 1px, transparent 1px),
        radial-gradient(circle, rgba(155, 127, 232, 0.08) 1px, transparent 1px);
    background-size: 80px 80px, 120px 120px;
    background-position: 0 0, 40px 40px;
    animation: constellation-pulse 10s ease-in-out infinite;
    z-index: -1;
    opacity: 0.6;
}

@keyframes constellation-pulse {
    0%, 100% { opacity: 0.03; }
    50%      { opacity: 0.07; }
}

.shooting-star {
    position: fixed;
    width: 2px;
    border-radius: 50px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.85));
    pointer-events: none;
    animation-name: shoot;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
    will-change: transform, opacity;
}

@keyframes shoot {
    0%   { opacity: 0; transform: translate3d(0, 0, 0) rotate(35deg); }
    8%   { opacity: 1; }
    75%  { opacity: 0.8; }
    100% { opacity: 0; transform: translate3d(40vw, 28vw, 0) rotate(35deg); }
}


/* ==========================================================================
   SECTION 8 — ENVELOPE
   ========================================================================== */

.envelope-outer {
    position: relative;
    width: 288px;
    height: 192px;
    cursor: pointer;
    perspective: 1000px;
    overflow: hidden;
    border-radius: 0;
    box-shadow: none;
    transition: transform var(--dur-normal) var(--ease-out);
}

.envelope-back {
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, #e8dcc8 0%, #d4c4a8 100%);
    border-radius: var(--radius-sm);
    z-index: 1;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.05);
}

@media (hover: hover) and (pointer: fine) {
    .envelope-outer:hover {
        transform: scale(1.05) translateY(-5px);
    }
}

.envelope-front {
    position: absolute;
    inset: 0;
    z-index: 20;
    pointer-events: none;
    overflow: hidden;
    border-radius: var(--radius-sm);
}

.envelope-front::before {
    content: '';
    position: absolute;
    left: 0; right: 0; bottom: 0;
    height: 100%;
    background: linear-gradient(180deg, #dfceb1 0%, #c9b896 100%);
    clip-path: polygon(0 40%, 50% 52%, 100% 40%, 100% 100%, 0 100%);
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
}

.envelope-front::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, #cebfa5 0%, #d9c9ad 40%, transparent 100%);
    clip-path: polygon(0 0, 50% 52%, 0 100%);
}

.envelope-back::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 22;
    pointer-events: none;
    background: linear-gradient(270deg, #cebfa5 0%, #d9c9ad 40%, transparent 100%);
    clip-path: polygon(100% 0, 50% 52%, 100% 100%);
}

.envelope-front-shadow {
    position: absolute;
    left: 0; right: 0;
    top: 38%;
    height: 4px;
    background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.06), transparent);
    z-index: 25;
    pointer-events: none;
}

.envelope-flap {
    position: absolute;
    top: 0; left: 0;
    width: 288px;
    height: 120px;
    background: linear-gradient(180deg, #f0e4d0 0%, #e5d6be 60%, #d4c4a8 100%);
    clip-path: polygon(0 0, 100% 0, 50% 100%);
    transform-origin: top center;
    z-index: 40;
    filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.12));
    pointer-events: none;
    backface-visibility: hidden;
}

.envelope-letter {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 85%;
    height: 90%;
    background-color: #fdfbf7;
    border-radius: 6px 6px 0 0;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.08), inset 0 0 10px rgba(0, 0, 0, 0.03);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 24px 16px 0;
    z-index: 10;
    opacity: 0;
}

.wax-seal {
    position: absolute;
    top: 118px;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 48px;
    height: 48px;
    background: radial-gradient(circle at 35% 35%, #c53030, #7f1d1d);
    border-radius: 50%;
    z-index: 50;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.35), inset 0 1px 3px rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid rgba(127, 29, 29, 0.4);
    backface-visibility: hidden;
}

.envelope-wrapper .envelope-outer {
    filter: drop-shadow(0 10px 25px rgba(0, 0, 0, 0.35));
}

@media (hover: hover) and (pointer: fine) {
    .envelope-wrapper:hover .envelope-outer {
        filter: drop-shadow(0 16px 35px rgba(0, 0, 0, 0.45));
    }
}

@keyframes envelope-shake {
    0%, 100% { transform: translate3d(0, 0, 0) rotate(0); }
    25%      { transform: translate3d(-5px, 0, 0) rotate(-2deg); }
    75%      { transform: translate3d(5px, 0, 0) rotate(2deg); }
}

.envelope-shaking {
    animation: envelope-shake 0.5s var(--ease-out);
}


/* ==========================================================================
   SECTION 9 — LETTER CARD
   ========================================================================== */

.letter-card {
    max-width: 680px;
    width: 90%;
    margin: auto;
    padding: 2rem;
    background:
        repeating-linear-gradient(
            transparent, transparent 27px,
            rgba(100, 120, 200, 0.1) 28px
        );
    background-color: #f5f0e8;
    color: #1a1a2e;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-xl);
}

.letter-from {
    font-family: var(--font-serif);
    color: var(--color-purple);
    margin-bottom: 1rem;
}

.letter-body {
    line-height: 2;
    font-size: 1.05rem;
}

.letter-card input,
.letter-card textarea,
.letter-card select {
    color: #1a1a2e;
    background-color: rgba(0, 0, 0, 0.05);
}


/* ==========================================================================
   SECTION 10 — MINI COMPONENTS
   ========================================================================== */

.orb {
    border-radius: 50%;
    position: absolute;
    filter: blur(2px);
}

.petal {
    position: fixed;
    pointer-events: none;
    width: 10px;
    height: 10px;
    border-radius: 50% 0 50% 0;
    background: rgba(255, 182, 193, 0.7);
    will-change: transform;
}

@keyframes petal-fall {
    from { transform: translateY(-5vh) rotate(0deg); opacity: 0.8; }
    to   { transform: translateY(105vh) rotate(720deg); opacity: 0; }
}

.petal {
    animation-name: petal-fall;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

.confetti-piece {
    position: fixed;
    pointer-events: none;
    width: 8px;
    height: 8px;
    border-radius: 2px;
    will-change: transform, opacity;
}

.typewriter {
    border-right: 2px solid var(--color-gold);
    animation: caret-blink 0.8s step-end infinite;
}

@keyframes caret-blink {
    0%, 100% { border-color: var(--color-gold); }
    50%      { border-color: transparent; }
}


/* ==========================================================================
   SECTION 11 — iOS TOGGLE
   ========================================================================== */

input.toggle-input {
    position: absolute;
    visibility: hidden;
    width: 0;
    height: 0;
}

label.toggle-label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    position: relative;
    user-select: none;
}

label.toggle-label::before {
    content: '';
    display: inline-block;
    width: 44px;
    height: 24px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    transition: background var(--dur-normal) var(--ease-out);
    position: relative;
    flex-shrink: 0;
}

label.toggle-label::after {
    content: '';
    position: absolute;
    left: 2px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    background: white;
    border-radius: 50%;
    transition: transform var(--dur-normal) var(--ease-spring);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

input.toggle-input:checked + label.toggle-label::before {
    background: var(--color-gold);
}

input.toggle-input:checked + label.toggle-label::after {
    transform: translateY(-50%) translateX(20px);
}


/* ==========================================================================
   SECTION 12 — TOAST
   ========================================================================== */

#toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    pointer-events: none;
}

#toast-container .toast {
    pointer-events: auto;
}

.toast {
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-md);
    background: rgba(15, 20, 35, 0.95);
    border: 1px solid var(--color-gold);
    color: white;
    font-size: 0.9rem;
    transform: translateX(120%);
    transition: transform var(--dur-slow) var(--ease-spring);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: var(--shadow-lg);
}

.toast.show {
    transform: translateX(0);
}

.toast.error {
    border-color: var(--color-danger);
}


/* ==========================================================================
   SECTION 13 — ADMIN
   ========================================================================== */

.admin-nav a {
    padding: 0.5rem 1rem;
    text-decoration: none;
    transition: color var(--dur-fast) var(--ease-out);
}

@media (hover: hover) {
    .admin-nav a:hover {
        color: var(--color-gold);
    }
}

table.admin-table {
    width: 100%;
    border-collapse: collapse;
}

table.admin-table tr {
    transition: background var(--dur-fast) var(--ease-out);
}

@media (hover: hover) {
    table.admin-table tr:hover {
        background: rgba(255, 255, 255, 0.05);
    }
}

.badge {
    padding: 0.2rem 0.6rem;
    border-radius: var(--radius-full);
    font-size: 0.75rem;
    font-weight: 500;
}

.badge-green {
    background: rgba(34, 197, 94, 0.2);
    color: #4ade80;
}

.badge-gray {
    background: rgba(255, 255, 255, 0.1);
    color: #9ca3af;
}


/* ==========================================================================
   SECTION 14 — WELCOME SCROLL LOCK
   ========================================================================== */

html.welcome-lock,
html.welcome-lock body {
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    touch-action: none;
    overscroll-behavior: none;
}

html.welcome-lock #page-wrapper {
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
    position: fixed;
    width: 100%;
}

html.welcome-lock footer {
    display: none;
}

html.welcome-lock body.contact-phase-active,
html.welcome-lock.contact-phase-active {
    overflow-y: auto;
    height: auto;
    max-height: none;
    position: static;
    touch-action: auto;
    overscroll-behavior: auto;
}

html.welcome-lock body.contact-phase-active #page-wrapper,
html.welcome-lock.contact-phase-active #page-wrapper {
    max-height: none;
    overflow: visible;
    position: static;
}

html.welcome-lock body.contact-phase-active footer,
html.welcome-lock.contact-phase-active footer {
    display: block;
}

html.welcome-lock #old-letters-modal,
html.welcome-lock #phase-reader {
    overflow-y: auto;
    touch-action: auto;
    -webkit-overflow-scrolling: touch;
}

/* Contact phase — ensure proper scroll on mobile */
#phase-contact {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

#phase-contact > div {
    padding-bottom: max(4rem, env(safe-area-inset-bottom, 2rem));
}

/* Old letters modal — ensure scroll works on mobile */
#old-letters-modal {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* Envelope fan container — proper sizing for fan layout */
#envelopes-fan {
    min-height: 220px;
}


/* ==========================================================================
   SECTION 14B — CRITICAL ELEMENT VISIBILITY SAFETY NET
   Forces critical welcome-page elements to ALWAYS be visible once shown.
   Prevents GSAP animation bugs, timing races, and cached-JS issues.
   ========================================================================== */

/* Contact form — everything inside must be visible */
#phase-contact:not(.hidden) {
    opacity: 1 !important;
}

#phase-contact:not(.hidden) .glass-card,
#phase-contact:not(.hidden) h2,
#phase-contact:not(.hidden) p,
#phase-contact:not(.hidden) form,
#phase-contact:not(.hidden) .contact-row,
#phase-contact:not(.hidden) input,
#phase-contact:not(.hidden) button,
#phase-contact:not(.hidden) label,
#phase-contact:not(.hidden) svg {
    opacity: 1 !important;
    visibility: visible !important;
}

/* Old letters modal — cards must always be visible */
#old-letters-modal:not(.hidden) {
    opacity: 1 !important;
}

#old-letters-modal:not(.hidden) .old-letter-envelope {
    opacity: 1 !important;
    visibility: visible !important;
}

#old-letters-modal:not(.hidden) .old-letter-envelope * {
    visibility: visible !important;
}

/* Envelopes action button — always visible once shown */
#envelopes-action[style*="display: flex"],
#envelopes-action[style*="display:flex"] {
    opacity: 1 !important;
}


/* ==========================================================================
   SECTION 15 — MODAL OVERLAYS
   ========================================================================== */

.modal-overlay-bg {
    background-color: rgba(10, 14, 26, 0.95);
}

@media (prefers-color-scheme: light) {
    .modal-overlay-bg {
        background-color: rgba(240, 242, 245, 0.95);
    }
}

#old-letters-modal {
    background-color: rgba(10, 14, 26, 0.97);
    color: var(--color-text);
}


/* ==========================================================================
   SECTION 16 — SCROLLBAR
   ========================================================================== */

::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--color-glass-border);
    border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
    background: var(--color-gold);
}

* {
    scrollbar-width: thin;
    scrollbar-color: var(--color-glass-border) transparent;
}


/* ==========================================================================
   SECTION 17 — FRIEND / MEMORY WALL
   ========================================================================== */

.friend-card {
    transition: transform var(--dur-normal) var(--ease-out),
                box-shadow var(--dur-normal) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
    .friend-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 32px rgba(245, 200, 66, 0.15), var(--shadow-md);
    }
}

.friend-card:active {
    transform: scale(0.97);
}

.memory-wall {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.memory-wall::-webkit-scrollbar {
    height: 0;
    display: none;
}

.memory-wall-container {
    position: relative;
}

.memory-wall-container::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 20px;
    background: linear-gradient(to left, transparent, var(--color-bg));
    pointer-events: none;
    z-index: 1;
    opacity: 0;
    transition: opacity 0.3s;
}

.memory-wall-container.scrolled-right::before {
    opacity: 1;
}

.memory-wall-container::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, transparent, var(--color-bg));
    pointer-events: none;
    z-index: 1;
}


/* ==========================================================================
   SECTION 17B — MOBILE COMPONENT FIXES
   Ensure toggles, close buttons, and small UI stay properly sized on mobile
   ========================================================================== */

@media (max-width: 640px) {
    /* ── Close / icon buttons: maintain circle shape ── */
    button.rounded-full[class*="flex"][class*="items-center"][class*="justify-center"] {
        flex-shrink: 0;
        aspect-ratio: 1;
        padding: 0;
    }

    /* ── Reader privacy toggle wrapper ── */
    #reader-privacy-toggle {
        flex-wrap: nowrap;
        gap: 0.5rem;
    }
    #reader-privacy-toggle .w-11 {
        min-width: 2.75rem;
        min-height: 1.5rem;
        max-height: 1.5rem;
        flex-shrink: 0;
    }

    /* ── Profile: messages row — prevent squeeze between content and toggle ── */
    .glass-card .flex.items-start.justify-between {
        gap: 0.5rem;
    }

    /* ── Profile: contact row — prevent squeeze ── */
    .flex.items-center.justify-between.bg-white\/5 {
        gap: 0.5rem;
        flex-wrap: nowrap;
    }

    /* ── Reaction buttons row — allow wrapping ── */
    .flex.items-center.space-x-2.flex-wrap {
        gap: 0.375rem;
    }
    .reaction-btn {
        padding: 0.25rem 0.5rem;
        font-size: 0.75rem;
    }

    /* ── Old letter toggle in JS (inline styles) — ensure dot stays inside track ── */
    .ol-track {
        min-width: 2.25rem;
        min-height: 1.25rem;
        flex-shrink: 0;
    }

    /* ── Write page toggle rows — prevent label from squishing the toggle ── */
    .flex.items-center.justify-between > button.rounded-full.relative.inline-flex {
        flex-shrink: 0;
    }
}


/* ==========================================================================
   SECTION 18 — RESPONSIVE
   ========================================================================== */

/* Very small (< 380px) */
@media (max-width: 380px) {
    .glass-card {
        padding: 0.75rem;
        border-radius: var(--radius-md);
    }

    .envelope-outer { width: 180px; height: 120px; }
    .envelope-flap  { width: 180px; height: 60px; }
    .wax-seal       { top: 58px; width: 32px; height: 32px; }

    #nickname-display { font-size: 2rem !important; }

    h1 { font-size: 1.3rem; }
    h2 { font-size: 1.1rem; }
}

/* Small mobile (< 480px) */
@media (max-width: 480px) {
    .letter-card {
        width: 96%;
        padding: 1rem 0.75rem;
        font-size: 0.92rem;
    }
    .letter-from { font-size: 1.1rem; }
    .letter-body { font-size: 0.92rem; line-height: 1.8; }

    .contact-row {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .contact-row select,
    .contact-row input[type="text"] { width: 100%; }
}

/* Mobile (< 640px) */
@media (max-width: 640px) {
    .glass-card {
        padding: 1rem;
        border-radius: var(--radius-lg);
    }

    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.2rem; }
    h3 { font-size: 1.05rem; }

    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="email"],
    input[type="date"],
    input[type="search"],
    textarea,
    select {
        font-size: 16px;
        padding: 12px;
        min-height: 44px;
    }

    /* Action buttons get 44px min tap target — but NOT toggle switches,
       small icon buttons, close (×) buttons, or reaction buttons.       */
    button, [type="submit"] { min-height: 44px; }

    /* ── Exempt: toggle switches ── */
    button[class*="inline-flex"][class*="rounded-full"][class*="w-12"],
    button[class*="inline-flex"][class*="rounded-full"][class*="w-11"] {
        min-height: 0;
        height: 1.5rem;    /* h-6 = 24px */
    }

    /* ── Exempt: small icon / close (×) buttons ── */
    button[class*="w-6"][class*="h-6"],
    button[class*="w-7"][class*="h-7"],
    button[class*="w-8"][class*="h-8"],
    button[class*="w-10"][class*="h-10"] {
        min-height: 0;
    }

    /* ── Exempt: reaction buttons ── */
    .reaction-btn {
        min-height: 0;
    }

    /* ── Exempt: toggle-btn pills (สาธารณะ / ส่วนตัว) ── */
    .contact-toggle-btn,
    .message-toggle-btn {
        min-height: 0;
    }

    /* ── Exempt: inline dismiss (×) buttons ── */
    button[class*="shrink-0"][class*="text-xs"] {
        min-height: 0;
    }

    .avatar-upload-btn,
    #profile-avatar-upload-btn { min-height: auto; }

    .envelope-outer { width: 220px; height: 148px; }
    .envelope-flap  { width: 220px; height: 92px; }
    .wax-seal       { top: 90px; width: 40px; height: 40px; }

    .profile-header-card {
        flex-direction: column;
        text-align: center;
    }

    #countdown { gap: 0.5rem; }
    #countdown span:first-child { font-size: 2rem; }

    #toast-container { left: 1rem; right: 1rem; }

    #nickname-display { font-size: 2.5rem !important; }

    .fixed.bottom-6.right-6 { bottom: 1rem; right: 1rem; }

    .overflow-x-auto { -webkit-overflow-scrolling: touch; }
    .overflow-x-auto table { min-width: 640px; }

    nav .container { padding-left: 0.5rem; padding-right: 0.5rem; }
    nav a { padding: 0.75rem; font-size: 0.8rem; }
}

/* Tablet (641px – 768px) */
@media (min-width: 641px) and (max-width: 768px) {
    .glass-card { padding: 1.25rem; }
    .letter-card { width: 92%; padding: 1.5rem; }
}

/* Tablet large (769px – 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
    .glass-card { padding: 1.5rem; }
}

/* Large desktop (1280px+) */
@media (min-width: 1280px) {
    .glass-card { padding: 2rem; }
    .letter-card { max-width: 720px; }
}


/* ==========================================================================
   SECTION 19 — SAFE AREA INSETS
   ========================================================================== */

@supports (padding: max(0px)) {
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }

    nav {
        padding-left: max(0.5rem, env(safe-area-inset-left));
        padding-right: max(0.5rem, env(safe-area-inset-right));
    }

    .fixed.bottom-6 {
        bottom: max(1.5rem, env(safe-area-inset-bottom));
    }
}


/* ==========================================================================
   SECTION 20 — REDUCED MOTION (Accessibility)
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .star, .petal, .confetti-piece, .bg-orb,
    .shooting-star, .constellation-bg {
        animation: none !important;
        display: none !important;
    }

    .glass-card, .glass-hover, .friend-card, .envelope-wrapper {
        transition: none !important;
    }

    .text-shimmer {
        animation: none !important;
        -webkit-text-fill-color: var(--color-gold);
        background: none;
    }

    body {
        opacity: 1 !important;
    }
    body.loaded {
        transition: none !important;
    }
}


/* ==========================================================================
   SECTION 21 — PRINT
   ========================================================================== */

@media print {
    nav, button, .no-print, #starfield,
    #bg-orbs, .constellation-bg, .shooting-star, footer {
        display: none !important;
    }

    body {
        background: white !important;
        color: black !important;
    }

    .glass-card {
        background: white !important;
        border: 1px solid #ddd !important;
        box-shadow: none !important;
        backdrop-filter: none !important;
    }

    .print-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 10mm;
    }
}


/* ==========================================================================
   SECTION 22 — UTILITY ANIMATIONS
   ========================================================================== */

@keyframes slideUp {
    from { opacity: 0; transform: translate3d(0, 20px, 0); }
    to   { opacity: 1; transform: translate3d(0, 0, 0); }
}
.animate-slide-up {
    animation: slideUp var(--dur-slow) var(--ease-out) forwards;
}

@keyframes scaleIn {
    from { opacity: 0; transform: scale(0.9); }
    to   { opacity: 1; transform: scale(1); }
}
.animate-scale-in {
    animation: scaleIn var(--dur-slow) var(--ease-spring) forwards;
}

@keyframes pulseGlow {
    0%, 100% { box-shadow: 0 0 0 0 rgba(245, 200, 66, 0.3); }
    50%      { box-shadow: 0 0 20px 5px rgba(245, 200, 66, 0.15); }
}
.animate-pulse-glow {
    animation: pulseGlow 2s ease-in-out infinite;
}

/* Stagger children (CSS-only) */
.stagger-children > * {
    opacity: 0;
    animation: slideUp var(--dur-slow) var(--ease-out) forwards;
}
.stagger-children > *:nth-child(1)  { animation-delay: 0ms; }
.stagger-children > *:nth-child(2)  { animation-delay: 60ms; }
.stagger-children > *:nth-child(3)  { animation-delay: 120ms; }
.stagger-children > *:nth-child(4)  { animation-delay: 180ms; }
.stagger-children > *:nth-child(5)  { animation-delay: 240ms; }
.stagger-children > *:nth-child(6)  { animation-delay: 300ms; }
.stagger-children > *:nth-child(7)  { animation-delay: 360ms; }
.stagger-children > *:nth-child(8)  { animation-delay: 420ms; }
.stagger-children > *:nth-child(9)  { animation-delay: 480ms; }
.stagger-children > *:nth-child(10) { animation-delay: 540ms; }

@keyframes gentleFloat {
    0%, 100% { transform: translate3d(0, 0, 0) rotate(0deg); }
    25%      { transform: translate3d(2px, -8px, 0) rotate(0.5deg); }
    75%      { transform: translate3d(1px, -10px, 0) rotate(0.3deg); }
}
.animate-float {
    animation: gentleFloat 6s ease-in-out infinite;
}

/* QR Scan line */
@keyframes scan-line {
    0%   { top: 0; opacity: 0; }
    10%  { opacity: 1; }
    90%  { opacity: 1; }
    100% { top: 100%; opacity: 0; }
}
.scan-line-anim {
    position: absolute;
    width: 100%;
    height: 2px;
    background: var(--color-gold);
    box-shadow: 0 0 10px var(--color-gold), 0 0 20px rgba(245, 200, 66, 0.3);
    animation: scan-line 2s linear infinite;
}

/* Welcome petals fall */
@keyframes fall {
    0%   { transform: translateY(-10vh) rotate(0deg); opacity: 0; }
    10%  { opacity: 0.8; }
    90%  { opacity: 0.6; }
    100% { transform: translateY(110vh) rotate(360deg); opacity: 0; }
}
