/* === CSS4: @layer - Cascade Layers === */
@layer reset, theme, layout, components, utilities;

/* === CSS4: @property - Typed Custom Properties === */
@property --glow-opacity {
    syntax: "<number>";
    initial-value: 0;
    inherits: false;
}

/* === RESET === */
@layer reset {
    *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
}

/* === THEME - CSS4: color-mix() + color-scheme === */
@layer theme {
    :root {
        color-scheme: light dark;
        --red: #cc0000;
        --red-dark: #a00000;
        --radius: 10px;
        --transition: 0.25s ease;
    }

    [data-theme="light"] {
        --red-glow: rgba(204, 0, 0, 0.15);
        --red-hover: color-mix(in oklch, var(--red) 85%, black);
        --red-subtle: color-mix(in oklch, var(--red) 10%, white);
        --dark: #1a1a2e;
        --dark-light: #16213e;
        --bg: #f4f5f7;
        --bg-card: #ffffff;
        --bg-table-even: #f8f9fa;
        --text: #212529;
        --text-secondary: #6c757d;
        --text-muted: #adb5bd;
        --border: #dee2e6;
        --border-light: #e9ecef;
        --shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
        --highlight-bg: #ffe066;
        --highlight-text: #000;
        --missing-text: #b85c00;
    }

    [data-theme="dark"] {
        --red-glow: rgba(204, 0, 0, 0.25);
        --red-hover: color-mix(in oklch, var(--red) 85%, white);
        --red-subtle: color-mix(in oklch, var(--red) 15%, #0d1117);
        --dark: #0d1117;
        --dark-light: #161b22;
        --bg: #0d1117;
        --bg-card: #161b22;
        --bg-table-even: #1c2333;
        --text: #e6edf3;
        --text-secondary: #8b949e;
        --text-muted: #484f58;
        --border: #30363d;
        --border-light: #21262d;
        --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.4);
        --highlight-bg: #665500;
        --highlight-text: #ffe066;
        --missing-text: #ffc107;
    }
}

