/* ============================================================================
 *  tc-shell.css — Taleclip SPA shell (UI revamp Phase 2, WP-B)
 *
 *  Restyles the EXISTING shell of /app: sidebar (#sidebar), top bar
 *  (header.top-bar) and the shared page heading (.page-title). Loaded after
 *  themes.css + tc-ui.css, so it wins ties against base.css/pages.css.
 *
 *  Rules of the road
 *  - Tokens only (var(--tc-*)). color-mix() derives tints from the active
 *    theme's tokens, so every theme/mode (incl. highContrast, emeraldCircuit)
 *    stays coherent without hard-coded violet.
 *  - The sidebar lives inside a <!-- DESIGNER:BEGIN section=sidebar --> region
 *    that a separate Designer process can regenerate, so it is restyled
 *    CSS-only via its existing classes. The only added sidebar node (brand
 *    tagline) is injected by tc-shell.js.
 *  - No ids/classes/handlers are renamed. JS rewrites `className` on
 *    #systemStatusBadge and #headerQueueBar, so those are styled through
 *    their existing classes only.
 *  - `!important` is used ONLY where an existing global `!important` rule
 *    must be beaten; each use says which rule it answers.
 * ========================================================================== */

/* ---------------------------------------------------------------------------
 * 0. Local shell variables (derived from theme tokens)
 * ------------------------------------------------------------------------ */
.app-layout {
    --tcs-tint: color-mix(in srgb, var(--tc-primary) 12%, transparent);
    --tcs-tint-strong: color-mix(in srgb, var(--tc-primary) 22%, transparent);
    --tcs-line: color-mix(in srgb, var(--tc-border) 75%, transparent);
    --tcs-surface: color-mix(in srgb, var(--tc-card-bg) 82%, transparent);
    --tcs-ctl-h: 34px;
    --tcs-radius: 10px;
}

/* ---------------------------------------------------------------------------
 * 1. Sidebar frame
 * ------------------------------------------------------------------------ */
.app-layout .sidebar {
    background:
        radial-gradient(130% 34% at 0% 0%, color-mix(in srgb, var(--tc-primary) 16%, transparent), transparent 72%),
        var(--tc-sidebar-bg);
    border-right: 1px solid var(--tcs-line);
    box-shadow: 1px 0 0 color-mix(in srgb, var(--tc-primary) 6%, transparent);
}

/* Brand lockup: mark + wordmark + tagline (tagline injected by tc-shell.js) */
.sidebar .sidebar-logo {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    column-gap: 11px;
    row-gap: 1px;
    align-items: center;
    padding: 16px 16px 14px;
    border-bottom: 1px solid var(--tcs-line);
}
.sidebar .sidebar-logo .logo-icon {
    grid-row: 1 / span 2;
    width: 38px;
    height: 38px;
    border-radius: 11px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.15rem;
    line-height: 1;
    color: #fff;
    background:
        linear-gradient(140deg, var(--tc-primary) 0%, var(--tc-accent) 55%, var(--tc-navy-deep) 130%);
    box-shadow:
        0 6px 18px -6px color-mix(in srgb, var(--tc-primary) 75%, transparent),
        inset 0 1px 0 rgba(255, 255, 255, 0.22);
}
/* Kit logo mark (Home rebuild): the SVG play-triangle replaces the sparkle
   glyph. The gradient chip is dropped when the mark is present. */
.sidebar .sidebar-logo .logo-icon .logo-mark { display: block; width: 100%; height: 100%; }
.sidebar .sidebar-logo .logo-icon:has(.logo-mark) { background: transparent; box-shadow: none; }
.sidebar .sidebar-logo .logo-text {
    align-self: end;
    font-size: 1.28rem;
    font-weight: 800;
    letter-spacing: -0.01em;
    line-height: 1.1;
    color: var(--tc-text);
}
.sidebar .sidebar-logo .tcs-brand-tagline {
    align-self: start;
    font-size: 0.72rem;
    font-weight: 500;
    letter-spacing: 0.02em;
    line-height: 1.2;
    color: var(--tc-text-muted);
    white-space: nowrap;
}
.sidebar .sidebar-logo:not(:has(.tcs-brand-tagline)) .logo-icon { grid-row: auto; }
.sidebar .sidebar-logo:not(:has(.tcs-brand-tagline)) .logo-text { align-self: center; }

/* Scroll area */
.sidebar .sidebar-nav {
    background: transparent;
    padding: 12px 10px 14px;
    scrollbar-width: thin;
    scrollbar-color: color-mix(in srgb, var(--tc-primary) 35%, transparent) transparent;
}
.sidebar .sidebar-nav::-webkit-scrollbar { width: 6px; }
.sidebar .sidebar-nav::-webkit-scrollbar-thumb {
    background: color-mix(in srgb, var(--tc-primary) 35%, transparent);
    border-radius: 3px;
}

/* ---------------------------------------------------------------------------
 * 2. Nav items
 * ------------------------------------------------------------------------ */
