/* Cryptogram-specific styles extracted from cryptogram2D.php */
:root {
    --crypto-cell-surface: #c1c1c1;
    --crypto-cell-surface-hover: #c1c1c1;
    --crypto-cell-surface-hinted: #c1c1c1;
    --crypto-cell-border: rgba(71, 85, 105, 0.28);
    --crypto-cell-text: #000000;
}

html[data-theme='dark'] {
    --news-bg: var(--bg-0);
    --news-paper: var(--bg-1);
    --news-ink: #e8eef5;
    --news-ink-light: #b2c0d0;
    --news-ink-muted: #8191a3;
    --news-rule: #d6dfeb;
    --news-rule-light: #3a495c;
    --news-accent: var(--accent);
    --news-highlight: color-mix(in srgb, var(--accent) 20%, transparent);
    --crypto-cell-surface: var(--bg-2);
    --crypto-cell-surface-hover: color-mix(in srgb, var(--bg-2) 80%, var(--text));
    --crypto-cell-surface-hinted: color-mix(in srgb, var(--accent) 22%, var(--bg-1));
    --crypto-cell-border: rgba(148, 163, 184, 0.35);
    --crypto-cell-text: #e5edf7;
}

html[data-theme='dark'] body {
    background:
        repeating-linear-gradient(0deg, transparent, transparent 39px, rgba(255,255,255,0.035) 39px, rgba(255,255,255,0.035) 40px),
        var(--bg-0) !important;
    background-attachment: fixed !important;
    color: var(--news-ink);
}

.crypto-container {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 2rem;
    width: 100%;
}

.crypto-container .puzzle-container {
    display: block;
    min-width: 0;
}

.crypto-container .puzzle-container .card + .card {
    margin-top: var(--spacing-md);
}

.puzzle-area { margin-top: var(--spacing-md); }

.classic-content .classic-grid-section {
    flex: 1 1 0;
    min-width: 0;
    max-width: calc(100% - 300px);
}

.classic-content .puzzle-area {
    min-width: 0;
}

.classic-content .classic-clues-section {
    flex: 0 0 272px;
    min-width: 272px;
}

.cryptogram-frequency-section {
    margin-top: 18px;
}

#puzzle-display {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: flex-end;
    gap: 0;
    padding: 0.5rem 0;
    width: 100%;
    /* Contain a word that is longer than the screen.
       Words wrap, but a single word cannot break, so one 16-character word is
       414px at the mobile cell size. That pushed the DOCUMENT wider, and on a
       phone Chrome then widens the layout viewport to fit, which stretched every
       fixed element with it — the cookie banner measured 450px on a 360px screen
       as collateral. Measured overflow before this: 12px at 412px and 64px at
       360px. Scrolling the word inside its own box keeps the page width honest. */
    overflow-x: auto;
    --crypto-cell-width: 28px;
    --crypto-cell-height: 28px;
    --crypto-input-height: 26px;
    --crypto-char-gap: 4px;
    --crypto-word-gap: 14px;
}

.puzzle-word {
    display: inline-flex;
    align-items: flex-end;
    gap: var(--crypto-char-gap);
    margin: 0 calc(var(--crypto-word-gap) / 2) 1.2rem;
}

.puzzle-char {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin: 0;
    width: var(--crypto-cell-width);
}

.encrypted-char {
    font-family: var(--font-family-mono);
    font-size: 1.1rem;
    font-weight: 700;
    width: var(--crypto-cell-width);
    height: var(--crypto-cell-height);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-primary);
    letter-spacing: 0;
    -webkit-user-select: none;
    user-select: none;
    line-height: 1;
}

.puzzle-char-symbol .encrypted-char {
    font-weight: 700;
    color: var(--color-text-muted);
}

input.decrypted-char-input,
input.decrypted-char-input[type="text"] {
    /* WCAG 2.2 SC 2.5.8 requires a 24x24 CSS px minimum target. The mobile
       breakpoints used to shrink these to 18x16, which measured as a
       target-size failure. The floor is declared here as well as in the
       breakpoints so no later override can drop below the minimum. */
    min-width: 24px;
    min-height: 24px;
    font-family: var(--font-family-mono);
    width: var(--crypto-cell-width) !important;
    height: var(--crypto-input-height) !important;
    padding: 0 !important;
    margin: 0;
    box-sizing: border-box;
    display: block;
    background-color: var(--crypto-cell-surface);
    border: 1px solid var(--crypto-cell-border);
    border-radius: var(--radius-sm);
    color: var(--crypto-cell-text);
    text-align: center;
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    outline: none;
    transition: all 0.2s;
    caret-color: var(--color-accent-primary);
    line-height: var(--crypto-input-height);
    overflow: visible;
}
.decrypted-char-input:focus { border-color: var(--color-accent-primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 40%, transparent); }
.decrypted-char-input.correct { background-color: rgba(16,185,129,0.2); border-color: var(--color-success); color: var(--color-success); }
.decrypted-char-input.incorrect { background-color: rgba(239,68,68,0.2); border-color: var(--color-error); color: var(--color-error); animation: shake 0.5s ease-in-out; }
@keyframes shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-5px)} 75%{transform:translateX(5px)} }

.char-spacer {
    width: var(--crypto-cell-width);
    height: var(--crypto-input-height);
}

