/* ============================================================
   tb.css — reusable control blocks (top bar & toolbars)

   One design system for every button/control in the app chrome:
     · .tb-btn            base button
     · .tb-btn--icon      icon-only square
     · .tb-btn--primary   solid accent (main action)
     · .tb-btn--danger    destructive
     · .tb-btn--toggle    on/off state (Edit availability, …)
     · .tb-btn--level     two-line tile (level + count)
     · .tb-seg            segmented group (Week/Month, filters, …)
     · .tb-seg-label      plain label slot inside a segment group
     · .tb-field          search / text field

   Tokens come from design.css (final export of design.html),
   scoped to .lessons-top-bar. Every var() below carries a
   fallback equal to the design.css export, so blocks also work
   outside the top bar (e.g. the statistics payment filter card).

   Two *modes* live on <body> attributes (see index.html):
     data-tb-active     fill | tint | solid | outline
     data-tb-segments   group | pills
   They mirror the --tb-active / --tb-segments tokens exported
   from design.html; the colors/sizes always follow design.css.
   ============================================================ */

/* ── 1 · Base button ────────────────────────────────────────── */
.tb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    height: var(--tb-h, 34px);
    padding: 0 var(--tb-cx, 16px);
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: var(--tb-size, 13px);
    font-weight: var(--tb-weight, 600);
    letter-spacing: var(--tb-tracking, 0px);
    text-transform: var(--tb-uppercase, normal);
    line-height: 1;
    color: var(--tb-text, rgb(226, 226, 230));
    background: var(--tb-bg, color-mix(in srgb, #fff 5%, transparent));
    border: 1px solid var(--tb-border, rgb(69, 69, 78));
    border-radius: var(--tb-radius, 17px);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    transition:
        background .12s ease,
        border-color .12s ease,
        color .12s ease,
        box-shadow .12s ease,
        transform .12s ease;
}
.tb-btn:hover {
    background: var(--tb-hover-bg, rgb(51, 51, 59));
    color: var(--tb-text, rgb(226, 226, 230));
    box-shadow: var(--tb-shadow, 0 2px 10px rgba(0, 0, 0, .35));
}
.tb-btn:active { transform: scale(.98); box-shadow: none; }
.tb-btn:focus-visible {
    outline: 2px solid var(--tb-accent, rgb(78, 201, 176));
    outline-offset: 2px;
}
.tb-btn:disabled { opacity: .4; cursor: not-allowed; pointer-events: none; }

.tb-btn .ico { font-size: 1.05em; line-height: 1; }

/* `hidden` attribute must always win over the display values above
   (author CSS overrides the UA `[hidden]` rule — see .lessons-canboard-back[hidden] in style.css). */
.tb-btn[hidden],
.tb-seg[hidden],
.tb-field[hidden] { display: none !important; }

/* icon-only square */
.tb-btn--icon { width: var(--tb-h, 34px); padding: 0; }

/* primary — solid accent (main action of a screen) */
.tb-btn--primary {
    background: var(--tb-accent, rgb(78, 201, 176));
    border-color: var(--tb-accent, rgb(78, 201, 176));
    color: var(--tb-on-accent, rgb(8, 34, 28));
}
.tb-btn--primary:hover {
    background: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 84%, #fff 16%);
    border-color: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 70%, #fff 30%);
    color: var(--tb-on-accent, rgb(8, 34, 28));
}

/* danger — outline that fills on hover */
.tb-btn--danger {
    color: var(--danger, #f14c4c);
    border-color: color-mix(in srgb, var(--danger, #f14c4c) 45%, rgb(69, 69, 78));
    background: transparent;
}
.tb-btn--danger:hover {
    background: color-mix(in srgb, var(--danger, #f14c4c) 12%, transparent);
    border-color: var(--danger, #f14c4c);
    color: #ff8f8f;
}

/* ── 2 · Toggle button (on/off, e.g. "Edit availability") ───── */
/* JS hooks that mark the ON state: .active / .is-on / .week-edit-btn--active / aria-pressed */
.tb-btn--toggle:is(.active, .is-on, .week-edit-btn--active, [aria-pressed="true"]) {
    background: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 16%, transparent);
    border-color: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 60%, var(--tb-border, rgb(69, 69, 78)));
    color: var(--tb-text, rgb(226, 226, 230));
}
.tb-btn--toggle:is(.active, .is-on, .week-edit-btn--active, [aria-pressed="true"])::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--tb-accent, rgb(78, 201, 176));
    flex: 0 0 auto;
}

/* ── 3 · Segmented control ──────────────────────────────────── */
.tb-seg {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
    gap: 0;
    padding: 3px;
    background: color-mix(in srgb, black 14%, transparent);
    border: 1px solid var(--tb-border, rgb(69, 69, 78));
    border-radius: calc(var(--tb-radius, 17px) + 3px);
    min-width: 0;
}
.tb-seg .tb-btn {
    min-height: calc(var(--tb-h, 34px) - 6px);
    height: auto;
    flex: 0 1 auto;
    min-width: 0;
    border-color: transparent;
    background: transparent;
    color: var(--tb-muted, rgb(154, 154, 164));
    box-shadow: none;
}
.tb-seg .tb-btn:hover {
    background: color-mix(in srgb, var(--tb-hover-bg, rgb(51, 51, 59)) 60%, transparent);
    color: var(--tb-text, rgb(226, 226, 230));
    box-shadow: none;
}

/* plain label slot inside a segment group (week/month name, …) */
.tb-seg-label {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 26ch;
    width: 26ch;
    min-width: 26ch;
    max-width: 26ch;
    box-sizing: border-box;
    min-height: calc(var(--tb-h, 34px) - 6px);
    padding: 0 14px;
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: var(--tb-size, 13px);
    font-weight: var(--tb-weight, 600);
    letter-spacing: var(--tb-tracking, 0px);
    text-transform: var(--tb-uppercase, normal);
    color: var(--tb-text, rgb(226, 226, 230));
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* active segment — mode "fill" (default, = --tb-active in design.css) */
.tb-seg .tb-btn:is(.active, .is-active, [aria-pressed="true"]) {
    background: var(--tb-seg-active, rgb(67, 67, 77));
    color: var(--tb-text, rgb(226, 226, 230));
}

/* alternate active modes (body[data-tb-active]) */
body[data-tb-active="tint"] .tb-seg .tb-btn:is(.active, .is-active, [aria-pressed="true"]) {
    background: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 18%, transparent);
    color: var(--tb-text, rgb(226, 226, 230));
}
body[data-tb-active="solid"] .tb-seg .tb-btn:is(.active, .is-active, [aria-pressed="true"]) {
    background: var(--tb-accent, rgb(78, 201, 176));
    color: var(--tb-on-accent, rgb(8, 34, 28));
}
body[data-tb-active="outline"] .tb-seg .tb-btn:is(.active, .is-active, [aria-pressed="true"]) {
    background: transparent;
    border-color: var(--tb-accent, rgb(78, 201, 176));
    color: var(--tb-text, rgb(226, 226, 230));
}

/* alternate segment styles (body[data-tb-segments="pills"]) */
body[data-tb-segments="pills"] .tb-seg {
    background: transparent;
    border-color: transparent;
    border-radius: 0;
    padding: 0;
}
body[data-tb-segments="pills"] .tb-seg .tb-btn:is(.active, .is-active, [aria-pressed="true"]) {
    border-color: var(--tb-border, rgb(69, 69, 78));
}

/* ── 4 · Level tiles (A1/A2/… + word count) ──────────────────── */
.tb-btn--level {
    flex-direction: column;
    gap: 3px;
    min-width: 52px;
    padding: 6px 10px;
    font-variant-numeric: tabular-nums;
}
/* inner spans — .tb-* for new markup, .vocabulary-level-* for the blocks
   that vocabulary.js writes with innerHTML (keep in sync) */
.tb-btn--level .tb-lvl,
.vocabulary-level-label {
    display: block;
    line-height: 1.1;
    font-size: var(--tb-size, 13px);
    font-weight: calc(var(--tb-weight, 600) + 100);
}
.tb-btn--level .tb-count,
.vocabulary-level-count {
    display: block;
    line-height: 1.1;
    font-size: calc(var(--tb-size, 13px) - 1px);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0;
    color: var(--tb-muted, rgb(154, 154, 164));
    opacity: .9;
}
.tb-btn:is(.active, .is-active, [aria-pressed="true"]) .tb-count,
.tb-btn:is(.active, .is-active, [aria-pressed="true"]) .vocabulary-level-count {
    color: inherit;
    opacity: 1;
}
/* locked / not-yet-unlocked level */
.tb-btn--level.is-locked { opacity: .55; cursor: not-allowed; }
.tb-btn--level.is-locked:hover { background: transparent; box-shadow: none; }
.tb-btn--level.is-locked .tb-lvl::after,
.tb-btn--level.is-locked .vocabulary-level-label::after { content: ' \1F512'; font-size: .85em; }

/* ── 5 · Text field (search box) ─────────────────────────────── */
.tb-field {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: var(--tb-h, 34px);
    padding: 0 12px;
    background: color-mix(in srgb, black 14%, transparent);
    border: 1px solid var(--tb-border, rgb(69, 69, 78));
    border-radius: var(--tb-radius, 17px);
    color: var(--tb-muted, rgb(154, 154, 164));
}
.tb-field .ico { font-size: 1em; line-height: 1; flex: 0 0 auto; }
.tb-field input {
    border: 0;
    outline: 0;
    background: transparent;
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: var(--tb-size, 13px);
    letter-spacing: var(--tb-tracking, 0px);
    text-transform: var(--tb-uppercase, normal);
    color: var(--tb-text, rgb(226, 226, 230));
    min-width: 0;
    width: 220px;
}

html[data-theme="light"] .tb-seg,
html[data-theme="light"] .tb-field {
    background: var(--bg-card);
    border-color: var(--border);
}

html[data-theme="light"] .tb-btn--danger:hover {
    background: color-mix(in srgb, var(--danger) 10%, transparent);
    color: var(--danger);
}
.tb-field input::placeholder { color: var(--tb-muted, rgb(154, 154, 164)); opacity: .8; }
.tb-field:focus-within { border-color: var(--tb-accent, rgb(78, 201, 176)); }

/* ============================================================
   Top-bar scope
   These rules add one specificity step inside .lessons-top-bar
   so the blocks win over the legacy per-view rules in style.css
   (e.g. `.lessons-top-bar :is(button, input)` font overrides).
   ============================================================ */
.lessons-top-bar .lessons-top-bar-inner {
    gap: 10px var(--tb-gap, 10px);
    column-gap: 18px;
}

/* Top-bar layout primitives. Keep controls and their groups intact while
   allowing flexible fields to consume the space left between them. */
.lessons-top-bar .lessons-top-bar-inner > * {
    min-width: 0;
}
.lessons-top-bar .lessons-top-bar-inner > .tb-seg,
.lessons-top-bar .lessons-top-bar-inner > .lessons-user-filter-wrap,
.lessons-top-bar .lessons-top-bar-inner > .library-toolbar-actions {
    flex-shrink: 0;
}
.lessons-top-bar .lessons-top-bar-inner > .week-toolbar {
    flex: 0 1 auto;
    min-width: 0;
    max-width: 100%;
    flex-wrap: wrap;
}
.lessons-top-bar .lessons-search-field {
    flex: 1 1 240px;
    min-width: min(200px, 100%);
    max-width: none;
}
#view-lessons .lessons-top-bar-inner .lessons-search-field {
    max-width: none;
}
.lessons-top-bar .lessons-search-input {
    min-width: 0;
    flex: 1 1 auto;
}
.lessons-top-bar .lessons-user-filter-wrap,
.lessons-top-bar .lessons-user-filter-combo,
.lessons-top-bar .lessons-user-filter-trigger {
    min-width: 0;
}
.lessons-top-bar .tb-seg,
.lessons-top-bar .week-toolbar,
.lessons-top-bar .notifications-toolbar-actions,
.lessons-top-bar .library-toolbar-actions,
.lessons-top-bar .library-toolbar-group,
.lessons-top-bar .users-toolbar-filters,
.lessons-top-bar .users-toolbar-actions {
    box-sizing: border-box;
}
.reservations-quick-actions,
.lessons-month-filter-row,
.conversations-filter-actions,
.vocabulary-user-actions {
    display: inline-flex;
    align-items: center;
    gap: var(--tb-gap, 8px);
    min-width: 0;
}
.vocabulary-user-actions:not(:has(> :not([hidden]))) {
    display: none;
}

@media (min-width: 721px) {
    #view-vocabulary .vocabulary-top-bar-inner {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 10px;
    }

    #view-vocabulary .vocabulary-user-actions {
        flex: 0 0 auto;
        white-space: nowrap;
    }

    #view-vocabulary .vocabulary-level-toggle {
        flex: 0 0 auto;
        width: max-content;
        max-width: 100%;
        overflow-x: auto;
    }

    #view-vocabulary .vocabulary-search-field.lessons-search-field {
        flex: 1 1 260px;
        width: auto;
        min-width: 180px;
        max-width: none;
    }

    #view-vocabulary .vocabulary-start-test-btn,
    #view-vocabulary .vocabulary-add-btn {
        flex: 0 0 auto;
    }
}