.sidebar .nav-item {
    position: relative;
    gap: 10px;
    padding: 8px 12px;
    margin-bottom: 2px;
    min-height: 38px;
    border-radius: 11px;
    color: var(--tc-text-muted);
    font-size: 0.9rem;
    transition: background-color 0.16s var(--tc-ease, ease), color 0.16s var(--tc-ease, ease), box-shadow 0.16s var(--tc-ease, ease);
}
.sidebar .nav-item .nav-icon,
.sidebar .nav-group .nav-icon {
    flex: 0 0 22px;
    width: 22px;
    height: 22px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.98rem;
    line-height: 1;
    text-align: center;
}
.sidebar .nav-item .nav-label {
    font-size: 0.9rem;
    font-weight: 500;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.sidebar .nav-item:hover {
    background: var(--tcs-tint);
    color: var(--tc-text);
}

/* ACTIVE = violet gradient pill + soft glow.
   !important answers themes.css "Cross-theme global surface adoption":
   `.nav-item.active { background: var(--tc-sidebar-active) !important; color: var(--tc-text) !important }`.
   Higher specificity (.sidebar prefix) + !important wins over it. Uses the
   theme's own --tc-primary / --tc-primary-text pair, so highContrast (sky +
   black text) and midnightViolet (lilac + dark text) stay legible. */
.sidebar .nav-item.active,
.sidebar .nav-item-child.active {
    background: linear-gradient(135deg, var(--tc-primary) 0%, var(--tc-primary-hover) 100%) !important;
    color: var(--tc-primary-text) !important;
    box-shadow:
        0 8px 22px -10px color-mix(in srgb, var(--tc-primary) 85%, transparent),
        0 0 0 1px color-mix(in srgb, var(--tc-primary) 55%, transparent),
        inset 0 1px 0 rgba(255, 255, 255, 0.16);
}
.sidebar .nav-item.active .nav-label { font-weight: 600; }

/* Focus ring: outline (not box-shadow) so it is independent of the
   themes.css `a:focus-visible { box-shadow: … !important }` rule and of the
   active-pill glow. */
.sidebar .nav-item:focus-visible,
.sidebar .nav-group:focus-visible,
.sidebar .sidebar-add-btn:focus-visible,
.sidebar .user-info-compact:focus-visible {
    outline: 2px solid var(--tc-primary);
    outline-offset: 2px;
}
.sidebar .nav-item:focus,
.sidebar .nav-group:focus { outline-color: var(--tc-primary); }

/* Designer link (inline style draws its own top rule) */
.sidebar #designerNavLink { border-top-color: var(--tcs-line) !important; }

/* ---------------------------------------------------------------------------
 * 3. Group headers — uppercase letter-spaced section labels + chevron
 * ------------------------------------------------------------------------ */
.sidebar .nav-group-wrap { margin: 0; }
.sidebar .nav-group-wrap + .nav-group-wrap,
.sidebar .nav-item + .nav-group-wrap {
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid color-mix(in srgb, var(--tc-border) 45%, transparent);
}
.sidebar .nav-group {
    gap: 8px;
    padding: 7px 10px 7px 12px;
    min-height: 32px;
    border-radius: 9px;
    color: var(--tc-link);
    transition: background-color 0.16s var(--tc-ease, ease), color 0.16s var(--tc-ease, ease);
}
.sidebar .nav-group .nav-icon {
    flex-basis: 18px;
    width: 18px;
    height: 18px;
    font-size: 0.82rem;
}
.sidebar .nav-group .nav-label {
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: var(--tc-tracking-eyebrow, 0.12em);
    text-transform: uppercase;
}
.sidebar .nav-group:hover {
    background: var(--tcs-tint);
    color: var(--tc-text);
}
/* Chevron: the ▼ glyph is kept in the DOM but drawn as a thin CSS chevron. */
.sidebar .nav-group .nav-group-chevron {
    margin-left: auto;
    font-size: 0;
    width: 7px;
    height: 7px;
    opacity: 0.85;
    border-right: 1.6px solid currentColor;
    border-bottom: 1.6px solid currentColor;
    transform: translateY(-2px) rotate(45deg);
    transition: transform 0.18s var(--tc-ease, ease);
}
.sidebar .nav-group-wrap.expanded .nav-group-chevron {
    transform: translateY(1px) rotate(-135deg);
}
.sidebar .nav-group-items {
    margin: 2px 0 4px 21px;
    padding-left: 8px;
    border-left: 1px solid color-mix(in srgb, var(--tc-primary) 22%, transparent);
}
.sidebar .nav-item-child {
    padding: 7px 10px;
    min-height: 34px;
}
.sidebar .nav-item-child .nav-label { font-size: 0.875rem; }

/* ---------------------------------------------------------------------------
 * 4. Projects / Tips mini-sections + user card
 * ------------------------------------------------------------------------ */
.sidebar .sidebar-section {
    padding: 10px 14px;
    border-top: 1px solid var(--tcs-line);
}
.sidebar .sidebar-section-header {
    color: var(--tc-text-soft);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: var(--tc-tracking-eyebrow, 0.12em);
    margin-bottom: 6px;
    border-bottom: 0;
}
.sidebar .sidebar-add-btn {
    width: 22px;
    height: 22px;
    border-radius: 7px;
    border: 1px solid var(--tcs-line);
    background: transparent;
    color: var(--tc-text-muted);
    line-height: 1;
}
.sidebar .sidebar-add-btn:hover {
    background: var(--tcs-tint-strong);
    border-color: color-mix(in srgb, var(--tc-primary) 50%, transparent);
    color: var(--tc-text);
}
.sidebar .projects-list,
.sidebar .tips-list { color: var(--tc-text-muted); font-size: 0.8rem; }

.sidebar .sidebar-footer {
    padding: 10px 12px 12px;
    border-top: 1px solid var(--tcs-line);
}
.sidebar .user-info-compact {
    gap: 10px;
    padding: 8px 10px;
    border-radius: 14px;
    border: 1px solid var(--tcs-line);
    background: var(--tc-card-glass);
    transition: border-color 0.16s var(--tc-ease, ease), background-color 0.16s var(--tc-ease, ease);
}
.sidebar .user-info-compact:hover {
    border-color: color-mix(in srgb, var(--tc-primary) 55%, transparent);
    background: var(--tcs-tint-strong);
}
.sidebar .user-info-compact .user-avatar {
    flex: 0 0 32px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    background: linear-gradient(140deg, var(--tc-primary), var(--tc-accent));
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--tc-primary) 30%, transparent);
}
.sidebar .user-info-compact .user-email-short {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--tc-text);
    font-weight: 600;
    font-size: 0.88rem;
}
.sidebar .user-info-compact .settings-gear {
    margin-left: auto;
    opacity: 0.75;
    font-size: 0.95rem;
}

