/* Crossword Viewer Styles */
.crossword-grid {
    display: grid;
    gap: 1px;
    background-color: #363636;
    padding: 10px;
    border-radius: 8px;
    max-width: 100%;
    margin: 0 auto;
    width: fit-content;
    transform-origin: top left;
    will-change: transform;
}

.crossword-cell {
    width: 30px;
    height: 30px;
    background-color: white;
    border: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
    user-select: none;
}

.crossword-cell:hover {
    background-color: #f5f5f5;
    border-color: #3273dc;
}

.crossword-cell.active {
    background-color: #e3f2fd;
    border-color: #3273dc;
    box-shadow: 0 0 0 2px rgba(50, 115, 220, 0.2);
}

.crossword-cell.blocked {
    background-color: #363636;
    border-color: #363636;
    cursor: default;
}

.crossword-cell.revealed {
    background-color: #e8f5e8;
    color: #2e7d32;
}

.crossword-cell.incorrect {
    background-color: #ffebee;
    color: #c62828;
    animation: shake 0.3s ease-in-out;
}

.crossword-cell-number {
    position: absolute;
    top: 1px;
    left: 2px;
    font-size: 8px;
    font-weight: normal;
    color: #666;
    line-height: 1;
    pointer-events: none;
}

/* Input styling for crossword cells */
.crossword-input {
    width: 100%;
    height: 100%;
    border: none;
    background: transparent;
    text-align: center;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    font-weight: bold;
    outline: none;
    text-transform: uppercase;
}

.crossword-input:focus {
    background-color: transparent;
}

/* Clue list styling */
.clue-list {
    display: none;
    max-height: 400px;
    overflow-y: auto;
}

.clue-list.is-active {
    display: block;
}

.clue-item {
    margin-bottom: 12px;
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.clue-item:hover {
    background-color: #f5f5f5;
}

.clue-item.active {
    background-color: #e3f2fd;
    border-left: 4px solid #3273dc;
}

.clue-number {
    font-weight: bold;
    color: #3273dc;
    margin-right: 8px;
}

.clue-text {
    color: #4a4a4a;
    line-height: 1.4;
}

.clue-answer {
    color: #2e7d32;
    font-weight: bold;
    margin-left: 8px;
}

/* Timer styling */
#timer-display {
    font-family: 'Courier New', monospace;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
    background-color: #f5f5f5;
}

/* Toast notifications */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 9999;
}

.toast {
    background-color: #3273dc;
    color: white;
    padding: 12px 16px;
    border-radius: 6px;
    margin-bottom: 10px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    opacity: 0;
    transform: translateX(100%);
    transition: all 0.3s ease;
    max-width: 300px;
    word-wrap: break-word;
}

.toast.show {
    opacity: 1;
    transform: translateX(0);
}

.toast.success {
    background-color: #48c774;
}

.toast.error {
    background-color: #f14668;
}

.toast.warning {
    background-color: #ffdd57;
    color: #363636;
}

/* Modal styling enhancements */
.modal-card-body {
    min-height: 200px;
}

#hint-content {
    line-height: 1.6;
}

/* Responsive design */
@media (max-width: 768px) {
    .crossword-cell {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }

    .crossword-grid {
        padding: 5px;
        gap: 1px;
    }

    .columns {
        flex-direction: column;
    }

    .column.is-8,
    .column.is-4 {
        width: 100%;
        max-width: 100%;
    }

    .field.is-grouped {
        flex-direction: column;
        align-items: stretch;
    }

    .field.has-addons {
        flex-direction: column;
        align-items: stretch;
    }

    .control {
        margin-bottom: 8px;
    }
}

@media (max-width: 480px) {
    .crossword-cell {
        width: 28px;
        height: 28px;
        font-size: 12px;
    }

    .crossword-cell.number {
        font-size: 8px;
        top: 1px;
        left: 1px;
    }

    .title.is-2 {
        font-size: 1.5rem;
    }

    .title.is-4 {
        font-size: 1.25rem;
    }
}

