/* ===== Layers ===== */
@layer reset, base, layout, components, utilities;

/* ===== Reset ===== */
@layer reset {
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    body { min-height: 100dvh; }
}

/* ===== Base ===== */
@layer base {
    :root {
        --c-bg:       #f5f0ff;
        --c-surface:  #ffffff;
        --c-primary:  #8b5cf6;
        --c-primary-hover: color-mix(in srgb, var(--c-primary) 85%, black);
        --c-accent:   #ec4899;
        --c-navy:     #4c1d95;
        --c-text:     #1f2937;
        --c-muted:    #6b7280;
        --c-border:   #e9d5ff;
        --c-success:  #10b981;
        --c-danger:   #ef4444;
        --c-grid-alt: #faf5ff;
        --c-solved:   #6366f1;

        --radius:     16px;
        --radius-sm:  10px;
        --shadow:     0 4px 12px rgba(139,92,246,.15);
        --shadow-lg:  0 10px 40px rgba(139,92,246,.2);
        --font:       'Segoe UI', system-ui, -apple-system, sans-serif;
        --mono:       'Cascadia Code', 'Fira Code', monospace;
        --transition: 200ms ease;

        accent-color: var(--c-primary);
    }

    body {
        font-family: var(--font);
        background: var(--c-bg);
        color: var(--c-text);
        line-height: 1.6;
        position: relative;
        overflow-x: hidden;
    }

    body::before,
    body::after {
        content: '';
        position: fixed;
        pointer-events: none;
        z-index: 0;
        opacity: 0.25;
    }

    /* Chessboard - top right (8x8 = 64 squares) */
    body::before {
        top: 100px;
        right: -30px;
        width: 160px;
        height: 160px;
        background:
            repeating-conic-gradient(var(--c-primary) 0% 25%, white 0% 50%)
            50% / 40px 40px;
        transform: rotate(20deg);
        border: 3px solid var(--c-primary);
        box-shadow: 0 10px 30px rgba(139, 92, 246, 0.2);
    }

    /* Sudoku grid - top left */
    body::after {
        top: 150px;
        left: -40px;
        width: 160px;
        height: 160px;
        background:
            linear-gradient(var(--c-border) 1.5px, transparent 1.5px),
            linear-gradient(90deg, var(--c-border) 1.5px, transparent 1.5px),
            linear-gradient(var(--c-primary) 2.5px, transparent 2.5px),
            linear-gradient(90deg, var(--c-primary) 2.5px, transparent 2.5px);
        background-size:
            17.77px 17.77px,
            17.77px 17.77px,
            53.33px 53.33px,
            53.33px 53.33px;
        transform: rotate(-18deg);
        border: 3px solid var(--c-accent);
        box-shadow: 0 10px 30px rgba(236, 72, 153, 0.2);
    }

    main {
        position: relative;
        z-index: 1;
    }

    header, footer {
        position: relative;
        z-index: 2;
    }

    a {
        color: var(--c-primary);
        text-decoration: none;

        &:hover { text-decoration: underline; }
    }

    button, select, input {
        font: inherit;
        border-radius: var(--radius-sm);
    }
}