/* ---------------------------------------------------------------------------
 * 5. Top bar
 * ------------------------------------------------------------------------ */
.main-content > .top-bar {
    gap: 12px;
    justify-content: flex-start;
    padding: 0 18px 0 22px;
    background: var(--tc-header-bg);
    border-bottom: 1px solid var(--tcs-line);
    box-shadow: 0 1px 0 color-mix(in srgb, var(--tc-primary) 6%, transparent);
}
.top-bar .top-bar-left {
    flex: 0 1 auto;
    min-width: 0;
    gap: 14px;
}
.top-bar .top-bar-center {
    flex: 0 1 auto;
    flex-wrap: nowrap;
    min-width: 0;
    justify-content: flex-start;
    gap: 8px;
}
.top-bar .top-bar-right {
    margin-left: auto;
    flex: 0 0 auto;
    gap: 8px;
}

/* Route-aware heading (#breadcrumb, text kept in sync by tc-shell.js) */
.top-bar #breadcrumb {
    flex: 0 0 auto;
    min-width: 0;
    max-width: 260px;
    gap: 6px;
    font-size: 1.02rem;
    font-weight: 700;
    letter-spacing: -0.01em;
    color: var(--tc-text);
    white-space: nowrap;
    overflow: hidden;
}
.top-bar #breadcrumb .breadcrumb-item {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}
.top-bar #breadcrumb .breadcrumb-item:last-child { flex: 0 0 auto; max-width: 100%; }
/* When space is short the group crumb ellipsizes first; the page name stays. */
.top-bar #breadcrumb .breadcrumb-item:not(:last-child) {
    flex: 0 1 auto;
    color: var(--tc-text-soft);
    font-weight: 600;
}
.top-bar #breadcrumb .breadcrumb-item:not(:last-child)::after {
    color: var(--tc-text-soft);
    margin-left: 6px;
}

/* Status pills ----------------------------------------------------------- */
.top-bar .system-status-badge {
    height: 30px;
    padding: 0 12px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    white-space: nowrap;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: color-mix(in srgb, currentColor 13%, transparent);
    border: 1px solid color-mix(in srgb, currentColor 38%, transparent);
}
.top-bar .system-status-badge::before {
    width: 7px;
    height: 7px;
    box-shadow: 0 0 8px currentColor;
}
/* Light modes: the bright status greens/ambers from generation.css are too
   pale on lavender — use the theme's semantic tokens instead. */
[data-theme="light"] .top-bar .system-status-badge.status-ready { color: var(--tc-success); }
[data-theme="light"] .top-bar .system-status-badge.status-ready::before { background: var(--tc-success); }
[data-theme="light"] .top-bar .system-status-badge.status-generating { color: var(--tc-warning); }
[data-theme="light"] .top-bar .system-status-badge.status-generating::before { background: var(--tc-warning); }
[data-theme="light"] .top-bar .system-status-badge.status-busy { color: var(--tc-danger); }
[data-theme="light"] .top-bar .system-status-badge.status-busy::before { background: var(--tc-danger); }

