/* ==========================================================================
   LAYOUT
   Three-column grid at >= 1280px: sidebar | list | reader.
   Two-column (sidebar | list) at 880-1279px; reader slides over list.
   Single column below 880px; reader overlays full-screen.

   The three-column breakpoint sits at 1280px because below that the rail
   (220) + list (720) + 2× gap leaves the reader column with too little
   room to render meaningfully. The list track is also wrapped in
   minmax(0, var(--list-w)) so it can shrink rather than overflow when
   the viewport sits just above the breakpoint.
   ========================================================================== */

.page {
    margin: 0 auto;
    padding: var(--s-4);
    display: grid;
    gap: var(--s-5);
    grid-template-columns: 1fr;
    min-height: 100vh;
    min-height: 100dvh;
}

@media (min-width: 880px) {
    .page {
        grid-template-columns: var(--rail-w) minmax(0, 1fr);
        padding: var(--s-6);
        gap: var(--s-8);
    }
}

@media (min-width: 1280px) {
    .page {
        grid-template-columns: var(--rail-w) minmax(0, var(--list-w)) minmax(0, 1fr);
        gap: var(--s-6);
    }
}

/* ---------- RAIL ---------- */
.rail {
    display: flex;
    flex-direction: column;
    gap: var(--s-5);
    align-self: start;
}

/* Group 1 (brand → theme → search) is the "tools" cluster.
   Group 2 (filters) is the "discovery" cluster. */
@media (max-width: 879px) {
    .rail {
        display: grid;
        grid-template-columns: 1fr auto;
        align-items: center;
        column-gap: var(--s-3);
        row-gap: var(--s-5);
    }

    .brand {
        grid-column: 1;
        grid-row: 1;
        /* Match the theme toggle's box height and flex-center the wordmark
           inside it so the glyph sits exactly on the toggle's mid-line. */
        min-height: var(--control-h);
        line-height: 1;
    }

    /* Touch viewports: theme toggle grows to 52px (WCAG 2.5.5), so the
       brand must grow with it to stay centered on the same row. */
    @media (pointer: coarse) {
        .brand {
            min-height: 52px;
        }
    }

    .rail-section-theme {
        grid-column: 2;
        grid-row: 1;
        justify-self: end;
        padding-top: 0;
    }

    .rail-section-theme .rail-section-label,
    .rail-section-search .rail-section-label {
        display: none;
    }

    .rail-section-search {
        grid-column: 1 / -1;
    }

    .filters {
        grid-column: 1 / -1;
    }
}

@media (min-width: 880px) {
    .rail {
        position: sticky;
        top: var(--s-7);
        max-height: calc(100vh - var(--s-7) * 2);
        overflow-y: auto;
        padding-right: var(--s-2);
        margin-right: calc(var(--s-2) * -1);

        scrollbar-width: thin;
        scrollbar-color: transparent transparent;
    }

    .rail:hover,
    .rail:focus-within {
        scrollbar-color: color-mix(in oklch, var(--rule) 75%, transparent) transparent;
    }

    .rail::-webkit-scrollbar {
        width: 6px;
    }

    .rail::-webkit-scrollbar-track {
        background: transparent;
    }

    .rail::-webkit-scrollbar-thumb {
        background: transparent;
        border-radius: 999px;
    }

    .rail:hover::-webkit-scrollbar-thumb,
    .rail:focus-within::-webkit-scrollbar-thumb {
        background: color-mix(in oklch, var(--rule) 80%, transparent);
    }

    .rail::-webkit-scrollbar-thumb:hover {
        background: color-mix(in oklch, var(--rule-strong) 95%, transparent);
    }
}

/* ---------- LIST (main) ---------- */
.main {
    min-width: 0;
    width: 100%;
}

@media (min-width: 1280px) {
    .main {
        /* The grid track already caps width via minmax(0, var(--list-w)),
           so .main fills its track and gets no separate max-width here. */
        position: sticky;
        top: var(--s-7);
        max-height: calc(100vh - var(--s-7) * 2);
        overflow-y: auto;
        padding-right: var(--s-3);
        margin-right: calc(var(--s-3) * -1);

        scrollbar-width: thin;
        scrollbar-color: transparent transparent;
        transition: scrollbar-color 220ms var(--ease-out);
    }

    .main:hover,
    .main:focus-within {
        scrollbar-color: color-mix(in oklch, var(--rule) 75%, transparent) transparent;
    }

    .main::-webkit-scrollbar {
        width: 8px;
    }

    .main::-webkit-scrollbar-track {
        background: transparent;
    }

    .main::-webkit-scrollbar-thumb {
        background: transparent;
        border-radius: 999px;
        transition: background-color 220ms var(--ease-out);
    }

    .main:hover::-webkit-scrollbar-thumb,
    .main:focus-within::-webkit-scrollbar-thumb {
        background: color-mix(in oklch, var(--rule) 80%, transparent);
    }

    .main::-webkit-scrollbar-thumb:hover {
        background: color-mix(in oklch, var(--rule-strong) 95%, transparent);
    }
}

/* ---------- READER ---------- */
.reader {
    min-width: 0;
    position: relative;
}

@media (max-width: 1279px) {

    /* On narrow screens the reader overlays on top of list; JS toggles .reader-open on body */
    .reader {
        position: fixed;
        inset: 0;
        background: var(--surface);
        z-index: 50;
        padding: var(--s-4);
        overflow-y: auto;
        transform: translateX(100%);
        transition: transform 200ms var(--ease-out);
    }

    body.reader-open .reader {
        transform: translateX(0);
    }
}