/**
 * Crossword Player - Interactive Grid Styles
 * Accessible crossword puzzle with variable-size grids
 */

/* ============================================
   GRID CONTAINER
   ============================================ */

#cw-grid-container {
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    margin: 5px auto;
    padding-top: 40px;
    padding-bottom: 16px;
    text-align: center;
    touch-action: manipulation;
    position: relative;
}

/* ============================================
   CROSSWORD GRID
   ============================================ */

.cw-grid {
    display: inline-flex;
    flex-direction: column;
    border: 2px solid #333;
    background: #333;
    gap: 1px;
    position: relative; /* anchor for .cw-mobile-input */
}

/* Hidden input that triggers the device soft keyboard on tap.
   Sized and positioned over the selected cell by JS so iOS/Android
   do not scroll or zoom on focus. Invisible but focusable. */
.cw-mobile-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: 0;
    border: 0;
    opacity: 0;
    pointer-events: none;
    color: transparent;
    background: transparent;
    caret-color: transparent;
    font-size: 16px; /* 16px+ prevents iOS Safari zoom-on-focus */
    z-index: 1;
}

.cw-row {
    display: flex;
    gap: 1px;
    background: #333;
}

.cw-cell {
    width: 40px;
    height: 40px;
    min-width: 24px;
    min-height: 24px;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4em;
    font-weight: bold;
    font-family: Arial, sans-serif;
    text-transform: uppercase;
    user-select: none;
    cursor: pointer;
    transition: background 0.15s;
}

.cw-black {
    background: #333;
    cursor: default;
}

.cw-white {
    background: #fff;
    border: none;
}

/* Cell number (clue reference) */
.cw-cell-num {
    position: absolute;
    top: 1px;
    left: 2px;
    font-size: 0.45em;
    font-weight: bold;
    color: #555;
    line-height: 1;
    pointer-events: none;
}

/* Cell letter */
.cw-cell-letter {
    font-size: 1em;
    color: #000;
    pointer-events: none;
}

/* ============================================
   CELL STATES
   Non-color indicators included for WCAG 1.4.1
   ============================================ */

/* Selected cell - blue background + thick inset border */
.cw-cell.cw-selected {
    background: #bbdefb !important;
    box-shadow: inset 0 0 0 2px #1976d2;
}

/* Current word highlight - light blue */
.cw-cell.cw-word-highlight {
    background: #e3f2fd;
}

/* Locked/correct cell - light green + checkmark indicator (must override cw-wrong) */
.cw-cell.cw-locked {
    background: #e8f5e9 !important;
    cursor: default;
}

.cw-cell.cw-locked .cw-cell-letter {
    color: #256427;
}

.cw-cell.cw-locked::after {
    content: '\2713';
    position: absolute;
    bottom: 0;
    right: 1px;
    font-size: 0.35em;
    color: #256427;
    line-height: 1;
    pointer-events: none;
}

/* Wrong word shake animation */
.cw-cell.cw-shake {
    animation: cw-shake 0.5s;
}

@keyframes cw-shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-3px); }
    40%, 80% { transform: translateX(3px); }
}

/* Error flash - reduced-motion fallback for shake */
.cw-cell.cw-error-flash {
    box-shadow: inset 0 0 0 2px #d32f2f;
    background: #ffebee !important;
}

/* ============================================
   FOCUS INDICATORS (WCAG 2.4.7)
   ============================================ */

.cw-cell:focus-visible {
    outline: 3px solid #1976d2;
    outline-offset: -3px;
    z-index: 1;
}

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

/* ============================================
   WIDE GRID (> 15 columns)
   ============================================ */

.cw-grid-wide .cw-cell {
    width: 32px;
    height: 32px;
    font-size: 1.1em;
}

.cw-grid-wide .cw-cell-num {
    font-size: 0.4em;
}

/* ============================================
   CLUES PANEL
   ============================================ */

#cw-clues {
    margin: 15px auto;
    max-width: 600px;
    text-align: left;
}

.cw-clues-section {
    margin-bottom: 15px;
}

.cw-clues-section h3 {
    font-weight: bold;
    font-size: 1.1em;
    color: #333;
    padding: 8px 12px;
    background: #f0f0f0;
    border-radius: 4px;
    margin: 0 0 8px 0;
}

.cw-clues-list {
    padding: 0 5px;
}

.cw-clue {
    padding: 8px 10px;
    margin: 3px 0;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
    font-size: 1em;
    min-height: 44px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    border-left: 3px solid transparent;
}

.cw-clue:hover {
    background: #f5f5f5;
}

