/**
 * Copyright (c) 2025
 * Authors: David Bueno Vallejo and David Bueno Carmona
 * Created with the assistance of Gemini and Claude Sonnet
 */

:root {
    --bg-color: #f0f2f5;
    --text-color: #1a1a1a;
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #e5e7eb;
    --secondary-text: #374151;
    --board-bg: #ffffff;
    --highlight-color: #e0e7ff;
    --error-bg: #fee2e2;
    --error-text: #ef4444;
    --success-bg: #dcfce7;
    --radius-sm: 0.375rem;
    --radius-md: 0.5rem;

    /* Dynamic Grid Variables */
    --grid-size: 9;
    --cell-size: clamp(30px, 5vw, 50px);
    --gap: 0px;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    padding: 1rem;
}

.site-backlink {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 1000;
}

.site-backlink a {
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 1rem;
    background-color: rgba(255, 255, 255, 0.95);
    color: var(--primary-color);
    text-decoration: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 0.9rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: all 0.2s ease;
}

.site-backlink a:hover {
    background-color: var(--primary-color);
    color: white;
    transform: translateX(-2px);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
}

.board-container {
    width: fit-content;
    margin: 0 auto;
}

header {
    text-align: center;
    margin-block-end: 2rem;
}

header h1 {
    font-size: 2.5rem;
    font-weight: 800;
    margin: 0;
    color: var(--primary-color);
}

header p {
    font-size: 1rem;
    color: var(--secondary-text);
    margin: 0.5rem 0;
}

.timer {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--primary-color);
    margin-block-start: 1rem;
}

.controls-top {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
    margin-block-end: 1.5rem;
    flex-wrap: wrap;
}

.difficulty-selector,
.size-selector {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

label {
    font-weight: 600;
    color: var(--secondary-text);
    font-size: 0.85rem;
}

select {
    padding: 0.4rem 0.8rem;
    border: 2px solid var(--secondary-color);
    border-radius: var(--radius-sm);
    background-color: white;
    color: var(--text-color);
    font-family: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    transition: border-color 0.2s;
}

select:hover {
    border-color: var(--primary-color);
}

select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
    font-family: inherit;
    font-size: 0.85rem;
}

.btn:active {
    transform: scale(0.98);
}

.primary {
    background-color: var(--primary-color);
    color: white;
}

.primary:hover {
    background-color: var(--primary-hover);
}

.game-controls {
    margin-block-start: 2rem;
    display: flex;
    justify-content: center;
}

.numpad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}

.num-btn,
.action-btn {
    width: var(--cell-size);
    height: var(--cell-size);
    border: none;
    border-radius: var(--radius-md);
    background-color: white;
    font-size: 1.5rem;
    font-weight: 400;
    color: #9ca3af;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-family: inherit;
}

.num-btn:hover,
.action-btn:hover {
    background-color: var(--highlight-color);
    color: var(--primary-color);
    transform: translateY(-2px);
}

.num-btn:active,
.action-btn:active {
    transform: translateY(0);
}

.action-btn {
    font-size: 0.8rem;
}

.action-btn .icon {
    font-size: 1.5rem;
    margin-block-end: 2px;
}

.action-btn .label {
    font-size: 0.7rem;
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--grid-size), var(--cell-size));
    grid-template-rows: repeat(var(--grid-size), var(--cell-size));
    gap: var(--gap);
    border: 2px solid var(--text-color);
    background-color: var(--board-bg);
    margin-block-end: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    border-right: 1px solid var(--secondary-color);
    border-bottom: 1px solid var(--secondary-color);
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s;
}

/* 6x6 Specific Borders (2x3 boxes) */
/* Vertical: Every 3rd column (3, 6) */
.board[data-size="6"] .cell:nth-child(6n+3) {
    border-right: 3px solid var(--text-color);
}

/* Horizontal: Every 2nd row (2, 4) */
/* Row 2: cells 7-12. Row 4: cells 19-24. */
.board[data-size="6"] .cell:nth-child(n+7):nth-child(-n+12),
.board[data-size="6"] .cell:nth-child(n+19):nth-child(-n+24) {
    border-bottom: 3px solid var(--text-color);
}

/* 9x9 Specific Borders (3x3 boxes) */
/* Vertical: Every 3rd column (3, 6) */
.board[data-size="9"] .cell:nth-child(9n+3),
.board[data-size="9"] .cell:nth-child(9n+6) {
    border-right: 3px solid var(--text-color);
}

/* Horizontal: Every 3rd row (3, 6) */
/* Row 3: cells 19-27. Row 6: cells 46-54. */
.board[data-size="9"] .cell:nth-child(n+19):nth-child(-n+27),
.board[data-size="9"] .cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 3px solid var(--text-color);
}

/* Remove right border from last column in each row */
.board[data-size="6"] .cell:nth-child(6n) {
    border-right: none;
}

.board[data-size="9"] .cell:nth-child(9n) {
    border-right: none;
}

/* Remove bottom border from last row */
.board[data-size="6"] .cell:nth-child(n+31) {
    border-bottom: none;
}

.board[data-size="9"] .cell:nth-child(n+73) {
    border-bottom: none;
}


/* Cell States */
.cell.fixed {
    color: var(--text-color);
    font-weight: 800;
}

.cell.user-input {
    color: var(--primary-color);
}

.cell.selected {
    background-color: var(--primary-color);
    color: white !important;
}

.cell.highlight {
    background-color: var(--highlight-color);
}

