/* Base Styles & Variables */
:root {
    --primary-color: #4CAF50; /* Green for main actions */
    --secondary-color: #6c757d; /* Gray for secondary actions */
    --accent-color: #FFD700; /* Gold for highlights/stars */
    --text-color: #333;
    --background-color: #f4f7f6;
    --card-background: #ffffff;
    --border-color: #e0e0e0;
    --input-border: #ccc;
    --hover-bg: #f0f0f0;
    --button-primary-bg: #4CAF50;
    --button-primary-text: #ffffff;
    --button-secondary-bg: #f8f9fa;
    --button-secondary-text: #333;
    --message-success: #28a745;
    --message-error: #dc3545;
    --toggle-bg: #ccc;
    --toggle-circle: #ffffff;
    --footer-bg: #e9ecef;

    /* Dark Mode Variables (will be overridden by :has selector) */
    --dark-text-color: #e0e0e0;
    --dark-background-color: #2c3e50; /* Dark blue/gray */
    --dark-card-background: #34495e;
    --dark-border-color: #444;
    --dark-input-border: #666;
    --dark-hover-bg: #444;
    --dark-button-primary-bg: #2ecc71; /* Lighter green for dark mode */
    --dark-button-primary-text: #ffffff;
    --dark-button-secondary-bg: #4a667f;
    --dark-button-secondary-text: #e0e0e0;
    --dark-message-success: #27ae60;
    --dark-message-error: #e74c3c;
    --dark-toggle-bg: #555;
    --dark-toggle-circle: #ffffff;
    --dark-footer-bg: #212529;
}

/* Dark Mode styles triggered by the checkbox */
body:has(.theme-toggle-checkbox:checked) {
    --text-color: var(--dark-text-color);
    --background-color: var(--dark-background-color);
    --card-background: var(--dark-card-background);
    --border-color: var(--dark-border-color);
    --input-border: var(--dark-input-border);
    --hover-bg: var(--dark-hover-bg);
    --button-primary-bg: var(--dark-button-primary-bg);
    --button-primary-text: var(--dark-button-primary-text);
    --button-secondary-bg: var(--dark-button-secondary-bg);
    --button-secondary-text: var(--dark-button-secondary-text);
    --message-success: var(--dark-message-success);
    --message-error: var(--dark-message-error);
    --toggle-bg: var(--dark-toggle-bg);
    --toggle-circle: var(--dark-toggle-circle);
    --footer-bg: var(--dark-footer-bg);
}

body {
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
    background-color: var(--background-color);
    color: var(--text-color);
    transition: background-color 0.3s ease, color 0.3s ease;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    scroll-behavior: smooth; /* For back-to-top button */
}

/* Typography */
h1, h2, h3 {
    color: var(--primary-color);
    font-weight: 600;
}

p {
    line-height: 1.6;
}

/* Main Header */
.main-header {
    background-color: var(--card-background);
    padding: 15px 30px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 1000;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1200px;
    margin: 0 auto;
}

.game-title {
    margin: 0;
    font-size: 1.8em;
    display: flex;
    align-items: center;
    color: var(--text-color); /* Title color adapts to theme */
}

.icon-container {
    display: flex;
    align-items: center;
    margin-right: 10px;
}

.game-icon {
    width: 35px;
    height: 35px;
    fill: var(--primary-color); /* Icon color */
    animation: bounce 2s infinite ease-in-out; /* Simple bounce animation */
}

@keyframes bounce {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-5px);
    }
}

.game-year {
    font-size: 0.7em;
    margin-left: 5px;
    opacity: 0.7;
}

/* Theme Toggle Switch */
.theme-toggle-checkbox {
    display: none; /* Hide the actual checkbox */
}

.theme-switch {
    width: 50px;
    height: 25px;
    background-color: var(--toggle-bg); /* Default background (light mode) */
    border-radius: 25px;
    position: relative;
    cursor: pointer;
    transition: background-color 0.3s ease;
    display: flex; /* For centering the icon */
    align-items: center;
    padding: 2px; /* Inner padding for the circle to sit in */
}