.cw-clue.cw-clue-active {
    background: #e3f2fd;
    border-left-color: #1976d2;
}

.cw-clue.cw-clue-complete {
    background: #e8f5e9;
    border-left-color: #256427;
}

.cw-clue.cw-clue-complete .cw-clue-text {
    text-decoration: line-through;
    color: #666;
}

.cw-clue-num {
    font-weight: bold;
    color: #333;
    min-width: 28px;
}

.cw-clue-text {
    color: #333;
    flex: 1;
    min-width: 120px;
}

.cw-clue-input-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-basis: 100%;
    justify-content: flex-end;
}

/* Individual letter cells in clue area */
.cw-clue-cells {
    display: inline-flex;
    gap: 2px;
}

.cw-clue-cell {
    width: 28px;
    height: 28px;
    border: 1px solid #ccc;
    border-radius: 2px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: Arial, sans-serif;
    font-size: 0.9em;
    font-weight: bold;
    text-transform: uppercase;
    background: #fff;
    color: #333;
    cursor: text;
    user-select: none;
}

.cw-clue-cell-filled {
    background: #f8f8f8;
}

.cw-clue-cell-active {
    border-color: #1976d2;
    box-shadow: inset 0 0 0 1px #1976d2;
    background: #e3f2fd;
}

.cw-clue-cell-locked {
    background: #e8f5e9;
    color: #256427;
    border-color: #a5d6a7;
    cursor: default;
}

@media (max-width: 500px) {
    .cw-clue-cell {
        width: 24px;
        height: 24px;
        font-size: 0.8em;
    }
}

/* Letter count hint after clue text */
.cw-clue-letters {
    color: #888;
    font-size: 0.85em;
    white-space: nowrap;
}

/* Status indicator - always reserves space so layout doesn't shift */
.cw-clue-status {
    display: inline-block;
    font-size: 1.1em;
    width: 24px;
    min-width: 24px;
    text-align: center;
    flex-shrink: 0;
    vertical-align: middle;
}

.cw-clue-status-correct {
    color: #256427;
}

.cw-clue-status-incorrect {
    color: #d32f2f;
}

/* Wrong answer clue row - light red background */
.cw-clue.cw-clue-wrong-answer {
    background: #fff0f0;
    border-left-color: #d32f2f;
}

/* Correct answer clue row - light green background */
.cw-clue.cw-clue-correct-answer {
    background: #f0fff0;
    border-left-color: #256427;
}

/* Wrong answer cells in the grid - light red */
.cw-cell.cw-wrong {
    background: #ffe0e0 !important;
}

/* ============================================
   SUBMIT / RESET BUTTONS
   ============================================ */

#cw-actions {
    margin: 15px auto;
    max-width: 600px;
    text-align: center;
}

#cw-actions .cw-submit-btn {
    font-size: 1em;
    font-weight: bold;
    background: linear-gradient(to bottom, #88ee88, #66cc66);
    color: #1a4d1a;
    padding: 10px 28px;
    border: 1px solid #55aa55;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
}

#cw-actions .cw-submit-btn:hover {
    background: linear-gradient(to bottom, #99ff99, #77dd77);
}

#cw-actions .cw-submit-btn:focus-visible {
    outline: 3px solid #1976d2;
    outline-offset: 2px;
}

#cw-actions .cw-reset-btn {
    font-size: 0.9em;
    padding: 10px 20px;
    background: #f5f5f5;
    border: 1px solid #ccc;
    border-radius: 6px;
    cursor: pointer;
    color: #555;
    transition: background 0.15s;
}

#cw-actions .cw-reset-btn:hover {
    background: #e8e8e8;
}

#cw-actions .cw-reset-btn:focus-visible {
    outline: 3px solid #1976d2;
    outline-offset: 2px;
}

/* ============================================
   ACTIVE CLUE DISPLAY (below grid)
   ============================================ */

.cw-active-clue {
    position: absolute;
    padding: 6px 14px;
    font-size: 0.9em;
    color: #1a2c5f;
    background: #ffffdd;
    border: 1px solid #ddd099;
    border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    display: none;
    min-width: 200px;
    max-width: min(320px, 80vw);
    text-align: left;
    line-height: 1.4;
    z-index: 100;
    cursor: grab;
    user-select: none;
    pointer-events: auto;
}

.cw-active-clue.cw-active-clue-visible {
    display: block;
}

.cw-active-clue.cw-dragging {
    cursor: grabbing;
    opacity: 0.9;
}

.cw-active-clue-num {
    font-weight: bold;
    color: #1976d2;
    margin-right: 4px;
    white-space: nowrap;
}

/* ============================================
   NOSCRIPT MESSAGE
   ============================================ */