/* Desktop sizing improvements */
@media (min-width: 1024px) {
    .crossword-cell {
        width: 40px;
        height: 40px;
        font-size: 18px;
    }

    .crossword-cell-number {
        font-size: 10px;
    }
}

/* Animation for incorrect answers */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

/* Loading state */
.loading {
    text-align: center;
    padding: 40px;
    color: #666;
}

.loading::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid #f3f3f3;
    border-top: 2px solid #3273dc;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-left: 10px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Accessibility improvements */
.crossword-cell:focus {
    outline: 3px solid #3273dc;
    outline-offset: 2px;
}

.clue-item:focus {
    outline: 2px solid #3273dc;
    outline-offset: 1px;
}

/* Print styles */
@media print {
    @page {
        margin: 0.5cm;
        size: letter portrait;
    }

    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    body {
        background-color: white !important;
        background-image: none !important;
        padding-top: 0 !important;
        color: black !important;
        margin: 0 !important;
    }

    /* Hide UI elements */
    .navbar,
    .navbar-menu,
    .buttons,
    .field,
    .toast-container,
    .modal,
    #difficulty-factors,
    #sticky-clue-bar,
    #grid-zoom-controls,
    .box > .title:not(#puzzle-title),
    .tabs {
        display: none !important;
    }

    /* Layout adjustments */
    .section {
        padding: 0 !important;
    }

    .container {
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 auto !important;
    }

    .box {
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
        margin-bottom: 1rem !important;
        background-color: transparent !important;
    }

    /* Puzzle Header */
    #puzzle-header {
        margin-bottom: 1rem !important;
        border-bottom: 2px solid #000 !important;
        padding-bottom: 1rem !important;
    }

    #puzzle-title {
        font-size: 24pt !important;
        margin-bottom: 0.5rem !important;
        color: black !important;
    }

    .tags .tag {
        border: 1px solid #000 !important;
        color: #000 !important;
        background: #fff !important;
    }

    /* Grid Layout */
    .columns {
        display: block !important;
    }

    .column {
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        display: block !important;
    }

    /* Crossword Grid */
    .crossword-grid {
        background-color: transparent !important;
        border: 2px solid #000 !important;
        gap: 0 !important;
        page-break-inside: avoid;
        margin: 0 auto 1.5rem auto !important;
        padding: 0 !important;
        --print-grid-size: min(90vw, 90%);
        width: var(--print-grid-size) !important;
        max-width: var(--print-grid-size) !important;
        transform: none !important;
    }

    .crossword-cell {
        background-color: #fff !important;
        border: 1px solid #000 !important;
        border-right-width: 1px !important;
        border-bottom-width: 1px !important;
        color: transparent !important; /* Hide answers for printing */
        width: calc(var(--print-grid-size) / var(--grid-cols)) !important;
        height: calc(var(--print-grid-size) / var(--grid-cols)) !important;
        font-size: 16pt !important;
        box-shadow: none !important;
        margin: 0 !important;
        padding: 0 !important;
        /* Collapse borders to avoid double-thick lines */
        margin-left: -1px !important;
        margin-top: -1px !important;
    }

    /* First column - no left margin collapse */
    .crossword-cell:nth-child(var(--grid-cols)n + 1) {
        margin-left: 0 !important;
    }

    /* First row - no top margin collapse */
    .crossword-cell:nth-child(-n + var(--grid-cols)) {
        margin-top: 0 !important;
    }

    .crossword-cell.blocked {
        background-color: #000 !important;
        border-color: #000 !important;
    }

    .crossword-cell.active {
        background-color: #fff !important;
    }

    .crossword-cell.incorrect,
    .crossword-cell.revealed {
        background-color: #fff !important;
        color: transparent !important;
    }

    .crossword-cell-number {
        color: #000 !important;
        font-size: 10pt !important;
        font-weight: bold !important;
        top: 2px !important;
        left: 2px !important;
    }

    .crossword-input {
        display: none !important;
    }

    .crossword-scroll-container {
        overflow: visible !important;
        display: flex !important;
        justify-content: center !important;
        align-items: flex-start !important;
        padding-bottom: 0 !important;
        margin-left: auto !important;
        margin-right: auto !important;
    }

    /* Clues Layout */
    .clue-list {
        display: block !important;
        max-height: none !important;
        overflow: visible !important;
        column-count: 2;
        column-gap: 2rem;
    }

    #across-clues, #down-clues {
        display: block !important;
        margin-top: 1rem;
        page-break-inside: avoid;
    }

    #across-clues h4, #down-clues h4 {
        border-bottom: 1px solid #000;
        padding-bottom: 0.25rem;
        margin-bottom: 0.5rem;
        font-size: 14pt !important;
        color: black !important;
    }

    .clue-item {
        margin-bottom: 0.25rem !important;
        padding: 0 !important;
        background-color: transparent !important;
        page-break-inside: avoid;
        font-size: 10pt !important;
        break-inside: avoid;
    }

    .clue-item:hover,
    .clue-item.active {
        background-color: transparent !important;
        border: none !important;
    }

    .clue-number {
        color: #000 !important;
        font-weight: bold;
    }

    .clue-text {
        color: #000 !important;
    }

    /* Hide the footer link */
    a[href^="/"] {
        text-decoration: none !important;
        color: black !important;
    }
}

