:root {
    --bg-color: #f0f2f5;
    --text-color: #1e293b;
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #94a3b8;
    --accent-color: #ef4444;
    /* For errors or crosses */
    --success-color: #22c55e;

    --grid-line-color: #cbd5e1;
    --grid-thick-line-color: #64748b;
    --cell-empty-bg: #ffffff;
    --cell-filled-bg: #334155;
    --cell-hover-bg: #e2e8f0;

    --radius-lg: 16px;
    --radius-md: 8px;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

body.dark-mode {
    --bg-color: #0f172a;
    --text-color: #f1f5f9;
    --primary-color: #818cf8;
    --primary-hover: #6366f1;
    --secondary-color: #475569;
    --grid-line-color: #334155;
    --grid-thick-line-color: #94a3b8;
    --cell-empty-bg: #1e293b;
    --cell-filled-bg: #f8fafc;
    /* Inverted for high contrast */
    --cell-hover-bg: #334155;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Outfit', sans-serif;
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

/* Global Layout Lock */
html,
body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    /* Prevent scrolling completely */
}

body {
    background-color: var(--bg-color);
    /* Fallback */
    /* Dynamic Geometric Background */
    background-image:
        radial-gradient(circle at 10% 20%, rgba(99, 102, 241, 0.15) 0%, transparent 20%),
        radial-gradient(circle at 90% 80%, rgba(239, 68, 68, 0.15) 0%, transparent 20%),
        linear-gradient(45deg, rgba(148, 163, 184, 0.05) 25%, transparent 25%, transparent 75%, rgba(148, 163, 184, 0.05) 75%, rgba(148, 163, 184, 0.05)),
        linear-gradient(45deg, rgba(148, 163, 184, 0.05) 25%, transparent 25%, transparent 75%, rgba(148, 163, 184, 0.05) 75%, rgba(148, 163, 184, 0.05));
    background-position: 0 0, 0 0, 0 0, 20px 20px;
    background-size: 100% 100%, 100% 100%, 40px 40px, 40px 40px;
    animation: bgScroll 30s linear infinite alternate;

    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

@keyframes bgScroll {
    0% {
        background-position: 0% 0%, 100% 100%, 0 0, 20px 20px;
    }

    100% {
        background-position: 20% 10%, 80% 90%, 80px 80px, 100px 100px;
    }
}

/* App Container - Flex Column */
.app-container {
    width: 100%;
    height: 100%;
    max-width: 800px;
    margin: 0 auto;
    padding: 0.5rem;
    /* Minimal padding */
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    gap: 0.5rem;
}

/* Header - Compact (Mobile Default) */
.game-header {
    flex: 0 0 auto;
    width: 100%;
    display: flex;
    flex-direction: row;
    /* Single line forced */
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    background: var(--cell-empty-bg);
    padding: 0.75rem 1rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}

.header-controls {
    width: auto;
    display: flex;
    justify-content: flex-end;
    gap: 0.8rem;
}

.header-content h1 {
    font-size: 1.1rem;
    /* Slightly smaller to fit on one line */
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0px;
    white-space: nowrap;
}

.header-content p {
    font-size: 0.75rem;
    color: var(--secondary-color);
    margin: 0;
    display: none;
    /* Hide subtitle on small screens/fit mode to save space */
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-color);
    font-size: 1.25rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
    transition: all 0.2s;
}

.icon-btn:hover {
    background-color: var(--cell-hover-bg);
}

/* Game Board Area - The Flexible Filler */
.game-board-area {
    flex: 1 1 auto;
    /* Grow to fill space */
    min-height: 0;
    /* Crucial for scrolling/fitting inside flex */
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    /* Clip anything too big */
    width: 100%;
    padding: 0;
    margin: 0;
    background: transparent;
    box-shadow: none;
    border-radius: 0;
}

.game-container {
    /* Fit logic: Scale based on available space */
    /* We limit the width/height to ensure it fits in the flexible game-board-area */
    /* Mobile/Default: Use viewport width mainly, but constrained by height */
    --game-size: min(95vw, 60vh);
    /* Fallback */

    width: var(--game-size);
    /* Ensure it never exceeds the flex parent's height */
    max-height: 100%;
    max-width: 100%;

    /* Center in parent */
    margin: auto;

    display: grid;
    gap: 0;
    /* Grid Definition driven by JS variables */
    grid-template-columns: max-content repeat(var(--col-count), 1fr);
    grid-template-rows: max-content repeat(var(--row-count), 1fr);

    border: 2px solid var(--grid-thick-line-color);
    background-color: var(--grid-line-color);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

/* Clues */
.clue-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    /* Increased font size for readability */
    font-size: clamp(0.9rem, 3vmin, 1.4rem);
    font-weight: 800;
    /* Extra bold */
    color: #2d3748;
    padding: 4px;
    background-color: #fee6c5;
    border: 1px solid #dcbfa0;
    /* Ensure they don't get crushed */
    min-width: 1.5rem;
    min-height: 1.5rem;
}

.clue-row {
    flex-direction: row;
    justify-content: flex-end;
    gap: 0.6rem;
    padding-right: 0.8rem;
    border-right: 2px solid var(--grid-thick-line-color);
}

.clue-col {
    flex-direction: column;
    justify-content: flex-end;
    gap: 0.3rem;
    padding-bottom: 0.8rem;
    border-bottom: 2px solid var(--grid-thick-line-color);
}

/* Top-left spacer styling */
.game-container>div:first-child {
    background-color: #fff8ee;
    border-right: 2px solid var(--grid-thick-line-color);
    border-bottom: 2px solid var(--grid-thick-line-color);
}

.clue-cell span {
    display: inline-block;
}

/* The actual board grid */
.cell {
    background-color: #ffffff;
    cursor: pointer;
    position: relative;
    border: 1px solid var(--grid-line-color);
    transition: background-color 0.05s;
    /* Responsive sizing */
    width: auto;
    height: auto;
    aspect-ratio: 1 / 1;
}

/* Cells on the far right / bottom borders fix (optional: usually grid handles this, but we want internal borders) */

.cell:hover {
    background-color: #f1f5f9;
}

.cell.filled {
    background-color: #64748b;
    /* Dark grey filled */
    border-color: #475569;
}

/* Cross styling (X) */
.cell.crossed::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60%;
    height: 60%;
    background-image:
        linear-gradient(45deg, transparent 45%, var(--accent-color) 45%, var(--accent-color) 55%, transparent 55%),
        linear-gradient(-45deg, transparent 45%, var(--accent-color) 45%, var(--accent-color) 55%, transparent 55%);
    transform: translate(-50%, -50%);
    opacity: 0.8;
}