.top-bar .header-queue-bar {
    height: 30px;
    padding: 0 12px;
    gap: 8px;
    border-radius: 999px;
    border: 1px solid var(--tcs-line);
    background: var(--tcs-surface);
    color: var(--tc-text-muted);
    font-size: 0.78rem;
}
.top-bar .header-queue-bar .header-queue-label {
    text-transform: none;
    letter-spacing: 0;
    font-weight: 600;
    color: var(--tc-text);
}
.top-bar .header-queue-bar .header-queue-track {
    width: 46px;
    height: 5px;
    background: color-mix(in srgb, var(--tc-text) 12%, transparent);
}
.top-bar .header-queue-bar.status-idle .header-queue-fill {
    background: var(--tc-progress-fill, var(--tc-primary));
}
.top-bar .header-queue-bar .header-queue-text { min-width: 0; }
.top-bar .header-queue-bar.status-idle .header-queue-text { color: var(--tc-text-muted); }
[data-theme="light"] .top-bar .header-queue-bar.status-queued .header-queue-text { color: var(--tc-warning); }
[data-theme="light"] .top-bar .header-queue-bar.status-generating .header-queue-text { color: var(--tc-success); }

/* RAM / GPU readout (shown by auth.js for admins via inline display) */
.top-bar .header-stats-strip {
    height: 30px;
    margin-left: 4px;
    padding-left: 14px;
    border-left: 1px solid var(--tcs-line);
    gap: 8px;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--tc-text-muted);
    font-family: var(--tc-font-sans);
    /* last to give way: clips (never overlaps) if the bar is crowded */
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
}

/* Right-cluster controls ------------------------------------------------- */
.top-bar .top-bar-right .btn-small,
.top-bar .top-bar-right .user-credits-badge {
    height: var(--tcs-ctl-h);
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0 14px;
    border-radius: var(--tcs-radius);
    font-size: 0.8rem;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    text-decoration: none;
}

