* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

/* Screen reader only - visually hidden but accessible */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

:root {
    --bg: #f8f7f4;
    --card: #fff;
    --text: #2d2d2d;
    --muted: #6b7280;
    --accent: #3b7dd8;
    --valid: #3b7dd8;
    --invalid: #e36a2e;
    --tile-bg: linear-gradient(145deg, #fff, #f5f5f4);
    --tile-border: #e5e5e4;
    --logo-tile-bg: linear-gradient(145deg, #fff, #f0f0f0);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #171717;
        --card: #262626;
        --text: #f5f5f4;
        --muted: #a1a1aa;
        --accent: #5b9ae8;
        --valid: #5b9ae8;
        --invalid: #f59e5e;
        --tile-bg: linear-gradient(145deg, #333, #262626);
        --tile-border: #404040;
        --logo-tile-bg: linear-gradient(145deg, #333, #262626);
    }
}

html, body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background-color: #eae8e4;
    background-image: linear-gradient(to bottom, #f5f4f2 0%, #eae8e4 100%);
    color: var(--text);
}

@media (prefers-color-scheme: dark) {
    body {
        background-color: #0f0f0f;
        background-image: linear-gradient(to bottom, #232323 0%, #0f0f0f 100%);
    }
}

.app {
    max-width: 400px;
    margin: 0 auto;
    padding: 12px 12px 28px;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    position: relative;
    z-index: 1;
}

/* Header */
header {
    text-align: center;
    padding: 4px 0;
    flex-shrink: 0;
    position: relative;
}

.logo {
    display: flex;
    justify-content: center;
    height: 44px;
}

.logo-img {
    height: 44px;
    width: 116px;
}

.icon-btn {
    position: absolute;
    right: 0;
    top: 4px;
}

.date {
    display: block;
    font-size: 11px;
    color: var(--muted);
    opacity: 0.7;
    position: relative;
    top: -6px;
    margin-bottom: -6px;
}

.objective {
    text-align: center;
    font-size: 14px;
    color: var(--muted);
    margin: 8px 0;
    flex-shrink: 0;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: none;
    color: var(--muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
    background: var(--tile-border);
    color: var(--text);
}

.icon-btn:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.controls {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.icon-btn svg {
    width: 18px;
    height: 18px;
}

/* Words list */
.words {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 4px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--card);
    border-radius: 10px;
    padding: 8px;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.04);
}

.word {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    background: var(--bg);
    border-radius: 8px;
    font-weight: 600;
    font-size: 16px;
    letter-spacing: 0.02em;
    border: 1px solid var(--tile-border);
    flex-shrink: 0;
    transition: border-color 0.15s;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.word:hover {
    border-color: var(--accent);
}

.word-text {
    flex: 1;
}

.word.new {
    animation: magicStack 0.5s ease-out;
}

.word.new::before {
    content: '';
    position: absolute;
    inset: -2px;
    border-radius: inherit;
    background: linear-gradient(90deg, transparent 0%, rgba(59, 125, 216, 0.25) 50%, transparent 100%);
    animation: shimmer 0.7s ease-out forwards;
    pointer-events: none;
}

@media (prefers-color-scheme: dark) {
    .word.new::before {
        background: linear-gradient(90deg, transparent 0%, rgba(255,255,255,0.7) 50%, transparent 100%);
    }
}

.word.new::after {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: inherit;
    background: linear-gradient(135deg, rgba(59, 125, 216, 0.4), rgba(147, 51, 234, 0.3));
    opacity: 0;
    animation: glowPulse 0.5s ease-out;
    z-index: -1;
    pointer-events: none;
}

@keyframes magicStack {
    0% {
        opacity: 0;
        transform: translateY(-20px) scale(0.9);
    }
    60% {
        transform: translateY(2px) scale(1.02);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes shimmer {
    0% {
        transform: translateX(-100%);
        opacity: 1;
    }
    100% {
        transform: translateX(100%);
        opacity: 0;
    }
}

@keyframes glowPulse {
    0% {
        opacity: 0.8;
        filter: blur(8px);
    }
    100% {
        opacity: 0;
        filter: blur(12px);
    }
}

/* Sparkle particles */
.sparkle {
    position: fixed;
    pointer-events: none;
    z-index: 2;
    animation: sparkleFloat 0.9s ease-out forwards;
}

.sparkle::before {
    content: '✦';
    position: absolute;
    font-size: var(--size, 14px);
    color: inherit;
    top: -4px;
    left: -4px;
}

@keyframes sparkleFloat {
    0% {
        opacity: 1;
        transform: translate(0, 0) scale(1) rotate(0deg);
    }
    50% {
        opacity: 0.8;
    }
    100% {
        opacity: 0;
        transform: translate(var(--tx), var(--ty)) scale(0.3) rotate(var(--rot, 180deg));
    }
}

.word button {
    margin-left: auto;
    width: 24px;
    height: 24px;
    border: none;
    background: none;
    color: var(--muted);
    font-size: 18px;
    cursor: pointer;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.word button:hover {
    color: var(--invalid);
    background: rgba(227, 106, 46, 0.1);
}

.previous-words {
    font-size: 12px;
    color: var(--muted);
    opacity: 0.5;
    padding: 6px 12px;
    margin-top: auto;
    line-height: 1.4;
}

/* Builder group */
.builder-group {
    background: var(--card);
    border-radius: 10px;
    padding: 8px;
    margin-bottom: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    flex-shrink: 0;
}

.builder-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 8px;
}

.builder {
    display: flex;
    align-items: center;
    gap: 4px;
    height: 44px;
    padding: 0 10px;
    background: rgba(74, 144, 217, 0.08);
    border-radius: 6px;
    border: 1px dashed var(--tile-border);
    transition: background 0.15s, border-color 0.15s;
    position: relative;
    flex: 1;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

.builder.valid { background: rgba(59, 125, 216, 0.12); }
.builder.invalid { background: rgba(227, 106, 46, 0.12); }

.delete-btn {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 6px;
    background: var(--tile-border);
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.delete-btn svg {
    width: 18px;
    height: 18px;
}

.delete-btn:hover {
    background: var(--invalid);
    color: #fff;
}

.delete-btn:active { transform: scale(0.9); }

.hint {
    color: var(--muted);
    font-size: 15px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.letter {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 1 32px;
    min-width: 20px;
    height: 32px;
    background: var(--tile-bg);
    border: 2px solid var(--tile-border);
    border-radius: 6px;
    font-weight: 700;
    font-size: 16px;
    color: var(--text);
    flex-shrink: 1;
}

.builder.valid .letter { border-color: var(--valid); }
.builder.invalid .letter { border-color: var(--invalid); }

/* Letter rack */
.rack {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 4px;
    padding: 8px;
    background: var(--card);
    border-radius: 10px;
    margin-bottom: 6px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    flex-shrink: 0;
}

.tile {
    border: 2px solid var(--tile-border);
    border-radius: 6px;
    background: var(--tile-bg);
    color: var(--text);
    font-weight: 700;
    font-size: 16px;
    cursor: pointer;
    position: relative;
    transition: transform 0.1s, border-color 0.15s, box-shadow 0.15s;
}

.rack .tile {
    aspect-ratio: 1 / 1.1;
}

.tile:hover:not(:disabled):not(.used) {
    border-color: var(--accent);
    box-shadow: 0 2px 8px rgba(59, 125, 216, 0.2);
}

.tile:active:not(:disabled) { transform: scale(0.95); }
.tile:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.tile:disabled { cursor: default; }
.tile.used { opacity: 0.25; cursor: default; }

/* Buttons */
.actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.btn {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    background: var(--tile-border);
    color: var(--text);
    transition: transform 0.1s, background 0.15s, box-shadow 0.15s;
}

.btn:hover:not(:disabled) {
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:focus { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) {
    background: #2d6bc4;
    box-shadow: 0 2px 12px rgba(59, 125, 216, 0.3);
}
.btn.primary:disabled { opacity: 0.4; cursor: default; }

.builder.valid + .actions .btn.primary {
    box-shadow: 0 0 12px rgba(59, 125, 216, 0.5);
    animation: btnPulse 1.5s ease-in-out infinite;
}

@keyframes btnPulse {
    0%, 100% { box-shadow: 0 0 8px rgba(59, 125, 216, 0.4); }
    50% { box-shadow: 0 0 18px rgba(59, 125, 216, 0.7); }
}

.progress-bar {
    height: 3px;
    background: var(--tile-border);
    border-radius: 2px;
    overflow: hidden;
    margin: 4px 0;
    opacity: 0.5;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.completed-message {
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 24px;
    background: var(--card);
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    flex: 1;
    cursor: pointer;
}

.completed-message p {
    margin: 0;
    color: var(--text);
    font-size: 16px;
}

.completed-emoji {
    font-size: 64px;
    line-height: 1;
    margin: 16px 0;
}

.completed-message .countdown {
    margin-top: 8px;
    color: var(--muted);
    font-size: 14px;
}

.completed-message .countdown span {
    font-weight: 600;
    color: var(--accent);
}

/* Modals */
.modal {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

.modal.show { opacity: 1; visibility: visible; }

.modal-content {
    background: var(--card);
    border-radius: 16px;
    padding: 28px 24px;
    width: 100%;
    max-width: 320px;
    max-height: calc(100dvh - 40px);
    overflow-y: auto;
    text-align: center;
    position: relative;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    transform: scale(0.8);
    transition: transform 0.25s ease-out;
}

.modal.show .modal-content {
    transform: scale(1);
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: var(--muted);
    font-size: 24px;
    cursor: pointer;
    line-height: 1;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    color: var(--text);
    background: var(--tile-border);
}

.modal-content h2 {
    margin-bottom: 16px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.stats-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin-bottom: 20px;
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.stats-row b {
    display: block;
    font-size: 28px;
    color: var(--accent);
    margin-bottom: 4px;
}

.score-intro {
    color: var(--muted);
    font-size: 14px;
    margin-bottom: 8px;
}

.final-score {
    font-size: 64px;
    font-weight: 800;
    color: var(--accent);
    line-height: 1;
    margin-top: -8px;
}

.final-score.shine {
    background: linear-gradient(
        120deg,
        var(--accent) 0%,
        var(--accent) 40%,
        #a8d4ff 48%,
        #ffffff 50%,
        #a8d4ff 52%,
        var(--accent) 60%,
        var(--accent) 100%
    );
    background-size: 300% 100%;
    background-position: 100% 0;
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: scoreShine 1.2s ease-in-out 0.5s forwards;
}

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

#summary {
    color: var(--muted);
    font-size: 14px;
    margin-top: 0;
    margin-bottom: 16px;
}

.par-message {
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 20px;
    font-style: italic;
    opacity: 0.7;
}

.modal-content .btn { margin-top: 8px; }

.btn-row { display: flex; justify-content: center; gap: 10px; margin-top: 8px; }
.btn-row .btn { flex: 1; margin-top: 0; }

.share-row { display: flex; justify-content: center; gap: 10px; margin-top: 8px; }
.share-social { width: 40px; height: 40px; padding: 0; display: flex; align-items: center; justify-content: center; background: var(--key-bg); border-radius: 8px; margin-top: 0; }
.share-social:hover { opacity: 0.85; }
#shareX:hover { background: #000; color: #fff; }
#shareFB:hover { background: #1877F2; color: #fff; }
#shareReddit:hover { background: #FF4500; color: #fff; }

.modal-footer {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--tile-border);
}

.modal-footer p {
    font-size: 13px;
    margin-bottom: 16px;
}

.coffee-btn {
    background: #f5c542;
    color: #1a1a1a;
    font-size: 13px;
    padding: 10px 16px;
}

.coffee-btn:hover {
    background: #e5b432;
}

/* Animations */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-6px); }
    75% { transform: translateX(6px); }
}

.shake { animation: shake 0.25s ease; }

/* Ambient background particles */
.ambient-particles {
    position: fixed;
    inset: 0;
    pointer-events: none;
    overflow: hidden;
    z-index: 0;
}

.ambient-particle {
    position: absolute;
    bottom: -10px;
    width: var(--size, 4px);
    height: var(--size, 4px);
    background: var(--accent);
    border-radius: 50%;
    opacity: 0;
    animation: ambientDrift linear infinite;
}

@keyframes ambientDrift {
    0% {
        opacity: 0;
        transform: translateY(0) translateX(0);
        bottom: -10px;
    }
    10% {
        opacity: 0.06;
    }
    50% {
        opacity: 0.08;
        transform: translateY(-50vh) translateX(var(--sway, 25px));
    }
    90% {
        opacity: 0.06;
    }
    100% {
        opacity: 0;
        transform: translateY(-105vh) translateX(0);
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .ambient-particles {
        display: none;
    }
}

/* Confetti */
/* Footer */
.site-footer {
    position: fixed;
    bottom: 8px;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
    font-size: 11px;
    color: var(--muted);
    opacity: 0.5;
}

.site-footer a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.site-footer a:hover {
    text-decoration: underline;
}

/* Landscape phone layout */
@media (orientation: landscape) and (max-height: 500px) {
    .app {
        max-width: 100%;
        padding: 6px 12px;
        display: grid;
        grid-template-columns: 1fr 1.3fr;
        grid-template-rows: auto 1fr;
        grid-template-areas:
            "header header"
            "words controls";
        gap: 4px 8px;
        height: 100dvh;
    }

    header {
        grid-area: header;
        padding: 0;
    }

    .logo { display: none; }
    #statsBtn { display: none; }
    .date { display: none; }
    .objective { display: none; }
    header { min-height: 0; }

    .words {
        grid-area: words;
        margin: 0;
        padding: 6px;
        gap: 3px;
    }

    .word {
        padding: 4px 8px;
        font-size: 13px;
    }

    .word button { width: 20px; height: 20px; font-size: 14px; }

    .controls {
        grid-area: controls;
        display: flex;
        flex-direction: column;
        gap: 2px;
        min-height: 0;
        overflow-y: auto;
    }

    .builder-group {
        padding: 4px;
        margin-bottom: 2px;
    }

    .builder-row {
        margin-bottom: 4px;
    }

    .builder {
        height: 36px;
        padding: 0 6px;
    }

    .builder .letter {
        flex: 0 1 24px;
        height: 24px;
        font-size: 13px;
    }

    .delete-btn {
        width: 26px;
        height: 26px;
    }

    .hint { font-size: 12px; }

    .rack {
        display: flex;
        padding: 2px;
        gap: 2px;
        margin: 0;
        flex: 1;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .tile {
        font-size: 14px;
        border-radius: 2px;
        border-width: 1px;
    }

    .rack .tile {
        flex: 0 0 30px;
        aspect-ratio: auto;
        height: 22px;
    }

    .actions {
        margin: 0;
    }

    .btn {
        flex: 1;
        padding: 10px 4px;
        font-size: 13px;
    }

    .completed-message { padding: 12px; }
    .site-footer { display: none; }
}

/* Desktop layout - require both width and height to avoid landscape phones */
@media (min-width: 700px) and (min-height: 500px) {
    .app {
        max-width: 680px;
        padding: 20px 20px 40px;
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto auto 1fr;
        grid-template-areas:
            "header header"
            "objective objective"
            "words controls";
        gap: 16px;
    }

    header { grid-area: header; }
    .objective { grid-area: objective; margin: 0; }
    .words { grid-area: words; margin: 0; }
    .completed-message { margin-top: 0; }

    .controls {
        grid-area: controls;
        display: flex;
        flex-direction: column;
        gap: 12px;
        min-width: 0;
    }

    .actions {
        margin: 0;
    }

    .builder-group {
        margin-bottom: 0;
    }

    .builder-row {
        margin-bottom: 10px;
    }

    .delete-btn {
        height: 52px;
    }

    .builder, .rack {
        flex-shrink: 0;
    }

    .rack .tile {
        font-size: 18px;
    }

    .builder {
        height: 52px;
    }

    .logo-img {
        height: 64px;
        width: 169px;
    }

    .logo {
        height: 64px;
    }

    .objective { font-size: 15px; }

    .word {
        font-size: 17px;
        padding: 10px 14px;
    }

    .btn {
        padding: 12px;
    }
}