.lessons-top-bar .period-control {
    flex: 0 0 auto;
}
.lessons-top-bar .week-toolbar > .period-control {
    order: 0;
}
#view-reservations .week-toolbar {
    margin-left: 0;
    justify-content: flex-start;
}
#view-statistics .week-toolbar {
    order: 0;
    margin-left: 0;
    margin-right: 0;
}
#view-statistics .statistics-topbar-totals {
    order: 1;
    margin-right: 0;
}
.lessons-top-bar .period-control .tb-seg-label {
    flex: 0 0 26ch;
    width: 26ch;
    min-width: 26ch;
    max-width: 26ch;
}
.lessons-top-bar .tb-btn {
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
}
.lessons-top-bar .tb-btn:focus-visible,
.lessons-top-bar .tb-seg .tb-btn:focus-visible,
.lessons-top-bar .tb-btn--primary:focus-visible {
    outline: 2px solid var(--tb-accent, rgb(78, 201, 176));
    outline-offset: 2px;
}
.lessons-top-bar .tb-btn:disabled { opacity: .4; cursor: not-allowed; }
.lessons-top-bar .tb-btn:disabled:hover { box-shadow: none; }
.lessons-top-bar .tb-seg-label {
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
}

/* ── Existing search fields (lessons / vocabulary / library) ───
   Markup keeps .lessons-search-* (JS hooks); restyle to tb-field. */