/* ===== Layout ===== */
@layer layout {
    .container {
        max-width: 1200px;
        margin-inline: auto;
        padding-inline: 1.5rem;
    }

    .site-header {
        background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #a855f7 100%);
        color: white;
        padding: 2.5rem 0 2rem;
        text-align: left;
        position: relative;
        overflow: hidden;

        &::before {
            content: '🎮🎲🧩🎯🃏';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            font-size: 4rem;
            opacity: 0.1;
            letter-spacing: 2rem;
            transform: translateY(-50%);
            pointer-events: none;
        }

        & .container {
            position: relative;
        }

        & .site-title {
            font-size: 72px;
            font-weight: 700;
            letter-spacing: -0.02em;
            position: relative;
            display: flex;
            align-items: center;
            gap: 0;
            line-height: 1;
            justify-content: space-between;
            width: 100%;
        }

        & .lighthouse {
            display: inline-block;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 40px;
            height: 68px;
            flex-shrink: 0;
        }

        & .lighthouse::before {
            content: '';
            position: absolute;
            bottom: 0;
            left: 5px;
            right: 5px;
            height: 48px;
            background: repeating-linear-gradient(
                to bottom,
                #fff 0px, #fff 7.5px,
                #ef4444 7.5px, #ef4444 15px
            );
            clip-path: polygon(18% 0%, 82% 0%, 100% 100%, 0% 100%);
            border-bottom: 3px solid #cbd5e1;
        }

        & .lighthouse::after {
            content: '';
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 22px;
            height: 22px;
            background: radial-gradient(circle at center, #fde047 20%, #fbbf24 45%, rgba(251, 191, 36, 0.3) 70%, transparent 100%);
            border: 2px solid #475569;
            border-radius: 5px 5px 2px 2px;
            box-shadow:
                0 0 12px 6px rgba(251, 191, 36, 0.5),
                0 0 28px 14px rgba(251, 191, 36, 0.2);
            animation: lighthouse-glow 3s ease-in-out infinite;
        }

        & .site-tagline {
            opacity: 0.9;
            font-size: 1.05rem;
            margin-top: 0.3rem;
            position: relative;
        }
    }

    .menu-toggle {
        display: none;
    }

    .hamburger {
        position: absolute;
        top: -1.5rem;
        right: 1.5rem;
        width: 30px;
        height: 24px;
        cursor: pointer;
        display: none;
        flex-direction: column;
        justify-content: space-between;
        z-index: 100;

        & span {
            display: block;
            width: 100%;
            height: 3px;
            background: white;
            border-radius: 2px;
            transition: all 0.3s ease;
        }
    }

    .menu-toggle:checked ~ .hamburger span:nth-child(1) {
        transform: rotate(45deg) translate(8px, 8px);
    }

    .menu-toggle:checked ~ .hamburger span:nth-child(2) {
        opacity: 0;
    }

    .menu-toggle:checked ~ .hamburger span:nth-child(3) {
        transform: rotate(-45deg) translate(7px, -7px);
    }

    .nav-menu {
        position: absolute;
        top: -1.5rem;
        right: 1.5rem;
        display: flex;
        gap: 2rem;
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(10px);
        padding: 0.75rem 1.5rem;
        border-radius: 50px;
        transition: transform 0.3s ease;
        z-index: 99;

        & a {
            color: white;
            font-weight: 600;
            font-size: 0.95rem;
            text-decoration: none;
            transition: opacity 0.2s ease;

            &:hover {
                opacity: 0.8;
                text-decoration: none;
            }

            &.active {
                text-decoration: underline;
                text-underline-offset: 4px;
            }
        }
    }

    @media (max-width: 768px) {
        .hamburger {
            display: flex;
        }

        .nav-menu {
            flex-direction: column;
            right: 1.5rem;
            gap: 1rem;
            padding: 1rem 1.5rem;
            border-radius: var(--radius);
            transform: translateX(150%);
        }

        .menu-toggle:checked ~ .nav-menu {
            transform: translateX(0);
        }
    }

    .main-content {
        padding-block: 2rem;
        position: relative;
    }

    /* Sudoku grid - bottom right */
    .main-content::before {
        content: '';
        position: absolute;
        bottom: 15%;
        right: -50px;
        width: 140px;
        height: 140px;
        background:
            linear-gradient(var(--c-border) 1.5px, transparent 1.5px),
            linear-gradient(90deg, var(--c-border) 1.5px, transparent 1.5px),
            linear-gradient(var(--c-primary) 2px, transparent 2px),
            linear-gradient(90deg, var(--c-primary) 2px, transparent 2px);
        background-size:
            15.55px 15.55px,
            15.55px 15.55px,
            46.66px 46.66px,
            46.66px 46.66px;
        transform: rotate(-30deg);
        opacity: 0.3;
        pointer-events: none;
        border: 2px solid var(--c-accent);
        border-radius: 8px;
        box-shadow: 0 8px 24px rgba(236, 72, 153, 0.15);
    }

    /* Chessboard - bottom left (8x8 = 64 squares) */
    .main-content::after {
        content: '';
        position: absolute;
        bottom: 20%;
        left: -45px;
        width: 128px;
        height: 128px;
        background:
            repeating-conic-gradient(#8b5cf6 0% 25%, #f5f0ff 0% 50%)
            50% / 32px 32px;
        transform: rotate(25deg);
        opacity: 0.28;
        pointer-events: none;
        border: 2px solid var(--c-primary);
        border-radius: 10px;
        box-shadow: 0 8px 24px rgba(139, 92, 246, 0.15);
    }

    .app-layout {
        display: grid;
        grid-template-columns: 320px 1fr;
        gap: 2rem;
        align-items: start;

        @container (max-width: 768px) {
            grid-template-columns: 1fr;
        }
    }

    @media (max-width: 768px) {
        .app-layout {
            grid-template-columns: 1fr;
        }
    }

    .sidebar {
        position: sticky;
        top: 1rem;
    }

    .history-section {
        margin-top: 2.5rem;
    }

    .site-footer {
        text-align: center;
        padding: 1.5rem 0;
        color: var(--c-muted);
        font-size: 0.9rem;
        border-top: 1px solid var(--c-border);
        margin-top: 2rem;
    }
}

/* ===== Component Shared ===== */
@layer components {
    .card {
        background: var(--c-surface);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        padding: 1.5rem;
    }

    .card-title {
        font-size: 1.1rem;
        font-weight: 600;
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        padding: 0.65rem 1.25rem;
        border: none;
        border-radius: var(--radius-sm);
        font-weight: 600;
        cursor: pointer;
        transition: all var(--transition);
        font-size: 0.95rem;
    }

    .btn-primary {
        background: var(--c-primary);
        color: white;

        &:hover:not(:disabled) {
            background: var(--c-primary-hover);
            transform: translateY(-1px);
            box-shadow: var(--shadow);
        }

        &:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
    }

    .btn-danger {
        background: var(--c-danger);
        color: white;

        &:hover:not(:disabled) {
            background: color-mix(in srgb, var(--c-danger) 85%, black);
        }
    }

    .btn-outline {
        background: transparent;
        border: 2px solid var(--c-primary);
        color: var(--c-primary);

        &:hover:not(:disabled) {
            background: var(--c-primary);
            color: white;
        }
    }

    .btn-sm {
        padding: 0.4rem 0.8rem;
        font-size: 0.85rem;
    }

    .btn-block {
        width: 100%;
    }

    .form-group {
        margin-bottom: 1rem;

        & label {
            display: block;
            font-weight: 600;
            font-size: 0.88rem;
            margin-bottom: 0.3rem;
            color: var(--c-text);
        }

        & select, & input {
            width: 100%;
            padding: 0.55rem 0.75rem;
            border: 2px solid var(--c-border);
            background: var(--c-surface);
            transition: border-color var(--transition);

            &:focus {
                border-color: var(--c-primary);
            }
            &:focus-visible {
                outline: 3px solid var(--c-primary);
                outline-offset: 1px;
            }
        }
    }

    .badge {
        display: inline-block;
        padding: 0.2rem 0.6rem;
        border-radius: 50px;
        font-size: 0.75rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.03em;
    }

    .badge-easy   { background: #d1fae5; color: #065f46; }
    .badge-medium { background: #fef3c7; color: #92400e; }
    .badge-hard   { background: #fecaca; color: #991b1b; }
    .badge-expert { background: #e9d5ff; color: #6b21a8; }
    .badge-mixed  { background: linear-gradient(90deg, #d1fae5, #fef3c7, #fecaca, #e9d5ff); color: #4c1d95; }

    .empty-state {
        text-align: center;
        padding: 3rem 1rem;
        color: var(--c-muted);

        & .empty-icon {
            font-size: 3rem;
            margin-bottom: 0.5rem;
        }
    }

    .hero {
        text-align: center;
        padding: 4rem 2rem;
        background: var(--c-surface);
        border-radius: var(--radius);
        box-shadow: var(--shadow-lg);
        margin-top: 2rem;

        & h2 {
            font-size: clamp(1.8rem, 4vw, 2.5rem);
            color: var(--c-primary);
            margin-bottom: 1rem;
        }

        & p {
            font-size: 1.2rem;
            color: var(--c-muted);
            margin-bottom: 2rem;
        }

        & .btn {
            font-size: 1.1rem;
            padding: 1rem 2rem;
        }
    }

    .about-section {
        max-width: 800px;
        margin: 2rem auto;

        & h2 {
            font-size: 2rem;
            color: var(--c-primary);
            margin-bottom: 1rem;
        }

        & h3 {
            font-size: 1.4rem;
            color: var(--c-navy);
            margin-top: 2rem;
            margin-bottom: 0.75rem;
        }

        & p {
            color: var(--c-text);
            line-height: 1.8;
            margin-bottom: 1rem;
        }

        & ul {
            list-style: none;
            padding-left: 0;
            margin: 1rem 0 1.5rem 0;

            & li {
                padding: 0.5rem 0;
                font-size: 1.05rem;
            }
        }
    }
}

/* ===== Utilities ===== */
@layer utilities {
    .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0,0,0,0);
        white-space: nowrap;
    }

    /* Skip-to-main link */
    .skip-link {
        position: absolute;
        top: -100%;
        left: 1rem;
        background: var(--c-primary);
        color: #fff;
        padding: 0.75rem 1.5rem;
        border-radius: 0 0 var(--radius-sm) var(--radius-sm);
        font-weight: 700;
        z-index: 1000;
        text-decoration: none;
    }
    .skip-link:focus {
        top: 0;
    }

    /* Global focus indicator */
    *:focus-visible {
        outline: 3px solid var(--c-primary);
        outline-offset: 2px;
    }

    .mt-1 { margin-top: 0.5rem; }
    .mt-2 { margin-top: 1rem; }
    .gap-1 { gap: 0.5rem; }

    @keyframes lighthouse-glow {
        0%, 100% {
            opacity: 1;
            box-shadow:
                0 0 8px 4px rgba(251, 191, 36, 0.5),
                0 0 20px 10px rgba(251, 191, 36, 0.2);
        }
        50% {
            opacity: 0.5;
            box-shadow:
                0 0 4px 2px rgba(251, 191, 36, 0.3),
                0 0 10px 5px rgba(251, 191, 36, 0.1);
        }
    }

    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(8px); }
        to   { opacity: 1; transform: translateY(0); }
    }

    @keyframes pulse {
        0%, 100% { opacity: 1; }
        50%      { opacity: 0.6; }
    }

    @keyframes progress-stripe {
        0%   { background-position: 1rem 0; }
        100% { background-position: 0 0; }
    }
}