/* === LAYOUT === */
@layer layout {
    body {
        font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
        line-height: 1.6;
        color: var(--text);
        background: var(--bg);
        transition: background var(--transition), color var(--transition);
        accent-color: var(--red);
    }

    /* Container sizing (replaces .container) */
    header > nav, main { width: min(92%, 1400px); margin: 0 auto; }

    /* Header (replaces .site-header) */
    body > header {
        background: linear-gradient(135deg, var(--dark) 0%, var(--dark-light) 100%);
        color: #fff;
        padding: 2.5rem 0 2rem;
        margin-bottom: 2rem;
        box-shadow: var(--shadow-lg);

        & h1 {
            font-size: clamp(1.4rem, 4vw, 2.4rem);
            font-weight: 800;
            letter-spacing: -0.5px;
            margin-bottom: 0.5rem;
            text-wrap: balance;

            & span { color: var(--red); }
        }

        /* Header meta (replaces .header-meta) */
        & hgroup > p {
            display: flex;
            flex-wrap: wrap;
            gap: 1.5rem;
            font-size: 0.9rem;
            color: var(--text-muted);

            & strong { color: #fff; }
            & a { color: #fff; text-decoration: underline; text-decoration-color: var(--red); text-underline-offset: 2px; }
            & a:hover { color: var(--red); }
        }
    }

    /* Header inner (replaces .header-inner) */
    header > nav {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 1rem;
    }

    /* Header controls (replaces .header-controls) */
    header fieldset {
        display: flex;
        gap: 0.5rem;
        align-items: center;
        flex-shrink: 0;
        margin-top: 0.3rem;
        border: none;
        padding: 0;

        & > legend {
            position: absolute;
            width: 1px;
            height: 1px;
            padding: 0;
            margin: -1px;
            overflow: hidden;
            clip: rect(0, 0, 0, 0);
            white-space: nowrap;
            border: 0;
        }
    }

    /* Stats — kompaktná úzka lišta */
    section:has(> article > strong) {
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem 1.6rem;
        margin-bottom: 1.5rem;
        padding: 0.55rem 1rem;
        background: var(--bg-card);
        border-radius: var(--radius);
        border-left: 3px solid var(--red);
        box-shadow: var(--shadow);
    }


    /* Search section (replaces .search-filter-section) */
    section:has(search) {
        margin-bottom: 1.5rem;

        & > h2 { font-size: 1.1rem; margin-bottom: 0.5rem; color: var(--text); }
    }

    /* Search row (replaces .search-row) */
    search {
        display: flex;
        gap: 1rem;
        align-items: center;
        flex-wrap: wrap;
        margin-bottom: 0.8rem;
    }

    /* Table wrapper (replaces .table-wrapper) */
    section:has(table) {
        overflow-x: auto;
        margin-bottom: 2rem;
        border-radius: var(--radius);
        box-shadow: var(--shadow-lg);
        background: var(--bg-card);
    }

    /* Footer (replaces .site-footer) */
    body > footer {
        text-align: center;
        padding: 2rem 0 3rem;
        color: var(--text-muted);
        font-size: 0.85rem;

        & a {
            color: var(--text-muted);
            text-decoration: none;
            transition: color var(--transition);

            &:hover { color: var(--red); }
        }

        & p { margin-bottom: 0.3rem; }
    }

    /* Responsive */
    @media (max-width: 768px) {
        body > header { padding: 1.5rem 0; }
        header > nav { flex-direction: column; }
        hgroup > p { flex-direction: column; gap: 0.3rem; }
        header fieldset { align-self: flex-end; margin-top: -2rem; order: 2; }
        header figure { order: 3; align-self: center; margin-top: 1rem; }
        section:has(> details) { grid-template-columns: 1fr; }
        search > label { max-width: 100%; min-width: unset; }
        search { flex-direction: column; align-items: stretch; }
        #backToTop { bottom: 1rem; right: 1rem; width: 42px; height: 42px; }

        /* Card layout for table on mobile */
        table#MyTable { min-width: unset; }
        table#MyTable thead { display: none; }
        table#MyTable tbody tr {
            display: grid;
            grid-template-columns: 1fr auto;
            gap: 0.2rem 1rem;
            padding: 0.8rem;
            margin-bottom: 0.5rem;
            border-radius: var(--radius);
            border: 1px solid var(--border-light);
            background: var(--bg-card);

            &:nth-child(even) { background: var(--bg-card); }
        }
        /* Name - full width */
        table#MyTable td:first-child {
            grid-column: 1 / -1;
            font-weight: 700;
            font-size: 0.95rem;

            &::before { display: none; }
        }
        /* Year + Author */
        table#MyTable td:nth-child(2) { color: var(--text-secondary); }
        table#MyTable td:nth-child(3) { color: var(--text-secondary); text-align: right; }
        /* Size */
        table#MyTable td:nth-child(4) { color: var(--text-muted); font-size: 0.78rem; }
        /* Category + download */
        table#MyTable td:nth-child(5) { text-align: right; }
        /* Date */
        table#MyTable td:nth-child(6) { color: var(--text-muted); font-size: 0.78rem; text-align: left; }
        /* Screenshot - larger on mobile */
        table#MyTable td:last-child {
            grid-column: 1 / -1;

            & img { width: 120px; }
        }
    }

    @media print {
        body > header { background: none; color: #000; box-shadow: none; }
        section:has(search), header figure, body > footer,
        #backToTop, #themeToggle, section:has(> article > strong),
        section:has(search) > nav, header fieldset > nav,
        section:has(> details) { display: none; }
        section:has(table) { box-shadow: none; }
        table#MyTable tbody tr:hover { background: none; color: inherit; }
    }
}

