/* ---------------------------------------------------------------------------
 * AliveYet Daily Puzzle styles.
 *
 * Mobile-first. Color palette mirrors the iOS app's check-in view so the
 * brand identity carries across the funnel:
 *   - 27AE60 / 2ECC71 : primary green (calm, "alive")
 *   - 3498DB         : info blue (highlights, links)
 *   - F39C12         : warm accent (CTAs)
 *   - 2C3E50         : text on light bg
 *
 * Piece colors come from server payload (hex strings) — see puzzle.js.
 * ------------------------------------------------------------------------- */

:root {
    --primary: #27AE60;
    --primary-dark: #1E8449;
    --info: #3498DB;
    --accent: #F39C12;
    --text-dark: #2C3E50;
    --text-mute: #6B7280;
    --surface: #FFFFFF;
    --surface-elev: #F5F7FA;
    --grid-target: #E8F5EE;
    --grid-empty: #FAFBFC;
    --grid-border: #D6DDE4;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --cell-size: 56px;
    --piece-cell-size: 32px;
}

@media (max-width: 380px) {
    :root {
        --cell-size: 48px;
        --piece-cell-size: 28px;
    }
}

@media (min-width: 768px) {
    :root {
        --cell-size: 64px;
        --piece-cell-size: 36px;
    }
}

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

html, body {
    height: 100%;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Noto Sans SC', 'Noto Sans TC', 'Noto Sans JP', 'Noto Sans KR', sans-serif;
    color: var(--text-dark);
    background: linear-gradient(180deg, #F8FFFC 0%, #EFF7F1 100%);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overflow-x: hidden;
}

body[data-lang="zh"], body[data-lang="zh-Hant"] { font-family: 'Noto Sans SC', 'Noto Sans TC', 'Inter', sans-serif; }
body[data-lang="ja"] { font-family: 'Noto Sans JP', 'Inter', sans-serif; }
body[data-lang="ko"] { font-family: 'Noto Sans KR', 'Inter', sans-serif; }

/* --- Header -------------------------------------------------------------- */

.puzzle-header {
    padding: 16px 0;
    background: var(--surface);
    border-bottom: 1px solid var(--grid-border);
}

.puzzle-nav {
    max-width: 560px;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}

.puzzle-logo {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text-dark);
    font-weight: 700;
    font-size: 18px;
}

.puzzle-logo-icon {
    width: 32px;
    height: 32px;
    border-radius: 8px;
}

.lang-switcher {
    padding: 6px 10px;
    border: 1px solid var(--grid-border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: 14px;
    cursor: pointer;
    color: var(--text-dark);
    outline: none;
}
.lang-switcher:focus { border-color: var(--primary); }

/* --- Main ---------------------------------------------------------------- */

.puzzle-main {
    max-width: 560px;
    margin: 0 auto;
    padding: 24px 20px 80px;
}

/* Loading / error overlay shown until the API fetch resolves. Centered
 * placeholder text — intentionally low-effort styling because most users
 * see it for <300ms. Stays visible on error too, with the message swapped. */
.puzzle-status {
    text-align: center;
    padding: 80px 20px;
    color: var(--text-mute);
    font-size: 15px;
}
.puzzle-status-msg { margin: 0; }

.puzzle-intro { text-align: center; margin-bottom: 24px; }
.puzzle-date {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-mute);
    letter-spacing: 0.04em;
    margin-bottom: 8px;
}
.puzzle-question {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-dark);
}

/* --- Board --------------------------------------------------------------- */

.puzzle-board-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

/* Grid is rendered as a CSS grid of cells. JS sets piece colors inline. */
.puzzle-grid {
    display: grid;
    grid-template-columns: repeat(5, var(--cell-size));
    grid-template-rows: repeat(5, var(--cell-size));
    gap: 4px;
    padding: 12px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    /* Prevent native long-press menus from interrupting drags. */
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.puzzle-cell {
    border-radius: var(--radius-sm);
    background: var(--grid-empty);
    border: 1px solid var(--grid-border);
    transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.puzzle-cell.is-target {
    background: var(--grid-target);
    border-color: #B7E0C5;
}
.puzzle-cell.is-filled {
    border-color: rgba(0, 0, 0, 0.08);
    box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.08);
}
.puzzle-cell.is-hover-valid {
    transform: scale(1.04);
    box-shadow: 0 0 0 2px var(--primary), 0 4px 12px rgba(39, 174, 96, 0.25);
}
.puzzle-cell.is-hover-invalid {
    transform: scale(1.02);
    box-shadow: 0 0 0 2px #E74C3C, 0 4px 12px rgba(231, 76, 60, 0.20);
}

/* --- Stats --------------------------------------------------------------- */

.puzzle-stats {
    display: flex;
    gap: 24px;
    padding: 8px 16px;
    background: var(--surface);
    border-radius: 999px;
    box-shadow: var(--shadow-sm);
    font-size: 14px;
}
.puzzle-stat { display: flex; align-items: baseline; gap: 6px; }
.puzzle-stat-label { color: var(--text-mute); }
.puzzle-stat-value {
    font-weight: 700;
    color: var(--text-dark);
    font-variant-numeric: tabular-nums;
}

/* --- Tray (pieces) ------------------------------------------------------- */

.puzzle-tray {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 16px;
    padding: 16px;
    min-height: 100px;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

.puzzle-piece {
    display: grid;
    gap: 2px;
    padding: 4px;
    border-radius: var(--radius-sm);
    cursor: grab;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    transition: transform 0.15s ease, opacity 0.2s ease;
}
.puzzle-piece:active { cursor: grabbing; }
.puzzle-piece.is-dragging { opacity: 0.4; }
.puzzle-piece.is-placed { display: none; }

.puzzle-piece-cell {
    width: var(--piece-cell-size);
    height: var(--piece-cell-size);
    border-radius: 4px;
    box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18);
}
.puzzle-piece-cell.is-empty {
    background: transparent;
    box-shadow: none;
}

/* Ghost piece that follows the cursor/finger during a drag. */
.puzzle-piece-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 1000;
    opacity: 0.85;
    display: grid;
    gap: 2px;
    transition: opacity 0.1s;
}

