/* ============================================================
   Sopa de Letras — estilos
   ============================================================ */

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

:root {
    color-scheme: light;

    --bg: #f3f5fb;
    --bg-glow-1: rgba(99, 102, 241, .17);
    --bg-glow-2: rgba(14, 165, 233, .14);

    --surface: rgba(255, 255, 255, .9);
    --surface-solid: #ffffff;
    --surface-2: #eef2f9;

    --text: #1b2333;
    --muted: #5b6884;
    --border: #dbe2ef;

    --primary: #4f46e5;
    --primary-2: #7c3aed;
    --primary-contrast: #ffffff;

    --accent: #0891b2;

    --cell-bg: #ffffff;
    --cell-border: #e2e8f3;
    --cell-text: #263046;
    --cell-hover: #eef2ff;
    --found-text: #1b2333;

    --success: #15803d;
    --danger: #dc2626;

    --shadow: 0 18px 40px -18px rgba(27, 35, 51, .35);
    --shadow-sm: 0 4px 14px rgba(27, 35, 51, .08);

    --toast-bg: #1e293b;
    --toast-text: #f8fafc;

    --f0: #fde68a;
    --f1: #bbf7d0;
    --f2: #bfdbfe;
    --f3: #fbcfe8;
    --f4: #ddd6fe;
    --f5: #fed7aa;
    --f6: #99f6e4;
    --f7: #fecaca;
}

html[data-theme="dark"] {
    color-scheme: dark;

    --bg: #0d1220;
    --bg-glow-1: rgba(99, 102, 241, .24);
    --bg-glow-2: rgba(6, 182, 212, .17);

    --surface: rgba(23, 30, 48, .9);
    --surface-solid: #171e30;
    --surface-2: #202943;

    --text: #edf1fa;
    --muted: #9aa7c2;
    --border: #2b3552;

    --primary: #818cf8;
    --primary-2: #a78bfa;
    --primary-contrast: #141a2b;

    --accent: #22d3ee;

    --cell-bg: #1a2238;
    --cell-border: #2c3857;
    --cell-text: #e6ecf9;
    --cell-hover: #253052;
    --found-text: #ffffff;

    --success: #4ade80;
    --danger: #f87171;

    --shadow: 0 24px 50px -18px rgba(0, 0, 0, .7);
    --shadow-sm: 0 4px 16px rgba(0, 0, 0, .3);

    --toast-bg: #eef2ff;
    --toast-text: #111827;

    --f0: #8a6d1a;
    --f1: #166534;
    --f2: #1d4ed8;
    --f3: #9d174d;
    --f4: #6d28d9;
    --f5: #9a3412;
    --f6: #0f766e;
    --f7: #b91c1c;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg);
    background-image:
        radial-gradient(900px 520px at 8% -12%, var(--bg-glow-1), transparent 62%),
        radial-gradient(820px 520px at 110% 8%, var(--bg-glow-2), transparent 58%);
    background-attachment: fixed;
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    transition: background-color .3s ease, color .3s ease;
}

body.modal-open {
    overflow: hidden;
}

.app {
    width: 100%;
    max-width: 1120px;
    margin: 0 auto;
    padding: clamp(16px, 4vw, 40px) clamp(14px, 3.5vw, 32px) 20px;
}

/* ---------- Encabezado ---------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding-bottom: clamp(18px, 3vw, 30px);
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.brand-mark {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    width: 50px;
    height: 50px;
    border-radius: 15px;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: #ffffff;
    font-size: 1.6rem;
    font-weight: 800;
    box-shadow: var(--shadow-sm);
}

.brand h1 {
    margin: 0;
    font-size: clamp(1.4rem, 3.4vw, 2rem);
    letter-spacing: -.025em;
    line-height: 1.15;
}

.tagline {
    margin: 3px 0 0;
    color: var(--muted);
    font-size: .85rem;
}

.icon-btn {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    width: 46px;
    height: 46px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text);
    font-size: 1.2rem;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: transform .2s ease, background .2s ease;
}

.icon-btn:hover {
    transform: translateY(-2px) rotate(-8deg);
    background: var(--surface-2);
}

.icon-btn:active {
    transform: scale(.94);
}

/* ---------- Botones ---------- */

