:root {
    --primary: #6366f1;
    --accent:  #a78bfa;
    --bg:      #0a0a12;
    --glass-border: rgba(255,255,255,0.75);
    --snap-ring: #6366f1;
}

*, *::before, *::after { box-sizing: border-box; }

.svg-icon {
    width: 1em;
    height: 1em;
    display: inline-block;
    vertical-align: -0.15em;
    flex-shrink: 0;
}

body, html {
    margin: 0; padding: 0;
    width: 100%; height: 100%;
    min-height: 100dvh;
    background: var(--bg);
    font-family: 'Be Vietnam Pro', 'Segoe UI', system-ui, sans-serif;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

#loading {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    gap: 14px;
    padding: 24px;
    transition: opacity 0.6s ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }

#loading h2 {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: #fff;
}
#loading p {
    margin: 0;
    font-size: 0.78rem;
    color: rgba(255,255,255,0.5);
    text-align: center;
    font-variant-numeric: tabular-nums;
}
.loading-progress {
    width: min(280px, 70vw);
    height: 6px;
    border-radius: 3px;
    background: rgba(255,255,255,0.08);
    overflow: hidden;
    margin-top: 4px;
}
.loading-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--primary), var(--accent));
    border-radius: 3px;
    transition: width 0.28s ease;
    box-shadow: 0 0 12px rgba(99,102,241,0.5);
}

#error-screen {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 100;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    gap: 12px;
    text-align: center;
    padding: 32px;
}
#error-screen.visible { display: flex; }
#error-screen .icon { font-size: 3rem; display: inline-flex; color: #f87171; }
#error-screen h2 { margin: 0; font-size: 1.3rem; color: #f87171; }
#error-screen p { margin: 0; color: rgba(255,255,255,0.5); font-size: 0.9rem; max-width: 340px; }

.video-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
}
.input_video { display: none; }
.output_canvas {
    width: 100%; height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
    cursor: pointer;
    touch-action: manipulation;
}
.output_canvas.no-mirror { transform: none; }


#countdown-overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: clamp(120px, 28vw, 220px);
    font-weight: 800;
    color: #fff;
    text-shadow: 0 10px 40px rgba(0,0,0,0.6), 0 0 80px rgba(99,102,241,0.6);
    z-index: 45;
    pointer-events: none;
    font-variant-numeric: tabular-nums;
}
#countdown-overlay.visible {
    display: flex;
}
#countdown-overlay.pulse {
    animation: countdown-pulse 1s ease-out;
}
@keyframes countdown-pulse {
    0%   { transform: scale(0.5); opacity: 0; }
    20%  { transform: scale(1.15); opacity: 1; }
    100% { transform: scale(0.85); opacity: 0.1; }
}

#gesture-tag {
    position: fixed;
    top: max(20px, calc(env(safe-area-inset-top) + 12px));
    left: 50%;
    transform: translateX(-50%);
    background: rgba(99, 102, 241, 0.28);
    border: 1px solid rgba(99, 102, 241, 0.55);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: #fff;
    padding: 7px 18px;
    border-radius: 99px;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.25s;
    box-shadow: 0 6px 20px rgba(99,102,241,0.25);
}
#gesture-tag.visible { opacity: 1; }

#switch-camera-btn {
    position: fixed;
    top: max(16px, calc(env(safe-area-inset-top) + 10px));
    left: max(16px, calc(env(safe-area-inset-left) + 10px));
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.16);
    background: rgba(10,12,22,0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    font-size: 1.1rem;
    cursor: pointer;
    z-index: 25;
    transition: transform 0.14s;
}
#switch-camera-btn:active { transform: scale(0.92); }

#fullscreen-btn {
    position: fixed;
    top: max(68px, calc(env(safe-area-inset-top) + 60px));
    left: max(16px, calc(env(safe-area-inset-left) + 10px));
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(10,12,22,0.55);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    color: #fff;
    border-radius: 999px;
    cursor: pointer;
    padding: 0;
    font-size: 1.1rem;
    z-index: 30;
    transition: transform 0.15s, background 0.15s;
}
#fullscreen-btn:active { transform: scale(0.92); }
#fullscreen-btn .fullscreen-exit { display: none; }
#fullscreen-btn.active .fullscreen-enter { display: none; }
#fullscreen-btn.active .fullscreen-exit { display: inline-block; }
#fullscreen-btn.active {
    background: rgba(99,102,241,0.28);
    border-color: rgba(99,102,241,0.7);
}

