.length-switch {
    margin-bottom: var(--space-5);
}

/* --- Search card --- */

.search-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    box-shadow: var(--shadow);
    margin-bottom: var(--space-5);
}

.search-card:has(.search-tabs) {
    --tab-h: 2.2rem;
    --tab-h-active: 2.75rem;
    margin-top: calc(var(--tab-h-active) + 0.85rem);
}

.search-tabs {
    display: flex;
    align-items: flex-end;
    gap: 0.35rem;
    height: var(--tab-h-active);
    margin: calc((var(--space-4) + var(--tab-h-active)) * -1) calc(var(--space-4) * -1) var(--space-4);
    padding: 0 1.15rem;
}

.search-tab {
    appearance: none;
    position: relative;
    display: inline-flex;
    align-items: center;
    height: var(--tab-h);
    margin: 0;
    padding: 0 1rem;
    border: 1px solid var(--border);
    border-radius: 12px 12px 0 0;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.92rem;
    font-weight: 700;
    line-height: 1;
}

.search-tab:hover {
    background: var(--surface-3);
    color: var(--text);
}

.search-tab.is-active {
    height: var(--tab-h-active);
    border-bottom-color: var(--surface);
    background: var(--surface);
    color: var(--text);
    z-index: 1;
}

.search-tab.is-active::after {
    content: "";
    position: absolute;
    right: 1px;
    bottom: -1px;
    left: 1px;
    height: 2px;
    background: var(--surface);
}

.search-tab.is-active:hover {
    background: var(--surface);
}

.search-tab:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.search-step + .search-step {
    margin-top: var(--space-3);
}

.search-label {
    margin: 0 0 0.3rem;
    font-size: 0.82rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
    text-align: center;
}

.search-hint {
    margin: 0;
    text-align: center;
    font-size: 0.85rem;
    color: var(--muted);
}

.mask-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    max-width: 100%;
}

.mask-row .mask-cells {
    flex: 0 1 auto;
    width: calc(var(--cells, 5) * 3.25rem);
    max-width: calc(100% - 4.4rem);
    min-width: 0;
}

.length-step {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border-radius: 999px;
    color: var(--brand);
    font-size: 1.45rem;
    font-weight: 700;
    line-height: 1;
    text-decoration: none;
    user-select: none;
}

.length-step:hover {
    background: color-mix(in srgb, var(--brand) 12%, transparent);
    color: var(--brand);
    text-decoration: none;
}

.length-step:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.length-step--spacer {
    visibility: hidden;
}

.mask-cells {
    display: flex;
    justify-content: center;
    gap: clamp(0.15rem, 1vw, 0.4rem);
    flex-wrap: nowrap;
    padding: 2px 0;
    container-type: inline-size;
}

.mask-cell {
    flex: 1 1 0;
    width: auto;
    min-width: 0;
    max-width: 3.25rem;
    aspect-ratio: 1;
    height: auto;
    padding: 0;
    border: 2px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--tile-empty);
    color: var(--text);
    text-align: center;
    font-size: clamp(1rem, calc(70cqw / var(--cells, 5)), 2.15rem);
    line-height: 1;
    font-weight: 800;
    text-transform: uppercase;
    caret-color: transparent;
    transition: background 0.12s, border-color 0.12s, transform 0.12s;
}

.mask-cell::placeholder {
    color: var(--border-strong);
}

.mask-cell::selection {
    background: transparent;
}

.mask-cell:focus {
    outline: none;
}

.mask-cell:focus-visible:not(.is-armed) {
    box-shadow: var(--focus-ring);
}

.mask-cell.is-filled {
    border-color: var(--tile-correct);
    background: var(--tile-correct);
    color: var(--tile-text);
}

.misplaced-cell {
    font-size: clamp(0.8rem, calc(52cqw / var(--cells, 5)), 1.55rem);
    letter-spacing: 0.02em;
    caret-color: auto;
}

.misplaced-cell::selection {
    background: color-mix(in srgb, var(--tile-text) 30%, transparent);
}

.mask-cell.misplaced-cell.is-filled {
    border-color: var(--tile-present);
    background: var(--tile-present);
}

.mask-cell.is-armed {
    border-color: var(--brand);
    border-width: 3px;
    background: color-mix(in srgb, var(--brand) 14%, var(--tile-empty));
    transform: translateY(-4px) scale(1.06);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand) 30%, transparent),
                0 8px 18px -6px color-mix(in srgb, var(--brand) 60%, transparent);
    animation: mask-cell-pulse 1.4s ease-in-out infinite;
}