.btn {
    appearance: none;
    border: 1px solid transparent;
    border-radius: 999px;
    padding: 10px 18px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: transform .15s ease, background .2s ease, border-color .2s ease, color .2s ease, box-shadow .2s ease, filter .2s ease;
}

.btn:active {
    transform: scale(.97);
}

.btn:disabled {
    opacity: .45;
    cursor: not-allowed;
    transform: none;
    filter: none;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: var(--primary-contrast);
    box-shadow: 0 10px 24px -10px rgba(79, 70, 229, .65);
}

.btn-primary:hover:not(:disabled) {
    filter: brightness(1.07);
}

.btn-ghost {
    background: var(--surface-solid);
    border-color: var(--border);
    color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--surface-2);
    border-color: var(--primary);
}

.btn-lg {
    width: 100%;
    padding: 14px 26px;
    font-size: 1.05rem;
}

.btn:focus-visible,
.icon-btn:focus-visible,
.word-item:focus-visible,
textarea:focus-visible,
.board:focus-visible {
    outline: 3px solid rgba(99, 102, 241, .5);
    outline-offset: 2px;
}

.segmented input:focus-visible + span,
.category-option input:focus-visible + span {
    outline: 3px solid rgba(99, 102, 241, .5);
    outline-offset: 2px;
}

/* ---------- Panel de configuración ---------- */

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.setup {
    max-width: 680px;
    margin: 0 auto;
    padding: clamp(22px, 4vw, 36px);
}

.setup h2 {
    margin: 0 0 4px;
    font-size: 1.5rem;
    letter-spacing: -.02em;
}

.muted {
    margin: 0 0 24px;
    color: var(--muted);
    font-size: .93rem;
}

fieldset {
    margin: 0 0 22px;
    padding: 0;
    border: 0;
}

.form-group {
    margin-bottom: 22px;
}

.form-group>label,
legend {
    display: block;
    margin-bottom: 8px;
    padding: 0;
    font-size: .93rem;
    font-weight: 600;
}

.segmented {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
}

.segmented label {
    position: relative;
    cursor: pointer;
}

.segmented input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.segmented span {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 62px;
    padding: 10px 6px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface-solid);
    font-weight: 600;
    text-align: center;
    transition: background .2s ease, color .2s ease, border-color .2s ease, box-shadow .2s ease;
}

.segmented small {
    color: var(--muted);
    font-size: .7rem;
    font-weight: 500;
}

.segmented label:hover span {
    border-color: var(--primary);
}

.segmented input:checked+span {
    border-color: transparent;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: var(--primary-contrast);
    box-shadow: 0 10px 24px -12px rgba(79, 70, 229, .65);
}

.segmented input:checked+span small {
    color: inherit;
    opacity: .82;
}

textarea {
    width: 100%;
    min-height: 104px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background-color: var(--surface-solid);
    color: var(--text);
    font: inherit;
    resize: vertical;
    text-transform: uppercase;
    letter-spacing: .04em;
    transition: border-color .2s ease, box-shadow .2s ease;
}

textarea:hover {
    border-color: var(--primary);
}

/* ---------- Categorías ---------- */

.category-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
    gap: 8px;
}

.category-option {
    position: relative;
    margin: 0;
    cursor: pointer;
}

.category-option input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.category-option>span {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 46px;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface-solid);
    font-size: .9rem;
    font-weight: 600;
    transition: background .2s ease, color .2s ease, border-color .2s ease, box-shadow .2s ease;
}

.category-option .cat-icon {
    flex: 0 0 auto;
    font-size: 1.15rem;
    line-height: 1;
}

.category-option:hover>span {
    border-color: var(--primary);
}

.category-option input:checked+span {
    border-color: transparent;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: var(--primary-contrast);
    box-shadow: 0 8px 20px -12px rgba(79, 70, 229, .65);
}

@media (min-width: 561px) {
    .category-option:last-child {
        grid-column: 1 / -1;
    }
}

.hint {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: .8rem;
}

.form-error {
    margin: 0 0 18px;
    padding: 10px 14px;
    border-radius: 12px;
    background: rgba(220, 38, 38, .1);
    color: var(--danger);
    font-size: .9rem;
    font-weight: 500;
}

/* ---------- Juego ---------- */

.game-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 14px 18px;
    margin-bottom: 22px;
    padding: 14px 18px;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.toolbar-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
}

.game-info {
    margin: 0;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .09em;
    text-transform: uppercase;
}

