/**
 * Cuppa Word Search - Styled for seniors with large touch targets
 * Based on Wordfind.js by BunKat LLC (MIT License)
 */

/* ========== Reset & Base ========== */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    margin: 0;
    padding: 0 0 calc(68px + env(safe-area-inset-bottom, 20px)) 0;
    background-color: #faf9f6;
    color: #2c3e50;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    overflow-x: hidden;
}

/* ========== Cuppa Navigation ========== */
.cuppa-header {
    background: #FFFFFF;
    border-bottom: 1px solid #E8E0D6;
    padding: 6px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 200;
}

.cuppa-back {
    display: flex;
    align-items: center;
    gap: 4px;
    text-decoration: none;
    color: #6B4226;
    font-size: 15px;
    font-weight: 600;
    padding: 6px 8px;
    border-radius: 8px;
    transition: background 0.15s;
    min-width: 40px;
    min-height: 32px;
}

.cuppa-back:hover { background: #F0E1C8; }
.cuppa-back:active { background: #E8E0D6; }

.cuppa-back svg {
    width: 20px;
    height: 20px;
    stroke: #6B4226;
    stroke-width: 2.5;
    fill: none;
}

.cuppa-header-title {
    font-size: 20px;
    font-weight: 700;
    color: #6B4226;
    flex: 1;
}

.cuppa-logo-mini {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, #6B4226, #D4A84B);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: white;
    font-weight: 800;
    text-decoration: none;
}

/* ========== Header ========== */
header {
    background: linear-gradient(135deg, #6B4226, #D4A84B);
    color: white;
    text-align: center;
    padding: 1rem 1rem 0.8rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

header h1 {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}

header .subtitle {
    margin: 0.3rem 0 0;
    font-size: 1rem;
    opacity: 0.92;
    font-weight: 400;
}

/* ========== Main Layout ========== */
#main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

@media only screen and (min-width: 768px) {
    #main {
        flex-direction: row;
        align-items: flex-start;
        justify-content: center;
        gap: 2rem;
        padding: 1.5rem;
    }
}

/* ========== Puzzle Grid ========== */
#puzzle-container {
    width: 100%;
    display: flex;
    justify-content: center;
}

#puzzle {
    /* Drags may start in the spacing between letter buttons too. */
    touch-action: none;
    display: inline-block;
    background: white;
    border-radius: 12px;
    padding: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
    border: 2px solid #e0e0e0;
    line-height: 0;
}

#puzzle > div {
    display: flex;
    justify-content: center;
    margin: 0;
}

/* Puzzle squares - mobile first (large touch targets) */
.puzzleSquare {
    width: var(--ws-cell-size, 9vw);
    height: var(--ws-cell-size, 9vw);
    max-width: 44px;
    max-height: 44px;
    min-width: 0;
    min-height: 0;
    text-transform: uppercase;
    background-color: white;
    border: 1px solid #eee;
    border-radius: 4px;
    font-size: var(--ws-font-size, 5.5vw);
    font-weight: 600;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    color: #2c3e50;
    cursor: pointer;
    padding: 0;
    margin: 1px;
    transition: background-color 0.15s ease, transform 0.1s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

@media only screen and (min-width: 480px) {
    .puzzleSquare {
        width: var(--ws-cell-size, 42px);
        height: var(--ws-cell-size, 42px);
        font-size: var(--ws-font-size, 1.4rem);
        margin: 1.5px;
    }
}

@media only screen and (min-width: 768px) {
    .puzzleSquare {
        width: var(--ws-cell-size, 46px);
        height: var(--ws-cell-size, 46px);
        font-size: var(--ws-font-size, 1.5rem);
        margin: 2px;
        border-radius: 6px;
    }
}

.puzzleSquare:hover {
    background-color: #f0f0f0;
}

button::-moz-focus-inner {
    border: 0;
}

/* Selection states */
#puzzle .selected {
    background-color: #D4A84B;
    color: white;
    transform: scale(1.08);
    border-color: #c49a3d;
    box-shadow: 0 0 6px rgba(212, 168, 75, 0.4);
}

