/* Desktop: a 232px sidebar. body.work-mode (set by a page) collapses it to a 56px icon rail.
   Tablet (761-1208px): the sidebar slides in behind a Menu button.
   Phone (760px and under): a bottom tab bar; a tapped group opens as a sheet above it. */
.nav-rail {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 232px;
    background: var(--ground);
    border-right: 1px solid var(--hair);
    color: var(--ink-2);
    display: flex;
    flex-direction: column;
    padding: var(--space-4) var(--space-3);
    z-index: 900;
    overflow-y: auto;
    overflow-x: hidden;
}

.nav-rail-header {
    padding: 0 var(--space-2);
    height: 24px;
    display: flex;
    align-items: center;
}

.nav-wordmark {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--ink);
    font-size: var(--fs-lg);
    font-weight: 600;
    letter-spacing: -0.01em;
    line-height: 1.1;
    text-decoration: none;
}

.nav-wordmark-mark {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
    background: var(--blue);
    -webkit-mask: url(/static/media/white_knight.svg) center / contain no-repeat;
    mask: url(/static/media/white_knight.svg) center / contain no-repeat;
}

.nav-wordmark:hover .nav-wordmark-mark,
.nav-wordmark:focus-visible .nav-wordmark-mark {
    animation: knight-bounce 0.8s ease-in-out;
}

@keyframes knight-bounce {
    0%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
    55% { transform: translateY(0); }
    75% { transform: translateY(-3px); }
}

.wordmark-brand {
    color: var(--blue);
}

.wordmark-neutral {
    color: var(--ink);
}

/* Search or jump to... */
.nav-search {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    height: 32px;
    margin: var(--space-4) 0;
    padding: 0 var(--space-2);
    background: var(--surface);
    border: 1px solid var(--hair);
    border-radius: var(--radius-ctl);
    color: var(--ink-3);
    font-family: var(--font);
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
    flex-shrink: 0;
}

.nav-search:hover {
    border-color: var(--hair-strong);
    color: var(--ink-2);
}

.nav-search-kbd {
    margin-left: auto;
    padding: 0;
    border: none;
    background: none;
    color: var(--ink-3);
}

.nav-icon {
    width: 16px;
    height: 16px;
    flex: none;
}