.cw-noscript {
    padding: 15px;
    margin: 10px auto;
    max-width: 500px;
    text-align: center;
    background: #fff3e0;
    border: 1px solid #ffb74d;
    border-radius: 4px;
    color: #333;
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */

@media (max-width: 500px) {
    .cw-cell {
        width: 32px;
        height: 32px;
        font-size: 1.1em;
    }

    .cw-cell-num {
        font-size: 0.4em;
    }

    .cw-clue {
        font-size: 1em;
        padding: 6px 8px;
    }

    .cw-clue-input {
        max-width: 140px;
    }

    .cw-grid-wide .cw-cell {
        width: 28px;
        height: 28px;
        font-size: 0.95em;
    }
}

@media (max-width: 350px) {
    .cw-cell {
        width: 28px;
        height: 28px;
        font-size: 0.95em;
    }

    .cw-cell-num {
        font-size: 0.35em;
    }

    .cw-clues-section h3 {
        font-size: 0.95em;
    }

    .cw-clue {
        font-size: 0.95em;
        padding: 5px 6px;
    }

    .cw-clue-input {
        max-width: 110px;
        font-size: 0.9em;
    }

    .cw-grid-wide .cw-cell {
        width: 24px;
        height: 24px;
        font-size: 0.85em;
    }

    .cw-grid-wide .cw-cell-num {
        font-size: 0.3em;
    }
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    .cw-cell.cw-shake {
        animation: none;
    }

    .cw-cell,
    .cw-clue {
        transition: none;
    }
}

@media (prefers-contrast: high) {
    .cw-cell.cw-selected {
        box-shadow: inset 0 0 0 3px #000;
    }

    .cw-cell.cw-word-highlight {
        box-shadow: inset 0 0 0 1px #000;
    }

    .cw-clue.cw-clue-active {
        border-left-width: 4px;
        border-left-color: #000;
    }

    .cw-cell-num {
        color: #000;
    }

    .cw-cell-letter {
        color: #000;
    }
}

@media (forced-colors: active) {
    .cw-cell.cw-selected {
        outline: 3px solid Highlight;
    }

    .cw-cell.cw-locked {
        outline: 2px solid ButtonText;
    }

    .cw-cell.cw-word-highlight {
        outline: 1px dashed Highlight;
    }

    .cw-cell.cw-error-flash {
        outline: 2px solid LinkText;
    }
}

/* ============================================
   MANAGE PAGE (Setup & Editor)
   Scoped under .cw-manage so none of these rules
   leak into the player UI.
   ============================================ */

.cw-manage {
    max-width: 900px;
    margin: 15px auto;
    padding: 0 10px;
    font-family: Arial, sans-serif;
    font-size: 14px;
    color: #222;
    line-height: 1.4;
}

.cw-manage h2,
.cw-manage h3 {
    margin: 15px 0 8px;
    color: #1a2c5f;
}

/* ----- Notices ----- */

.cw-manage-notice,
.cw-manage-error {
    margin: 12px auto;
    padding: 10px 15px;
    max-width: 650px;
    border-radius: 4px;
    border: 2px solid;
    font-size: 0.95em;
    text-align: center;
    line-height: 1.5;
}

.cw-manage-notice {
    border-color: #1976d2;
    background: #e3f2fd;
    color: #0d3a6b;
}

.cw-manage-error {
    border-color: #c00;
    background: #fff4f4;
    color: #600;
}

/* ----- Grid Preview ----- */

.cw-manage-preview-wrap {
    position: relative;
    margin: 15px auto;
    padding: 20px 10px;
    text-align: center;
    background: #f9f9f9;
    border: 1px solid #ddd;
    border-radius: 6px;
    transition: opacity 0.2s;
}

.cw-manage-preview-links {
    margin: 0 0 10px;
    font-size: 0.95em;
    text-align: center;
}

.cw-manage-preview-links a {
    margin: 0 8px;
}

.cw-manage-preview {
    display: inline-block;
}

/* In management mode the preview always shows the letters. The grid
   render function only writes letters into "locked" cells, so
   MANAGE_CROSSWORD marks every filled cell as locked before rendering.
   Override the player's green/checkmark locked styling so cells look
   neutral in the preview. */
.cw-manage-preview .cw-cell.cw-locked {
    background: #fff !important;
    cursor: default;
}

.cw-manage-preview .cw-cell.cw-locked .cw-cell-letter {
    color: #222;
}

.cw-manage-preview .cw-cell.cw-locked::after {
    content: none;
}

.cw-manage-preview-wrap.cw-manage-preview--loading .cw-manage-preview {
    opacity: 0.45;
    pointer-events: none;
}

.cw-manage-preview-wrap.cw-manage-preview--loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 36px;
    height: 36px;
    margin: -18px 0 0 -18px;
    border: 3px solid #1976d2;
    border-top-color: transparent;
    border-radius: 50%;
    animation: cw-manage-spin 0.8s linear infinite;
    z-index: 2;
}