#puzzle .found {
    background-color: #27ae60;
    color: white;
    border-color: #219a52;
}

#puzzle .solved {
    background-color: #8e44ad;
    color: white;
    border-color: #7d3c98;
}

#puzzle .complete {
    background-color: #27ae60;
    color: white;
    animation: celebrate 0.5s ease;
}

@keyframes celebrate {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* ========== Sidebar ========== */
#sidebar {
    width: 100%;
    max-width: 400px;
}

/* ========== Word List ========== */
#words-container {
    background: white;
    border-radius: 12px;
    padding: 1rem 1.2rem;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    border: 2px solid #e0e0e0;
}

#words-container h2 {
    margin: 0 0 0.6rem;
    font-size: 1.2rem;
    color: #6B4226;
    border-bottom: 2px solid #D4A84B;
    padding-bottom: 0.4rem;
}

#words-container h2 span {
    font-size: 0.9rem;
    font-weight: 400;
    color: #7f8c8d;
}

#words {
    list-style-type: none;
    padding: 0;
    margin: 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px 12px;
}

@media only screen and (min-width: 480px) {
    #words {
        grid-template-columns: 1fr 1fr;
        gap: 6px 16px;
    }
}

#words li {
    padding: 0;
}

/* Word inputs styled as labels */
input.word {
    border: none;
    background: #f8f9fa;
    font-size: 1rem;
    font-weight: 600;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    text-transform: uppercase;
    color: #2c3e50;
    padding: 6px 10px;
    border-radius: 6px;
    width: 100%;
    cursor: default;
    transition: all 0.3s ease;
    letter-spacing: 0.03em;
}

input.word:focus {
    outline: none;
}

/* Word found state */
input.word.wordFound {
    text-decoration: line-through;
    color: #95a5a6;
    background-color: #eafaf1;
}

/* ========== Controls ========== */
#controls {
    display: flex;
    gap: 10px;
    margin-top: 1rem;
    padding: 0;
}

#controls button {
    flex: 1;
    padding: 14px 20px;
    font-size: 1.05rem;
    font-weight: 600;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    min-height: 48px;
}

#solve {
    background-color: #ecf0f1;
    color: #7f8c8d;
}

#solve:hover {
    background-color: #d5dbdb;
    color: #5d6d7e;
}

#solve:active {
    transform: scale(0.97);
}

#new-game {
    background: #6B4226;
    color: white;
    box-shadow: 0 2px 6px rgba(107, 66, 38, 0.3);
}

#new-game:hover {
    box-shadow: 0 4px 12px rgba(107, 66, 38, 0.4);
}

#new-game:active {
    transform: scale(0.97);
}

#result-message {
    text-align: center;
    font-size: 1.1rem;
    margin-top: 0.5rem;
    min-height: 1.5em;
}

/* ========== Completion Modal ========== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 20000;
    animation: fadeIn 0.3s ease;
    padding: 12px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: white;
    border-radius: 16px;
    padding: 2rem 2.5rem;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    animation: slideUp 0.3s ease;
    max-width: 90vw;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.modal-content h2 {
    color: #6B4226;
    font-size: 1.8rem;
    margin: 0 0 0.5rem;
}

.modal-content p {
    font-size: 1.15rem;
    color: #555;
    margin: 0 0 1.2rem;
}

#play-again {
    background: #6B4226;
    color: white;
    border: none;
    padding: 14px 36px;
    font-size: 1.1rem;
    font-weight: 600;
    border-radius: 10px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(107, 66, 38, 0.3);
    min-height: 48px;
    -webkit-tap-highlight-color: transparent;
}

#play-again:hover {
    box-shadow: 0 4px 16px rgba(107, 66, 38, 0.4);
}

#play-again:active {
    transform: scale(0.97);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* ========== Completion Trophy ========== */