.cell.debug-hint {
    color: #aaa;
    font-style: italic;
    pointer-events: none;
}

.cell.error {
    background-color: var(--error-bg);
    color: var(--error-text);
    animation: shake 0.4s ease-in-out;
}

.cell.flash-success {
    animation: flash 0.5s ease-out;
}

/* Animations */
@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-4px);
    }

    75% {
        transform: translateX(4px);
    }
}

@keyframes flash {
    0% {
        background-color: var(--success-bg);
        color: white;
    }

    100% {
        background-color: var(--board-bg);
    }
}

/* Dialog / Modal */
dialog {
    border: none;
    border-radius: 16px;
    padding: 0;
    background: transparent;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

dialog::backdrop {
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
}

.modal-content {
    background: white;
    padding: 2.5rem;
    border-radius: 16px;
    text-align: center;
}

.modal-content h2 {
    color: var(--primary-color);
    margin-block-end: 1rem;
    font-size: 2rem;
}

.modal-content p {
    margin-block-end: 2rem;
    font-size: 1.1rem;
}

/* Fireworks Canvas */
#fireworks {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
}

.num-btn:active,
.action-btn:active {
    transform: translateY(0);
}

.action-btn {
    font-size: 0.8rem;
}

.action-btn .icon {
    font-size: 1.5rem;
    margin-block-end: 2px;
}

.action-btn .label {
    font-size: 0.7rem;
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--grid-size), var(--cell-size));
    grid-template-rows: repeat(var(--grid-size), var(--cell-size));
    gap: var(--gap);
    border: 2px solid var(--text-color);
    background-color: var(--board-bg);
    margin-block-end: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    border-right: 1px solid var(--secondary-color);
    border-bottom: 1px solid var(--secondary-color);
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s;
}

/* 6x6 Specific Borders (2x3 boxes) */
/* Vertical: Every 3rd column (3, 6) */
.board[data-size="6"] .cell:nth-child(6n+3) {
    border-right: 3px solid var(--text-color);
}

/* Horizontal: Every 2nd row (2, 4) */
/* Row 2: cells 7-12. Row 4: cells 19-24. */
.board[data-size="6"] .cell:nth-child(n+7):nth-child(-n+12),
.board[data-size="6"] .cell:nth-child(n+19):nth-child(-n+24) {
    border-bottom: 3px solid var(--text-color);
}

/* 9x9 Specific Borders (3x3 boxes) */
/* Vertical: Every 3rd column (3, 6) */
.board[data-size="9"] .cell:nth-child(9n+3),
.board[data-size="9"] .cell:nth-child(9n+6) {
    border-right: 3px solid var(--text-color);
}

/* Horizontal: Every 3rd row (3, 6) */
/* Row 3: cells 19-27. Row 6: cells 46-54. */
.board[data-size="9"] .cell:nth-child(n+19):nth-child(-n+27),
.board[data-size="9"] .cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 3px solid var(--text-color);
}

/* Remove right border from last column in each row */
.board[data-size="6"] .cell:nth-child(6n) {
    border-right: none;
}

.board[data-size="9"] .cell:nth-child(9n) {
    border-right: none;
}

/* Remove bottom border from last row */
.board[data-size="6"] .cell:nth-child(n+31) {
    border-bottom: none;
}

.board[data-size="9"] .cell:nth-child(n+73) {
    border-bottom: none;
}


/* Cell States */
.cell.fixed {
    color: var(--text-color);
    font-weight: 800;
}

.cell.user-input {
    color: var(--primary-color);
}

.cell.selected {
    background-color: var(--primary-color);
    color: white !important;
}

.cell.highlight {
    background-color: var(--highlight-color);
}

.cell.debug-hint {
    color: #aaa;
    font-style: italic;
    pointer-events: none;
}

.cell.error {
    background-color: var(--error-bg);
    color: var(--error-text);
    animation: shake 0.4s ease-in-out;
}

.cell.flash-success {
    animation: flash 0.5s ease-out;
}

/* Animations */
@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-4px);
    }

    75% {
        transform: translateX(4px);
    }
}

@keyframes flash {
    0% {
        background-color: var(--success-bg);
        color: white;
    }

    100% {
        background-color: var(--board-bg);
    }
}

/* Dialog / Modal */
dialog {
    border: none;
    border-radius: 16px;
    padding: 0;
    background: transparent;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

dialog::backdrop {
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
}

.modal-content {
    background: white;
    padding: 2.5rem;
    border-radius: 16px;
    text-align: center;
}

.modal-content h2 {
    color: var(--primary-color);
    margin-block-end: 1rem;
    font-size: 2rem;
}

.modal-content p {
    margin-block-end: 2rem;
    font-size: 1.1rem;
}

/* Fireworks Canvas */
#fireworks {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

.secondary {
    background-color: white;
    color: var(--primary-color);
    border: 2px solid var(--primary-color);
}

.secondary:hover {
    background-color: var(--highlight-color);
}

.action-buttons {
    display: flex;
    gap: 0.5rem;
}

.modal-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

/* Toast Notification */
#toast {
    visibility: hidden;
    min-width: 250px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 8px;
    padding: 16px;
    position: fixed;
    z-index: 100;
    left: 50%;
    bottom: 30px;
    transform: translateX(-50%);
    font-size: 1rem;
    opacity: 0;
    transition: opacity 0.3s, bottom 0.3s;
}

#toast.show {
    visibility: visible;
    opacity: 1;
    bottom: 50px;
}