#timer-toggle {
    position: fixed;
    top: max(16px, calc(env(safe-area-inset-top) + 10px));
    right: max(16px, calc(env(safe-area-inset-right) + 10px));
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px 8px 12px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.16);
    background: rgba(10,12,22,0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    font-family: inherit;
    font-weight: 700;
    font-size: 0.78rem;
    cursor: pointer;
    z-index: 25;
    transition: transform 0.14s, background 0.14s;
}
#timer-toggle:active { transform: scale(0.94); }
#timer-toggle .svg-icon { font-size: 1rem; }
#timer-toggle.off {
    color: rgba(255,255,255,0.5);
}
#timer-toggle.off .svg-icon { opacity: 0.55; }

#palette-bar {
    display: flex;
    gap: 8px;
    padding: 2px 4px;
    overflow-x: auto;
    scrollbar-width: none;
}
#palette-bar::-webkit-scrollbar { display: none; }
.palette-chip {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(10,12,22,0.55);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: rgba(255,255,255,0.72);
    font-family: inherit;
    font-size: 0.72rem;
    font-weight: 600;
    padding: 6px 12px 6px 8px;
    border-radius: 999px;
    cursor: pointer;
    transition: transform 0.12s, background 0.12s;
}
.palette-chip:active { transform: scale(0.94); }
.palette-chip.active {
    background: rgba(255,255,255,0.16);
    border-color: rgba(255,255,255,0.4);
    color: #fff;
}
.palette-swatch {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 1px solid rgba(255,255,255,0.35);
    box-shadow: 0 0 6px rgba(255,255,255,0.25);
    flex-shrink: 0;
}

#filter-bar {
    display: flex;
    gap: 8px;
    padding: 2px 4px;
    overflow-x: auto;
    scrollbar-width: none;
}
#filter-bar::-webkit-scrollbar { display: none; }
.filter-chip {
    flex-shrink: 0;
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(10,12,22,0.55);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: rgba(255,255,255,0.72);
    font-family: inherit;
    font-size: 0.74rem;
    font-weight: 600;
    padding: 7px 14px;
    border-radius: 999px;
    cursor: pointer;
    transition: transform 0.12s, background 0.12s;
}
.filter-chip:active { transform: scale(0.94); }
.filter-chip.active {
    background: rgba(99,102,241,0.32);
    border-color: rgba(99,102,241,0.7);
    color: #fff;
    box-shadow: 0 6px 16px rgba(99,102,241,0.3);
}

#capture-dock {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 0 28px max(20px, calc(env(safe-area-inset-bottom) + 16px));
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 20px;
    z-index: 30;
    pointer-events: none;
}
#capture-dock > * {
    pointer-events: auto;
}

.capture-dock-stack {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.capture-dock-btn {
    width: 52px;
    height: 52px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(10,12,22,0.55);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-size: 1.35rem;
    flex-shrink: 0;
    transition: transform 0.15s, background 0.15s;
}
.capture-dock-btn:active {
    transform: scale(0.92);
    background: rgba(99,102,241,0.22);
}

#capture-btn {
    position: relative;
    width: 84px;
    height: 84px;
    border: 4px solid rgba(255,255,255,0.92);
    border-radius: 999px;
    background: linear-gradient(180deg, #ffffff 0%, #dbe4ff 100%);
    box-shadow:
        0 18px 34px rgba(0,0,0,0.35),
        0 0 0 10px rgba(99,102,241,0.18);
    color: #111827;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 0;
    user-select: none;
    touch-action: manipulation;
    overflow: hidden;
    transition: transform 0.16s ease, box-shadow 0.16s ease;
    flex-shrink: 0;
}
#capture-btn::before {
    content: '';
    position: absolute;
    inset: 10px;
    border-radius: inherit;
    background: linear-gradient(180deg, #ffffff 0%, #f4f7ff 100%);
    border: 1px solid rgba(255,255,255,0.9);
}
#capture-btn img {
    position: relative;
    z-index: 1;
    width: 44px;
    height: 44px;
    object-fit: contain;
    display: block;
    filter: drop-shadow(0 4px 10px rgba(15, 23, 42, 0.16));
}
#capture-btn .capture-fallback {
    position: relative;
    z-index: 1;
    display: none;
    font-size: 2rem;
    line-height: 1;
}
#capture-btn.fallback-visible img { display: none; }
#capture-btn.fallback-visible .capture-fallback { display: block; }
#capture-btn:active {
    transform: scale(0.94);
    box-shadow:
        0 12px 24px rgba(0,0,0,0.3),
        0 0 0 8px rgba(99,102,241,0.16);
}
#capture-btn:focus-visible {
    outline: 3px solid rgba(191, 219, 254, 0.95);
    outline-offset: 4px;
}