.mask-cell.is-filled.is-armed {
    background: var(--tile-correct);
    border-color: var(--surface);
    box-shadow: 0 0 0 3px var(--brand),
                0 8px 18px -6px color-mix(in srgb, var(--brand) 60%, transparent);
    animation: none;
}

@keyframes mask-cell-pulse {
    50% {
        box-shadow: 0 0 0 7px color-mix(in srgb, var(--brand) 12%, transparent),
                    0 8px 18px -6px color-mix(in srgb, var(--brand) 60%, transparent);
    }
}

.search-hint.is-active {
    color: var(--brand);
    font-weight: 600;
}

.search-card.has-armed-cell .keyboard {
    border-radius: var(--radius-sm);
    outline: 2px dashed color-mix(in srgb, var(--brand) 55%, transparent);
    outline-offset: 6px;
}

@media (prefers-reduced-motion: reduce) {
    .mask-cell.is-armed {
        animation: none;
        transform: none;
    }
}

/* --- Keyboard --- */

.keyboard {
    display: grid;
    gap: 6px;
    max-width: 34rem;
    margin: 0 auto;
    user-select: none;
    -webkit-user-select: none;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 5px;
}

.key {
    flex: 1 1 0;
    max-width: 2.75rem;
    min-width: 0;
    height: 3.1rem;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: var(--surface-3);
    color: var(--text);
    font-size: clamp(0.85rem, 3.6vw, 1.05rem);
    font-weight: 700;
    text-transform: uppercase;
    touch-action: manipulation;
    transition: background 0.1s, color 0.1s, transform 0.08s;
}

.key:hover {
    filter: brightness(0.96);
}

.key:active {
    transform: scale(0.95);
}

.key[data-state="absent"] {
    background: var(--tile-absent);
    color: var(--tile-text);
}

.key[data-state="present"] {
    background: var(--tile-present);
    color: var(--tile-text);
}

.key[data-state="correct"] {
    background: var(--tile-correct);
    color: var(--tile-text);
}

.keyboard-legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-2) var(--space-4);
    margin: var(--space-3) 0 0;
    font-size: 0.8rem;
    color: var(--muted);
}

.keyboard-legend span {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
}

.legend-swatch {
    display: inline-block;
    width: 0.8rem;
    height: 0.8rem;
    border-radius: 3px;
}

.legend-swatch--absent { background: var(--tile-absent); }
.legend-swatch--present { background: var(--tile-present); }
.legend-swatch--correct { background: var(--tile-correct); }

/* --- Text filters --- */

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
    margin-top: var(--space-3);
}

.field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.field span {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-2);
}

.field input {
    width: 100%;
    min-height: 2.75rem;
    padding: 0 var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: 1.05rem;
    letter-spacing: 0.08em;
}

.field input:focus {
    outline: none;
    border-color: var(--brand);
    box-shadow: var(--focus-ring);
}

.field input::placeholder {
    letter-spacing: normal;
    font-size: 0.95rem;
    color: var(--muted);
}

.search-more-toggle {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-top: var(--space-2);
    padding: 0.1rem 0;
    border: 0;
    background: none;
    color: var(--brand);
    font-size: 0.9rem;
    font-weight: 700;
    line-height: 1.3;
    text-align: left;
}

.search-more-toggle::before {
    content: "";
    flex: 0 0 auto;
    width: 0.4rem;
    height: 0.4rem;
    margin-top: -0.15rem;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(-45deg);
}

.search-more-toggle[aria-expanded="true"]::before {
    margin-top: 0.15rem;
    transform: rotate(45deg);
}

.search-more-toggle:hover {
    color: var(--brand-hover);
}

.search-more-count {
    color: var(--muted);
    font-weight: 700;
}

.search-more-toggle[hidden],
.search-more-count[hidden],
.search-misplaced-step[hidden] {
    display: none;
}

.search-misplaced-step {
    margin-top: var(--space-2);
}

/* With JS the keyboard replaces the text filters; they stay in the form as the submitted fields. */
.has-keyboard .field-row {
    display: none;
}