/* Footer Controls (Mobile Default) */
.game-controls {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background: var(--cell-empty-bg);
    padding: 0.75rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}

.tools {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    width: 100%;
}

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    width: 100%;
}

.actions .action-btn:last-child {
    grid-column: span 2;
}

.tool-btn,
.action-btn {
    padding: 0.8rem;
    font-size: 1rem;
    justify-content: center;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border: 1px solid var(--grid-line-color);
    background: transparent;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-weight: 500;
    color: var(--text-color);
    transition: all 0.2s;
}

.tool-btn.active {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
    transform: translateY(-2px);
    font-weight: 700;
}

.action-btn {
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius-md);
    border: none;
    cursor: pointer;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: transform 0.1s;
}

.action-btn:active {
    transform: scale(0.98);
}

.action-btn.secondary {
    background-color: var(--cell-hover-bg);
    color: var(--text-color);
}

.action-btn.primary {
    background-color: var(--primary-color);
    color: white;
}

.action-btn.primary:hover {
    background-color: var(--primary-hover);
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
    opacity: 1;
    transition: opacity 0.3s;
}

.modal-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal {
    background: var(--cell-empty-bg);
    padding: 2rem;
    border-radius: var(--radius-lg);
    text-align: center;
    box-shadow: var(--shadow-lg);
    max-width: 400px;
    width: 90%;
    transform: translateY(0);
    transition: transform 0.3s;
    max-height: 85vh;
    /* Fit on mobile screen */
    display: flex;
    flex-direction: column;
    position: relative;
}

.modal-overlay.hidden .modal {
    transform: translateY(20px);
}

.modal-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
    color: var(--success-color);
    flex: 0 0 auto;
}

.modal-icon.warning {
    color: #f59e0b;
    /* Amber/Orange */
}

.modal-icon.error {
    color: var(--accent-color);
    /* Red */
}

.modal h2 {
    margin-bottom: 0.5rem;
    color: var(--text-color);
    flex: 0 0 auto;
}

.modal p {
    color: var(--secondary-color);
    margin-bottom: 1.5rem;
    flex: 0 0 auto;
}

/* Level Badge */
.level-badge {
    background-color: var(--primary-color);
    color: white;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 600;
    margin-right: 0.5rem;
    display: inline-block;
}

/* Modal Actions */
.modal-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
    width: 100%;
    flex: 0 0 auto;
    margin-top: auto;
    /* Push to bottom if space permits */
}

.modal.help-modal {
    text-align: left;
    max-width: 600px;
    border: 2px solid var(--grid-thick-line-color);
    padding: 1.5rem;
    /* Reduced padding for mobile */
}

.help-content {
    overflow-y: auto;
    flex: 1 1 auto;
    padding-right: 0.5rem;
    /* Space for scrollbar */
    margin-top: 1rem;
    /* Custom Scrollbar for webkit */
    scrollbar-width: thin;
    scrollbar-color: var(--primary-color) var(--cell-empty-bg);
}

.help-content::-webkit-scrollbar {
    width: 6px;
}

.help-content::-webkit-scrollbar-thumb {
    background-color: var(--primary-color);
    border-radius: 3px;
}

.close-icon-btn {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: transparent;
    border: none;
    font-size: 1.5rem;
    color: var(--secondary-color);
    cursor: pointer;
}