.completion-trophy {
    font-size: 48px;
    margin-bottom: 8px;
    animation: trophyBounce 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes trophyBounce {
    0% { transform: scale(0); opacity: 0; }
    60% { transform: scale(1.2); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

/* ========== Confetti ========== */
.confetti-piece {
    position: fixed;
    width: 10px;
    height: 10px;
    z-index: 1001;
    pointer-events: none;
}

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

/* ========== Hide add-word button ========== */
#add-word {
    display: none !important;
}

/* ========== Print styles ========== */
@media print {
    header { background: none; color: black; }
    #controls { display: none; }
    .modal { display: none; }
}

/* ========== 2026 Standalone Puzzle Polish ========== */
/*
 * Keep these overrides scoped to the standalone shell. The older Wordfind
 * layout is still used elsewhere and should retain its square-based states.
 */
html body.ws-2026 {
    --ws-board-paper: #fffdf8;
    --ws-board-ink: #281e18;
    --ws-focus: #f6c95f;
}

html body.ws-2026 #puzzle {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    max-width: 100%;
    background: var(--ws-board-paper) !important;
    border: 1px solid rgba(76, 43, 8, 0.1) !important;
    border-radius: 22px !important;
    padding: clamp(10px, 1.6vw, 16px) !important;
    box-shadow:
        0 16px 36px rgba(76, 43, 8, 0.12),
        0 3px 9px rgba(76, 43, 8, 0.07),
        inset 0 0 0 5px rgba(255, 255, 255, 0.48) !important;
}

html body.ws-2026 #puzzle-container {
    min-width: 0;
    max-width: calc(100% - 268px);
}

html body.ws-2026 #puzzle > div {
    position: relative;
    z-index: 2;
}

/* Letters sit above the SVG capsules, with the board acting as one surface. */
html body.ws-2026 #puzzle .puzzleSquare {
    position: relative;
    z-index: 2;
    width: var(--ws-cell-size, clamp(26px, 9vw, 41px)) !important;
    height: var(--ws-cell-size, clamp(26px, 9vw, 41px)) !important;
    max-width: none;
    max-height: none;
    margin: clamp(0.5px, 0.25vw, 1.5px) !important;
    appearance: none;
    background: transparent !important;
    border: 0 !important;
    border-radius: 9px !important;
    box-shadow: none !important;
    color: var(--ws-board-ink) !important;
    font-size: var(--ws-font-size, clamp(15px, 5vw, 22px)) !important;
    font-weight: 700;
    line-height: 1;
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.82);
    transition:
        transform 120ms ease,
        color 140ms ease,
        background-color 140ms ease;
}

html body.ws-2026 #puzzle .puzzleSquare:hover {
    background: transparent !important;
}

@media (hover: hover) and (pointer: fine) {
    html body.ws-2026 #puzzle .puzzleSquare:hover {
        background: rgba(107, 66, 38, 0.07) !important;
        color: #11161a !important;
        transform: translateY(-1px) scale(1.04);
    }
}

html body.ws-2026 #puzzle .puzzleSquare.selected,
html body.ws-2026 #puzzle .puzzleSquare.found,
html body.ws-2026 #puzzle .puzzleSquare.solved,
html body.ws-2026 #puzzle .puzzleSquare.complete,
html body.ws-2026 #puzzle .puzzleSquare.wordfind-letter--found,
html body.ws-2026 #puzzle .puzzleSquare.wordfind-letter--solved {
    background: transparent !important;
    border-color: transparent !important;
    box-shadow: none !important;
    color: #15191d !important;
}

html body.ws-2026 #puzzle .puzzleSquare.selected {
    transform: scale(1.08);
}

html body.ws-2026 #puzzle .puzzleSquare:focus-visible {
    outline: 3px solid #714424 !important;
    outline-offset: -2px;
    background: rgba(255, 255, 255, 0.5) !important;
}