.stats {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.stat {
    min-width: 88px;
    padding: 6px 14px;
    border-radius: 12px;
    background: var(--surface-2);
}

.stat-label {
    display: block;
    color: var(--muted);
    font-size: .66rem;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
}

.stat strong {
    font-size: 1.08rem;
    font-variant-numeric: tabular-nums;
    line-height: 1.25;
}

.toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.toolbar-actions .btn {
    padding: 8px 14px;
    font-size: .87rem;
}

/* ---------- Tablero ---------- */

.play-area {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(260px, .75fr);
    gap: 22px;
    align-items: start;
}

.board-wrap {
    padding: clamp(12px, 2.4vw, 20px);
    border: 1px solid var(--border);
    border-radius: 22px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--size, 10), minmax(0, 1fr));
    grid-template-rows: repeat(var(--size, 10), minmax(0, 1fr));
    gap: clamp(1px, .3vw, 3px);
    width: 100%;
    max-width: 640px;
    aspect-ratio: 1 / 1;
    margin: 0 auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.cell {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 0;
    border: 1px solid var(--cell-border);
    border-radius: clamp(3px, .5vw, 7px);
    background: var(--cell-bg);
    color: var(--cell-text);
    font-size: var(--cell-font, 18px);
    font-weight: 600;
    cursor: pointer;
    transition: background .12s ease, color .12s ease, box-shadow .12s ease;
}

@media (hover: hover) {
    .cell:hover {
        background: var(--cell-hover);
    }
}

.cell.found {
    border-color: transparent;
    color: var(--found-text);
    font-weight: 700;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .06);
    animation: cell-pop .28s ease;
}

.cell.f0 { background: var(--f0); }
.cell.f1 { background: var(--f1); }
.cell.f2 { background: var(--f2); }
.cell.f3 { background: var(--f3); }
.cell.f4 { background: var(--f4); }
.cell.f5 { background: var(--f5); }
.cell.f6 { background: var(--f6); }
.cell.f7 { background: var(--f7); }

.cell.selecting {
    border-color: var(--primary);
    background: var(--primary);
    color: var(--primary-contrast);
}

.cell.wrong {
    border-color: var(--danger);
    background: rgba(220, 38, 38, .22);
    animation: cell-shake .35s ease;
}

.cell.hint {
    animation: hint-pulse 1.1s ease-in-out 2;
    z-index: 1;
}

.cell.revealed {
    border-color: transparent;
    background: var(--surface-2) !important;
    color: var(--muted) !important;
    box-shadow: inset 0 0 0 2px var(--primary);
}

@keyframes cell-pop {
    0% { transform: scale(.82); }
    60% { transform: scale(1.07); }
    100% { transform: scale(1); }
}

@keyframes cell-shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-2px); }
    75% { transform: translateX(2px); }
}

@keyframes hint-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(8, 145, 178, 0); }
    50% { box-shadow: 0 0 0 3px var(--accent); background: var(--cell-hover); }
}

.board-help {
    max-width: 640px;
    margin: 12px auto 0;
    color: var(--muted);
    font-size: .78rem;
    text-align: center;
}

/* ---------- Panel de palabras ---------- */

.words-panel {
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: 22px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.words-panel h2 {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 0 0 14px;
    font-size: 1.08rem;
}

.badge {
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: .78rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.word-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.word-item {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px 7px 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface-solid);
    font-size: .85rem;
    font-weight: 600;
    letter-spacing: .04em;
    cursor: pointer;
    user-select: none;
    transition: transform .15s ease, border-color .2s ease, background .2s ease, opacity .2s ease;
}

.word-item:hover {
    border-color: var(--primary);
    transform: translateY(-1px);
}

.word-item .dot {
    flex: 0 0 auto;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--muted);
}

.word-item.f0 .dot { background: var(--f0); }
.word-item.f1 .dot { background: var(--f1); }
.word-item.f2 .dot { background: var(--f2); }
.word-item.f3 .dot { background: var(--f3); }
.word-item.f4 .dot { background: var(--f4); }
.word-item.f5 .dot { background: var(--f5); }
.word-item.f6 .dot { background: var(--f6); }
.word-item.f7 .dot { background: var(--f7); }

.word-item .word-check {
    display: none;
    color: var(--success);
    font-weight: 800;
    line-height: 1;
}