.help-content {
    margin-top: 1rem;
    color: var(--text-color);
    line-height: 1.6;
}

.help-content ul {
    list-style: none;
    padding: 0;
    margin-top: 1rem;
}

.help-content li {
    margin-bottom: 0.8rem;
    display: flex;
    align-items: center;
    gap: 0.8rem;
}

.help-content i {
    color: var(--primary-color);
    width: 20px;
    text-align: center;
}

/* Responsiveness */
/* Desktop Overrides */
@media (min-width: 769px) {
    .app-container {
        justify-content: space-between;
    }

    .game-header {
        flex-direction: row;
        justify-content: space-between;
        text-align: left;
        padding: 0.5rem 1rem;
    }

    .header-controls {
        width: auto;
        justify-content: flex-end;
    }

    .game-controls {
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: 1rem;
    }

    .tools {
        display: flex;
        width: auto;
        gap: 0.5rem;
    }

    .actions {
        display: flex;
        width: auto;
        gap: 0.5rem;
    }

    .actions .action-btn:last-child {
        grid-column: auto;
    }

    .tool-btn,
    .action-btn {
        width: auto;
        padding: 0.6rem 1.2rem;
    }
}

/* Side-by-Side Layout for Wide but Short Screens (e.g., Laptops) */
/* Side-by-Side Layout for Wide but Short Screens (e.g., Laptops) */
@media (min-width: 900px) and (max-height: 850px) {
    .app-container {
        display: grid;
        grid-template-columns: 1fr 260px;
        /* Board takes space, fixed sidebar */
        grid-template-rows: auto 1fr;
        grid-template-areas:
            "board header"
            "board controls";
        gap: 1rem;
        padding: 1rem;
        max-width: 1400px;
        align-items: start;
        overflow: hidden;
        /* Prevent scroll */
    }

    /* Assign Grid Areas */
    .game-board-area {
        grid-area: board;
        width: 100%;
        height: 100%;
        align-items: center;
        justify-content: center;
    }

    .game-header {
        grid-area: header;
        flex-direction: column;
        width: 100%;
        padding: 1rem;
        gap: 1rem;
        align-self: end;
        /* Push to bottom of its cell if needed */
    }

    .game-controls {
        grid-area: controls;
        flex-direction: column;
        width: 100%;
        padding: 1rem;
        gap: 0.8rem;
        overflow-y: auto;
        /* Allow scroll if controls get too tall */
        justify-content: flex-start;
    }

    /* Sidebar Internal Styling */
    .header-content h1 {
        font-size: 1.2rem;
        /* Smaller Title */
    }

    .header-controls {
        width: 100%;
        justify-content: center;
    }

    .tools,
    .actions {
        flex-direction: column;
        width: 100%;
        gap: 0.5rem;
    }

    .actions .action-btn:last-child {
        grid-column: auto;
    }

    .tool-btn,
    .action-btn {
        width: 100%;
        justify-content: center;
        padding: 0.6rem;
        font-size: 0.9rem;
        /* Compact Font */
    }

    /* Game Board Sizing */
    .game-container {
        /* Relax vertical constraint but leave space for padding (was 90vh) */
        --game-size: min(90vw, 82vh);
    }

    .game-board-area {
        padding-bottom: 1rem;
        /* Extra safety buffer */
    }
}

/* Aggressive Vertical Shrink for Short Mobile Screens */
/* Aggressive Vertical Shrink for Short Mobile Screens */
@media (max-height: 720px) and (max-width: 899px) {
    .app-container {
        padding: 0.25rem;
        gap: 0.25rem;
    }

    .game-header {
        flex-direction: row;
        /* Single line */
        justify-content: space-between;
        align-items: center;
        padding: 0.25rem 0.5rem;
        gap: 0.5rem;
        min-height: auto;
    }

    .header-content h1 {
        font-size: 1rem;
        margin: 0;
        white-space: nowrap;
        /* Prevent wrap */
    }

    .header-controls {
        width: auto;
        /* Take only needed space */
        gap: 0.4rem;
        margin-top: 0;
        justify-content: flex-end;
    }

    .level-badge {
        padding: 0.1rem 0.5rem;
        font-size: 0.75rem;
    }

    .icon-btn {
        padding: 0.2rem;
        font-size: 0.9rem;
    }

    .game-controls {
        padding: 0.25rem;
        gap: 0.25rem;
    }

    .tools,
    .actions {
        gap: 0.25rem;
    }

    .tool-btn,
    .action-btn {
        padding: 0.3rem 0.2rem;
        /* Vertical compress */
        font-size: 0.8rem;
        min-height: 0;
        gap: 0.25rem;
    }

    .game-board-area {
        padding: 0;
        margin: 0 !important;
    }

    .game-container {
        /* Force resize if aspect ratio pushes height */
        /* width calculation needs to consider height constraint explicitly */
        /* Critically reduced to 50vh to guarantee fit with controls */
        --game-size: min(98vw, 50vh);
    }
}