/* Square icon buttons: theme toggle, system ⚙, ⋮ overflow, 📷 */
.top-bar .header-theme-toggle,
.top-bar .btn-small.btn-system-toggle,
.top-bar .header-overflow-btn,
.top-bar .header-screenshot-only {
    width: var(--tcs-ctl-h);
    min-width: var(--tcs-ctl-h);
    height: var(--tcs-ctl-h);
    padding: 0;
    border-radius: var(--tcs-radius);
    border: 1px solid var(--tcs-line);
    background: var(--tcs-surface);
    color: var(--tc-text);
    font-size: 0.95rem;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.top-bar .header-theme-toggle:hover,
.top-bar .btn-small.btn-system-toggle:hover,
.top-bar .header-overflow-btn:hover,
.top-bar .header-screenshot-only:hover,
.top-bar .header-overflow-wrap.open .header-overflow-btn {
    background: var(--tcs-tint-strong);
    border-color: color-mix(in srgb, var(--tc-primary) 55%, transparent);
    color: var(--tc-text);
}
.top-bar .btn-small.btn-system-toggle.active {
    background: color-mix(in srgb, var(--tc-danger) 18%, transparent);
    border-color: color-mix(in srgb, var(--tc-danger) 50%, transparent);
    color: var(--tc-danger);
}

/* Credits pill (violet) */
.top-bar .user-credits-badge {
    border-radius: 999px;
    background: color-mix(in srgb, var(--tc-primary) 30%, transparent);
    border: 1px solid color-mix(in srgb, var(--tc-primary) 60%, transparent);
    color: var(--tc-text);
    font-variant-numeric: tabular-nums;
    box-shadow: 0 0 16px -6px color-mix(in srgb, var(--tc-primary) 80%, transparent);
}

/* Free Memory (was an inline red→orange gradient): danger chip from tokens */
.top-bar .header-freemem {
    border: 0;
    color: #fff;
    background: linear-gradient(90deg, var(--tc-danger), color-mix(in srgb, var(--tc-danger) 45%, var(--tc-amber)));
    box-shadow: 0 6px 16px -8px color-mix(in srgb, var(--tc-danger) 80%, transparent);
}
.top-bar .header-freemem:hover:not(:disabled) { filter: brightness(1.08); }
.top-bar .header-freemem-msg {
    font-size: 11px;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Buy Credits stays AMBER (accent chip). base.css paints it with
   `--accent !important`; only the shape is changed here. */
.top-bar .btn-buy-credits { border: 0; }

/* Admin stays ORANGE/AMBER (accent chip) via the theme's --tc-admin token. */
.top-bar .btn-small.btn-admin {
    background: var(--tc-admin);
    border: 0;
    color: #000;
}
.top-bar .btn-small.btn-admin:hover { background: var(--tc-admin-hover); }

/* #adminLink is an <a>; auth.js sets inline display:inline-block, so centre
   its label with line-height instead of flex alignment. */
.top-bar a.btn-small.btn-admin { line-height: calc(var(--tcs-ctl-h) - 2px); }

/* Logout = ghost button */
.top-bar .btn-small.btn-logout {
    background: transparent;
    border: 1px solid var(--tcs-line);
    color: var(--tc-text);
}
.top-bar .btn-small.btn-logout:hover {
    background: var(--tcs-tint);
    border-color: color-mix(in srgb, var(--tc-primary) 50%, transparent);
}

/* Focus ring for every header control (outline, independent of the
   themes.css box-shadow ring which still applies on top). */
.top-bar button:focus-visible,
.top-bar a:focus-visible,
.top-bar [tabindex]:focus-visible {
    outline: 2px solid var(--tc-primary);
    outline-offset: 2px;
}

/* Overflow dropdown */
.top-bar .header-overflow-dropdown {
    min-width: 240px;
    padding: 6px;
    border-radius: 14px;
    background: var(--tc-bg-elevated);
    border: 1px solid var(--tc-border);
    box-shadow: var(--tc-shadow), 0 0 0 1px color-mix(in srgb, var(--tc-primary) 10%, transparent);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}
.top-bar .header-overflow-item {
    padding: 9px 12px;
    border-radius: 9px;
    font-size: 0.86rem;
    color: var(--tc-text);
}
.top-bar .header-overflow-item:hover,
.top-bar .header-overflow-item:focus-visible {
    background: var(--tcs-tint-strong);
}
.top-bar .header-overflow-divider { border-top: 1px solid var(--tcs-line); }

/* ---------------------------------------------------------------------------
 * 6. Quick-jump search (markup injected by tc-shell.js)
 * ------------------------------------------------------------------------ */
.tcs-search {
    position: relative;
    flex: 0 1 300px;
    min-width: 0;
}
.tcs-search__box {
    display: flex;
    align-items: center;
    gap: 8px;
    height: var(--tcs-ctl-h);
    width: clamp(180px, 15vw, 300px);
    padding: 0 8px 0 11px;
    box-sizing: border-box;
    border-radius: var(--tcs-radius);
    border: 1px solid var(--tcs-line);
    background: var(--tcs-surface);
    color: var(--tc-text-muted);
    transition: border-color 0.16s var(--tc-ease, ease), box-shadow 0.16s var(--tc-ease, ease);
}
.tcs-search__box:hover { border-color: color-mix(in srgb, var(--tc-primary) 45%, transparent); }
.tcs-search__box:focus-within {
    border-color: var(--tc-primary);
    box-shadow: var(--tc-focus-ring);
}
.tcs-search__icon { flex: 0 0 auto; width: 15px; height: 15px; opacity: 0.8; }
/* !important answers themes.css global
   `input[type=text|search] { background/border/color: … !important }` and
   `input:focus { box-shadow: var(--tc-focus-ring) !important }` — the visible
   field and its focus ring are drawn by .tcs-search__box instead. */
.tcs-search .tcs-search__input {
    flex: 1 1 auto;
    min-width: 0;
    height: 100%;
    padding: 0;
    margin: 0;
    border: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
    outline: none;
    color: var(--tc-text) !important;
    font: inherit;
    font-size: 0.84rem;
}
.tcs-search .tcs-search__input::placeholder { color: var(--tc-text-soft); opacity: 1; }
.tcs-search .tcs-search__input::-webkit-search-cancel-button { display: none; }
.tcs-search__kbd {
    flex: 0 0 auto;
    padding: 2px 6px;
    border-radius: 6px;
    border: 1px solid var(--tcs-line);
    background: color-mix(in srgb, var(--tc-text) 6%, transparent);
    color: var(--tc-text-muted);
    font-family: var(--tc-font-sans);
    font-size: 0.68rem;
    font-weight: 600;
    line-height: 1.3;
    white-space: nowrap;
}
.tcs-search__toggle { display: none; }

.tcs-search__panel {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    width: max(100%, 340px);
    max-height: min(60vh, 440px);
    overflow-y: auto;
    padding: 6px;
    box-sizing: border-box;
    border-radius: 14px;
    border: 1px solid var(--tc-border);
    background: var(--tc-bg-elevated);
    box-shadow: var(--tc-shadow), 0 0 0 1px color-mix(in srgb, var(--tc-primary) 12%, transparent);
    z-index: 1100;
    text-align: left;
}
.tcs-search__panel[hidden] { display: none; }
.tcs-search__opt {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 9px;
    cursor: pointer;
    color: var(--tc-text);
    font-size: 0.87rem;
}
.tcs-search__opt-icon {
    flex: 0 0 22px;
    width: 22px;
    text-align: center;
}
.tcs-search__opt-label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tcs-search__opt-group {
    flex: 0 0 auto;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--tc-text-soft);
}
.tcs-search__opt-ext { font-size: 0.75rem; color: var(--tc-text-soft); }
.tcs-search__opt:hover { background: var(--tcs-tint); }
.tcs-search__opt[aria-selected="true"] {
    background: var(--tcs-tint-strong);
    box-shadow: inset 2px 0 0 var(--tc-primary);
}
.tcs-search__empty {
    padding: 14px 12px;
    color: var(--tc-text-muted);
    font-size: 0.85rem;
    text-align: center;
}

/* 6b. Smart-search rows (header + sidebar share them): workspace icon,
       label with a one-line "why it matched", AI badge for semantic picks. */
.tcs-search__opt-icon svg { display: block; width: 16px; height: 16px; margin: 0 auto; opacity: 0.85; }
.tcs-search__opt-main {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 1px;
}
.tcs-search__opt-reason {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.72rem;
    color: var(--tc-text-soft);
}
.tcs-search__opt-ai {
    flex: 0 0 auto;
    padding: 1px 6px;
    border-radius: 999px;
    border: 1px solid color-mix(in srgb, var(--tc-primary) 40%, transparent);
    background: var(--tcs-tint-strong);
    color: var(--tc-text);
    font-size: 0.62rem;
    font-weight: 700;
    letter-spacing: 0.06em;
}
.tcs-search__status {
    margin-top: 4px;
    padding: 7px 10px 4px;
    border-top: 1px solid var(--tcs-line);
    color: var(--tc-text-soft);
    font-size: 0.72rem;
}

/* 6c. Sidebar search (markup injected by tc-shell.js into .sidebar-footer).
       The sidebar clips sideways, so the list spans its width and opens up. */
.sidebar .tcs-sidesearch { position: relative; margin-bottom: 10px; }
.tcs-sidesearch__box {
    display: flex;
    align-items: center;
    gap: 8px;
    height: 38px;
    padding: 0 10px 0 12px;
    box-sizing: border-box;
    border-radius: 12px;
    border: 1px solid var(--tcs-line);
    background: var(--tcs-surface);
    color: var(--tc-text-muted);
    transition: border-color 0.16s var(--tc-ease, ease), box-shadow 0.16s var(--tc-ease, ease);
}
.tcs-sidesearch__box:hover { border-color: color-mix(in srgb, var(--tc-primary) 45%, transparent); }
.tcs-sidesearch__box:focus-within {
    border-color: var(--tc-primary);
    box-shadow: var(--tc-focus-ring);
}
/* !important answers the same themes.css input overrides as the header box. */
.tcs-sidesearch .tcs-sidesearch__input {
    flex: 1 1 auto;
    min-width: 0;
    height: 100%;
    padding: 0;
    margin: 0;
    border: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
    outline: none;
    color: var(--tc-text) !important;
    font: inherit;
    font-size: 0.86rem;
}
.tcs-sidesearch .tcs-sidesearch__input::placeholder { color: var(--tc-text-soft); opacity: 1; }
.tcs-sidesearch .tcs-sidesearch__input::-webkit-search-cancel-button { display: none; }
.tcs-sidesearch .tcs-sidesearch__panel {
    top: auto;
    bottom: calc(100% + 8px);
    left: 0;
    width: 100%;
    max-height: min(62vh, 460px);
    padding: 5px;
}
.tcs-sidesearch__panel .tcs-search__opt { gap: 8px; padding: 7px 8px; }
.tcs-sidesearch__panel .tcs-search__opt-icon { flex-basis: 18px; width: 18px; }
.tcs-sidesearch__panel .tcs-search__opt-group { display: none; } /* too narrow; the reason line carries context */
.tcs-sidesearch__panel .tcs-search__empty { padding: 12px 8px; font-size: 0.8rem; }

/* ---------------------------------------------------------------------------
 * 7. Page header — one consistent look for every section's h1.page-title
 * ------------------------------------------------------------------------ */
.main-content .page-section .page-title {
    font-size: clamp(1.45rem, 1.1rem + 0.9vw, 1.95rem);
    font-weight: 750;
    letter-spacing: -0.02em;
    line-height: 1.2;
    color: var(--tc-text);
    text-wrap: balance;
}
.main-content .page-section .page-subtitle {
    color: var(--tc-text-muted);
    font-size: 0.95rem;
    margin-top: -16px;
    margin-bottom: 20px;
}

/* ---------------------------------------------------------------------------
 * 8. Legacy primary CTAs → violet (Jeremy: violet is the primary CTA colour)
 *    .btn-hero ("Start Creating") and the header .btn-icon hover were amber
 *    via --accent. Buy Credits / Admin intentionally stay amber.
 * ------------------------------------------------------------------------ */
.main-content .btn-hero {
    background: linear-gradient(135deg, var(--tc-primary), var(--tc-primary-hover));
    color: var(--tc-primary-text);
    box-shadow: 0 10px 24px -12px color-mix(in srgb, var(--tc-primary) 85%, transparent);
}
.main-content .btn-hero:hover {
    background: var(--tc-primary-hover);
    color: var(--tc-primary-text);
}
/* light-mode-overrides.css forces `.page-section button { color … !important }`
   for buttons without btn-primary in the class; keep CTA text legible. */
[data-theme="light"] .main-content .page-section .btn-hero { color: var(--tc-primary-text) !important; }
.main-content .btn-hero:focus-visible,
.main-content .btn-hero-outline:focus-visible {
    outline: 2px solid var(--tc-primary);
    outline-offset: 3px;
}
.top-bar .btn-icon:hover {
    background: var(--tcs-tint-strong);
    border-color: color-mix(in srgb, var(--tc-primary) 55%, transparent);
    color: var(--tc-text);
}

/* ---------------------------------------------------------------------------
 * 9. Mobile bottom nav + "More" sheet (≤768px, pages.css owns layout)
 * ------------------------------------------------------------------------ */
.mobile-bottom-nav {
    background: var(--tc-sidebar-bg);
    border-top: 1px solid var(--tcs-line, var(--tc-border));
}
.mobile-bottom-nav .mobile-nav-item {
    border-radius: 12px;
    color: var(--tc-text-muted);
    font-size: 0.72rem;
    font-weight: 600;
}
.mobile-bottom-nav .mobile-nav-item.active {
    color: var(--tc-link);
    background: color-mix(in srgb, var(--tc-primary) 16%, transparent);
}
.mobile-bottom-nav .mobile-nav-item:focus-visible,
.mobile-more-menu .mobile-more-item:focus-visible,
.mobile-more-menu .mobile-more-close:focus-visible {
    outline: 2px solid var(--tc-primary);
    outline-offset: 2px;
}
.mobile-more-menu .mobile-more-menu-content {
    background: var(--tc-bg-elevated);
    border-top: 1px solid var(--tc-border);
    box-shadow: 0 -12px 40px -12px color-mix(in srgb, var(--tc-primary) 35%, transparent);
}
.mobile-more-menu .mobile-more-section-label {
    color: var(--tc-link);
    font-weight: 700;
    letter-spacing: var(--tc-tracking-eyebrow, 0.12em);
    font-size: 0.68rem;
}
.mobile-more-menu .mobile-more-item {
    border-radius: 12px;
    color: var(--tc-text);
}
.mobile-more-menu .mobile-more-item:hover,
.mobile-more-menu .mobile-more-item:active,
.mobile-more-menu .mobile-more-item.active {
    background: var(--tcs-tint-strong, color-mix(in srgb, var(--tc-primary) 22%, transparent));
}
.mobile-header {
    background: var(--tc-sidebar-bg);
    border-bottom: 1px solid var(--tc-border);
}
@media (max-width: 768px) {
    /* readable tab labels (pages.css shrinks them below 11px) */
    .mobile-bottom-nav .mobile-nav-item > span:not(.nav-icon) { font-size: 12px; line-height: 1.2; }
    .mobile-more-menu .mobile-more-section-label { font-size: 12px; }
    .mobile-more-menu .mobile-more-item { min-height: 44px; }
    .mobile-more-menu .mobile-more-close { min-width: 44px; min-height: 44px; }
}

/* ---------------------------------------------------------------------------
 * 10. Responsive top bar
 *     Everything hidden here stays reachable from the ⋮ overflow menu
 *     (Logout, Buy Credits, Free Memory, Admin are all mirrored there).
 * ------------------------------------------------------------------------ */
@media (min-width: 1681px) {
    /* At the widest widths Logout + Buy Credits + Admin all show, crowding
       the bar; narrow the quick-jump search so the RAM/GPU stats strip is
       not clipped (Home rebuild, 1728px reference width). */
    .top-bar .tcs-search { flex-basis: 180px; }
    .top-bar .tcs-search__box { width: clamp(150px, 9vw, 180px); }
    /* Long breadcrumbs (e.g. SmartAICoach › OnlyFaith) crowd the bar too:
       cap the crumb so the group name ellipsizes and the page name stays.
       The stats strip and Ready pill never shrink: they clip (never
       overlap) when crowded, so protect them and let the search + queue
       track give way instead. */
    .top-bar #breadcrumb { max-width: 180px; }
    .top-bar .header-stats-strip { flex-shrink: 0; }
    .top-bar .system-status-badge { flex-shrink: 0; }
    .top-bar .header-queue-bar .header-queue-track { display: none; }
}
@media (max-width: 1680px) {
    .top-bar .top-bar-right .header-logout { display: none; }
}
@media (max-width: 1600px) {
    .top-bar .top-bar-right .header-buy-credits { display: none; }
    .top-bar .header-queue-bar .header-queue-track { display: none; }
}
@media (max-width: 1440px) {
    .top-bar .top-bar-right .header-admin { display: none !important; } /* beats auth.js inline display */
    .top-bar #breadcrumb { max-width: 200px; }
}
@media (max-width: 1280px) {
    .top-bar .header-stats-strip .header-stats-sep { display: none; }
}
/* Free Memory goes to ⋮ only while RAM/GPU + full search compete for room;
   below 1024px the search is an icon and the stats are hidden, so it fits again. */
@media (min-width: 1025px) and (max-width: 1280px) {
    .top-bar .top-bar-right .header-freemem,
    .top-bar .top-bar-right .header-freemem-msg { display: none !important; } /* inline display set by JS */
}
@media (max-width: 1180px) {
    /* RAM/GPU readout is admin telemetry; collapse it before the controls. */
    .top-bar .header-stats-strip { display: none !important; } /* beats auth.js inline display */
    .tcs-search__box { width: 190px; }
}
@media (max-width: 1024px) {
    .main-content > .top-bar { padding: 0 12px 0 16px; gap: 8px; }
    .top-bar #breadcrumb { max-width: 140px; }
    /* Search collapses to an icon that opens a popover field. */
    .tcs-search { flex: 0 0 auto; position: static; }
    .tcs-search__toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: var(--tcs-ctl-h);
        height: var(--tcs-ctl-h);
        padding: 0;
        border-radius: var(--tcs-radius);
        border: 1px solid var(--tcs-line);
        background: var(--tcs-surface);
        color: var(--tc-text);
        cursor: pointer;
    }
    .tcs-search__toggle:hover { background: var(--tcs-tint-strong); }
    .tcs-search__toggle:focus-visible { outline: 2px solid var(--tc-primary); outline-offset: 2px; }
    .tcs-search__toggle .tcs-search__icon { width: 16px; height: 16px; opacity: 1; }
    .tcs-search__pop {
        display: none;
        position: absolute;
        top: calc(100% + 6px);
        left: 12px;
        right: 12px;
        padding: 8px;
        border-radius: 14px;
        border: 1px solid var(--tc-border);
        background: var(--tc-bg-elevated);
        box-shadow: var(--tc-shadow);
        z-index: 1100;
    }
    .tcs-search.is-open .tcs-search__pop { display: block; }
    .tcs-search__pop .tcs-search__box { width: 100%; }
    .tcs-search__pop .tcs-search__panel {
        position: static;
        width: 100%;
        margin-top: 8px;
        box-shadow: none;
        border-color: var(--tcs-line);
    }
}
@media (max-width: 768px) {
    .main-content > .top-bar { padding: 0 10px 0 12px; }
    /* Tap targets: 44px on the controls that stay on screen. Text badges keep
       their height but are not forced to a 44px width (that overflows 390px). */
    .top-bar .btn-small,
    .top-bar .btn-icon,
    .top-bar .tcs-search__toggle,
    .mobile-bottom-nav .mobile-nav-item,
    .sidebar.mobile-open .nav-item,
    .mobile-more-menu .mobile-more-item,
    .mobile-more-menu .mobile-more-close {
        min-height: 44px;
    }
    .top-bar .btn-icon,
    .top-bar .tcs-search__toggle,
    .mobile-bottom-nav .mobile-nav-item {
        min-width: 44px;
    }
    .top-bar .top-bar-left { gap: 8px; }
    .top-bar #breadcrumb { max-width: 38vw; font-size: 0.95rem; }
    .top-bar .top-bar-left { flex: 1 1 auto; }
    .top-bar .top-bar-right { flex-wrap: nowrap; gap: 6px; }
    .top-bar .tcs-search__kbd { display: none; }
    /* 16px stops mobile browsers from zooming the page when this field focuses. */
    .tcs-search .tcs-search__input,
    .tcs-sidesearch .tcs-sidesearch__input { font-size: 16px; }
    .tcs-sidesearch__box { min-height: 44px; }
    .tcs-search__pop .tcs-search__panel { max-height: min(40dvh, 240px); }
    /* Both are mirrored in the ⋮ menu (Free Memory, Capture Screenshot). */
    .top-bar .top-bar-right .header-freemem,
    .top-bar .top-bar-right .header-screenshot-only { display: none !important; } /* inline display set by JS */
}
@media (max-width: 430px) {
    /* book-studio.css hides `.top-bar-left` here with !important (to save room
       on phones). Keep it so the quick-jump search icon stays reachable, but
       drop the route heading — the page's own h1 carries the title. */
    .top-bar .top-bar-left { display: flex !important; flex: 0 0 auto; }
    .top-bar #breadcrumb { display: none; }
    .top-bar .user-credits-badge { padding: 0 9px; font-size: 0.72rem; }
}