/*
 * The ws-* selectors are the standalone contract. The wordfind-* aliases keep
 * the SVG layer compatible with the underlying game module during rollout.
 */
html body.ws-2026 #puzzle .ws-highlight-layer,
html body.ws-2026 #puzzle .wordfind-highlights {
    position: absolute;
    inset: 0;
    z-index: 1;
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none !important;
    user-select: none;
    touch-action: none;
}

html body.ws-2026 #puzzle .ws-highlight-layer *,
html body.ws-2026 #puzzle .wordfind-highlights * {
    pointer-events: none !important;
}

html body.ws-2026 #puzzle .ws-highlight,
html body.ws-2026 #puzzle .wordfind-highlight {
    --ws-capsule-color: var(--word-color, var(--wordfind-color, var(--wordfind-highlight, #31c9c3)));
    fill: none;
    stroke: var(--ws-capsule-color);
    stroke-width: var(--ws-highlight-width, calc(var(--ws-cell-size, 41px) - 10px));
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
    shape-rendering: geometricPrecision;
    opacity: 0.52;
    filter: drop-shadow(0 1px 1px rgba(38, 29, 24, 0.16));
    transition: opacity 180ms ease, filter 180ms ease;
    pointer-events: none !important;
}

html body.ws-2026 #puzzle .ws-highlight-preview,
html body.ws-2026 #puzzle .wordfind-highlight--preview {
    stroke: var(--word-color, var(--wordfind-color, #efb34c));
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: 0.4;
    filter: drop-shadow(0 2px 2px rgba(107, 66, 38, 0.2));
    pointer-events: none !important;
}

html body.ws-2026 #puzzle .wordfind-highlight--solved {
    opacity: 0.36;
}

/* Colour-keyed word chips mirror their matching highlight capsule. */
html body.ws-2026 #words li {
    position: relative;
    min-width: 0;
}

html body.ws-2026 #words input.word {
    --ws-chip-color: var(--word-color, var(--wordfind-color, #d4a84b));
    --ws-chip-soft: var(--word-color-soft, var(--wordfind-highlight, rgba(212, 168, 75, 0.2)));
    min-height: 38px;
    padding: 8px 34px 8px 15px !important;
    appearance: none;
    background: var(--ws-cream, #f7f2e9) !important;
    border: 1px solid rgba(76, 43, 8, 0.1) !important;
    border-radius: 999px !important;
    box-shadow:
        inset 5px 0 0 var(--ws-chip-color),
        0 3px 9px rgba(76, 43, 8, 0.07) !important;
    color: var(--ws-ink, #4c2b08) !important;
    caret-color: transparent;
    font-size: 14px !important;
    font-weight: 600 !important;
    letter-spacing: 0.02em !important;
    text-overflow: ellipsis;
    transition:
        background-color 180ms ease,
        border-color 180ms ease,
        box-shadow 180ms ease,
        color 180ms ease;
}

html body.ws-2026 #words input.word.wordFound,
html body.ws-2026 #words input.word.wordfind-word--found,
html body.ws-2026 #words input.word.wordfind-word--solved {
    background: var(--ws-chip-soft) !important;
    border-color: var(--ws-chip-color) !important;
    box-shadow:
        inset 5px 0 0 var(--ws-chip-color),
        0 5px 14px rgba(76, 43, 8, 0.1) !important;
    color: #4c2b08 !important;
    opacity: 1;
    text-decoration-color: var(--ws-chip-color);
    text-decoration-thickness: 2px;
}

html body.ws-2026 #words li:has(> input.word.wordFound)::after,
html body.ws-2026 #words li:has(> input.word.wordfind-word--found)::after,
html body.ws-2026 #words li:has(> input.word.wordfind-word--solved)::after {
    content: "\2713";
    position: absolute;
    top: 50%;
    right: 9px;
    display: grid;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    place-items: center;
    background: var(--ws-brown, #674329);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
    transform: translateY(-50%);
    pointer-events: none;
}

html body.ws-2026.cuppa-dark #words input.word {
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(255, 255, 255, 0.14) !important;
    box-shadow:
        inset 5px 0 0 var(--ws-chip-color),
        0 3px 9px rgba(0, 0, 0, 0.12) !important;
    color: #fff !important;
}

html body.ws-2026.cuppa-dark #words input.word.wordFound,
html body.ws-2026.cuppa-dark #words input.word.wordfind-word--found,
html body.ws-2026.cuppa-dark #words input.word.wordfind-word--solved {
    background: var(--ws-chip-soft) !important;
    border-color: var(--ws-chip-color) !important;
    color: rgba(255, 255, 255, 0.86) !important;
}

html body.ws-2026.cuppa-dark #words li:has(> input.word.wordFound)::after,
html body.ws-2026.cuppa-dark #words li:has(> input.word.wordfind-word--found)::after,
html body.ws-2026.cuppa-dark #words li:has(> input.word.wordfind-word--solved)::after {
    background: rgba(255, 255, 255, 0.16);
    color: #fff;
}