.lessons-top-bar .lessons-search-field {
    align-items: center;
    height: var(--tb-h, 34px);
    padding: 0 8px 0 12px;
    background: color-mix(in srgb, black 14%, transparent);
    border: 1px solid var(--tb-border, rgb(69, 69, 78));
    border-radius: var(--tb-radius, 17px);
    box-shadow: none;
}
.lessons-top-bar .lessons-search-field:focus-within {
    border-color: var(--tb-accent, rgb(78, 201, 176));
    box-shadow: 0 0 0 1px color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 35%, transparent);
}
.lessons-top-bar .lessons-search-input {
    min-height: 0;
    height: auto;
    padding: 0;
    width: 100%;
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: var(--tb-size, 13px);
    font-weight: var(--tb-weight, 600);
    color: var(--tb-text, rgb(226, 226, 230));
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
}
.lessons-top-bar .lessons-search-input:focus,
.lessons-top-bar .lessons-search-input:focus-visible {
    outline: none;
    border-color: transparent;
    box-shadow: none;
}
.lessons-top-bar .lessons-search-input::placeholder {
    color: var(--tb-muted, rgb(154, 154, 164));
    opacity: .85;
}
.lessons-top-bar .lessons-search-clear {
    width: 24px;
    height: 24px;
    min-width: 24px;
    padding: 0;
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: 15px;
    color: var(--tb-muted, rgb(154, 154, 164));
    background: transparent;
    border: 0;
    border-radius: 999px;
    cursor: pointer;
}
.lessons-top-bar .lessons-search-clear:hover {
    background: color-mix(in srgb, var(--tb-hover-bg, rgb(51, 51, 59)) 60%, transparent);
    color: var(--tb-text, rgb(226, 226, 230));
}
.lessons-top-bar .lessons-search-clear:focus-visible {
    outline: 2px solid var(--tb-accent, rgb(78, 201, 176));
    outline-offset: 2px;
}