/* === COMPONENTS === */
@layer components {

    /* Badge (replaces .badge — now <data> element in hgroup) */
    hgroup data {
        display: inline-block;
        background: var(--red);
        color: #fff;
        padding: 0.15em 0.65em;
        border-radius: 20px;
        font-size: 0.85em;
        font-weight: 700;
    }

    /* Theme toggle (replaces .theme-toggle) */
    #themeToggle {
        background: color-mix(in srgb, white 10%, transparent);
        border: 1px solid color-mix(in srgb, white 20%, transparent);
        color: #fff;
        border-radius: 50%;
        width: 40px;
        height: 40px;
        cursor: pointer;
        font-size: 1.2rem;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all var(--transition);

        &:hover {
            background: color-mix(in srgb, white 20%, transparent);
            transform: rotate(20deg);
        }
    }

    /* Language switcher (replaces .lang-switcher) */
    header fieldset > nav {
        display: flex;
        gap: 3px;
        background: color-mix(in srgb, white 10%, transparent);
        border-radius: 6px;
        padding: 3px;
    }

    /* Language buttons (replaces .lang-btn) */
    header fieldset nav > a {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 34px;
        height: 28px;
        border-radius: 4px;
        text-decoration: none;
        font-size: 0.7rem;
        font-weight: 700;
        color: color-mix(in srgb, white 60%, transparent);
        transition: all var(--transition);
        text-transform: uppercase;
        letter-spacing: 0.5px;

        &:hover {
            background: color-mix(in srgb, white 15%, transparent);
            color: #fff;
        }

        &[aria-current] {
            background: var(--red);
            color: #fff;
        }
    }

    /* Stat položka — inline číslo + popis */
    section:has(> article > strong) > article {
        display: inline-flex;
        align-items: baseline;
        gap: 0.35rem;

        & > strong {
            font-size: 1rem;
            font-weight: 700;
            color: var(--red);
            line-height: 1.2;
        }

        & > small:first-of-type {
            font-size: 0.78rem;
            color: var(--text-secondary);
        }

        /* Detail (názov najstaršieho/najväčšieho programu) cez tooltip */
        & > small + small { display: none; }
    }

    /* Info cards - details/summary */
    section:has(> details) {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        align-items: start;
        gap: 1.5rem;
        margin-bottom: 2rem;
    }

    section:has(> details) > details {
        background: var(--bg-card);
        border-radius: var(--radius);
        padding: 1.5rem;
        box-shadow: var(--shadow);
        border-top: 3px solid var(--red);

        & summary {
            font-size: 0.95rem;
            font-weight: 700;
            color: var(--text);
            text-transform: uppercase;
            letter-spacing: 0.5px;
            cursor: pointer;
            list-style: none;
            display: flex;
            align-items: center;
            justify-content: space-between;

            &::after { content: "\25B6"; font-size: 0.6em; color: var(--red); transition: transform var(--transition); }
            &::-webkit-details-marker { display: none; }
        }

        &[open] > summary::after { transform: rotate(90deg); }
        &[open] > ul { margin-top: 0.8rem; }

        & ul { list-style: none; font-size: 0.85rem; line-height: 1.9; }
        & ul li::before { content: "\25B8"; color: var(--red); font-weight: bold; margin-right: 0.5em; }
        & li strong { color: var(--red-dark); }

        & a {
            color: var(--red);
            text-decoration: none;
            border-bottom: 1px solid transparent;
            transition: border-color var(--transition);

            &:hover { border-bottom-color: var(--red); }
            &[target="_blank"]::after { content: " \2197"; font-size: 0.75em; }
        }
    }

    [data-theme="dark"] section:has(> details) li strong { color: #ff4444; }

    /* Chýbajúce — položka v stat lište, zalomí celý zoznam pod stat čísla */
    section:has(> article > strong) > article.missing {
        flex-basis: 100%;
        align-items: baseline;
        flex-wrap: wrap;
        gap: 0.35rem;

        & > small { color: var(--missing-text); }
    }

    /* Video — v hlavičke, kompaktný thumbnail */
    header figure {
        flex-shrink: 0;
        align-self: center;

        & iframe { border-radius: var(--radius); box-shadow: var(--shadow); width: 160px; aspect-ratio: 4/3; height: auto; }
    }

    /* Video facade — klikateľný thumbnail */
    #videoFacade {
        position: relative;
        display: block;
        border: none;
        padding: 0;
        cursor: pointer;
        border-radius: var(--radius);
        overflow: hidden;
        box-shadow: var(--shadow);
        width: 160px;
        aspect-ratio: 4/3;

        & img { display: block; width: 100%; height: 100%; object-fit: cover; }

        & > span {
            position: absolute;
            inset: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2.2rem;
            color: #fff;
            background: rgba(0,0,0,0.4);
            transition: background var(--transition);
        }

        &:hover > span { background: rgba(204,0,0,0.7); }
    }

    /* Search box (replaces .search-box) */
    search > label {
        position: relative;
        flex: 1;
        max-width: 500px;
        min-width: 250px;

        &::before {
            content: "\1F50D";
            position: absolute;
            left: 0.8rem;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1rem;
            pointer-events: none;
        }

        & input {
            width: 100%;
            padding: 0.7rem 1rem 0.7rem 2.6rem;
            border: 2px solid var(--border);
            border-radius: var(--radius);
            font-size: 1rem;
            font-family: inherit;
            transition: border-color var(--transition), box-shadow var(--transition);
            background: var(--bg-card);
            color: var(--text);

            &:focus {
                outline: none;
                border-color: var(--red);
                box-shadow: 0 0 0 3px var(--red-glow);
            }

            &:not(:placeholder-shown) {
                border-color: color-mix(in oklch, var(--red) 50%, var(--border));
            }
        }
    }

    search > output {
        font-size: 0.85rem;
        color: var(--text-secondary);
        white-space: nowrap;
        padding: 0.5rem 1rem;
        background: var(--bg-card);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        display: none;

        &.visible { display: inline-block; }
    }

    /* Search hint (replaces .search-hint) */
    search + p { font-size: 0.78rem; color: var(--text-muted); }

    /* Category filters (replaces .cat-filters) */
    section:has(search) > nav {
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem;
        margin-top: 0.8rem;
    }

    /* Category buttons */
    .cat-btn {
        display: inline-flex;
        align-items: center;
        gap: 0.3em;
        padding: 0.3em 0.8em;
        border: 1px solid var(--border);
        border-radius: 20px;
        background: var(--bg-card);
        color: var(--text-secondary);
        font-size: 0.78rem;
        cursor: pointer;
        transition: all var(--transition);
        font-family: inherit;
        white-space: nowrap;

        &:hover {
            border-color: var(--red);
            color: var(--red);
            background: var(--red-subtle);
        }

        &.active { background: var(--red); color: #fff; border-color: var(--red); }
        & > span { font-size: 0.7em; opacity: 0.7; }
        &.active > span { opacity: 0.9; }
    }

    /* Table */
    table#MyTable {
        counter-reset: line-number;
        width: 100%;
        min-width: 700px;
        border-collapse: collapse;

        & thead {
            background: var(--dark);
            color: var(--text-muted);
            position: sticky;
            top: 0;
            z-index: 10;

            & th {
                padding: 0.75rem 0.6rem;
                font-size: 0.78rem;
                font-weight: 700;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                cursor: pointer;
                user-select: none;
                white-space: nowrap;
                border: none;
                text-align: left;
                transition: background var(--transition);

                &:hover { background: color-mix(in srgb, white 10%, transparent); color: #fff; }
                &.sort-asc::after { content: ' \25B2'; font-size: 0.6em; }
                &.sort-desc::after { content: ' \25BC'; font-size: 0.6em; }
                &:nth-child(4) { width: 80px; }

                /* Download label in category header (replaces .download-label) */
                &:nth-child(5) > span { float: right; margin-right: 0.5em; }
            }
        }

        & tbody tr {
            transition: background var(--transition), color var(--transition);
            border-bottom: 1px solid var(--border-light);

            &:nth-child(even) { background: var(--bg-table-even); }

            &:hover {
                background: var(--red);
                color: #fff;

                & td { color: #fff; }
                & td:nth-child(5) > a { background: #fff; color: var(--red); }
                & td:first-child::before { color: color-mix(in srgb, white 60%, transparent); }
            }
        }

        & td {
            padding: 0.35rem 0.6rem;
            font-size: 0.85rem;
            vertical-align: middle;
            border: none;

            &:first-child::before {
                content: counter(line-number);
                counter-increment: line-number;
                display: inline-block;
                min-width: 1.8em;
                padding-right: 0.5em;
                color: var(--text-muted);
                font-size: 0.72rem;
                text-align: right;
            }

            /* Category column (replaces .td-category) */
            &:nth-child(5) { white-space: nowrap; }

            /* Date column (replaces .td-date) */
            &:nth-child(6) { text-align: center; white-space: nowrap; }

            /* Screenshot column (replaces .td-screenshot) */
            &:last-child {
                text-align: center;

                & img {
                    width: 50px;
                    display: block;
                    margin: 0 auto;
                    border-radius: 3px;
                    cursor: zoom-in;
                    transition: transform 0.3s ease, box-shadow 0.3s ease;
                }
            }
        }
    }

    /* Download button (replaces .dl-btn) */
    #MyTable td:nth-child(5) > a {
        display: inline-block;
        background: var(--red);
        color: #fff;
        padding: 0.25em 0.7em;
        border-radius: 4px;
        text-decoration: none;
        font-size: 0.78em;
        font-weight: 700;
        transition: all var(--transition);
        white-space: nowrap;
        float: right;

        &:hover { background: var(--red-hover); transform: translateY(-1px); }
    }

    .new-badge {
        display: inline-block;
        background: #00c853;
        color: #fff;
        font-size: 0.6rem;
        font-weight: 800;
        padding: 0.1em 0.5em;
        border-radius: 3px;
        vertical-align: middle;
        letter-spacing: 0.5px;
        animation: pulse-glow-green 2s ease-in-out infinite;
    }

    mark.highlight {
        background: var(--highlight-bg);
        color: var(--highlight-text);
        padding: 0 2px;
        border-radius: 2px;
    }

    #lightbox {
        border: none;
        background: transparent;
        max-width: 90vw;
        max-height: 90vh;
        inset: 0;
        margin: auto;
        padding: 0;
        cursor: zoom-out;
        animation: fadeIn 0.2s ease;

        &::backdrop { background: rgba(0, 0, 0, 0.85); }

        & img {
            max-width: 90vw;
            max-height: 90vh;
            border-radius: var(--radius);
            box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
            image-rendering: pixelated;
        }

        & .lightbox-title {
            position: fixed;
            bottom: 1.5rem;
            left: 50%;
            transform: translateX(-50%);
            color: #fff;
            font-size: 1rem;
            background: rgba(0,0,0,0.6);
            padding: 0.5rem 1.5rem;
            border-radius: 20px;
            pointer-events: none;
            text-wrap: nowrap;
        }

        & .lightbox-close {
            position: fixed;
            top: 1rem;
            right: 1.5rem;
            color: #fff;
            font-size: 2rem;
            cursor: pointer;
            background: rgba(0,0,0,0.5);
            border: none;
            width: 44px;
            height: 44px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background var(--transition);

            &:hover { background: var(--red); }
        }
    }

    #backToTop {
        position: fixed;
        bottom: 2rem;
        right: 2rem;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: var(--red);
        color: #fff;
        border: none;
        font-size: 1.4rem;
        cursor: pointer;
        box-shadow: var(--shadow-lg);
        opacity: 0;
        visibility: hidden;
        transform: translateY(10px);
        transition: all 0.3s ease;
        z-index: 100;
        display: flex;
        align-items: center;
        justify-content: center;
        --glow-opacity: 0;

        &.visible {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
            animation: pulse-glow 2s ease-in-out infinite;
        }

        &:hover {
            background: var(--red-hover);
            transform: translateY(-2px);
            --glow-opacity: 0.4;
            box-shadow: 0 0 20px rgba(204, 0, 0, var(--glow-opacity));
        }
    }
}