.word-item.found {
    opacity: .62;
    text-decoration: line-through;
    cursor: default;
    transform: none;
}

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

.word-item.found .word-check {
    display: inline;
}

.game-msg {
    min-height: 1.4em;
    margin: 14px 0 0;
    color: var(--muted);
    font-size: .83rem;
}

/* ---------- Aviso flotante ---------- */

.toast {
    position: fixed;
    bottom: 26px;
    left: 50%;
    z-index: 100;
    max-width: min(90vw, 520px);
    padding: 12px 20px;
    border-radius: 999px;
    background: var(--toast-bg);
    color: var(--toast-text);
    box-shadow: var(--shadow);
    font-size: .88rem;
    font-weight: 600;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 16px);
    transition: opacity .25s ease, transform .25s ease;
}

.toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

/* ---------- Modal ---------- */

.modal {
    position: fixed;
    inset: 0;
    z-index: 90;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(10, 15, 30, .55);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    animation: fade-in .2s ease;
}

.modal-card {
    position: relative;
    width: min(440px, 100%);
    padding: 32px 26px 26px;
    border: 1px solid var(--border);
    border-radius: 26px;
    background: var(--surface-solid);
    color: var(--text);
    box-shadow: var(--shadow);
    text-align: center;
    animation: modal-pop .32s cubic-bezier(.2, .9, .3, 1.25);
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--text);
    font-size: 1.35rem;
    line-height: 1;
    cursor: pointer;
    transition: background .2s ease;
}

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

.victory-emoji {
    font-size: 3.4rem;
    line-height: 1;
    animation: bob 1.6s ease-in-out infinite;
}

.modal-card h2 {
    margin: 10px 0 4px;
    font-size: 1.6rem;
    letter-spacing: -.02em;
}

.victory-sub {
    margin: 0 0 22px;
    color: var(--muted);
}

.victory-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 16px;
}

.victory-stats>div {
    padding: 10px 6px;
    border-radius: 14px;
    background: var(--surface-2);
}

.victory-stats span {
    display: block;
    color: var(--muted);
    font-size: .66rem;
    font-weight: 700;
    letter-spacing: .07em;
    text-transform: uppercase;
}

.victory-stats strong {
    font-size: 1.12rem;
    font-variant-numeric: tabular-nums;
}

.record-badge {
    display: inline-block;
    margin: 0 0 16px;
    padding: 5px 14px;
    border-radius: 999px;
    background: rgba(21, 128, 61, .13);
    color: var(--success);
    font-size: .83rem;
    font-weight: 700;
}

.modal-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-top: 8px;
}

.modal-actions .btn {
    flex: 1 1 160px;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes modal-pop {
    from { opacity: 0; transform: translateY(18px) scale(.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes bob {
    0%, 100% { transform: translateY(0) rotate(-4deg); }
    50% { transform: translateY(-8px) rotate(4deg); }
}

/* ---------- Pie ---------- */

.footer {
    padding: 30px 0 4px;
    color: var(--muted);
    font-size: .78rem;
    text-align: center;
}

.footer p {
    margin: 0;
}

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
    .play-area {
        grid-template-columns: minmax(0, 1fr);
    }

    .words-panel {
        order: 2;
    }
}

@media (max-width: 560px) {
    .topbar {
        align-items: flex-start;
    }

    .brand-mark {
        width: 44px;
        height: 44px;
        border-radius: 13px;
        font-size: 1.35rem;
    }

    .segmented {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
    }

    .segmented span {
        min-height: 58px;
        font-size: .85rem;
    }

    .segmented small {
        font-size: .62rem;
    }

    .category-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .category-option>span {
        min-height: 42px;
        padding: 8px 10px;
        font-size: .84rem;
    }

    .category-option .cat-icon {
        font-size: 1.05rem;
    }

    .toolbar-actions {
        width: 100%;
    }

    .toolbar-info,
    .stats {
        width: 100%;
    }

    .toolbar-actions .btn {
        flex: 1 1 auto;
    }

    .stat {
        min-width: 0;
        flex: 1 1 0;
        padding: 6px 10px;
    }

    .stat-label {
        font-size: .62rem;
        letter-spacing: .04em;
    }

    .stat strong {
        font-size: 1rem;
    }

    .victory-stats {
        gap: 6px;
    }
}

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