/* Vocabulary Start-Test: keep the legacy disabled dimming (its old rule has
   higher specificity than .tb-btn--primary), recolor it to the tb tokens. */
.tb-btn--primary.vocabulary-start-test-btn:disabled,
.tb-btn--primary.vocabulary-start-test-btn:disabled:hover {
    background: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 32%, transparent);
    border-color: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 30%, var(--tb-border, rgb(69, 69, 78)));
    color: var(--tb-muted, rgb(154, 154, 164));
}

/* ── Student-filter combo trigger (lessons / conversations /    */
/* vocabulary) — styled as a tb-btn with a trailing chevron. ──── */
.lessons-top-bar .lessons-user-filter-trigger {
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    height: var(--tb-h, 34px);
    gap: 8px;
    padding: 0 12px;
    font-family: var(--tb-font, 'Segoe UI', system-ui, sans-serif);
    font-size: var(--tb-size, 13px);
    font-weight: var(--tb-weight, 600);
    letter-spacing: var(--tb-tracking, 0px);
    text-transform: var(--tb-uppercase, normal);
    text-align: left;
    color: var(--tb-text, rgb(226, 226, 230));
    background: var(--tb-bg, color-mix(in srgb, #fff 5%, transparent));
    border: 1px solid var(--tb-border, rgb(69, 69, 78));
    border-radius: var(--tb-radius, 17px);
    cursor: pointer;
    box-shadow: none;
}
.lessons-top-bar .lessons-user-filter-trigger:hover {
    background: var(--tb-hover-bg, rgb(51, 51, 59));
    color: var(--tb-text, rgb(226, 226, 230));
    border-color: var(--tb-border, rgb(69, 69, 78));
}
.lessons-top-bar .lessons-user-filter-trigger:focus {
    outline: none;
    border-color: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 60%, var(--tb-border, rgb(69, 69, 78)));
    box-shadow: none;
}
.lessons-top-bar .lessons-user-filter-trigger:focus-visible {
    outline: 2px solid var(--tb-accent, rgb(78, 201, 176));
    outline-offset: 2px;
}
.lessons-top-bar .lessons-user-filter-trigger[aria-expanded="true"] {
    border-color: color-mix(in srgb, var(--tb-accent, rgb(78, 201, 176)) 60%, var(--tb-border, rgb(69, 69, 78)));
}
.lessons-top-bar .lessons-user-filter-chevron {
    color: var(--tb-muted, rgb(154, 154, 164));
}
.lessons-top-bar .lessons-user-filter-trigger-text {
    color: var(--tb-text, rgb(226, 226, 230));
}

/* ── Library toolbar: icon buttons use the .library-toolbar-icon */
/* span (16×16, JS injects SVGs) — keep it crisp inside tb-btns. ─ */
.lessons-top-bar .tb-btn .library-toolbar-icon {
    width: 15px;
    height: 15px;
    min-width: 15px;
    color: currentColor;
}
.lessons-top-bar .tb-btn .library-toolbar-icon svg {
    width: 15px;
    height: 15px;
}

/* Keep icon + label actions visually anchored to the left edge. Icon-only
   controls and segmented buttons retain their centered treatment. */
.lessons-top-bar .tb-btn:has(> .library-toolbar-icon):has(> .library-toolbar-label),
.lessons-top-bar .tb-btn:has(> .users-detail-edit-btn-icon):has(> span) {
    justify-content: flex-start;
    text-align: left;
}

/* ── Add-user (Users bar) — svg keeps .users-detail-edit-btn-icon ─ */
.lessons-top-bar .tb-btn--primary .users-detail-edit-btn-icon {
    width: 15px;
    height: 15px;
    color: currentColor;
    opacity: 1;
}

/* ============================================================
   Mobile layout (mirrors the max-720px rules in style.css that
   relied on legacy button classes now replaced by tb-btn)
   ============================================================ */
@media (max-width: 720px) {
    .lessons-top-bar .lessons-top-bar-inner {
        align-items: stretch;
        row-gap: 8px;
    }

    .lessons-top-bar .lessons-top-bar-inner > .week-toolbar {
        flex: 1 1 100%;
        width: 100%;
        max-width: 100%;
        margin-left: 0;
        flex-wrap: wrap;
    }

    .lessons-top-bar .lessons-search-field {
        flex: 1 1 100%;
        width: 100%;
        min-width: 0;
        max-width: none;
    }

    .lessons-top-bar .lessons-top-bar-inner > .period-control,
    .lessons-top-bar .week-toolbar .period-control {
        display: flex;
        flex: 1 1 100%;
        width: 100%;
        min-width: 0;
    }
    .lessons-top-bar .period-control .tb-btn--icon {
        flex: 0 0 var(--tb-h, 34px);
    }
    .lessons-top-bar .period-control .tb-seg-label {
        flex: 1 1 auto;
        width: auto;
        min-width: 0;
        max-width: none;
    }

    /* Reservations: view, period, then the contextual actions. */
    #view-reservations .week-toolbar > .tb-seg:first-child { order: 1; }
    #view-reservations .week-toolbar > .period-control { order: 2; }
    #view-reservations .week-toolbar > .tb-btn,
    #view-reservations .week-toolbar > .tb-seg:not(.period-control) { order: 3; }
    #view-reservations .week-toolbar > .tb-seg,
    #view-reservations .week-toolbar > .tb-btn { min-width: 0; }

    /* Lessons: month, search, student filter, contextual actions. */
    #view-lessons .lessons-top-bar-inner .lessons-month-toolbar { order: 2; }
    #view-lessons .lessons-top-bar-inner .lessons-search-field { order: 3; }
    #view-lessons .lessons-top-bar-inner .lessons-user-filter-wrap {
        order: 4;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-lessons .lessons-top-bar-inner .lessons-canboard-actions {
        order: 5;
        flex: 1 1 100%;
        width: 100%;
    }

    /* Conversations: title, mode, student filter, refresh. */
    #view-conversations .conversations-title-block { order: 1; }
    #view-conversations .conversations-mode-switch { order: 2; }
    #view-conversations .conversations-user-filter-wrap {
        order: 3;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-conversations #conversations-refresh { order: 4; }

    /* Notifications: title followed by full-width actions. */
    #view-notifications .notifications-view-title { order: 1; }
    #view-notifications .notifications-toolbar-actions {
        order: 2;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-notifications .notifications-toolbar-actions .tb-btn {
        flex: 1 1 0;
    }

    /* Statistics: totals, period, then the month action. */
    #view-statistics .statistics-topbar-totals { order: 1; }
    #view-statistics .week-toolbar {
        order: 2;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-statistics .week-toolbar > .period-control { order: 1; }
    #view-statistics .week-toolbar > .tb-btn {
        order: 2;
        flex: 1 1 100%;
    }

    /* Vocabulary: search, student, filters, actions, metadata. */
    #view-vocabulary .vocabulary-search-field.lessons-search-field {
        order: 1;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-vocabulary .vocabulary-user-filter-wrap {
        order: 2;
        flex: 1 1 100%;
        width: 100%;
    }
    #view-vocabulary .vocabulary-level-toggle,
    #view-vocabulary .vocabulary-type-toggle {
        order: 3;
        flex: 1 1 100%;
        width: 100%;
        max-width: 100%;
    }
    #view-vocabulary .vocabulary-type-toggle { display: inline-flex; }
    #view-vocabulary .vocabulary-start-test-btn,
    #view-vocabulary .vocabulary-add-btn { order: 4; }
    /* Library: search, item filters, filter actions, create actions. */
    #view-library .library-search-field.lessons-search-field {
        order: 1;
        flex: 1 1 100%;
        width: 100%;
        min-width: 0;
        max-width: none;
    }
    #view-library .library-toolbar-actions { order: 2; }
    #view-library .library-type-filters { order: 1; }
    #view-library .library-toolbar-group--filters { order: 2; }
    #view-library .library-toolbar-group--create { order: 3; }

    /* Users: status filters, then add-user action. */
    #view-users .users-toolbar-filters { order: 1; }
    #view-users .users-toolbar-actions { order: 2; }

    .conversations-mode-switch.tb-seg {
        width: 100%;
        justify-content: stretch;
    }
    .conversations-mode-switch.tb-seg .tb-btn { flex: 1 1 0; }

    #view-library .library-type-filters.tb-seg {
        flex: 1 1 100%;
        justify-content: stretch;
    }
    #view-library .library-type-filters.tb-seg .tb-btn { flex: 1 1 0; }
    #view-library .library-toolbar-group--create {
        flex: 1 1 100%;
        justify-content: stretch;
        display: flex;
    }
    #view-library .library-toolbar-group--create .tb-btn {
        flex: 1 1 0;
        justify-content: center;
    }

    .vocabulary-level-toggle.tb-seg {
        flex: 1 0 100%;
        max-width: 100%;
        overflow-x: auto;
    }
    .vocabulary-level-toggle.tb-seg .tb-btn { flex: 0 0 auto; }

    .lessons-top-bar .lessons-top-bar-inner > .mobile-nav-slot {
        order: 0;
        flex: 0 0 auto;
        align-self: flex-start;
        height: 44px;
    }

    /* Flatten compound period toolbars so wrapped controls restart at the
       viewport edge instead of staying indented beside the hamburger. */
    #view-reservations .week-toolbar,
    #view-lessons .lessons-month-toolbar,
    #view-statistics .week-toolbar {
        display: contents;
    }
    #view-reservations .week-toolbar > .period-control,
    #view-lessons .lessons-month-toolbar > .period-control,
    #view-statistics .week-toolbar > .period-control {
        flex: 1 1 calc(100% - 52px);
        width: auto;
        max-width: calc(100% - 52px);
        min-width: 0;
        box-sizing: border-box;
    }
    #view-reservations .week-toolbar > *,
    #view-lessons .lessons-month-toolbar > *,
    #view-statistics .week-toolbar > * {
        min-width: 0;
        box-sizing: border-box;
    }
    #view-statistics .week-toolbar > .period-control { order: 0; }
    #view-lessons .lessons-month-toolbar > .tb-btn { order: 1; }

    /* Final first-row placement overrides the view-specific lane orders above. */
    #view-reservations .lessons-top-bar-inner > .week-toolbar,
    #view-lessons .lessons-top-bar-inner .lessons-month-toolbar,
    #view-conversations .lessons-top-bar-inner .conversations-title-block,
    #view-notifications .lessons-top-bar-inner .notifications-toolbar-actions,
    #view-statistics .lessons-top-bar-inner .statistics-topbar-totals,
    #view-vocabulary .lessons-top-bar-inner .vocabulary-search-field,
    #view-library .lessons-top-bar-inner .library-search-field,
    #view-users .lessons-top-bar-inner .users-toolbar-filters,
    #view-account .lessons-top-bar-inner .account-view-title {
        order: 0;
        flex: 1 1 0;
        width: auto;
        min-width: 0;
        max-width: none;
    }
    #view-lessons .lessons-top-bar-inner .lessons-month-toolbar {
        flex-basis: 0;
    }
    #view-reservations .week-toolbar > .period-control { order: 0; }
    #view-reservations .week-toolbar > .tb-seg:first-child { order: 1; }
    #view-statistics .lessons-top-bar-inner > .week-toolbar { order: 0; }
    #view-statistics .lessons-top-bar-inner > .statistics-topbar-totals { order: 1; }
}