#help-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 55vh;
    background: rgba(11,15,28,0.98);
    border-top: 1px solid rgba(255,255,255,0.1);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: 0 -14px 40px rgba(0,0,0,0.5);
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
    z-index: 40;
    display: flex;
    flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom);
    pointer-events: none;
}
#help-sheet.visible {
    transform: translateY(0);
    pointer-events: auto;
}
#help-sheet .bottom-sheet-body {
    flex: 1;
    overflow-y: auto;
    padding: 14px 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.gesture-guide-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 14px;
}
.gesture-guide-icon {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: rgba(99,102,241,0.18);
    color: var(--accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    flex-shrink: 0;
}
.gesture-guide-item strong {
    display: block;
    color: #fff;
    font-size: 0.9rem;
    margin-bottom: 2px;
}
.gesture-guide-item p {
    margin: 0;
    color: rgba(255,255,255,0.6);
    font-size: 0.76rem;
    line-height: 1.45;
}
.help-hint {
    color: rgba(255,255,255,0.5);
    font-size: 0.76rem;
    line-height: 1.6;
    padding: 10px 12px;
    background: rgba(255,255,255,0.03);
    border-radius: 12px;
    text-align: center;
}
.help-hint kbd {
    display: inline-block;
    padding: 2px 8px;
    font-family: inherit;
    font-size: 0.72rem;
    color: #fff;
    background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 6px;
    margin: 0 2px;
}

/* Shared base for the pair of frozen-glass action pills — same neutral
   dark glassmorphism as #timer-toggle / .capture-dock-btn, differentiated
   only by the icon + label. */
#clear-frame-btn,
#insert-photo-btn {
    position: fixed;
    /* Sit below the top-left stack (switch-camera ~16px + fullscreen
       ~68px) so the pills don't collide with them on narrow screens. */
    top: max(124px, calc(env(safe-area-inset-top) + 118px));
    left: 50%;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px 8px 12px;
    background: rgba(10, 12, 22, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    color: #fff;
    font-family: inherit;
    font-size: 0.82rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    border-radius: 999px;
    cursor: pointer;
    z-index: 32;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
}
#clear-frame-btn .svg-icon,
#insert-photo-btn .svg-icon {
    font-size: 1rem;
}

#clear-frame-btn {
    transform: translate(calc(-50% - 96px), -6px);
}
#clear-frame-btn.visible {
    opacity: 1;
    transform: translate(calc(-50% - 96px), 0);
    pointer-events: auto;
}
#clear-frame-btn:active {
    transform: translate(calc(-50% - 96px), 0) scale(0.94);
}

#insert-photo-btn {
    transform: translate(calc(-50% + 96px), -6px);
}
#insert-photo-btn.visible {
    opacity: 1;
    transform: translate(calc(-50% + 96px), 0);
    pointer-events: auto;
}
#insert-photo-btn:active {
    transform: translate(calc(-50% + 96px), 0) scale(0.94);
}

#effects-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(148px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 0 12px;
    z-index: 25;
    pointer-events: none;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}
#effects-sheet.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
.effects-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.effects-group-label {
    color: rgba(255,255,255,0.72);
    font-size: 0.66rem;
    font-weight: 700;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    padding: 0 6px;
    text-shadow: 0 2px 6px rgba(0,0,0,0.55);
}
.beauty-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 6px;
    background: rgba(10,12,22,0.62);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 999px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}
