:root {
    --color-correct: #6aaa64;
    /* Green */
    --color-present: #c9b458;
    /* Yellow */
    --color-absent: #787c7e;
    /* Gray */
    --color-absent-bg: #2b1d1d;
    /* Dark Red/Grey */
    --color-absent-border: #7f1d1d;
    --color-absent-text: #ffcccc;
    --color-bg: #121213;
    --color-tone-1: #ffffff;
    --color-tone-2: #818384;
    --color-tone-3: #565758;
    --color-tone-4: #3a3a3c;
    /* Scratchpad Colors */
    --color-scratchpad-border: #4a90e2;
    --color-scratchpad-bg: #1a2634;

    --tile-size: 60px;
    --gap: 5px;
    --cols: 5;
    /* Dynamic */
}

* {
    box-sizing: border-box;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--color-bg);
    color: var(--color-tone-1);
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100vh;
    overflow: hidden;
}

/* --- Loading Overlay --- */
#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--color-bg);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loader-text {
    margin-bottom: 20px;
    font-size: 1.2rem;
    font-weight: bold;
}

.progress-bar-container {
    width: 80%;
    max-width: 300px;
    height: 10px;
    background-color: var(--color-tone-4);
    border-radius: 5px;
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background-color: var(--color-correct);
    transition: width 0.3s;
}

/* --- Win Overlay --- */
#win-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(18, 18, 19, 0.4);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    text-align: center;
    padding: 20px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

#win-overlay .win-overlay-inner {
    background-color: rgba(0, 0, 0, 0.85);
    padding: 6%;
    border-radius: 4%;
}

#win-overlay.show {
    opacity: 1;
    pointer-events: all;
}

.reload-icon {
    font-size: 48px;
    margin-bottom: 20px;
    color: var(--color-correct);
    cursor: pointer;
    transition: transform 0.3s;
}

.reload-icon:hover {
    transform: rotate(180deg);
}

.win-title {
    font-size: 32px;
    margin-bottom: 10px;
    color: var(--color-tone-1);
    font-weight: 800;
}

.win-message {
    font-size: 18px;
    margin-bottom: 30px;
    color: var(--color-correct);
    line-height: 1.5;
}

.secret-word-reveal {
    color: var(--color-tone-1);
    font-size: 1.4rem;
    font-weight: 800;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
    display: inline-block;
    margin: 5px 0;
}

.play-again-btn {
    background-color: var(--color-correct);
    color: white;
    border: none;
    padding: 15px 30px;
    font-size: 18px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

/* --- Header --- */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    max-width: 800px;
    /* Increased for sidebar layout */
    padding: 10px 20px;
    border-bottom: 1px solid var(--color-tone-4);
    flex-shrink: 0;
    position: relative;
}

h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 700;
    letter-spacing: 1px;
}

.review-mode-badge {
    margin: 0 4px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(74, 144, 226, 0.15);
    border: 1px solid rgba(74, 144, 226, 0.4);
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 12px;
    color: #4a90e2;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.review-mode-badge svg {
    stroke: #4a90e2;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 15px;
}

.score-display {
    font-size: 14px;
    color: var(--color-present);
    font-weight: bold;
}

select {
    background-color: var(--color-tone-4);
    color: var(--color-tone-1);
    border: none;
    padding: 5px 10px;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
}

/* --- Main Layout --- */
#main-layout {
    display: flex;
    flex-direction: row;
    width: 100%;
    max-width: 900px;
    flex-grow: 1;
    overflow: hidden;
    justify-content: center;
}

/* --- Game Area --- */
#game-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-grow: 1;
    max-width: 500px;
    position: relative;
    /* For win overlay positioning relative to game */
    height: 100%;
}

#game-container {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    width: 100%;
    padding: 20px 10px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

#game-grid {
    display: grid;
    grid-template-columns: repeat(var(--cols), 1fr);
    gap: var(--gap);
    width: 100%;
    max-width: 350px;
    margin: auto;
}

/* --- History Sidebar --- */
#history-panel {
    width: 250px;
    background-color: #1a1a1b;
    border-left: 1px solid var(--color-tone-4);
    padding: 15px;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    padding-bottom: 50px;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--color-tone-4);
    padding-bottom: 10px;
    margin-bottom: 10px;
}

#history-panel h3 {
    margin: 0;
    font-size: 16px;
    color: var(--color-tone-2);
    text-transform: uppercase;
}