/* Authoritative mobile top-bar grid. The first row always contains the menu
   button plus one useful control; every later row restarts at the left edge. */
@media (max-width: 720px) {
    .lessons-top-bar .lessons-top-bar-inner {
        display: grid;
        grid-template-columns: 44px minmax(0, 1fr);
        grid-auto-flow: row;
        grid-auto-rows: max-content;
        align-items: start;
        gap: 8px;
    }

    .lessons-top-bar .lessons-top-bar-inner > *,
    #view-reservations .week-toolbar > *,
    #view-lessons .lessons-month-toolbar > *,
    #view-statistics .week-toolbar > * {
        order: 0;
        min-width: 0;
        max-width: 100%;
        box-sizing: border-box;
    }

    .lessons-top-bar .lessons-top-bar-inner > .mobile-nav-slot {
        grid-column: 1;
        grid-row: 1;
        align-self: start;
        width: 44px;
        height: 44px;
        margin: 0;
    }

    .lessons-top-bar .lessons-top-bar-inner > :not(.mobile-nav-slot),
    #view-reservations .week-toolbar > *,
    #view-lessons .lessons-month-toolbar > *,
    #view-statistics .week-toolbar > * {
        grid-column: 1 / -1;
        width: 100%;
        margin-left: 0;
        margin-right: 0;
    }

    #view-reservations .week-toolbar,
    #view-lessons .lessons-month-toolbar,
    #view-statistics .week-toolbar {
        display: contents;
    }

    /* Period summary pill is desktop-only */
    #view-reservations .week-toolbar > .period-summary-wrap {
        display: none !important;
    }

    #view-reservations .week-toolbar > .period-control,
    #view-lessons .lessons-month-toolbar > .period-control,
    #view-statistics .week-toolbar > .period-control,
    #view-conversations .lessons-top-bar-inner > .conversations-title-block,
    #view-notifications .lessons-top-bar-inner > .notifications-view-title,
    #view-account .lessons-top-bar-inner > .account-view-title,
    #view-vocabulary .lessons-top-bar-inner > .vocabulary-search-field,
    #view-library .lessons-top-bar-inner > .library-search-field,
    #view-users .lessons-top-bar-inner > .users-toolbar-filters {
        grid-column: 2;
        grid-row: 1;
        width: 100%;
        max-width: 100%;
        align-self: center;
    }

    .lessons-top-bar .period-control {
        display: flex;
        min-width: 0;
    }

    .lessons-top-bar .period-control .tb-seg-label {
        flex: 1 1 auto;
        width: auto;
        min-width: 0;
        max-width: none;
    }

    #view-reservations .week-toolbar > .tb-seg:not(.period-control),
    #view-lessons .lessons-month-toolbar > .tb-btn,
    #view-statistics .week-toolbar > .tb-btn,
    #view-conversations .conversations-mode-switch,
    #view-notifications .notifications-toolbar-actions,
    #view-vocabulary .vocabulary-level-toggle,
    #view-vocabulary .vocabulary-type-toggle,
    #view-library .library-type-filters,
    #view-library .library-toolbar-group--create,
    #view-users .users-status-toggle {
        justify-self: stretch;
    }

    #view-reservations .week-toolbar > .tb-seg:not(.period-control) .tb-btn,
    #view-conversations .conversations-mode-switch .tb-btn,
    #view-library .library-type-filters .tb-btn,
    #view-library .library-toolbar-group--create .tb-btn {
        flex: 1 1 0;
    }

    .reservations-quick-actions,
    .lessons-month-filter-row,
    .conversations-filter-actions,
    .vocabulary-user-actions {
        display: flex;
        width: 100%;
        min-width: 0;
        gap: 8px;
    }

    .reservations-quick-actions > .tb-btn {
        flex: 1 1 0;
        min-width: 0;
    }

    .lessons-month-filter-row > #lessons-month-today,
    .conversations-filter-actions > #conversations-refresh,
    .vocabulary-user-actions > .vocabulary-add-btn {
        flex: 0 0 auto;
    }

    .lessons-month-filter-row > .lessons-user-filter-wrap,
    .conversations-filter-actions > .lessons-user-filter-wrap,
    .vocabulary-user-actions > .vocabulary-user-filter-wrap {
        flex: 1 1 0;
        width: auto;
        min-width: 0;
        max-width: none;
    }

    .lessons-month-filter-row .lessons-user-filter-combo,
    .conversations-filter-actions .lessons-user-filter-combo,
    .vocabulary-user-actions .lessons-user-filter-combo {
        width: 100%;
    }

    #view-notifications .lessons-top-bar-inner > .notifications-toolbar-actions {
        grid-column: 2;
        grid-row: 1;
        display: flex;
        width: 100%;
        min-width: 0;
        margin: 0;
        gap: 8px;
        align-self: center;
    }

    #view-notifications .notifications-toolbar-actions > .tb-btn {
        flex: 1 1 0;
        min-width: 0;
        padding-inline: 8px;
    }

    #view-users .lessons-top-bar-inner {
        grid-template-columns: 44px minmax(0, 1fr) 34px;
    }

    #view-users .lessons-top-bar-inner > .users-toolbar-filters {
        grid-column: 2;
        grid-row: 1;
        width: 100%;
        align-self: center;
    }

    #view-users .users-status-toggle {
        display: flex;
        width: 100%;
    }

    #view-users .users-status-toggle > .tb-btn {
        flex: 1 1 0;
        min-width: 0;
        padding-inline: 8px;
    }

    #view-users .lessons-top-bar-inner > .users-toolbar-actions {
        grid-column: 3;
        grid-row: 1;
        width: 34px;
        margin: 0;
        align-self: center;
    }

    #view-users #btn-add-user {
        width: 34px;
        min-width: 34px;
        padding: 0;
        justify-content: center;
    }

    #view-users #btn-add-user > span {
        display: none;
    }

    #view-conversations .lessons-top-bar-inner > .conversations-mode-switch {
        grid-column: 2;
        grid-row: 1;
        width: 100%;
        align-self: center;
    }

    #view-notifications .lessons-top-bar-inner {
        grid-template-columns: 44px minmax(0, 1fr) auto;
    }

    #view-notifications .lessons-top-bar-inner > .notifications-toolbar-actions {
        display: contents;
    }

    #view-notifications #notifications-mark-all-read {
        grid-column: 2;
        grid-row: 1;
        width: 100%;
        align-self: center;
    }

    #view-notifications #notifications-refresh {
        grid-column: 3;
        grid-row: 1;
        width: auto;
        align-self: center;
    }

    #view-library .library-toolbar-actions {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
    }

    #view-library .library-type-filters.tb-seg {
        order: 1;
        flex: 1 1 0;
        width: auto;
        min-width: 0;
        justify-content: stretch;
    }

    #view-library .library-type-filters.tb-seg > .tb-btn {
        flex: 1 1 0;
        min-width: 0;
        padding-inline: 8px;
    }

    #view-library .library-toolbar-group--filters {
        order: 1;
        display: flex;
        flex: 0 0 auto;
        justify-content: flex-start;
    }

    #view-library .library-toolbar-group--filters > .tb-btn {
        justify-content: center;
        text-align: center;
    }

    #view-library .library-toolbar-group--create {
        order: 2;
        flex: 1 1 100%;
    }

    #view-statistics #stats-month-today {
        grid-column: 1 / -1;
        grid-row: 2;
        width: 100%;
    }

    #view-statistics .statistics-topbar-totals {
        grid-column: 1 / -1;
        grid-row: 3;
        width: 100%;
    }
}