@media (max-width: 560px) {
    .search-card {
        padding: var(--space-4) var(--space-3);
    }

    .search-tabs {
        margin-right: calc(var(--space-3) * -1);
        margin-left: calc(var(--space-3) * -1);
    }

    .search-tab {
        padding: 0 0.7rem;
        font-size: 0.85rem;
    }

    .field-row {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .field {
        gap: 2px;
    }

    .field input {
        min-height: 2.5rem;
    }

    .length-step {
        width: 1.65rem;
        height: 1.65rem;
        font-size: 1.25rem;
    }
}

.search-actions {
    display: flex;
    gap: var(--space-2);
    margin-top: var(--space-3);
}

.all-words {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    margin: 0 0 var(--space-3);
    font-size: 0.95rem;
    font-weight: 700;
    line-height: 1.35;
    color: var(--text-2);
    cursor: pointer;
}

.all-words input {
    width: 1.15rem;
    height: 1.15rem;
    margin: 0.15rem 0 0;
    flex-shrink: 0;
    accent-color: var(--brand);
}

.search-submit {
    flex: 1;
    min-height: 3.1rem;
    font-size: 1.05rem;
}

.search-feedback {
    margin-top: var(--space-3);
    text-align: center;
}

.search-feedback[hidden],
.search-feedback [hidden] {
    display: none;
}

.search-feedback-question {
    margin: 0 0 var(--space-2);
    color: var(--muted);
    font-size: 0.88rem;
}

.search-feedback-buttons {
    display: flex;
    justify-content: center;
    gap: var(--space-2);
}

.search-feedback-btn {
    min-height: 2.1rem;
    padding: 0 var(--space-4);
    font-size: 0.85rem;
    font-weight: 600;
}

.search-feedback-thanks {
    margin: 0;
    color: var(--ok);
    font-size: 0.9rem;
    font-weight: 600;
}

.feedback-dialog {
    width: min(28rem, calc(100vw - 2rem));
    padding: var(--space-5);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-lg);
}

.feedback-dialog::backdrop {
    background: rgb(0 0 0 / 0.45);
}

.feedback-dialog-title {
    margin: 0 0 var(--space-2);
    font-size: 1.2rem;
}

.feedback-dialog-hint {
    margin: 0 0 var(--space-3);
    color: var(--muted);
    font-size: 0.9rem;
}

.feedback-dialog-text {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    resize: vertical;
}

.feedback-dialog-actions {
    display: flex;
    gap: var(--space-2);
    margin-top: var(--space-4);
}

/* --- Results --- */

.results {
    margin-bottom: var(--space-5);
}

.results-title {
    margin-bottom: var(--space-1);
}

.results-tip {
    margin-bottom: var(--space-3);
    font-size: 0.88rem;
}

.word-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, max(7.5rem, calc(var(--word-length, 6) * 1.35ch + 2.25rem))), 1fr));
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.word-grid > li {
    min-width: 0;
}

.word {
    display: block;
    width: 100%;
    min-height: 2.75rem;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 1.02rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    line-height: 1.25;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
    transition: border-color 0.12s, transform 0.12s, box-shadow 0.12s;
}

a.word {
    text-decoration: none;
}

.word:hover {
    border-color: var(--brand);
    box-shadow: var(--shadow-sm);
}

.word.is-selected {
    border-color: var(--brand);
    box-shadow: var(--focus-ring);
}

.hl-correct {
    color: var(--tile-correct);
    font-weight: 800;
}

.hl-present {
    color: var(--tile-present);
    font-weight: 800;
}

.results-more {
    display: flex;
    justify-content: center;
    margin-top: var(--space-4);
}

.results.is-loading {
    opacity: 0.55;
    transition: opacity 0.15s;
}

/* --- Word detail: fixed to the viewport so a long result list does not hide it --- */

.word-detail {
    position: fixed;
    z-index: 40;
    bottom: var(--space-3);
    left: max(1rem, calc((100% - min(var(--container), 100% - 2rem)) / 2));
    width: min(42rem, calc(100% - 2rem));
    max-height: min(45vh, 22rem);
    margin: 0;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    border-color: var(--brand);
}

@media (min-width: 1024px) {
    .word-detail {
        width: calc(min(var(--container), 100% - 2rem) - var(--sidebar) - var(--space-5));
    }
}

.word-detail-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    margin-bottom: var(--space-3);
}

.word-detail-title {
    margin: 0;
    min-width: 0;
    font-size: 1.5rem;
    letter-spacing: 0.04em;
    overflow-wrap: anywhere;
}

.word-detail-label {
    margin: var(--space-3) 0 var(--space-1);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
}

.word-detail-defs {
    margin: 0;
    padding-left: 1.25rem;
    color: var(--text-2);
}

.word-detail-defs li + li {
    margin-top: var(--space-2);
}

.related-queries .chip {
    min-width: 2.75rem;
    justify-content: center;
}