.clear-history-btn {
    background: none;
    border: none;
    color: var(--color-tone-2);
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: color 0.2s, background-color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.clear-history-btn:hover {
    color: #ff6b6b;
    background-color: rgba(255, 107, 107, 0.1);
}

.clear-history-btn:active {
    transform: scale(0.95);
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.history-table th {
    text-align: left;
    color: var(--color-tone-3);
    padding: 5px;
    font-weight: normal;
}

.history-table td {
    padding: 8px 5px;
    border-bottom: 1px solid var(--color-tone-4);
}

.history-word {
    font-family: monospace;
    font-weight: bold;
    color: var(--color-tone-1);
}

.history-tries {
    text-align: right;
    color: var(--color-correct);
}

.history-score {
    text-align: right;
    color: var(--color-present);
    font-weight: bold;
}

.history-table tr.reviewing {
    background-color: rgba(74, 144, 226, 0.15);
    border-left: 3px solid #4a90e2;
}

.history-table tr.reviewing td {
    border-bottom-color: rgba(74, 144, 226, 0.3);
}

/* Mobile Layout for Sidebar */
@media (max-width: 768px) {
    #main-layout {
        flex-direction: column;
        align-items: center;
    }

    #history-panel {
        width: 100%;
        height: 150px;
        border-left: none;
        border-top: 1px solid var(--color-tone-4);
        order: 2;
        /* Move to bottom */
    }

    #game-area {
        order: 1;
        width: 100%;
        max-width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
}

/* Square viewport constraint (e.g., iPad in square orientation) */
@media (max-width: 780px) and (max-height: 780px) {
    #game-area {
        max-height: 600px;
    }
}

/* --- Tile Styles --- */
.tile {
    width: 100%;
    aspect-ratio: 1 / 1;
    border: 2px solid var(--color-tone-4);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    font-weight: bold;
    text-transform: uppercase;
    transition: transform 0.2s, border-color 0.2s, background-color 0.2s;
}

@media (max-height: 700px) {
    .tile {
        font-size: 1.5rem;
    }
}

.tile.active {
    border-color: var(--color-tone-2);
    animation: pop 0.1s;
}

/* Checking Glow Animation */
@keyframes checking-pulse {
    0% {
        box-shadow: 0 0 5px var(--color-tone-2);
        border-color: var(--color-tone-2);
    }

    50% {
        box-shadow: 0 0 15px var(--color-present);
        border-color: var(--color-present);
    }

    100% {
        box-shadow: 0 0 5px var(--color-tone-2);
        border-color: var(--color-tone-2);
    }
}

.tile.checking {
    animation: checking-pulse 1.5s infinite;
}

.tile.correct {
    background-color: var(--color-correct);
    border-color: var(--color-correct);
}

.tile.present {
    background-color: var(--color-present);
    border-color: var(--color-present);
}

.tile.absent {
    background-color: var(--color-absent);
    border-color: var(--color-absent);
}

.tile.flip {
    animation: flip 0.5s ease forwards;
}

/* --- Scratchpad & Toolbar --- */
#toolbar {
    width: 100%;
    max-width: 500px;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 0 10px 5px 10px;
    flex-shrink: 0;
}

.tool-btn {
    background: none;
    border: none;
    color: var(--color-tone-2);
    cursor: pointer;
    padding: 5px;
    border-radius: 4px;
    transition: color 0.2s, background-color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tool-btn:hover {
    color: var(--color-tone-1);
    background-color: var(--color-tone-4);
}

.tool-btn.active {
    color: var(--color-scratchpad-border);
    background-color: rgba(74, 144, 226, 0.2);
}

.tool-btn.disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

#scratchpad-container {
    width: 100%;
    max-width: 350px;
    padding: 0 10px 10px 10px;
    display: none;
    /* Hidden by default */
    flex-shrink: 0;
}

#scratchpad-container.visible {
    display: block;
}

#scratchpad-grid {
    display: grid;
    grid-template-columns: repeat(var(--cols), 1fr);
    gap: var(--gap);
    width: 100%;
}

.scratch-tile {
    width: 100%;
    aspect-ratio: 1 / 1;
    border: 2px dashed var(--color-scratchpad-border);
    background-color: var(--color-scratchpad-bg);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    font-weight: bold;
    text-transform: uppercase;
    color: var(--color-tone-1);
}

.scratch-tile.fixed {
    background-color: var(--color-correct);
    border-style: solid;
    border-color: var(--color-correct);
}

.scratch-tile.selected {
    border-color: #ffffff;
    border-style: solid;
    box-shadow: 0 0 10px rgba(74, 144, 226, 0.6);
    animation: pulse-border 1s infinite;
}