/* === UTILITIES === */
@layer utilities {
    :focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }

    section:has(input:not(:placeholder-shown)) search > label {
        &::after {
            content: "";
            position: absolute;
            inset: -3px;
            border-radius: calc(var(--radius) + 3px);
            background: var(--red-glow);
            z-index: -1;
            animation: fadeIn 0.3s ease;
        }
    }
}

/* === ANIMATIONS === */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse-glow {
    0%, 100% { box-shadow: 0 4px 15px rgba(204, 0, 0, 0.2); }
    50% { box-shadow: 0 4px 25px rgba(204, 0, 0, 0.4); }
}
@keyframes pulse-glow-green {
    0%, 100% { box-shadow: 0 0 3px rgba(0, 200, 83, 0.3); }
    50% { box-shadow: 0 0 8px rgba(0, 200, 83, 0.6); }
}

section:has(> details) > details,
section:has(> article > strong) > article { animation: slideUp 0.4s ease backwards; }
section:has(> article > strong) > article:nth-child(2) { animation-delay: 0.06s; }
section:has(> article > strong) > article:nth-child(3) { animation-delay: 0.12s; }
section:has(> article > strong) > article:nth-child(4) { animation-delay: 0.18s; }
section:has(> article > strong) > article:nth-child(5) { animation-delay: 0.24s; }
section:has(> details) > details:nth-child(2) { animation-delay: 0.1s; }

/* === CSS4: View Transitions === */
@view-transition { navigation: auto; }
body > header { view-transition-name: header; }
section:has(> article > strong) { view-transition-name: stats; }
table#MyTable { view-transition-name: table; }
