/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    touch-action: none;
    position: fixed;
    font-family: -apple-system, 'Helvetica Neue', sans-serif;
}

/* ==================== SCREENS ==================== */
.screen {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    display: none;
    transition: opacity 0.4s ease;
}
.screen.active {
    display: flex;
}

/* ==================== MENU SCREEN ==================== */
.menu-bg {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #6C5CE7, #a29bfe, #74b9ff);
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
}

.menu-title {
    flex-shrink: 0;
    margin: 10px 0 4px 0;
    display: flex;
    align-items: center;
    gap: 10px;
}
.title-logo {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid white;
    box-shadow: 0 3px 10px rgba(0,0,0,0.25);
}

/* Main 3 sections */
.main-sections {
    display: flex;
    gap: 20px;
    justify-content: center;
    align-items: center;
    flex: 1;
    flex-wrap: wrap;
    padding: 20px;
}

.section-card {
    width: 200px;
    height: 200px;
    background: rgba(255,255,255,0.95);
    border: none;
    border-radius: 28px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    cursor: pointer;
    transition: transform 0.2s ease;
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    padding: 20px;
    -webkit-appearance: none;
    appearance: none;
}
.section-card:active {
    transform: scale(0.92);
}

.section-icon {
    width: 90px;
    height: 90px;
}
.section-icon svg {
    width: 100%;
    height: 100%;
}

.section-label {
    font-size: 22px;
    font-weight: 800;
    color: #444;
    letter-spacing: 0.5px;
}

/* List screen header */
.list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 10px 16px;
    flex-shrink: 0;
}

.list-title-text {
    color: white;
    font-size: 24px;
    font-weight: 800;
    text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.title-svg {
    width: 220px;
    height: 65px;
    filter: drop-shadow(0 3px 6px rgba(0,0,0,0.2));
}

/* Category tabs */
.category-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    justify-content: center;
    flex-shrink: 0;
}

.cat-tab {
    height: 50px;
    padding: 0 20px;
    border: none;
    border-radius: 25px;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.15s ease, background 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
    color: white;
    background: rgba(255,255,255,0.2);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
.cat-tab:active { transform: scale(0.92); }
.cat-tab.active {
    background: white;
    color: #6C5CE7;
    box-shadow: 0 4px 15px rgba(0,0,0,0.15);
}
.cat-tab svg { width: 24px; height: 24px; }

.gallery-menu-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 20px;
    border: none;
    border-radius: 25px;
    background: rgba(255,255,255,0.25);
    color: white;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.15s ease;
}
.gallery-menu-btn:active { transform: scale(0.92); }
.gallery-label { pointer-events: none; }

/* Saved Gallery screen */
.gallery-bg {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #6C5CE7, #a29bfe, #74b9ff);
    display: flex;
    flex-direction: column;
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.gallery-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    flex-shrink: 0;
}

.gallery-title {
    color: white;
    font-size: 24px;
    font-weight: 800;
    text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.saved-gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 14px;
    width: 100%;
    padding-bottom: 30px;
}

.saved-item {
    aspect-ratio: 1;
    background: white;
    border-radius: 16px;
    overflow: hidden;
    position: relative;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    cursor: pointer;
    transition: transform 0.15s ease;
}
.saved-item:active { transform: scale(0.95); }
.saved-item img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}
.saved-item .saved-delete {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 68, 68, 0.85);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.saved-item .saved-delete svg {
    width: 18px;
    height: 18px;
}
.saved-item .saved-date {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,0.06);
    text-align: center;
    padding: 4px;
    font-size: 10px;
    font-weight: 600;
    color: #666;
}

.gallery-empty {
    grid-column: 1 / -1;
    text-align: center;
    color: rgba(255,255,255,0.6);
    font-size: 18px;
    font-weight: 600;
    padding: 60px 20px;
}

/* Save feedback toast */
.save-toast {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 16px 28px;
    border-radius: 16px;
    font-size: 18px;
    font-weight: 700;
    z-index: 500;
    pointer-events: none;
    transition: transform 0.3s ease;
}
.save-toast.show {
    transform: translate(-50%, -50%) scale(1);
}

/* Drawing grid - scrollable area */
.drawing-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 14px;
    width: 100%;
    max-width: 900px;
    padding: 8px 16px 30px 16px;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    flex: 1;
    align-content: start;
}