.beauty-toggle {
    flex-shrink: 0;
    padding: 8px 14px;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(255,255,255,0.06);
    color: rgba(255,255,255,0.82);
    font-family: inherit;
    font-size: 0.78rem;
    font-weight: 700;
    border-radius: 999px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.beauty-toggle.active {
    background: rgba(236,72,153,0.32);
    border-color: rgba(236,72,153,0.7);
    color: #fff;
    box-shadow: 0 4px 12px rgba(236,72,153,0.28);
}
.beauty-toggle:active {
    transform: scale(0.96);
}
#beauty-strength,
#skin-whiten-strength {
    flex: 1;
    min-width: 0;
    accent-color: #ec4899;
    cursor: pointer;
}
#beauty-strength:disabled,
#skin-whiten-strength:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
.beauty-strength-value {
    flex-shrink: 0;
    width: 2.2em;
    text-align: right;
    color: rgba(255,255,255,0.85);
    font-size: 0.78rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}
#effects-btn.active {
    background: rgba(99,102,241,0.28);
    border-color: rgba(99,102,241,0.7);
}

/* Glass editor overlay — reuses the frozen glass shape as a clip mask so
   the user can pick any photo (from history or upload) and drag / pinch
   it inside the glass before saving a new capture. */
#glass-editor {
    position: fixed;
    inset: 0;
    z-index: 60;
    background:
        radial-gradient(circle at top left, rgba(99,102,241,0.12), transparent 28%),
        rgba(3,6,14,0.96);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    display: none;
    flex-direction: column;
}
#glass-editor.visible { display: flex; }
.glass-editor-topbar {
    flex-shrink: 0;
    padding: max(14px, calc(env(safe-area-inset-top) + 10px)) 16px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}
.glass-editor-title {
    margin: 0;
    flex: 1;
    text-align: center;
    color: #fff;
    font-size: 1rem;
    font-weight: 700;
}
.glass-editor-stage {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    overflow: hidden;
}
#glass-editor-canvas {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    border-radius: 14px;
    background: #0a0a12;
    box-shadow: 0 16px 40px rgba(0,0,0,0.5);
    touch-action: none;
    cursor: grab;
}
#glass-editor-canvas.mirrored { transform: scaleX(-1); }
#glass-editor-canvas.dragging { cursor: grabbing; }
.glass-editor-picker {
    flex-shrink: 0;
    padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
    background: rgba(5,8,18,0.88);
    backdrop-filter: blur(12px);
    border-top: 1px solid rgba(255,255,255,0.08);
}
.glass-editor-hint {
    margin: 0 2px 8px;
    color: rgba(255,255,255,0.58);
    font-size: 0.76rem;
    line-height: 1.45;
}
.glass-editor-picker-scroll {
    display: flex;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 2px;
}
.glass-editor-picker-scroll::-webkit-scrollbar { display: none; }
.glass-editor-picker-list {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.glass-editor-picker-empty {
    padding: 18px 14px;
    color: rgba(255,255,255,0.5);
    font-size: 0.78rem;
}
.glass-editor-picker-item {
    flex-shrink: 0;
    width: 72px;
    height: 72px;
    border-radius: 12px;
    overflow: hidden;
    padding: 0;
    border: 2px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.04);
    cursor: pointer;
    transition: transform 0.12s, border-color 0.12s;
}
.glass-editor-picker-item:active { transform: scale(0.94); }
.glass-editor-picker-item.active {
    border-color: rgba(236,72,153,0.9);
    box-shadow: 0 0 0 2px rgba(236,72,153,0.35);
}
.glass-editor-picker-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.glass-editor-upload-btn {
    flex-shrink: 0;
    width: 72px;
    height: 72px;
    border-radius: 12px;
    border: 1px dashed rgba(255,255,255,0.28);
    background: rgba(255,255,255,0.04);
    color: rgba(255,255,255,0.78);
    font-family: inherit;
    font-size: 0.68rem;
    font-weight: 700;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.glass-editor-upload-btn:active { background: rgba(255,255,255,0.1); }
.glass-editor-upload-btn .svg-icon { font-size: 1.1rem; color: var(--accent); }

#save-toast {
    position: fixed;
    bottom: calc(200px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: rgba(34, 197, 94, 0.2);
    border: 1px solid rgba(34, 197, 94, 0.5);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: #86efac;
    padding: 8px 20px;
    border-radius: 99px;
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    z-index: 30;
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
#save-toast .svg-icon { font-size: 1.1rem; }
#save-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

#history-drawer {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    background: rgba(5,8,18,0.98);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    z-index: 50;
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
    display: flex;
    flex-direction: column;
}
#history-drawer.visible {
    transform: translateY(0);
}
.drawer-header {
    padding: max(18px, calc(env(safe-area-inset-top) + 12px)) 18px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.drawer-header h3 {
    margin: 0;
    color: #fff;
    font-size: 1rem;
}
.drawer-header p {
    margin: 4px 0 0;
    color: rgba(255,255,255,0.5);
    font-size: 0.8rem;
}
.drawer-close {
    width: 38px;
    height: 38px;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.04);
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
}
.drawer-content {
    flex: 1;
    overflow-y: auto;
    padding: 14px 14px calc(24px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 14px;
}
#history-empty {
    padding: 18px;
    border-radius: 18px;
    border: 1px dashed rgba(255,255,255,0.14);
    color: rgba(255,255,255,0.58);
    background: rgba(255,255,255,0.03);
    line-height: 1.55;
}
#history-preview {
    display: none;
    position: sticky;
    top: 0;
    z-index: 5;
    border-radius: 16px;
    overflow: hidden;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(5,8,18,0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}