html body.ws-2026 #words input.word:focus-visible {
    outline: 3px solid var(--ws-focus) !important;
    outline-offset: 3px;
}

html body.ws-2026 .ws-back:focus-visible,
html body.ws-2026 .ws-howto:focus-visible,
html body.ws-2026 #controls button:focus-visible,
html body.ws-2026 #play-again:focus-visible {
    outline: 3px solid var(--ws-focus) !important;
    outline-offset: 3px;
}

@media only screen and (max-width: 900px) {
    html body.ws-2026 #puzzle-container {
        width: 100% !important;
        max-width: 100%;
        min-width: 0;
    }

    html body.ws-2026 #puzzle {
        margin-inline: auto;
    }

    html body.ws-2026 #words {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media only screen and (max-width: 480px) {
    html body.ws-2026 #puzzle {
        border-radius: 18px !important;
        padding: clamp(7px, 2.4vw, 10px) !important;
        box-shadow:
            0 12px 28px rgba(76, 43, 8, 0.11),
            0 2px 7px rgba(76, 43, 8, 0.07),
            inset 0 0 0 4px rgba(255, 255, 255, 0.46) !important;
    }

    html body.ws-2026 #words input.word {
        min-height: 40px;
        font-size: 14px !important;
    }
}

@media only screen and (max-width: 420px) {
    html body.ws-2026 #words {
        grid-template-columns: minmax(0, 1fr);
        gap: 8px !important;
    }
}

@media only screen and (max-width: 350px) {
    html body.ws-2026 #main {
        padding-inline: 8px;
    }
}

@media (prefers-reduced-motion: reduce) {
    html body.ws-2026 #puzzle .puzzleSquare,
    html body.ws-2026 #puzzle .ws-highlight,
    html body.ws-2026 #puzzle .ws-highlight-preview,
    html body.ws-2026 #puzzle .wordfind-highlight,
    html body.ws-2026 #words input.word {
        animation: none !important;
        transition: none !important;
    }

    html body.ws-2026 #puzzle .puzzleSquare:hover,
    html body.ws-2026 #puzzle .puzzleSquare.selected {
        transform: none !important;
    }
}

@media (forced-colors: active) {
    html body.ws-2026 #puzzle {
        border: 2px solid CanvasText !important;
    }

    html body.ws-2026 #puzzle .ws-highlight,
    html body.ws-2026 #puzzle .ws-highlight-preview,
    html body.ws-2026 #puzzle .wordfind-highlight {
        stroke: Highlight;
        opacity: 0.5;
        filter: none;
    }

    html body.ws-2026 #words input.word {
        border-color: CanvasText !important;
    }
}