.theme-switch-icon {
    width: 21px;
    height: 21px;
    border-radius: 50%;
    background-color: var(--toggle-circle); /* Circle color */
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.1em;
    transition: transform 0.3s ease, background-color 0.3s ease;
}

/* When checkbox is checked (Dark Mode) */
body:has(.theme-toggle-checkbox:checked) .theme-switch {
    background-color: var(--dark-button-primary-bg); /* Change track color for dark mode */
}

body:has(.theme-toggle-checkbox:checked) .theme-switch-icon {
    transform: translateX(25px); /* Move circle to right */
    background-color: var(--dark-toggle-circle); /* Make the circle white in dark mode for contrast */
}

/* Game Container */
.game-container {
    flex-grow: 1; /* Allows main content to fill space */
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    max-width: 900px;
    margin: 20px auto;
    background-color: var(--card-background);
    border-radius: 8px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
}

.difficulty-select, .game-info {
    width: 100%;
    max-width: 500px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.difficulty-select label, .game-info .timer {
    font-size: 1.1em;
    font-weight: 600;
}

#difficulty {
    padding: 8px 12px;
    border: 1px solid var(--input-border);
    border-radius: 5px;
    background-color: var(--background-color);
    color: var(--text-color);
    font-family: 'Poppins', sans-serif;
    font-size: 1em;
    cursor: pointer;
    transition: border-color 0.3s ease;
}

#difficulty:focus {
    outline: none;
    border-color: var(--primary-color);
}

.messages {
    min-height: 20px; /* Reserve space for messages */
    text-align: center;
    font-weight: 600;
    margin-bottom: 15px;
}

.messages.success {
    color: var(--message-success);
}

.messages.error {
    color: var(--message-error);
}

/* Sudoku Board */
.sudoku-board {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    width: 100%;
    max-width: 500px; /* Max width for square board */
    aspect-ratio: 1 / 1; /* Keep it square */
    border: 3px solid var(--text-color);
    margin-bottom: 30px;
}

.sudoku-cell {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid var(--border-color);
    font-size: 1.8em;
    font-family: 'Roboto Mono', monospace; /* Monospace for numbers */
    background-color: var(--card-background);
    position: relative; /* For error highlighting */
    box-sizing: border-box; /* Include padding and border in element's total width and height */
}

/* Thicker borders for 3x3 blocks */
.sudoku-cell:nth-child(3n) {
    border-right: 3px solid var(--text-color);
}
.sudoku-cell:nth-child(9n) {
    border-right: 1px solid var(--border-color); /* Reset last column */
}
.sudoku-cell:nth-child(n) {
    border-top: 1px solid var(--border-color);
}
.sudoku-cell:nth-child(n + 19):nth-child(-n + 27), /* 3rd row block bottom */
.sudoku-cell:nth-child(n + 46):nth-child(-n + 54) { /* 6th row block bottom */
    border-bottom: 3px solid var(--text-color);
}

/* Input styles for editable cells */
.sudoku-cell input {
    width: 90%;
    height: 90%;
    border: none;
    text-align: center;
    font-size: inherit;
    font-family: inherit;
    background-color: transparent;
    color: var(--text-color);
    padding: 0;
    box-sizing: border-box;
    -moz-appearance: textfield; /* Hide arrows for number inputs in Firefox */
}

/* Hide arrows for number inputs in Webkit browsers */
.sudoku-cell input::-webkit-outer-spin-button,
.sudoku-cell input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.sudoku-cell.prefilled {
    background-color: var(--hover-bg); /* Slightly different background for prefilled */
    font-weight: 700;
    color: var(--text-color);
    cursor: default;
}

.sudoku-cell.selected {
    background-color: var(--primary-color);
    color: var(--button-primary-text);
}