#history-preview.visible { display: block; }
#history-preview img {
    width: 100%;
    max-height: 30vh;
    object-fit: contain;
    display: block;
    background: #050812;
}
@media (max-height: 640px) {
    #history-preview img { max-height: 24vh; }
}
.history-preview-meta {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.history-preview-meta strong {
    color: #fff;
    font-size: 0.92rem;
}
.history-preview-meta span {
    color: rgba(255,255,255,0.54);
    font-size: 0.75rem;
}
.history-actions-bar {
    flex-shrink: 0;
    padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
    border-top: 1px solid rgba(255,255,255,0.08);
    background: rgba(5,8,18,0.88);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
}
.history-actions {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
}
.history-action {
    min-width: 0;
    border: 0;
    border-radius: 12px;
    padding: 12px 6px;
    font-weight: 700;
    font-size: 0.74rem;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-action.primary {
    grid-column: 1 / -1;
    background: #22c55e;
    color: #08110f;
}
.history-action.danger {
    background: rgba(248,113,113,0.18);
    color: #fca5a5;
    border: 1px solid rgba(248,113,113,0.4);
}
.history-action.secondary {
    background: rgba(255,255,255,0.08);
    color: #fff;
}
.history-action:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}
.history-note {
    color: rgba(255,255,255,0.45);
    font-size: 0.72rem;
    line-height: 1.45;
    padding-top: 2px;
}
.history-selection-count {
    color: rgba(134, 239, 172, 0.95);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}
#history-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 8px;
    align-content: start;
}
.history-item {
    position: relative;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 14px;
    background: rgba(255,255,255,0.04);
    overflow: hidden;
    cursor: pointer;
    padding: 0;
    color: inherit;
}
.history-item.focused {
    border-color: rgba(99,102,241,0.75);
    box-shadow: 0 0 0 1px rgba(99,102,241,0.45);
}
.history-item.selected {
    border-color: rgba(34,197,94,0.75);
    box-shadow: 0 0 0 1px rgba(34,197,94,0.35);
}
.history-item.selected.focused {
    box-shadow:
        0 0 0 1px rgba(34,197,94,0.35),
        0 0 0 3px rgba(99,102,241,0.2);
}
.history-item img {
    width: 100%;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    display: block;
    background: #111827;
}
.history-item-toggle {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 30px;
    height: 30px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(8,12,24,0.82);
    color: #fff;
    font-size: 0.9rem;
    font-weight: 800;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}
.history-item.selected .history-item-toggle {
    background: #22c55e;
    color: #08110f;
    border-color: rgba(34,197,94,0.9);
}
.history-item-meta {
    padding: 8px 9px 10px;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.history-item-meta strong {
    color: #fff;
    font-size: 0.76rem;
}
.history-item-meta span {
    color: rgba(255,255,255,0.5);
    font-size: 0.67rem;
}

#overlay-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(2,6,14,0.45);
    z-index: 44;
    display: none;
}
#overlay-backdrop.visible { display: block; }