.nav-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Rows: Home, group heads, sub links, Progress */
.nav-rail-section {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-rail-link,
.nav-group-head {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    height: 32px;
    box-sizing: border-box;
    padding: 0 var(--space-2);
    background: transparent;
    border: none;
    border-radius: var(--radius-ctl);
    color: var(--ink-2);
    font-family: var(--font);
    font-size: var(--fs-md);
    font-weight: 500;
    text-align: left;
    text-decoration: none;
    cursor: pointer;
    flex-shrink: 0;
}

.nav-rail-link.nav-sub {
    height: 28px;
    padding-left: 32px;
    font-size: var(--fs-sm);
    font-weight: 400;
}

.nav-sub-list {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.nav-group.collapsed .nav-sub-list {
    display: none;
}

.nav-chevron {
    width: 12px;
    height: 12px;
    flex: none;
    margin-left: auto;
    color: var(--ink-3);
    transition: transform 0.15s ease;
}

.nav-group.collapsed .nav-chevron {
    transform: rotate(-90deg);
}

.nav-rail-link:hover:not(:disabled),
.nav-group-head:hover {
    background: var(--surface);
    color: var(--ink);
}

.nav-rail-link.active,
.nav-rail-link.active:hover:not(:disabled) {
    background: var(--surface-2);
    color: var(--ink);
}

/* The active marker: 2px cyan on the sidebar's left edge (the rail has 12px padding) */
.nav-rail-link.active::before {
    content: "";
    position: absolute;
    left: calc(-1 * var(--space-3));
    top: 6px;
    bottom: 6px;
    width: 2px;
    background: var(--blue);
    border-radius: 0 2px 2px 0;
}

.nav-group.has-active > .nav-group-head {
    color: var(--ink);
}

.nav-group.has-active > .nav-group-head .nav-icon {
    color: var(--blue);
}

.nav-rail-link:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.nav-rail .nav-rail-link:focus-visible,
.nav-rail button:focus-visible,
.nav-rail a:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: -2px;
}

/* Count on the Home row: how much is due today */
.nav-count {
    margin-left: auto;
    color: var(--blue);
    font-family: var(--font-mono);
    font-size: var(--fs-xs);
    font-variant-numeric: tabular-nums;
}

.nav-rail-spacer {
    flex: 1;
    min-height: var(--space-4);
}

/* User chip */
.nav-rail-footer {
    position: relative;
    flex-shrink: 0;
}

.nav-account-trigger {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    min-height: 40px;
    background: none;
    border: none;
    border-radius: var(--radius-ctl);
    color: var(--ink);
    cursor: pointer;
    padding: var(--space-2);
    font-family: var(--font);
    font-size: var(--fs-md);
    font-weight: 500;
    text-align: left;
}

.nav-account-trigger:hover {
    background: var(--surface);
}

.nav-account-avatar {
    display: grid;
    place-items: center;
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    border-radius: 50%;
    background: var(--surface-2);
    border: 1px solid var(--hair-strong);
    color: var(--ink);
    font-size: 11px;
    font-weight: 600;
    line-height: 1;
}

.nav-rail .auth-user-label {
    color: inherit;
    font-size: var(--fs-md);
}

.nav-account-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-account-rating {
    margin-left: auto;
    flex-shrink: 0;
    color: var(--ink-3);
    font-size: var(--fs-sm);
}

/* One account menu everywhere: same width and place whether the sidebar is full, collapsed or a rail */
.nav-account-popover {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 0;
    width: 208px;
    background: var(--surface-2);
    border: 1px solid var(--hair-strong);
    border-radius: var(--radius-card);
    padding: var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.nav-account-popover-link {
    font-size: var(--fs-sm);
    color: var(--ink-2);
    text-decoration: none;
    padding: var(--space-2) 0;
}

.nav-account-popover-link:hover {
    color: var(--ink);
}

.nav-account-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.nav-account-head {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-bottom: var(--space-3);
    border-bottom: 1px solid var(--hair);
    min-width: 0;
}

.nav-account-head-name {
    font-size: var(--fs-md);
    font-weight: 500;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-account-head-email {
    font-size: var(--fs-xs);
    color: var(--ink-3);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-menu-title {
    margin-top: var(--space-1);
    font-size: var(--fs-xs);
    font-weight: 500;
    color: var(--ink-3);
}

.nav-setting {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.nav-sign-out {
    margin-top: var(--space-1);
    padding-top: var(--space-3);
    border: 0;
    border-top: 1px solid var(--hair);
    background: none;
    text-align: left;
    font-family: var(--font);
    cursor: pointer;
}

.nav-setting-label {
    font-size: var(--fs-xs);
    color: var(--ink-3);
}

.nav-board-themes {
    display: flex;
    gap: var(--space-2);
}

.nav-board-theme {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 40px;
    height: 24px;
    padding: 0;
    overflow: hidden;
    border: 1px solid var(--hair-strong);
    border-radius: var(--radius-ctl);
    background: none;
    cursor: pointer;
}

.nav-board-theme[aria-checked="true"] {
    outline: 2px solid var(--blue);
    outline-offset: 1px;
}

.nav-rail-backdrop {
    display: none;
}

.nav-tabbar {
    display: none;
}

/* Jump palette (Ctrl+K / Cmd+K) */
.jump {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 14vh var(--space-3) var(--space-3);
    background: rgba(11, 12, 14, 0.7);
}

.jump-box {
    width: min(560px, 100%);
    max-height: 70vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--hair-strong);
    border-radius: var(--radius-card);
    overflow: hidden;
    animation: jump-in 0.12s ease-out;
}

@keyframes jump-in {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: none; }
}

.jump-field {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0 var(--space-4);
    height: 48px;
    border-bottom: 1px solid var(--hair);
    color: var(--ink-3);
}

.jump-field input {
    flex: 1;
    min-width: 0;
    height: 100%;
    min-height: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: transparent;
    color: var(--ink);
    font-size: var(--fs-lg);
}

.jump-field input:focus-visible {
    outline: none;
}

.jump-list {
    list-style: none;
    margin: 0;
    padding: var(--space-2);
    overflow-y: auto;
}

.jump-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    height: 36px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-ctl);
    color: var(--ink);
    font-size: var(--fs-md);
    cursor: pointer;
}

.jump-item[aria-selected="true"] {
    background: var(--surface-2);
}

.jump-item[aria-selected="true"] .jump-name {
    color: var(--blue);
}

.jump-hint {
    margin-left: auto;
    color: var(--ink-3);
    font-size: var(--fs-xs);
}

.jump-empty {
    padding: var(--space-4) var(--space-3);
    color: var(--ink-3);
    font-size: var(--fs-md);
}

.jump-foot {
    display: flex;
    gap: var(--space-4);
    margin: 0;
    padding: var(--space-2) var(--space-4);
    border-top: 1px solid var(--hair);
    color: var(--ink-3);
    font-size: var(--fs-xs);
}

/* Collapse / expand button in the sidebar header (desktop only) */
.nav-collapse {
    display: none;
    margin-left: auto;
    width: 24px;
    height: 24px;
    padding: 0;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 0;
    border-radius: var(--radius-ctl);
    color: var(--ink-3);
    cursor: pointer;
}

.nav-collapse:hover {
    background: var(--surface);
    color: var(--ink);
}

@media (min-width: 1209px) {
    .nav-collapse {
        display: flex;
    }
}

@media (min-width: 1209px) {
    body:not([data-page="report"]) {
        margin-left: 232px;
    }

    /* Work mode: pages add body.work-mode and the sidebar becomes a 56px icon rail (tooltips come from title) */
    html body.work-mode,
    html body.rail-collapsed {
        margin-left: 56px;
    }

    body.work-mode:not(.rail-peek) .nav-rail,
    body.rail-collapsed:not(.rail-peek) .nav-rail {
        width: 56px;
        padding: var(--space-4) var(--space-2);
        overflow: visible;
    }

    body.work-mode:not(.rail-peek) .nav-rail-header,
    body.rail-collapsed:not(.rail-peek) .nav-rail-header {
        justify-content: center;
        padding: 0;
    }

    body.work-mode:not(.rail-peek) .nav-wordmark-text,
    body.rail-collapsed:not(.rail-peek) .nav-wordmark-text,
    body.work-mode:not(.rail-peek) .nav-label,
    body.rail-collapsed:not(.rail-peek) .nav-label,
    body.work-mode:not(.rail-peek) .nav-chevron,
    body.rail-collapsed:not(.rail-peek) .nav-chevron,
    body.work-mode:not(.rail-peek) .nav-count,
    body.rail-collapsed:not(.rail-peek) .nav-count,
    body.work-mode:not(.rail-peek) .nav-search-kbd,
    body.rail-collapsed:not(.rail-peek) .nav-search-kbd,
    body.work-mode:not(.rail-peek) .nav-sub-list,
    body.rail-collapsed:not(.rail-peek) .nav-sub-list {
        display: none;
    }

    body.work-mode:not(.rail-peek) .nav-search,
    body.rail-collapsed:not(.rail-peek) .nav-search,
    body.work-mode:not(.rail-peek) .nav-rail-link,
    body.rail-collapsed:not(.rail-peek) .nav-rail-link,
    body.work-mode:not(.rail-peek) .nav-group-head,
    body.rail-collapsed:not(.rail-peek) .nav-group-head,
    body.work-mode:not(.rail-peek) .nav-account-trigger,
    body.rail-collapsed:not(.rail-peek) .nav-account-trigger {
        justify-content: center;
        padding-left: 0;
        padding-right: 0;
    }

    body.work-mode:not(.rail-peek) .nav-rail-link.active::before,
    body.rail-collapsed:not(.rail-peek) .nav-rail-link.active::before {
        left: calc(-1 * var(--space-2));
    }

    /* Collapsed header: only the expand button, in the same column and size as the icons below */
    body.work-mode:not(.rail-peek) .nav-wordmark,
    body.rail-collapsed:not(.rail-peek) .nav-wordmark {
        display: none;
    }

    body.work-mode:not(.rail-peek) .nav-collapse,
    body.rail-collapsed:not(.rail-peek) .nav-collapse {
        width: 100%;
        height: 32px;
        margin: 0;
        justify-content: center;
    }

    /* Search is a plain icon like the links, not a boxed field */
    body.work-mode:not(.rail-peek) .nav-search,
    body.rail-collapsed:not(.rail-peek) .nav-search {
        border-color: transparent;
        background: transparent;
    }

    body.work-mode:not(.rail-peek) .nav-search:hover,
    body.rail-collapsed:not(.rail-peek) .nav-search:hover {
        background: var(--surface);
    }

    /* Peeking in work mode: the full sidebar overlays the page without moving the body */
    body.rail-peek .nav-rail {
        box-shadow: 0 0 0 1px var(--hair-strong), 8px 0 24px rgba(0, 0, 0, 0.4);
    }

    body.work-mode:not(.rail-peek) .nav-account-popover,
    body.rail-collapsed:not(.rail-peek) .nav-account-popover {
        left: 0;
    }
}

/* Narrow laptop and tablet: the sidebar is hidden off-screen, then shown as the icon rail or the phone sheet below */
@media (max-width: 1208px) {
    /* visibility (delayed until the slide finishes) keeps the closed sidebar out of the tab order */
    .nav-rail {
        transform: translateX(-100%);
        visibility: hidden;
        transition: transform 0.2s ease-out, visibility 0s linear 0.2s;
    }

    .nav-rail.open {
        transform: translateX(0);
        visibility: visible;
        transition: transform 0.2s ease-out, visibility 0s linear 0s;
    }

    .nav-rail-backdrop.open {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(11, 12, 14, 0.7);
        z-index: 899;
        animation: nav-backdrop-in 0.2s ease-out;
    }
}

/* Narrow laptop and tablet (761-1208px): no hidden drawer. The sidebar stays on screen as the
   56px icon rail (the same rail work mode uses on wide screens), so navigation is never cut off. */
@media (min-width: 761px) and (max-width: 1208px) {
    .nav-rail,
    .nav-rail.open {
        transform: none;
        visibility: visible;
        width: 56px;
        padding: var(--space-4) var(--space-2);
        overflow: visible;
    }

    .nav-rail-backdrop.open {
        display: none;
    }

    html body,
    html body:not([data-page="report"]) {
        margin-left: 56px;
        padding-top: 0;
    }

    .nav-rail-header {
        justify-content: center;
        padding: 0;
    }

    .nav-wordmark-text,
    .nav-label,
    .nav-chevron,
    .nav-count,
    .nav-search-kbd,
    .nav-sub-list {
        display: none;
    }

    .nav-search,
    .nav-rail-link,
    .nav-group-head,
    .nav-account-trigger {
        justify-content: center;
        padding-left: 0;
        padding-right: 0;
    }

    .nav-rail-link.active::before {
        left: calc(-1 * var(--space-2));
    }

    .nav-account-popover {
        left: 0;
    }
}

@keyframes nav-backdrop-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Phone: bottom tab bar. A group tab opens that group's links (and the user chip) as a sheet above it. */
@media (max-width: 760px) {
    body {
        padding-bottom: calc(64px + env(safe-area-inset-bottom));
    }

    .nav-tabbar {
        display: flex;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 901;
        padding-bottom: env(safe-area-inset-bottom);
        background: var(--ground);
        border-top: 1px solid var(--hair);
    }

    .nav-tab {
        position: relative;
        flex: 1;
        min-height: 64px;
        padding: 8px 2px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        box-sizing: border-box;
        background: transparent;
        border: none;
        border-radius: 0;
        color: var(--ink-2);
        font-family: var(--font);
        font-size: 11px;
        font-weight: 500;
        text-decoration: none;
        cursor: pointer;
    }

    .nav-tab .nav-icon {
        width: 20px;
        height: 20px;
    }

    .nav-tab.active {
        color: var(--blue);
    }

    /* 2px cyan marker on the bar's top edge */
    .nav-tab.active::before {
        content: "";
        position: absolute;
        top: 0;
        left: 25%;
        right: 25%;
        height: 2px;
        background: var(--blue);
        border-radius: 0 0 2px 2px;
    }

    .nav-tab:focus-visible {
        outline: 2px solid var(--blue);
        outline-offset: -3px;
    }

    .nav-rail {
        top: auto;
        left: var(--space-2);
        right: var(--space-2);
        bottom: calc(64px + env(safe-area-inset-bottom) + var(--space-2));
        width: auto;
        max-height: 70vh;
        padding: var(--space-2);
        background: var(--surface);
        border: 1px solid var(--hair-strong);
        border-radius: var(--radius-card);
        transform: translateY(calc(100% + 80px + env(safe-area-inset-bottom)));
    }

    .nav-rail.open {
        transform: translateY(0);
    }

    /* The sheet shows one group: its links, then the user chip */
    .nav-rail-header,
    .nav-search,
    .nav-rail-spacer,
    .nav-group-head,
    .nav-chevron {
        display: none;
    }

    .nav-rail[data-sheet="train"] .nav-group:not([data-group="train"]),
    .nav-rail[data-sheet="openings"] .nav-group:not([data-group="openings"]),
    .nav-rail[data-sheet="games"] .nav-group:not([data-group="games"]) {
        display: none;
    }

    .nav-group.collapsed .nav-sub-list {
        display: flex;
    }

    .nav-rail-link.nav-sub {
        height: 40px;
        padding-left: var(--space-3);
        font-size: var(--fs-md);
    }

    .nav-rail-link.active::before {
        left: calc(-1 * var(--space-2));
    }

    .nav-rail-footer {
        margin-top: var(--space-2);
        padding-top: var(--space-2);
        border-top: 1px solid var(--hair);
    }

    .nav-account-popover {
        position: static;
        width: auto;
        margin-top: var(--space-2);
    }
}

@media (prefers-reduced-motion: reduce) {
    .nav-rail,
    .nav-rail.open,
    .nav-chevron {
        transition: none;
    }

    .nav-rail-backdrop.open,
    .jump-box {
        animation: none;
    }

    .nav-wordmark:hover .nav-wordmark-mark,
    .nav-wordmark:focus-visible .nav-wordmark-mark {
        animation: none;
    }
}