@keyframes cw-manage-spin {
    to { transform: rotate(360deg); }
}

/* ----- Buttons ----- */

.cw-manage .cw-btn {
    display: inline-block;
    min-width: 44px;
    min-height: 44px;
    padding: 10px 22px;
    margin: 4px;
    font-size: 1em;
    font-weight: bold;
    font-family: inherit;
    border: 1px solid #aaa;
    border-radius: 6px;
    background: #f5f5f5;
    color: #333;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    text-align: center;
    vertical-align: middle;
    line-height: 1.2;
}

.cw-manage .cw-btn:hover {
    background: #eaeaea;
}

.cw-manage .cw-btn:focus-visible {
    outline: 3px solid #1976d2;
    outline-offset: 2px;
}

.cw-manage .cw-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.cw-manage .cw-btn-primary {
    background: linear-gradient(to bottom, #88ee88, #66cc66);
    color: #1a4d1a;
    border-color: #55aa55;
}

.cw-manage .cw-btn-primary:hover {
    background: linear-gradient(to bottom, #99ff99, #77dd77);
}

.cw-manage .cw-btn-secondary {
    background: #f5f5f5;
    color: #444;
    border-color: #ccc;
}

.cw-manage .cw-btn-secondary:hover {
    background: #e8e8e8;
}

.cw-manage .cw-btn-shuffle {
    padding: 10px 16px;
    min-width: 50px;
    font-size: 0.95em;
}

/* ----- Shuffle Panel ----- */

.cw-manage-shuffle {
    margin: 15px auto;
    padding: 12px 18px;
    max-width: 650px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: #fafafa;
}

.cw-manage-shuffle legend {
    padding: 0 8px;
    font-weight: bold;
    color: #1a2c5f;
}

.cw-manage-shuffle-buttons {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    margin: 4px 0 10px;
}

.cw-manage-shuffle-help {
    margin: 8px 0 0;
    font-size: 0.88em;
    color: #555;
    line-height: 1.5;
}

/* ----- Words Editor ----- */

.cw-manage-words {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin: 20px 0;
}

.cw-manage-word-section {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 12px 15px;
}

.cw-manage-word-section h3 {
    margin: 0 0 10px;
    padding-bottom: 6px;
    border-bottom: 2px solid #1976d2;
    font-size: 1.1em;
}

.cw-manage-word-row {
    display: grid;
    grid-template-columns: 34px 1fr;
    gap: 4px 10px;
    padding: 12px 0;
    border-bottom: 1px solid #eee;
    align-items: start;
}

.cw-manage-word-row:last-child {
    border-bottom: none;
}

.cw-manage-word-num {
    grid-column: 1;
    grid-row: 1 / span 2;
    font-weight: bold;
    font-size: 1.15em;
    text-align: right;
    color: #1a2c5f;
    padding-top: 10px;
}

.cw-manage-word-field,
.cw-manage-clue-field {
    grid-column: 2;
    display: flex;
    flex-direction: column;
}

.cw-manage-word-label,
.cw-manage-clue-label {
    font-size: 0.78em;
    font-weight: bold;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 2px 0 3px;
}

.cw-manage-word-input,
.cw-manage-clue-input {
    padding: 10px 12px;
    font-size: 1em;
    font-family: inherit;
    border: 1px solid #bbb;
    border-radius: 4px;
    background: #fff;
    color: #222;
    min-height: 44px;
    box-sizing: border-box;
    width: 100%;
}

.cw-manage-word-input:focus,
.cw-manage-clue-input:focus {
    outline: none;
    border-color: #1976d2;
    box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.25);
}

.cw-manage-word-input {
    text-transform: uppercase;
    font-weight: bold;
    letter-spacing: 1px;
    max-width: 220px;
}

.cw-manage-word-input-row {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.cw-manage-word-input-row .cw-manage-word-input {
    flex: 1 1 auto;
    min-width: 0;
}

.cw-manage-delete-btn {
    background: transparent;
    border: none;
    color: #c00;
    font-size: 0.85em;
    font-weight: bold;
    font-family: inherit;
    cursor: pointer;
    padding: 8px 4px;
    text-decoration: underline;
    flex: 0 0 auto;
}

.cw-manage-delete-btn:hover {
    color: #900;
    text-decoration: none;
}

.cw-manage-delete-btn:focus-visible {
    outline: 2px solid #1976d2;
    outline-offset: 2px;
    border-radius: 2px;
}

.cw-manage-word-row.cw-manage-row-deleting {
    opacity: 0.55;
}

.cw-manage-word-row.cw-manage-row-deleting .cw-manage-word-input,
.cw-manage-word-row.cw-manage-row-deleting .cw-manage-clue-input {
    text-decoration: line-through;
    background: #f8f8f8;
}

.cw-manage-word-hint {
    font-size: 0.78em;
    color: #888;
    margin-top: 3px;
}

.cw-manage-clue-counter {
    font-size: 0.78em;
    color: #888;
    margin-top: 3px;
    text-align: right;
}

.cw-manage-clue-counter.cw-over-limit {
    color: #c00;
    font-weight: bold;
}

/* ----- Add Row Section ----- */

.cw-manage-new-rows {
    margin: 15px 0 5px;
}

.cw-manage-new-rows .cw-manage-word-row {
    background: #fafffa;
    border-radius: 4px;
    padding-left: 8px;
    padding-right: 8px;
}

#cw-manage-add-row {
    display: block;
    width: 100%;
    margin: 10px 0;
    padding: 14px;
    border: 2px dashed #aaa;
    background: #fafafa;
    color: #555;
    font-size: 1em;
    font-weight: bold;
    font-family: inherit;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

#cw-manage-add-row:hover {
    background: #f0f6ff;
    border-color: #1976d2;
    color: #1976d2;
}

#cw-manage-add-row:focus-visible {
    outline: 3px solid #1976d2;
    outline-offset: 2px;
}