@keyframes pulse-border {
    0%, 100% {
        border-color: #ffffff;
    }
    50% {
        border-color: var(--color-scratchpad-border);
    }
}

/* --- AI Hint Display --- */
#ai-hint-display {
    margin-bottom: 20px;
    background: linear-gradient(135deg, rgba(106, 170, 100, 0.12) 0%, rgba(106, 170, 100, 0.08) 100%);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(106, 170, 100, 0.3);
    color: var(--color-correct);
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 1.2rem;
    font-style: italic;
    z-index: 90;
    max-width: 90%;
    box-shadow: 0 2px 8px rgba(106, 170, 100, 0.2);
    text-align: center;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: opacity 0.4s ease, transform 0.4s ease, width 0.4s ease;
    opacity: 1;
}

#ai-hint-display.ai-hint-hidden {
    opacity: 0;
    transform: translateY(-10px);
    pointer-events: none;
}

#ai-hint-display.hint-loading {
    animation: hint-loading-pulse 2s ease-in-out infinite;
}

@keyframes hint-loading-pulse {
    0%, 100% {
        box-shadow: 0 2px 8px rgba(106, 170, 100, 0.2);
        border-color: rgba(106, 170, 100, 0.3);
    }
    50% {
        box-shadow: 0 0 20px rgba(106, 170, 100, 0.6), 0 0 30px rgba(106, 170, 100, 0.3);
        border-color: rgba(106, 170, 100, 0.6);
    }
}

.toggle-hint-btn {
    background: rgba(106, 170, 100, 0.15);
    border: 1px solid rgba(106, 170, 100, 0.4);
    border-radius: 6px;
    padding: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.toggle-hint-btn:hover {
    background: rgba(106, 170, 100, 0.25);
    border-color: rgba(106, 170, 100, 0.6);
}

.toggle-hint-btn:active {
    transform: scale(0.95);
}

.toggle-hint-btn svg {
    stroke: var(--color-correct);
    display: block;
}

#ai-hint-text {
    line-height: 1.4;
    flex: 1;
    transition: filter 0.3s ease;
    user-select: none;
}

#ai-hint-text.blurred {
    filter: blur(8px);
    cursor: pointer;
}

#ai-hint-text.blurred::selection {
    background: transparent;
}

.refresh-hint-btn {
    background: rgba(106, 170, 100, 0.15);
    border: 1px solid rgba(106, 170, 100, 0.4);
    border-radius: 6px;
    padding: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.refresh-hint-btn:not(:disabled):hover {
    background: rgba(106, 170, 100, 0.25);
    border-color: rgba(106, 170, 100, 0.6);
    transform: rotate(90deg);
}

.refresh-hint-btn:not(:disabled):active {
    transform: rotate(90deg) scale(0.95);
}

.refresh-hint-btn svg {
    stroke: var(--color-correct);
    display: block;
}

.refresh-hint-btn.refreshing svg {
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* --- Message Display --- */
#message-display {
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--color-tone-1);
    color: var(--color-bg);
    padding: 10px 20px;
    border-radius: 4px;
    font-weight: bold;
    z-index: 100;
    display: none;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
    text-align: center;
    width: max-content;
}

#message-display.show {
    display: block;
}

/* --- Keyboard --- */
#keyboard {
    width: 100%;
    max-width: 500px;
    padding: 5px 5px 15px 5px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}

.row {
    display: flex;
    width: 100%;
    gap: 4px;
    justify-content: center;
}

.key {
    flex: 1;
    height: 48px;
    background-color: var(--color-tone-2);
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    font-size: 13px;
    cursor: pointer;
    text-transform: uppercase;
    border: none;
    color: white;
    touch-action: manipulation;
    transition: background-color 0.1s;
}

.key#key-ENTER,
.key#key-BACKSPACE {
    flex: 1.5;
    font-size: 11px;
}

.key#key-ENTER {
    background-color: var(--color-correct);
}

.key.correct {
    background-color: var(--color-correct) !important;
}

.key.present {
    background-color: var(--color-present) !important;
}

.key.absent {
    background-color: var(--color-absent-bg) !important;
    border: 1px solid var(--color-absent-border);
    color: var(--color-absent-text);
}

/* --- Animations --- */
@keyframes pop {
    50% {
        transform: scale(1.1);
    }
}

@keyframes flip {
    0% {
        transform: rotateX(0);
    }

    50% {
        transform: rotateX(90deg);
    }

    100% {
        transform: rotateX(0);
    }
}