.drawing-card {
    background: white;
    border-radius: 20px;
    cursor: pointer;
    transition: transform 0.2s ease;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    padding: 12px;
    position: relative;
    overflow: hidden;
}
/* Use padding trick for square cards (works on all iPadOS versions) */
.drawing-card {
    padding-top: calc(100% - 24px); /* 100% width minus padding */
}
.drawing-card img,
.drawing-card svg {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 28px; /* leave space for label */
    width: calc(100% - 20px);
    height: calc(100% - 38px);
    object-fit: contain;
    display: block;
}
.drawing-card:active {
    transform: scale(0.92);
}
.drawing-card .card-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,0.05);
    text-align: center;
    padding: 4px;
    font-size: 11px;
    font-weight: 700;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.color-badge {
    display: inline-block;
    background: #FF69B4;
    color: white;
    font-size: 8px;
    padding: 1px 5px;
    border-radius: 6px;
    vertical-align: middle;
    margin-right: 2px;
    letter-spacing: 0;
}

.coloring-card {
    border: 2px solid #FFB6C1;
}

/* ==================== DRAW SCREEN ==================== */
#screen-draw {
    background: #FFF9E6;
    flex-direction: column;
}

#guide-canvas, #draw-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    touch-action: none;
}
#guide-canvas { z-index: 1; }
#draw-canvas { z-index: 2; }

/* Top bar */
#draw-top-bar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    z-index: 20;
    pointer-events: none;
}
#draw-top-bar > * { pointer-events: auto; }

/* Circle buttons */
.circle-btn {
    width: 52px;
    height: 52px;
    border: none;
    border-radius: 50%;
    background: rgba(0,0,0,0.25);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.15s ease;
    padding: 10px;
}
.circle-btn:active { transform: scale(0.85); }
.circle-btn svg { width: 100%; height: 100%; }

/* Emoji-based buttons */
.emoji-btn {
    font-size: 24px;
    line-height: 1;
    padding: 0;
    text-align: center;
    -webkit-text-size-adjust: none;
}
.mode-btn.emoji-btn {
    font-size: 22px;
}

/* Step indicators */
.step-indicators {
    display: flex;
    gap: 8px;
    align-items: center;
}
.step-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: rgba(255,255,255,0.4);
    transition: background 0.3s ease, transform 0.3s ease;
    border: 2px solid rgba(255,255,255,0.6);
}
.step-dot.done {
    background: #00b894;
    border-color: #00b894;
}
.step-dot.current {
    background: #fdcb6e;
    border-color: #fdcb6e;
    transform: scale(1.3);
    box-shadow: 0 0 10px rgba(253,203,110,0.5);
}

/* Bottom color dots */
#draw-bottom-bar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20;
    pointer-events: none;
}

.color-dots {
    display: flex;
    gap: 4px;
    padding: 6px 16px;
    padding-top: 45px;
    background: transparent;
    pointer-events: auto;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    max-width: 100vw;
    scrollbar-width: none;
}
.color-dots::-webkit-scrollbar { display: none; }

/* Pencil-shaped color selector - realistic style */
.color-pencil {
    flex-shrink: 0;
    width: 70px;
    height: 120px;
    cursor: pointer;
    transition: transform 0.15s ease;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: none;
    border: none;
    padding: 0;
    margin: 0;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}
.color-pencil:active { transform: scale(0.9); }
.color-pencil.active {
    transform: translateY(-28px) scale(1.25);
    filter: drop-shadow(0 6px 12px rgba(0,0,0,0.35));
    z-index: 10;
}

/* Color tip (small triangle at very top) */
.color-pencil .pencil-color-tip {
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 14px solid;
    flex-shrink: 0;
    position: relative;
    z-index: 2;
}

/* Wood cone */
.color-pencil .pencil-wood {
    width: 0;
    height: 0;
    border-left: 28px solid transparent;
    border-right: 28px solid transparent;
    border-bottom: 22px solid #F0C06E;
    flex-shrink: 0;
    margin-top: -6px;
    position: relative;
    z-index: 1;
}

/* Scalloped edge between wood and body */
.color-pencil .pencil-scallop {
    width: 56px;
    height: 10px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
    margin-top: -1px;
}
.color-pencil .pencil-scallop::before {
    content: '';
    position: absolute;
    top: -6px;
    left: -4px;
    right: -4px;
    height: 12px;
    background: radial-gradient(circle 7px at 7px 0, transparent 6px, currentColor 6.5px);
    background-size: 14px 12px;
    background-position: 0 0;
}

/* Body (main color, rounded bottom) */
.color-pencil .pencil-body {
    width: 56px;
    flex: 1;
    min-height: 52px;
    border-radius: 0 0 10px 10px;
}