#cw-manage-add-row:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    border-color: #ccc;
    background: #f5f5f5;
    color: #999;
}

/* ----- Unplaced Words Warning ----- */

.cw-manage-unplaced {
    margin: 15px auto;
    padding: 10px 15px;
    max-width: 650px;
    background: #fff8e1;
    border: 1px solid #ffb74d;
    border-radius: 4px;
    color: #5d4010;
    font-size: 0.95em;
    text-align: center;
    line-height: 1.5;
}

.cw-manage-unplaced:empty {
    display: none;
}

.cw-manage-unplaced-title {
    font-weight: bold;
    margin-right: 6px;
}

/* ----- Form Wrapper & Submit ----- */

.cw-manage form {
    margin: 0;
}

.cw-manage-submit {
    margin: 20px auto;
    text-align: center;
}

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

@media (max-width: 760px) {
    .cw-manage-words {
        grid-template-columns: 1fr;
        gap: 15px;
    }
}

@media (max-width: 500px) {
    .cw-manage {
        padding: 0 6px;
        font-size: 13px;
    }

    .cw-manage .cw-btn {
        padding: 10px 14px;
    }

    .cw-manage .cw-btn-shuffle {
        padding: 10px 12px;
        min-width: 44px;
    }

    .cw-manage-word-row {
        grid-template-columns: 28px 1fr;
        gap: 4px 8px;
        padding: 10px 0;
    }

    .cw-manage-word-num {
        font-size: 1em;
        padding-top: 8px;
    }

    .cw-manage-word-input {
        max-width: 100%;
    }

    .cw-manage-preview-wrap {
        padding: 12px 4px;
    }
}

/* ----- Manage Accessibility ----- */

@media (prefers-reduced-motion: reduce) {
    .cw-manage-preview-wrap,
    .cw-manage .cw-btn,
    #cw-manage-add-row,
    .cw-manage-word-input,
    .cw-manage-clue-input {
        transition: none;
    }

    .cw-manage-preview-wrap.cw-manage-preview--loading::after {
        animation: none;
        border-top-color: #1976d2;
    }
}

@media (prefers-contrast: high) {
    .cw-manage-notice,
    .cw-manage-error,
    .cw-manage-unplaced {
        border-width: 3px;
    }

    .cw-manage-word-input,
    .cw-manage-clue-input {
        border-color: #000;
    }

    .cw-manage-word-input:focus,
    .cw-manage-clue-input:focus {
        border-color: #000;
        box-shadow: 0 0 0 3px #000;
    }

    .cw-manage-word-section h3 {
        border-bottom-color: #000;
    }
}

@media (forced-colors: active) {
    .cw-manage .cw-btn:focus-visible,
    #cw-manage-add-row:focus-visible {
        outline: 3px solid Highlight;
    }

    .cw-manage-word-input:focus,
    .cw-manage-clue-input:focus {
        outline: 2px solid Highlight;
    }
}