#editor-overlay {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: none;
    background:
        radial-gradient(circle at top left, rgba(99,102,241,0.12), transparent 28%),
        rgba(3,6,14,0.96);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
}
#editor-overlay.visible {
    display: block;
}
.editor-shell {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.editor-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    height: 56px;
    padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    background: rgba(9,13,26,0.85);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    flex-shrink: 0;
}
.editor-title {
    margin: 0;
    flex: 1;
    color: #fff;
    font-size: 1rem;
    font-weight: 600;
    text-align: center;
}
.editor-icon-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.04);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1.1rem;
    padding: 0;
}
.editor-icon-btn:active {
    background: rgba(255,255,255,0.12);
}
.editor-save-btn {
    height: 44px;
    padding: 0 20px;
    border: 0;
    border-radius: 12px;
    background: linear-gradient(180deg, #34d399, #22c55e);
    color: #07110d;
    font-weight: 800;
    font-size: 0.92rem;
    cursor: pointer;
    font-family: inherit;
    box-shadow: 0 6px 16px rgba(34,197,94,0.3);
    flex-shrink: 0;
}
.editor-save-btn:active {
    transform: translateY(1px);
}

.editor-stage-wrap {
    flex: 1;
    min-height: 0;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    overflow: hidden;
}
#editor-canvas {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    border-radius: 16px;
    background: #ffffff;
    box-shadow: 0 20px 50px rgba(0,0,0,0.45);
    cursor: grab;
    touch-action: none;
    object-fit: contain;
}
#editor-canvas.dragging { cursor: grabbing; }
#editor-canvas.eraser-active { cursor: crosshair; }

.editor-sticker-toolbar {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%) translateY(-8px);
    display: flex;
    gap: 6px;
    padding: 6px;
    background: rgba(9,13,26,0.92);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 14px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 10px 30px rgba(0,0,0,0.4);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s, transform 0.18s;
    max-width: calc(100% - 24px);
    overflow-x: auto;
    scrollbar-width: none;
    z-index: 5;
}
.editor-sticker-toolbar::-webkit-scrollbar { display: none; }
.editor-sticker-toolbar.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateX(-50%) translateY(0);
}
.sticker-tool-btn {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border: 0;
    border-radius: 10px;
    background: rgba(255,255,255,0.06);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-size: 1.1rem;
}
.sticker-tool-btn:active {
    background: rgba(255,255,255,0.16);
    transform: scale(0.95);
}
.sticker-tool-btn.active {
    background: rgba(34,197,94,0.24);
    color: #86efac;
    box-shadow: inset 0 0 0 1px rgba(34,197,94,0.5);
}
.sticker-tool-btn.danger {
    color: #fca5a5;
}
.sticker-tool-btn.danger:active {
    background: rgba(248,113,113,0.22);
}

.editor-selection-note {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(9,13,26,0.82);
    color: rgba(255,255,255,0.7);
    font-size: 0.7rem;
    padding: 6px 14px;
    border-radius: 99px;
    border: 1px solid rgba(255,255,255,0.08);
    backdrop-filter: blur(10px);
    max-width: calc(100% - 24px);
    text-align: center;
    line-height: 1.4;
    pointer-events: none;
}
.editor-selection-note.hidden {
    display: none;
}
.editor-selection-note strong {
    color: #fff;
}

.editor-tabbar {
    display: flex;
    align-items: stretch;
    background: rgba(9,13,26,0.92);
    border-top: 1px solid rgba(255,255,255,0.08);
    padding-bottom: env(safe-area-inset-bottom);
    flex-shrink: 0;
    z-index: 10;
}
.editor-tab {
    flex: 1;
    border: 0;
    background: transparent;
    color: rgba(255,255,255,0.55);
    padding: 10px 6px 12px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    font-family: inherit;
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    min-height: 60px;
    transition: color 0.15s;
}
.editor-tab .svg-icon {
    font-size: 1.5rem;
}
.editor-tab:active {
    background: rgba(255,255,255,0.04);
}
.editor-tab.active {
    color: var(--primary);
}