/* ---------------------------------------------------------------------------
 * 10b. Included fragments on the shared page header
 *      templates/tools/diagram.html is {% include %}d into #page-diagram
 *      (/tools/diagram redirects to /app#diagram). Its header uses the
 *      .tc-page-header primitive; keep title / breadcrumb / subtitle stacked.
 * ------------------------------------------------------------------------ */
#page-diagram .dg-page-header.tc-page-header {
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 6px;
    margin-bottom: 16px;
}
/* the header sits on the app background, not on the diagram's light canvas:
   use shell text tokens (diagram.css's --dg-text is a dark canvas ink) */
#page-diagram .dg-page-header .tc-page-header__title { margin: 0; color: var(--tc-text); }
#page-diagram .dg-page-header .tc-page-header__subtitle { color: var(--tc-text-muted); }
#page-diagram .dg-page-header :is(.breadcrumb-page, .breadcrumb-item) { color: var(--tc-text-muted); }
#page-diagram .dg-page-header .breadcrumb-item.current { color: var(--tc-text); }

/* ---------------------------------------------------------------------------
 * 11. Motion preferences
 * ------------------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    .sidebar .nav-item,
    .sidebar .nav-group,
    .sidebar .nav-group-chevron,
    .sidebar .user-info-compact,
    .tcs-search__box,
    .main-content .btn-hero { transition: none; }
    .main-content .btn-hero:hover { transform: none; }
}