.sudoku-cell.error {
    background-color: var(--message-error);
    color: var(--button-primary-text);
    /* Animation for error */
    animation: shake 0.3s ease-in-out;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-5px); }
    40%, 80% { transform: translateX(5px); }
}

.sudoku-cell.highlight {
    background-color: rgba(76, 175, 80, 0.3); /* Lighter primary color for highlight */
}

.sudoku-cell.success-flash {
    animation: successFlash 1s forwards;
}

@keyframes successFlash {
    0% { background-color: var(--message-success); }
    100% { background-color: var(--card-background); }
}


/* Controls */
.controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 15px;
    margin-bottom: 30px;
    width: 100%;
    max-width: 500px;
}

.btn {
    padding: 12px 25px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1.1em;
    font-weight: 600;
    transition: background-color 0.3s ease, color 0.3s ease, transform 0.1s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.btn-primary {
    background-color: var(--button-primary-bg);
    color: var(--button-primary-text);
}

.btn-primary:hover {
    background-color: color-mix(in srgb, var(--button-primary-bg) 80%, black);
    transform: translateY(-2px);
}

.btn-secondary {
    background-color: var(--button-secondary-bg);
    color: var(--button-secondary-text);
    border: 1px solid var(--input-border);
}

.btn-secondary:hover {
    background-color: var(--hover-bg);
    transform: translateY(-2px);
}

.btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Scorecard & Leaderboard */
.scorecard, .leaderboard-section, .customization-section {
    width: 100%;
    max-width: 500px;
    background-color: var(--card-background);
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    margin-top: 20px;
}

.scorecard h2, .leaderboard-section h2, .customization-section h2 {
    margin-top: 0;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 20px;
}

.scorecard p {
    margin: 10px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.scorecard p span {
    font-weight: 600;
    color: var(--primary-color);
}
.scorecard .btn {
    margin-top: 15px;
    width: auto;
}

.leaderboard-list {
    list-style: none;
    padding: 0;
}
.leaderboard-list li {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px dashed var(--border-color);
}
.leaderboard-list li:last-child {
    border-bottom: none;
}
.leaderboard-empty-message {
    font-style: italic;
    opacity: 0.7;
    text-align: center;
}

/* Customizations */
.customization-section {
    display: flex;
    flex-direction: column;
    gap: 15px;
}
.font-style-select, .font-size-select {
    display: flex;
    align-items: center;
    gap: 10px;
}
#font-style {
    flex-grow: 1;
}
#font-size {
    flex-grow: 1;
    -webkit-appearance: none;
    appearance: none;
    height: 8px;
    background: var(--input-border);
    outline: none;
    border-radius: 5px;
    opacity: 0.7;
    transition: opacity .2s;
}
#font-size::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#font-size::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#font-size-value {
    min-width: 50px;
    text-align: right;
    font-weight: 600;
}


/* Modal Overlay */
.modal-overlay {
    visibility: hidden;
    opacity: 0;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 1001; /* Above header */
}

.modal-overlay.active {
    visibility: visible;
    opacity: 1;
}

.modal-content {
    background-color: var(--card-background);
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    text-align: center;
    max-width: 400px;
    width: 90%;
    position: relative;
    transform: translateY(-20px);
    opacity: 0;
    transition: transform 0.3s ease, opacity 0.3s ease;
}

.modal-overlay.active .modal-content {
    transform: translateY(0);
    opacity: 1;
}

.modal-close {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 2em;
    color: var(--text-color);
    text-decoration: none;
    line-height: 1;
    transition: color 0.2s ease;
}

.modal-close:hover {
    color: var(--message-error);
}

.modal-content h3 {
    margin-top: 0;
    color: var(--primary-color);
    font-size: 1.5em;
}

.modal-content p {
    margin-bottom: 25px;
}

.modal-buttons {
    display: flex;
    justify-content: center;
    gap: 15px;
}