.editor-bottom-sheet {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 55vh;
    background: rgba(11,15,28,0.98);
    border-top: 1px solid rgba(255,255,255,0.1);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: 0 -14px 40px rgba(0,0,0,0.5);
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
    display: flex;
    flex-direction: column;
    padding-bottom: calc(60px + env(safe-area-inset-bottom));
    z-index: 20;
    pointer-events: none;
}
.editor-bottom-sheet.visible {
    transform: translateY(0);
    pointer-events: auto;
}
.bottom-sheet-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 12px 8px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    position: relative;
}
.bottom-sheet-handle {
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255,255,255,0.22);
}
.bottom-sheet-title {
    margin: 6px 0 0;
    flex: 1;
    color: #fff;
    font-size: 0.95rem;
    font-weight: 700;
}
.bottom-sheet-close {
    margin-top: 6px;
}
.bottom-sheet-body {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    -webkit-overflow-scrolling: touch;
}
.editor-tab-panel {
    display: none;
    flex-direction: column;
    gap: 12px;
}
.editor-tab-panel.active {
    display: flex;
}
.editor-helper {
    color: rgba(255,255,255,0.55);
    font-size: 0.75rem;
    line-height: 1.5;
}

.editor-option-row {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
}
.editor-option {
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    background: rgba(255,255,255,0.04);
    color: #fff;
    text-align: left;
    padding: 12px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-height: 56px;
    font-family: inherit;
}
.editor-option:active {
    background: rgba(255,255,255,0.08);
}
.editor-option strong {
    font-size: 0.82rem;
}
.editor-option span {
    color: rgba(255,255,255,0.5);
    font-size: 0.68rem;
}
.editor-option.selected {
    border-color: var(--primary);
    background: rgba(99,102,241,0.16);
    box-shadow: inset 0 0 0 1px rgba(99,102,241,0.3);
}

.editor-photo-picker {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 10px;
}
.editor-photo-chip {
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    background: rgba(255,255,255,0.03);
    overflow: hidden;
    padding: 0;
    cursor: pointer;
    color: inherit;
    font-family: inherit;
}
.editor-photo-chip:active {
    transform: scale(0.98);
}
.editor-photo-chip.selected {
    border-color: rgba(34,197,94,0.8);
    box-shadow: inset 0 0 0 2px rgba(34,197,94,0.4);
}
.editor-photo-chip img {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    display: block;
    background: rgba(0,0,0,0.3);
}
.editor-photo-chip-meta {
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    text-align: left;
}
.editor-photo-chip-meta strong {
    color: #fff;
    font-size: 0.75rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.editor-photo-chip-meta span {
    color: rgba(255,255,255,0.5);
    font-size: 0.66rem;
}

.editor-search {
    width: 100%;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    background: rgba(255,255,255,0.05);
    color: #fff;
    padding: 12px 14px;
    outline: none;
    font-family: inherit;
    font-size: 0.9rem;
}
.editor-search::placeholder {
    color: rgba(255,255,255,0.32);
}
.editor-search:focus {
    border-color: var(--primary);
    background: rgba(255,255,255,0.08);
}

.sticker-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
    gap: 8px;
}
.sticker-item {
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    background: rgba(255,255,255,0.04);
    padding: 10px 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
    justify-content: center;
    min-height: 92px;
    font-family: inherit;
}
.sticker-item:active {
    background: rgba(255,255,255,0.1);
    transform: scale(0.96);
}
.sticker-item img {
    width: 48px;
    height: 48px;
    object-fit: contain;
}
.sticker-item span {
    color: rgba(255,255,255,0.58);
    font-size: 0.62rem;
    text-align: center;
    line-height: 1.3;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
}

.editor-empty {
    color: rgba(255,255,255,0.42);
    font-size: 0.75rem;
    line-height: 1.5;
    padding: 12px;
    text-align: center;
}