/* Keep old color-dot for guided drawing screen */
.color-dot {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 4px solid transparent;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.15s ease;
    flex-shrink: 0;
}
.color-dot:active { transform: scale(0.85); }
.color-dot.active {
    border-color: #333;
    transform: scale(1.15);
    box-shadow: 0 0 0 3px white, 0 0 0 5px #333;
}

/* Step hint */
.step-hint {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 30;
    pointer-events: none;
}
.step-hint.hidden { display: none; }
.step-hint svg {
    width: 200px;
    height: 200px;
    opacity: 0.15;
    animation: pulse-hint 2s ease-in-out infinite;
}
@keyframes pulse-hint {
    0%, 100% { opacity: 0.1; transform: scale(1); }
    50% { opacity: 0.2; transform: scale(1.05); }
}

/* ==================== COLORING SCREEN ==================== */
#screen-color {
    background: #F5F5F5;
    flex-direction: column;
}

/* Layer order: fill(1) -> paint(2) -> outlines(4)
   Paint strokes always go BELOW the black outlines
   so lines are never covered by the brush */
#fill-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 1;
    touch-action: none;
    pointer-events: none;
}

#paint-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 2;
    touch-action: none;
    pointer-events: none;
}

#color-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 4;
    touch-action: none;
}

.color-top-bar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    z-index: 20;
    pointer-events: none;
}
.color-top-bar > * { pointer-events: auto; }

.color-top-right {
    display: flex;
    gap: 8px;
}

.mode-toggle {
    display: flex;
    gap: 4px;
    background: rgba(0,0,0,0.15);
    border-radius: 30px;
    padding: 4px;
}

.mode-btn {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 50%;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.15s ease;
    padding: 10px;
}
.mode-btn:active { transform: scale(0.85); }
.mode-btn.active {
    background: rgba(255,255,255,0.3);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.color-bottom-bar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 165px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 20;
    pointer-events: none;
}
.color-bottom-bar .color-dots {
    pointer-events: auto;
}

/* ==================== DONE SCREEN ==================== */
#screen-done {
    background: linear-gradient(135deg, #00b894, #55efc4, #81ecec);
    align-items: center;
    justify-content: center;
}

.done-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.done-stars {
    display: flex;
    gap: 12px;
}
.star {
    width: 60px;
    height: 60px;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}
.star-1 { animation: star-pop 0.5s ease 0.2s both; }
.star-2 { animation: star-pop 0.5s ease 0.5s both; }
.star-3 { animation: star-pop 0.5s ease 0.8s both; }

@keyframes star-pop {
    0% { transform: scale(0) rotate(-30deg); opacity: 0; }
    60% { transform: scale(1.3) rotate(10deg); opacity: 1; }
    100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

.result-canvas {
    width: 320px;
    height: 320px;
    border-radius: 24px;
    background: white;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}

.done-buttons {
    display: flex;
    gap: 16px;
}

.pill-btn {
    width: 80px;
    height: 80px;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.15s ease;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}
.pill-btn:active { transform: scale(0.88); }
.pill-btn.green { background: #00b894; }
.pill-btn.purple { background: #6C5CE7; }

/* ==================== CONFETTI ==================== */
.confetti-piece {
    position: fixed;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    z-index: 100;
    pointer-events: none;
    animation: confetti-fall 2s ease-out forwards;
}
@keyframes confetti-fall {
    0% { transform: translateY(0) rotate(0deg); opacity: 1; }
    100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* ==================== TRACING LINE ANIMATIONS ==================== */
@keyframes trace-pulse {
    0%, 100% { stroke-dashoffset: 0; opacity: 0.6; }
    50% { opacity: 1; }
}

/* ==================== RESPONSIVE ==================== */
@media (max-height: 500px) {
    .menu-title { margin-bottom: 4px; }
    .title-svg { width: 160px; height: 48px; }
    .drawing-grid { gap: 10px; }
    .drawing-card { border-radius: 14px; }
    .done-stars .star { width: 44px; height: 44px; }
    .result-canvas { width: 240px; height: 240px; }
}

@media (min-width: 700px) {
    .drawing-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    }
}

/* Safe area */
@supports (padding: env(safe-area-inset-left)) {
    #draw-top-bar {
        padding-top: calc(8px + env(safe-area-inset-top));
    }
    #draw-bottom-bar {
        padding-bottom: env(safe-area-inset-bottom);
    }
    .menu-bg {
        padding-top: calc(16px + env(safe-area-inset-top));
    }
}