/* Footer */
.main-footer {
    background-color: var(--footer-bg);
    padding: 30px 20px 10px;
    color: var(--text-color);
    margin-top: 40px;
    box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
}

.footer-content {
    display: flex;
    flex-direction: column; /* Stack by default */
    align-items: center;
    gap: 20px;
    max-width: 1200px;
    margin: 0 auto;
    text-align: center;
}

.made-with-love {
    font-size: 1.1em;
    opacity: 0.8;
    display: flex;
    align-items: center;
    gap: 5px;
}

.heartbeat-icon {
    font-size: 1.4em;
    display: inline-block;
    animation: heartbeat 1.5s infinite ease-in-out;
    color: #e74c3c; /* Red color for the heart */
}

@keyframes heartbeat {
    0% { transform: scale(1); }
    20% { transform: scale(1.15); }
    40% { transform: scale(1); }
    60% { transform: scale(1.10); }
    80% { transform: scale(1); }
    100% { transform: scale(1); }
}

.footer-links {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

.footer-logo {
    font-size: 1.6em;
    font-weight: 700;
    text-decoration: none;
    color: var(--primary-color);
}

.footer-nav-links {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
}

.footer-nav-links a, .social-media a {
    color: var(--text-color);
    text-decoration: none;
    transition: color 0.3s ease;
}

.footer-nav-links a:hover, .social-media a:hover {
    color: var(--primary-color);
}

.social-media {
    display: flex;
    gap: 20px;
    margin-top: 15px;
}

.social-icon {
    width: 28px;
    height: 28px;
    fill: var(--text-color);
    transition: fill 0.3s ease;
}

.social-media a:hover .social-icon {
    fill: var(--primary-color);
}

.back-to-top {
    position: sticky;
    bottom: 20px;
    left: calc(100% - 70px); /* Adjust based on button size */
    background-color: var(--primary-color);
    color: var(--button-primary-text);
    border: none;
    border-radius: 50%;
    width: 45px;
    height: 45px;
    font-size: 1.8em;
    line-height: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    transition: background-color 0.3s ease, transform 0.2s ease;
    text-decoration: none; /* For the anchor inside button */
    padding: 0;
    z-index: 999;
}

.back-to-top:hover {
    background-color: color-mix(in srgb, var(--primary-color) 80%, black);
    transform: translateY(-3px);
}

.back-to-top-link {
    color: inherit;
    text-decoration: none;
}


/* Media Queries for Responsiveness */
@media (min-width: 768px) {
    .main-header {
        padding: 15px 50px;
    }

    .game-title {
        font-size: 2.2em;
    }

    .game-container {
        padding: 30px;
        margin: 40px auto;
    }

    .difficulty-select, .game-info, .controls,
    .scorecard, .leaderboard-section, .customization-section {
        max-width: 600px; /* Wider on larger screens */
    }

    .sudoku-board {
        max-width: 600px; /* Keep board square and a bit larger */
    }

    .controls {
        justify-content: space-between; /* Spread buttons out */
    }

    .btn {
        flex-grow: 1; /* Allow buttons to grow and fill space */
        max-width: 180px; /* Max width for individual buttons */
    }

    /* Footer on larger screens */
    .footer-content {
        flex-direction: row; /* Layout in a row */
        justify-content: space-between;
        text-align: left;
        align-items: flex-start; /* Align items to the top */
    }

    .made-with-love {
        order: 2; /* Put love message in the middle */
        margin-top: 0;
    }

    .footer-links {
        align-items: flex-start; /* Align logo and nav links to start */
        order: 1;
    }

    .footer-nav-links {
        flex-direction: column; /* Stack nav links vertically */
        align-items: flex-start;
        gap: 8px;
    }

    .social-media {
        order: 3;
        margin-top: 0;
    }

    .back-to-top {
        left: unset; /* Remove left positioning */
        right: 20px; /* Position to the right */
        bottom: 20px;
    }
}