.text-input {
    resize: vertical;
    min-height: 60px;
    font-family: inherit;
    line-height: 1.4;
}
.text-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    color: rgba(255,255,255,0.72);
    font-size: 0.78rem;
}
.text-field > span {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}
.text-field strong {
    color: #fff;
    font-size: 0.72rem;
    font-weight: 600;
}
.text-select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.05);
    color: #fff;
    font-family: inherit;
    font-size: 0.85rem;
    appearance: none;
}
.text-color-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.text-color-swatch {
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: 2px solid rgba(255,255,255,0.18);
    background: #fff;
    cursor: pointer;
    padding: 0;
    transition: transform 0.14s, border-color 0.14s;
}
.text-color-swatch:active { transform: scale(0.92); }
.text-color-swatch.active {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(99,102,241,0.3);
}
.text-add-btn {
    width: 100%;
    height: 48px;
    border: 0;
    border-radius: 14px;
    background: linear-gradient(180deg, #818cf8, #6366f1);
    color: #fff;
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(99,102,241,0.36);
    transition: transform 0.14s;
}
.text-add-btn:active {
    transform: translateY(1px) scale(0.98);
}

.sticker-upload-btn {
    width: 100%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    border: 1px dashed rgba(255,255,255,0.25);
    border-radius: 12px;
    background: rgba(255,255,255,0.04);
    color: #fff;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
}
.sticker-upload-btn:active { background: rgba(255,255,255,0.1); }
.sticker-upload-btn .svg-icon { font-size: 1.1rem; color: var(--accent); }

@media (max-width: 900px) {
    #gesture-tag {
        max-width: calc(100vw - 24px);
        text-align: center;
    }

    #capture-btn {
        width: 76px;
        height: 76px;
    }

    #capture-btn img {
        width: 40px;
        height: 40px;
    }

    #capture-btn .capture-fallback {
        font-size: 1.72rem;
    }

    .capture-dock-btn {
        width: 48px;
        height: 48px;
    }

    #capture-dock {
        padding: 0 22px max(18px, calc(env(safe-area-inset-bottom) + 14px));
        gap: 16px;
    }

    #save-toast {
        max-width: calc(100vw - 120px);
        text-align: center;
    }

}

@media (max-width: 640px) {
    #history-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .editor-title { font-size: 0.92rem; }
    .editor-save-btn { padding: 0 14px; font-size: 0.85rem; }
    .sticker-tool-btn { width: 38px; height: 38px; }
    .editor-sticker-toolbar { gap: 4px; padding: 4px; }
}

/* Landscape: rotate the whole capture UI so shutter + secondary buttons
   live on the right edge like stock phone camera apps. Every floating
   element that anchors to bottom in portrait has to be moved out from
   under the vertical dock. */
@media (orientation: landscape) {
    #capture-dock {
        top: 0;
        right: 0;
        bottom: 0;
        left: auto;
        padding:
            max(20px, calc(env(safe-area-inset-top) + 16px))
            max(20px, calc(env(safe-area-inset-right) + 16px))
            max(20px, calc(env(safe-area-inset-bottom) + 16px))
            20px;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        gap: 16px;
    }

    #save-toast {
        top: 50%;
        bottom: auto;
        left: max(20px, calc(env(safe-area-inset-left) + 16px));
        transform: translateY(-50%) translateX(-10px);
    }
    #save-toast.visible {
        transform: translateY(-50%) translateX(0);
    }

    #effects-sheet {
        top: 50%;
        bottom: auto;
        right: calc(112px + env(safe-area-inset-right));
        left: auto;
        max-width: 56vw;
        max-height: 85vh;
        overflow-y: auto;
        padding: 0 4px 0 12px;
        transform: translateY(-50%) translateX(8px);
    }
    #effects-sheet.visible {
        transform: translateY(-50%) translateX(0);
    }

    #help-sheet {
        left: auto;
        right: calc(112px + env(safe-area-inset-right));
        max-width: min(520px, 60vw);
        border-radius: 20px 0 0 20px;
    }

    /* Stack the top-left controls vertically so they don't fight the
       vertical dock on the right edge. switch / fullscreen already sit
       on the left, move the timer toggle there too instead of the
       top-right corner where it used to live. */
    #timer-toggle {
        top: max(120px, calc(env(safe-area-inset-top) + 112px));
        right: auto;
        left: max(16px, calc(env(safe-area-inset-left) + 10px));
    }

    /* Landscape has no vertical dock on top, so the clear / insert pills
       can sit next to the fullscreen button at the same y-level instead
       of floating mid-screen like portrait. */
    #clear-frame-btn,
    #insert-photo-btn {
        top: max(72px, calc(env(safe-area-inset-top) + 66px));
    }
}