.letter-frequency-table {
    display: flex;
    flex-direction: row;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
}
.freq-item {
    background-color: var(--crypto-cell-surface); border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem; display: flex; flex-direction: column; align-items: center;
    font-family: var(--font-family-mono); color: var(--crypto-cell-text); cursor: pointer; transition: all 0.2s;
}
.freq-item:hover { background-color: var(--crypto-cell-surface-hover); }
.freq-item.hinted { background-color: var(--crypto-cell-surface-hinted); border-color: var(--color-accent-primary); }
.freq-char { font-weight: var(--font-weight-semibold); }
.freq-count { font-size: 0.8rem; color: var(--crypto-cell-text); }

.game-stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--spacing-md); }
.stat-label { color: var(--color-text-muted); }
.controls-grid { display: grid; gap: var(--spacing-sm); }
.progress-zero { width: 0; }

.cryptogram-frequency-help {
    font-size: 0.85rem;
    color: var(--text-secondary-color);
    margin-bottom: 10px;
}

.cryptogram-difficulty-wrap {
    margin-bottom: var(--spacing-sm);
}

.cryptogram-difficulty-label {
    display: block;
    margin-bottom: 6px;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

.cryptogram-difficulty-select {
    width: 100%;
}

.cryptogram-howto-extra {
    margin-top: 8px;
}

.cryptogram-hidden {
    display: none;
}

#victory-overlay{-webkit-backdrop-filter:blur(4px);backdrop-filter:blur(4px)}
#victory-overlay.active{opacity:1;pointer-events:auto}
.victory-content{background:var(--color-secondary-bg);padding:40px;border-radius:var(--radius-xl);text-align:center;max-width:450px;border:1px solid var(--color-border)}
.victory-content h2{font-size:var(--font-size-2xl);color:var(--color-success);margin-bottom:var(--spacing-sm)}
.victory-content p{color:var(--color-text-muted);margin-bottom:var(--spacing-xl)}

#message{position:fixed;top:18px;right:18px;background:var(--color-error);color:white;padding:var(--spacing-sm) var(--spacing-md);border-radius:var(--radius-lg);box-shadow:var(--shadow-xl);z-index:var(--z-toast);opacity:0;transition:opacity 0.3s;pointer-events:none}
#message.show{opacity:1}

/* Button variant styles to replace inline styles */
.control-btn-hint {
    background: linear-gradient(135deg, var(--color-accent-primary), var(--accent)) !important;
}
.control-btn-check {
    background: linear-gradient(135deg, var(--color-success), #059669) !important;
}
.control-btn-reset {
    background: var(--color-tertiary-bg) !important;
    border: 1px solid var(--color-border) !important;
}

@media (max-width: 900px) { .crypto-container { grid-template-columns: 1fr; } }

/* Responsive adjustments for classic layout used by the page */
@media (max-width: 768px) {
    /* Ensure the main grid section can collapse to full width */
    .classic-content .classic-grid-section { max-width: 100% !important; width: 100% !important; }
    /* Let the clues / sidebar flow beneath the puzzle area */
    .classic-content .classic-clues-section { flex: 1 1 auto !important; min-width: 0 !important; width: 100% !important; }

    /* Reserve room for the grid before JavaScript builds it.
       #puzzle-display starts empty and is filled after load; in this stacked
       layout the clues section sits BELOW it, so the grid growing from zero
       height pushed everything down — the largest layout shift on the puzzle
       pages (measured CLS 0.16, with 0.124 attributed to .classic-clues-section
       alone). At 375px a nine-word grid is 405px tall and a shorter quote is
       less, so this floor covers the common case without leaving a large gap.
       The complete fix is to render the grid server-side; this removes most of
       the shift in the meantime. */
    #puzzle-display { min-height: 300px; }

    /* Reduce puzzle cell sizes for smaller screens */
    #puzzle-display { --crypto-cell-width: 24px; --crypto-cell-height: 24px; --crypto-input-height: 24px; --crypto-char-gap: 3px; --crypto-word-gap: 10px; }
    .puzzle-word { margin-bottom: 1rem; }

    /* Frequency items: allow wrapping and smaller padding */
    .letter-frequency-table { gap: 0.35rem; }
    .freq-item { padding: 0.35rem 0.5rem; }

    /* Explicitly stack puzzle, progress, then frequency chart */
    .classic-content .classic-grid-section { display: flex; flex-direction: column; align-items: stretch; }
    .classic-content .classic-grid-section .puzzle-area { order: 1; }
    .classic-content .classic-grid-section .classic-progress-bar { order: 2; margin-top: 8px; }
    .classic-content .classic-grid-section .cryptogram-frequency-section { order: 3; width: 100%; margin-top: 12px; }
}

@media (max-width: 420px) {
    /* Further scale down for narrow phones */
    #puzzle-display { --crypto-cell-width: 24px; --crypto-cell-height: 24px; --crypto-input-height: 24px; --crypto-char-gap: 2px; --crypto-word-gap: 8px; }
    .encrypted-char { font-size: 0.95rem; }
    input.decrypted-char-input { font-size: 0.75rem; }
    .puzzle-word { margin: 0 4px 0.8rem; }
    .classic-masthead-title { font-size: 1.6rem; }
}