/* Scroll Container for large grids */
.crossword-scroll-container {
    width: 100%;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1rem;
    padding-bottom: 5px; /* Space for scrollbar */
    position: relative;
    cursor: grab;
}

.crossword-scroll-container.is-panning {
    cursor: grabbing;
}

/* Sticky Clue Bar */
#sticky-clue-bar {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    border-radius: 4px;
    background-color: #e3f2fd; /* Light blue matching active cell */
    border: 1px solid #3273dc;
    transition: all 0.2s ease;
    display: grid;
    gap: 4px;
    font-size: 0.95rem;
}

#sticky-clue-bar .icon {
    color: #3273dc;
}

.sticky-clue-line {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 2px 4px;
    border-radius: 4px;
    cursor: pointer;
}

.sticky-clue-line.is-active {
    background-color: rgba(50, 115, 220, 0.15);
}

.sticky-clue-label {
    font-weight: 600;
    color: #3273dc;
}

.sticky-clue-text {
    font-weight: 600;
    color: #1f2d3d;
}

#grid-zoom-controls {
    justify-content: flex-end;
}

/* Show sticky bar mostly on mobile/tablet or large grids */
@media (min-width: 1024px) {
    #sticky-clue-bar {
        /* Optional: Hide on desktop if not desired, but useful for large puzzles too */
        /* display: none !important; */
    }
}

/*
   Large Grid Adjustments (e.g. 21x21 Sunday Puzzles)
   Dynamically applied via JS when cols > 15
*/
.crossword-grid.large-grid {
    overflow-x: auto;
    justify-content: flex-start;
    max-width: 100%; /* Constrain to parent column */
    display: flex;   /* Use flex to allow scrolling contents */
    flex-wrap: wrap; /* Grid is updated via JS style grid-template-columns */
    /* Wait, the element has display: grid in main CSS.
       We should keep display: grid but wrap it in a scroll container?
       Or use display: grid + overflow: auto works?
       Actually standard grid with overflow auto works if min-content is large.
    */
    display: grid;
}

/* Default mobile/base size for large grids */
.large-grid .crossword-cell {
    width: 20px;
    height: 20px;
    font-size: 9px;
}

.large-grid .crossword-cell-number {
    font-size: 6px;
    top: 0;
    left: 1px;
}

/* Tablet sizing */
@media (min-width: 768px) {
    .large-grid .crossword-cell {
        width: 24px;
        height: 24px;
        font-size: 11px;
    }
    .large-grid .crossword-cell-number {
        font-size: 8px;
    }
}

/* Desktop sizing */
@media (min-width: 1024px) {
    .large-grid .crossword-cell {
        width: 26px; /* 23 * 26 = ~600px, fits in column */
        height: 26px;
        font-size: 12px;
    }
    .large-grid .crossword-cell-number {
        font-size: 9px;
    }
}