/* --- Tools --------------------------------------------------------------- */

.puzzle-tools {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
}

.puzzle-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 12px 20px;
    border: none;
    border-radius: 999px;
    font-family: inherit;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s ease, box-shadow 0.15s ease;
}
.puzzle-btn:active { transform: scale(0.97); }

.puzzle-btn-primary {
    background: var(--primary);
    color: white;
    box-shadow: 0 4px 12px rgba(39, 174, 96, 0.3);
}
.puzzle-btn-primary:hover { background: var(--primary-dark); }

.puzzle-btn-secondary {
    background: var(--surface);
    color: var(--text-dark);
    box-shadow: var(--shadow-sm);
}
.puzzle-btn-secondary:hover { background: var(--surface-elev); }

.puzzle-btn-cta {
    width: 100%;
    justify-content: center;
    padding: 14px 24px;
    font-size: 16px;
    margin-top: 8px;
    text-decoration: none;
}

/* --- Completion overlay -------------------------------------------------- */

.puzzle-complete {
    margin-top: 32px;
    animation: fadeUp 0.5s ease;
}
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
}

.puzzle-complete[hidden] { display: none !important; }

.puzzle-complete-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 32px 24px;
    box-shadow: var(--shadow-lg);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.reveal-emoji {
    font-size: 80px;
    line-height: 1;
    animation: pop 0.6s ease;
}
@keyframes pop {
    0%   { transform: scale(0); }
    60%  { transform: scale(1.2); }
    100% { transform: scale(1); }
}

.reveal-name {
    font-size: 24px;
    font-weight: 700;
    color: var(--text-dark);
}
.reveal-stats {
    font-size: 14px;
    color: var(--text-mute);
}
.reveal-stats strong {
    color: var(--text-dark);
    font-variant-numeric: tabular-nums;
}

.share-grid {
    background: var(--surface-elev);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    font-family: 'Apple Color Emoji', 'Segoe UI Emoji', monospace;
    font-size: 24px;
    line-height: 1.3;
    letter-spacing: 0;
    color: var(--text-dark);
    white-space: pre;
    overflow-x: auto;
}

.puzzle-cta {
    margin-top: 12px;
    padding-top: 16px;
    border-top: 1px solid var(--grid-border);
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}
.puzzle-cta p { color: var(--text-mute); font-size: 14px; }

/* --- Persistent Download promo ------------------------------------------- */
/* Always visible between the board and the completion overlay. The post-
 * solve overlay has its own richer CTA — this one is the safety net for
 * visitors who landed via a shared link and bounce before solving. */
.puzzle-promo {
    margin: 32px auto 0;
    padding: 20px;
    max-width: 480px;
    text-align: center;
    background: rgba(52, 152, 219, 0.06);
    border: 1px solid rgba(52, 152, 219, 0.18);
    border-radius: 18px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}
.puzzle-promo-msg {
    margin: 0;
    color: var(--text-mute);
    font-size: 14px;
    line-height: 1.5;
}

/* --- Footer -------------------------------------------------------------- */

.puzzle-footer {
    text-align: center;
    padding: 24px 20px 32px;
    color: var(--text-mute);
    font-size: 13px;
}
.puzzle-footer a { color: var(--info); text-decoration: none; }
.puzzle-footer a:hover { text-decoration: underline; }
.puzzle-footer .dot { margin: 0 8px; opacity: 0.5; }
.puzzle-footer .copyright { margin-top: 8px; opacity: 0.6; }

/* --- Reduce motion ------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
    *, ::before, ::after {
        animation-duration: 0.01s !important;
        transition-duration: 0.01s !important;
    }
}

/* --- Dark mode ---------------------------------------------------------- */

@media (prefers-color-scheme: dark) {
    :root {
        --text-dark: #ECEFF4;
        --text-mute: #9CA3AF;
        --surface: #1F2933;
        --surface-elev: #2A3441;
        --grid-target: #2A4A3A;
        --grid-empty: #2A3441;
        --grid-border: #3A4452;
        --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.5);
    }
    body { background: linear-gradient(180deg, #161D27 0%, #1F2933 100%); }
    .lang-switcher { color: var(--text-dark); }
}
