/* ═══════════════════════════════════════════════════════════════════════════════
   Library — Google-Drive-like document store (center pane)
   ═══════════════════════════════════════════════════════════════════════════════
   Renders into #investing-content, the same center pane Watchlists/Portfolios/
   Alerts use. See static/library.js.
*/

/* ── Shell: left folder-tree sidebar + main content ──────────────────── */

/* #investing-content (styles.css) is shared with Watchlists/Portfolios/
   Alerts, which want a narrower, centered column (max-width: 1400px;
   margin: 0 auto; padding: 24px) for their lists/forms. Library keeps that
   SAME max-width/centering — so its overall pane width matches every other
   section's browse-all view exactly when fully open — but cancels only the
   24px padding, since the Drive-style shell wants its sidebar flush against
   the (still centered, still 1400px-capped) box's own left edge rather than
   sitting 24px in from it. .lib-view (the main grid/list pane) still has its
   own 20px/24px padding, so the right-hand content isn't flush to that edge. */
#investing-content:has(> .lib-shell) {
    padding: 0;
}

.lib-shell {
    display: flex;
    height: 100%;
    min-height: 0;
}

.lib-sidebar {
    width: 200px;
    flex-shrink: 0;
    overflow-y: auto;
    padding: 16px 8px;
    border-right: 1px solid var(--border);
    box-sizing: border-box;
}

.lib-sidebar-head {
    display: flex;
    justify-content: flex-end;
    padding: 0 2px 8px;
}

.lib-tree-collapse {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    padding: 0;
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
}

.lib-tree-collapse:hover {
    color: var(--neon-cyan);
    background: var(--bg-hover);
}

.lib-sidebar--collapsed {
    width: 36px;
    padding: 12px 4px;
    overflow: hidden;
}

.lib-sidebar--collapsed .lib-tree-collapse {
    margin: 0 auto;
}

.lib-tree-row {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 5px 6px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
}

.lib-tree-row:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.lib-tree-row--active {
    background: var(--bg-hover);
    color: var(--neon-cyan);
    font-weight: 600;
}

/* Dragging a card (or another tree row) directly over a folder row in the
   sidebar tree moves it into that folder — same affordance as .lib-card--dragover. */
.lib-tree-row--dragover {
    background: rgba(0, 240, 255, 0.08);
    color: var(--neon-cyan);
    box-shadow: inset 0 0 0 1px var(--neon-cyan);
}

/* The tree row currently being dragged (moving a folder via the tree). */
.lib-tree-row--dragging {
    opacity: 0.4;
}

.lib-tree-toggle {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0;
    border-radius: var(--radius-sm);
}

.lib-tree-toggle:hover {
    color: var(--neon-cyan);
}

.lib-tree-toggle-spacer {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
}

.lib-tree-name {
    display: flex;
    align-items: center;
    gap: 6px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lib-tree-name svg {
    flex-shrink: 0;
    color: #fbbf24;
}

.lib-view {
    position: relative;
    flex: 1;
    min-width: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 20px 24px;
    box-sizing: border-box;
    overflow-y: auto;
    color: var(--text-primary);
}

/* ── Drag-and-drop upload (whole browse pane, not just the Upload button) ── */

.lib-drop-hint {
    display: none;
    position: absolute;
    inset: 8px;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    color: var(--neon-cyan);
    background: rgba(0, 240, 255, 0.05);
    border: 2px dashed var(--neon-cyan);
    border-radius: var(--radius-lg);
    z-index: 40;
    pointer-events: none;
}

/* .lib-dragover is only ever added for a genuine external OS file drag
   (see library.js ondragenter — it's gated on dragSource being unset), so
   this hint never competes with the internal move flow's own
   .lib-card--dragover/.lib-tree-row--dragover target highlight. */
#investing-content.lib-dragover .lib-drop-hint {
    display: flex;
}

/* ── Toolbar ───────────────────────────────────────────────────────────── */

.lib-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 18px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border);
}

.lib-breadcrumbs {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    flex-wrap: wrap;
}

.lib-crumb {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: var(--radius-sm);
}

.lib-crumb:last-of-type {
    color: var(--text-primary);
    font-weight: 600;
}

.lib-crumb:hover {
    color: var(--neon-cyan);
    background: var(--bg-hover);
}

.lib-crumb-sep {
    color: var(--text-muted);
}

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

.lib-search {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    color: var(--text-muted);
}

.lib-search input {
    background: none;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 13px;
    width: 160px;
}

.lib-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-primary);
    font-size: 13px;
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
}

.lib-btn:hover {
    border-color: var(--border-active);
    color: var(--neon-cyan);
    background: var(--bg-hover);
}

.lib-btn[disabled] {
    opacity: 0.5;
    cursor: default;
}

.lib-btn--sm {
    font-size: 12px;
    padding: 4px 8px;
}

.lib-up-btn {
    display: none;
}

/* ── Grid/List view toggle ────────────────────────────────────────────── */

.lib-view-toggle {
    display: flex;
    align-items: center;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.lib-view-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: var(--transition);
}

.lib-view-toggle-btn:not(:last-child) {
    border-right: 1px solid var(--border);
}

.lib-view-toggle-btn:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.lib-view-toggle-btn.active {
    color: var(--neon-cyan);
    background: var(--bg-hover);
}

/* ── Grid of folder/file cards ────────────────────────────────────────── */

.lib-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 10px;
}

/* ── Grouped grid — Drive-style date sections (Today, Yesterday, …) ──────
   Grid mode groups items by last-modified date instead of one big
   undifferentiated grid; folders get their own (always-first) group. */

.lib-group + .lib-group {
    margin-top: 26px;
}

.lib-group-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    margin-bottom: 10px;
}

/* ── List view — Drive-style rows (icon, name, modified, size) ───────────
   Reuses .lib-card/.lib-card-icon/.lib-card-name/.lib-card-delete etc.
   as-is (just full-width instead of grid-cell-width) so all existing
   click/drag-and-drop selectors and handlers in library.js work unchanged
   in either view mode — only the container + two extra meta columns differ. */

.lib-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.lib-list-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 34px 8px 14px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--border);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
}

/* Offset to line up with the card's icon (18px) + gap (10px) above it. */
.lib-list-header .lib-list-col-name {
    flex: 1;
    padding-left: 28px;
}

.lib-list-col-modified {
    width: 120px;
    flex-shrink: 0;
}

.lib-list-col-size {
    width: 80px;
    flex-shrink: 0;
    text-align: right;
}

/* Header cells are clickable <button>s (sort by that column) — reset
   button chrome so they look identical to the old plain <span> labels,
   just with a hover state and an ascending/descending arrow appended. */
.lib-list-sort-btn {
    display: flex;
    align-items: center;
    background: none;
    border: none;
    margin: 0;
    padding: 0;
    font: inherit;
    text-transform: inherit;
    letter-spacing: inherit;
    color: inherit;
    cursor: pointer;
}

.lib-list-header .lib-list-col-size.lib-list-sort-btn {
    justify-content: flex-end;
}

.lib-list-sort-btn:hover {
    color: var(--neon-cyan);
}

.lib-card--row {
    padding-top: 8px;
    padding-bottom: 8px;
}

.lib-card--row .lib-list-col-modified,
.lib-card--row .lib-list-col-size {
    font-size: 12px;
    color: var(--text-muted);
}

.lib-card {
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    /* Extra right padding reserves a strip for .lib-card-delete so its
       absolute positioning never overlaps .lib-card-meta (item count/date). */
    padding: 12px 34px 12px 14px;
    cursor: pointer;
    transition: var(--transition);
}

.lib-card:hover {
    border-color: var(--border-active);
    background: var(--bg-hover);
    box-shadow: var(--glow-sm);
}

.lib-card:hover .lib-card-delete {
    opacity: 1;
}

/* Dragging a file directly over a folder card uploads into that folder,
   bypassing whatever folder is currently open (see library.js _bindBrowseEvents). */
.lib-card--dragover {
    border-color: var(--neon-cyan);
    background: rgba(0, 240, 255, 0.08);
    box-shadow: var(--glow-sm);
}

/* The card currently being dragged (existing item/folder, not an OS file
   drop) — dimmed so it's clear which card is the drag source. */
.lib-card--dragging {
    opacity: 0.4;
}

.lib-card-icon {
    flex-shrink: 0;
    display: flex;
    color: var(--text-muted);
}

.lib-card-icon--folder { color: #fbbf24; }
.lib-card-icon--md     { color: var(--neon-cyan); }
.lib-card-icon--html   { color: var(--neon-cyan); }

.lib-kind-toggle {
    display: inline-flex;
    gap: 4px;
    margin-left: 8px;
    flex-shrink: 0;
}

.lib-kind-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 11px;
    padding: 3px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.lib-kind-btn--active {
    color: var(--text-primary);
    border-color: var(--border-active);
}
.lib-card-icon--pdf    { color: var(--loss); }

/* Alerts' browse-all card icon slot: a real stock logo instead of the
   generic bell icon, clickable to open that stock's page. */
.lib-card-icon--logo {
    width: 24px;
    height: 24px;
    border-radius: 5px;
    overflow: hidden;
    background: var(--bg-input);
    cursor: pointer;
}

.lib-card-icon--logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.alert-limit-note {
    margin: 0 0 12px;
    padding: 10px 12px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--loss, #b91c1c) 10%, transparent);
    color: var(--text-primary);
    font-size: 13px;
}
.alert-browse-switch { margin-right: 8px; }
.alert-browse-switch .alert-toggle-btn { flex: 0 0 auto; padding: 7px 12px; }

.alert-table-wrap {
    padding: 4px 16px 28px;
    overflow-x: auto;
}
.alert-table {
    width: 100%;
    border-collapse: collapse;
}
.alert-table th {
    text-align: left;
    padding: 0 10px 8px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.alert-table-sort {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 0;
    border: none;
    background: none;
    color: var(--text-muted);
    font: inherit;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: pointer;
}
.alert-table-sort.is-sorted { color: var(--text-primary); }
.alert-table-arrow { font-size: 10px; letter-spacing: 0; }
.alert-table td {
    padding: 11px 10px;
    border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
    font-size: 13px;
    color: var(--text-primary);
    vertical-align: middle;
}
.alert-table-row { cursor: pointer; }
.alert-table-row:hover td {
    background: color-mix(in srgb, var(--text-primary) 4%, transparent);
}
.alert-table-name { font-weight: 650; }
.alert-table-rule {
    color: var(--text-secondary, var(--text-muted));
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.alert-table-ticker {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.alert-table-actions {
    width: 44px;
    text-align: right;
    position: relative;
}
.alert-table-actions .rs-hover-delete {
    position: static;
    transform: none;
}
.alert-table-row:hover .rs-hover-delete { opacity: 1; }

.alert-fired-list {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 4px 20px 28px;
}

.alert-fired-day {
    display: flex;
    align-items: center;
    gap: 14px;
    margin: 22px 4px 6px;
    font-size: 12px;
    font-weight: 650;
    color: var(--text-muted);
}

.alert-fired-list > .alert-fired-day:first-child {
    margin-top: 8px;
}

.alert-fired-day::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--border);
}

.alert-fired-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 9px 4px;
    border: none;
    border-radius: 6px;
    background: transparent;
}

.alert-fired-row:hover {
    background: color-mix(in srgb, var(--text-primary) 5%, transparent);
}

.alert-fired-time {
    flex: 0 0 auto;
    margin-left: auto;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: var(--text-muted);
    white-space: nowrap;
}

.alert-fired-row .alert-bot-btn {
    margin-left: auto;
}

.alert-fired-row .alert-bot-btn + .alert-fired-time {
    margin-left: 8px;
}

.alert-fired-item {
    border-radius: 6px;
}

.alert-fired-name {
    flex: 0 0 180px;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-width: 0;
    font-size: 13px;
    font-weight: 650;
    color: var(--text-primary);
    overflow: hidden;
    cursor: pointer;
    border-radius: 4px;
}
.alert-fired-name-text {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.alert-fired-name:hover {
    color: var(--accent, var(--neon-cyan, #0ea5e9));
}

.alert-fired-chevron {
    flex: 0 0 auto;
    opacity: 0.55;
    transition: transform 0.15s ease;
}
.alert-fired-item.is-open .alert-fired-chevron {
    transform: rotate(180deg);
}

.alert-fired-ticker {
    cursor: pointer;
    color: inherit;
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, currentColor 35%, transparent);
    text-underline-offset: 2px;
}
.alert-fired-ticker:hover {
    color: var(--accent, var(--neon-cyan, #0ea5e9));
    text-decoration-color: currentColor;
}

.alert-fired-config {
    margin: 0 4px 8px 40px;
    padding: 10px 12px;
    border-radius: 8px;
    background: color-mix(in srgb, var(--text-primary) 4%, transparent);
    border: 1px solid var(--border);
}
.alert-fired-config[hidden] {
    display: none !important;
}
.alert-fired-config-grid {
    display: grid;
    gap: 6px;
    margin: 0;
}
.alert-fired-config-row {
    display: grid;
    grid-template-columns: 72px 1fr;
    gap: 10px;
    align-items: start;
}
.alert-fired-config-row dt {
    margin: 0;
    font-size: 11px;
    font-weight: 650;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.alert-fired-config-row dd {
    margin: 0;
    font-size: 13px;
    color: var(--text-primary);
    white-space: pre-wrap;
    word-break: break-word;
}
.alert-fired-config-empty {
    font-size: 12px;
    color: var(--text-muted);
}

@media (max-width: 720px) {
    .alert-fired-config { margin-left: 4px; }
}

.alert-fired-msg {
    flex: 1;
    font-size: 13px;
    color: var(--text-secondary, var(--text-muted));
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@media (max-width: 768px) {
    #investing-content:has(> .lib-view) {
        padding: 0;
        max-width: none;
    }
    #investing-content > .lib-view {
        padding: 8px 12px 0;
    }
    #investing-content > .lib-view .lib-toolbar {
        flex-wrap: nowrap;
        gap: 6px;
        margin: 0 0 6px;
        padding: 0 0 8px;
    }
    #investing-content > .lib-view .lib-breadcrumbs {
        display: none;
    }
    #investing-content > .lib-view .lib-toolbar-actions {
        flex: 1;
        flex-wrap: nowrap;
        min-width: 0;
        gap: 6px;
    }
    #investing-content > .lib-view .lib-search {
        flex: 1 1 auto;
        min-width: 0;
        padding: 4px 8px;
    }
    #investing-content > .lib-view .lib-search input {
        width: 100%;
        min-width: 0;
    }
    #investing-content > .lib-view .lib-btn-label,
    #investing-content > .lib-view .alert-browse-label,
    .ws-refresh-prefix {
        display: none;
    }
    #investing-content > .lib-view .lib-btn-create {
        padding: 6px;
        gap: 0;
    }
    #investing-content > .lib-view .alert-browse-switch {
        margin-right: 0;
    }
    #investing-content > .lib-view .alert-browse-switch .alert-toggle-btn {
        padding: 6px 8px;
    }
    #investing-content > .lib-view .alert-table-wrap,
    #investing-content > .lib-view .alert-fired-list {
        padding-left: 0;
        padding-right: 0;
    }
    #investing-content > .lib-view .alert-table th,
    #investing-content > .lib-view .alert-table td {
        white-space: nowrap;
    }
    #investing-content > .lib-view .alert-fired-list {
        overflow-x: hidden;
    }
    #investing-content > .lib-view .alert-fired-row {
        flex-wrap: nowrap;
        width: 100%;
        min-width: 0;
        max-width: 100%;
        gap: 8px;
        padding-left: 0;
        padding-right: 0;
    }
    #investing-content > .lib-view .alert-fired-name {
        flex: 0 1 9.5rem;
        max-width: 9.5rem;
    }
    #investing-content > .lib-view .alert-fired-msg {
        flex: 0 1 8.5rem;
        max-width: 8.5rem;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    #investing-content > .lib-view .alert-fired-row .alert-bot-btn {
        margin-left: auto;
        flex: 0 0 28px;
    }
    #investing-content > .lib-view .alert-fired-time {
        flex: 0 0 4.5rem;
        width: 4.5rem;
        margin-left: auto;
        text-align: right;
    }
    #investing-content > .lib-view .alert-fired-row .alert-bot-btn + .alert-fired-time {
        margin-left: 6px;
    }
    #investing-content > .lib-view .alert-fired-day {
        margin-left: 0;
        margin-right: 0;
    }
}

.lib-card-name {
    flex: 1;
    font-size: 13px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lib-card-meta {
    font-size: 11px;
    color: var(--text-muted);
    flex-shrink: 0;
}

/* Hover-reveal delete trigger — same muted red as .rs-hover-delete
   (Watchlists/Portfolios/Alerts' equivalent in the right sidebar), a small
   ✕ glyph rather than a trash-can icon, so it matches everywhere else in
   the app instead of being its own one-off. */
.lib-card-delete {
    position: absolute;
    top: 50%;
    right: 6px;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #b05252;
    font-size: 12px;
    line-height: 1;
    opacity: 0;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.lib-card-delete:hover {
    color: #b05252;
}

.lib-card--editing {
    cursor: default;
    /* .lib-card reserves 34px of right padding for the absolutely-positioned
       .lib-card-delete icon (see above) — the inline create row has no such
       icon, so reclaim that space for the input/confirm-actions instead of
       leaving it as dead padding that makes the card even more cramped. */
    padding-right: 14px;
}

.lib-inline-input {
    flex: 1;
    /* <input> has a non-zero intrinsic min-width by default, which wins
       over flex-shrink and stops it shrinking to fit a narrow card —
       without this, the input refused to shrink below ~170px, pushing
       .rs-confirm-actions (the ✓/✕ buttons) out past the card's own right
       edge and visually spilling into the next grid cell. */
    min-width: 0;
    background: var(--bg-input);
    border: 1px solid var(--border-active);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 13px;
    padding: 4px 8px;
    outline: none;
}

/* The ✓ (confirm)/✕ (cancel) buttons themselves — both here (inline
   create) and in the delete-confirmation cards below — are .rs-confirm-yes/
   .rs-confirm-no/.rs-confirm-divider/.rs-confirm-actions, defined once in
   styles.css and shared with the Watchlists/Portfolios/Alerts sidebar, not
   redefined here. That shared definition is a fixed 24x24 flex box, so
   these never stretch/deform inside the card's flex row regardless of how
   much space is available — the old library-only buttons had no fixed
   size and used the generic --text-secondary color instead of the
   established red/cyan scheme, which is what read as "wrong shape, wrong
   colors" compared to every other section. */
.lib-card--confirming {
    justify-content: space-between;
    cursor: default;
    background: rgba(255, 80, 80, 0.08) !important;
    /* Same reclaimed padding as .lib-card--editing above — no delete icon
       shown while confirming, so no need to reserve space for it. */
    padding-right: 14px;
}

/* ── Empty / loading states ───────────────────────────────────────────── */

.lib-empty {
    color: var(--text-muted);
    font-size: 13px;
    text-align: center;
    padding: 40px 20px;
}

.lib-empty-hint {
    font-size: 12px;
    margin-top: 6px;
    opacity: 0.8;
}

.lib-loading {
    color: var(--text-muted);
    font-size: 13px;
    padding: 40px 20px;
    text-align: center;
}

/* ── Search results ────────────────────────────────────────────────────── */

.lib-section-title {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 10px;
}

.lib-search-hit {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 10px 14px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: var(--transition);
}

.lib-search-hit:hover {
    border-color: var(--border-active);
    background: var(--bg-hover);
}

.lib-search-hit-title {
    font-size: 13px;
    color: var(--text-primary);
    font-weight: 600;
    margin-bottom: 4px;
}

.lib-search-hit-snippet {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.lib-search-hit-snippet b,
.lib-search-hit-snippet strong {
    color: var(--neon-cyan);
}

/* ── Item view (editor / viewer) ──────────────────────────────────────── */

.lib-view--item {
    padding: 16px 24px;
}

.lib-item-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding-bottom: 14px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.lib-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 13px;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: var(--radius-sm);
}

.lib-back-btn:hover {
    color: var(--neon-cyan);
    background: var(--bg-hover);
}

.lib-item-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lib-item-actions {
    display: flex;
    gap: 8px;
}

.lib-editor-textarea {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.6;
    padding: 14px;
    resize: none;
    outline: none;
    box-sizing: border-box;
}

.lib-editor-textarea--full {
    flex: 1;
    width: 100%;
    height: calc(100% - 60px);
}

.lib-editor-textarea:focus {
    border-color: var(--border-active);
}

/* HTML reader. The iframe viewport is the reader column, not the monitor,
   so vw and @media follow the space on screen. PDF stays 760px. */
.lib-view--item:has(.lib-html-frame) {
    padding-left: 0;
    padding-right: 0;
    overflow: hidden;
}

.lib-view--item:has(.lib-html-frame) .lib-item-header,
.lib-view--item:has(.lib-html-frame) .lib-version-banner,
.lib-view--item:has(.lib-html-frame) .lib-empty {
    padding-left: 24px;
    padding-right: 24px;
}

.lib-html-frame {
    align-self: stretch;
    width: 100%;
    flex: 1 1 auto;
    min-height: 0;
    border: 0;
    background: transparent;
    display: block;
}

.lib-html-preview {
    box-sizing: border-box;
}

.lib-html-preview img,
.lib-html-preview svg,
.lib-html-preview video {
    max-width: 100%;
    height: auto;
}

/* Substack-style single-post reading layout — same font family as the rest
   of the app (no font-family override here), just a noticeably bigger,
   more generously-spaced typographic scale than the 13px/1.6 used
   everywhere else, plus a centered ~720px prose column (tables/code blocks
   opt out of that cap via the more specific selectors below, since a
   narrow column makes wide financial tables useless). */
.lib-md-preview {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 36px clamp(20px, 6%, 56px);
    overflow-y: auto;
    font-size: 16px;
    line-height: 1.75;
    color: var(--text-secondary);
    height: 100%;
    box-sizing: border-box;
}

.lib-md-preview > * {
    max-width: 720px;
    margin-left: auto;
    margin-right: auto;
}

.lib-md-preview > *:first-child { margin-top: 0; }
.lib-md-preview > *:last-child { margin-bottom: 0; }

.lib-md-preview h1,
.lib-md-preview h2,
.lib-md-preview h3,
.lib-md-preview h4,
.lib-md-preview h5,
.lib-md-preview h6 {
    color: var(--text-primary);
    font-weight: 700;
    letter-spacing: -0.01em;
}

.lib-md-preview h1 { font-size: 32px; line-height: 1.25; font-weight: 800; margin: 0 0 12px; }
.lib-md-preview h2 { font-size: 23px; line-height: 1.3; margin: 42px 0 14px; }
.lib-md-preview h3 { font-size: 19px; line-height: 1.35; margin: 30px 0 10px; color: var(--neon-cyan); }
.lib-md-preview h4 { font-size: 16.5px; margin: 24px 0 8px; }
.lib-md-preview h5,
.lib-md-preview h6 { font-size: 15px; margin: 20px 0 8px; }

.lib-md-preview p {
    margin: 0 0 18px;
}

.lib-md-preview strong {
    color: var(--text-primary);
    font-weight: 700;
}

.lib-md-preview em {
    color: inherit;
}

.lib-md-preview a {
    color: var(--neon-cyan);
    text-decoration: none;
    border-bottom: 1px solid rgba(0, 240, 255, 0.35);
}

.lib-md-preview a:hover {
    border-bottom-color: var(--neon-cyan);
}

.lib-md-preview ul,
.lib-md-preview ol {
    margin: 0 0 18px;
    padding-left: 1.4em;
}

.lib-md-preview li {
    margin-bottom: 8px;
}

.lib-md-preview li > ul,
.lib-md-preview li > ol {
    margin-top: 8px;
    margin-bottom: 0;
}

/* Tables/code blocks are allowed to use the full available width instead
   of the ~720px prose column — a narrow column is unreadable for wide
   financial tables. */
.lib-md-preview > table,
.lib-md-preview > pre {
    max-width: 100%;
}

.lib-md-preview table {
    width: 100%;
    border-collapse: collapse;
    margin: 6px 0 26px;
    font-size: 14.5px;
    line-height: 1.5;
}

.lib-md-preview th,
.lib-md-preview td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.lib-md-preview th {
    font-weight: 700;
    color: var(--text-primary);
    border-bottom: 2px solid var(--border-active);
    white-space: nowrap;
}

.lib-md-preview tr:hover td {
    background: var(--bg-hover);
}

.lib-md-preview pre {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px 16px;
    margin: 6px 0 24px;
    overflow-x: auto;
    font-size: 13.5px;
    line-height: 1.55;
}

.lib-md-preview code {
    font-family: var(--font-mono);
    font-size: 0.88em;
}

.lib-md-preview :not(pre) > code {
    background: var(--bg-input);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--text-primary);
}

.lib-md-preview--full {
    flex: 1;
    height: calc(100% - 110px);
}

/* Plain view mode (no version banner above it) — offset only for the item header. */
.lib-md-preview--view {
    flex: 1;
    height: calc(100% - 60px);
}

/* Off-screen clone of the on-screen reader. Keep .lib-md-preview type,
   tables, and chart chrome — only lift the scroll clip for html2canvas. */
#lib-pdf-export-host.lib-md-preview,
#lib-pdf-export-host.lib-html-preview {
    position: fixed;
    left: 0;
    top: 0;
    height: auto;
    max-height: none;
    overflow: visible;
    opacity: 0.01;
    pointer-events: none;
    z-index: 0;
    border: none;
    border-radius: 0;
    box-shadow: none;
    background: #ffffff;
}

.lib-pdf-frame {
    flex: 1;
    width: 100%;
    height: calc(100% - 60px);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: #fff;
}

/* ── Version history overlay ──────────────────────────────────────────── */

.lib-version-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    background: rgba(251, 191, 36, 0.08);
    border: 1px solid rgba(251, 191, 36, 0.25);
    border-radius: var(--radius-md);
    padding: 8px 14px;
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.lib-versions-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
}

.lib-versions-panel {
    width: min(420px, 90%);
    max-height: 70vh;
    background: var(--bg-primary);
    border: 1px solid var(--border-active);
    border-radius: var(--radius-lg);
    box-shadow: var(--glow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.lib-versions-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
}

.lib-versions-panel-header button {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
}

.lib-versions-panel-header button:hover {
    color: var(--loss);
}

.lib-versions-list {
    overflow-y: auto;
    padding: 6px;
}

.lib-version-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 12px;
    color: var(--text-secondary);
}

.lib-version-row:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.lib-version-row--current {
    color: var(--neon-cyan);
}

.lib-version-size {
    color: var(--text-muted);
    font-size: 11px;
}

/* ── Responsive ────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
    .lib-sidebar {
        display: none;
    }
    .lib-shell .lib-view {
        padding: 8px 12px 0;
    }
    .lib-shell .lib-toolbar {
        flex-wrap: nowrap;
        gap: 6px;
        margin-bottom: 8px;
        padding-bottom: 8px;
    }
    .lib-shell .lib-breadcrumbs {
        display: none;
    }
    .lib-shell .lib-up-btn {
        display: inline-flex;
        padding: 6px;
        flex-shrink: 0;
    }
    .lib-shell .lib-toolbar-actions {
        flex: 1;
        flex-wrap: nowrap;
        min-width: 0;
        gap: 6px;
    }
    .lib-shell .lib-search {
        flex: 1 1 auto;
        min-width: 0;
        padding: 4px 8px;
    }
    .lib-shell .lib-search input {
        width: 100%;
        min-width: 0;
    }
    .lib-shell .lib-view-toggle,
    .lib-shell .ws-refresh,
    .lib-shell .wl-close-btn {
        flex-shrink: 0;
    }
    .lib-shell .lib-toolbar .lib-btn-label {
        display: none;
    }
    .lib-shell .lib-toolbar .lib-btn {
        padding: 6px;
        gap: 0;
        flex-shrink: 0;
    }
    .lib-shell .lib-toolbar .lib-btn svg {
        width: 14px;
        height: 14px;
    }
    .lib-list-header .lib-list-col-modified,
    .lib-list-header .lib-list-col-size,
    .lib-card--row .lib-list-col-modified,
    .lib-card--row .lib-list-col-size {
        display: none;
    }
}

/* Last-updated + reload (Watchlists / Portfolios / Alerts / Library / Journal) */
.ws-refresh {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    font-size: 12px;
    white-space: nowrap;
}
.ws-refresh-stamp { line-height: 1; }
.ws-refresh-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 6px);
    background: var(--bg-card, transparent);
    color: var(--text-secondary);
    cursor: pointer;
}
.ws-refresh-btn:hover:not(:disabled) {
    color: var(--neon-cyan);
    border-color: var(--neon-cyan);
}
.ws-refresh-btn:disabled { opacity: 0.55; cursor: default; }
.ws-refresh-btn.ws-refresh-spinning svg {
    animation: ws-refresh-spin 0.7s linear infinite;
}
@keyframes ws-refresh-spin {
    to { transform: rotate(360deg); }
}
.journal-root .ws-refresh { color: var(--j-faint, var(--text-muted)); }
.journal-root .ws-refresh-btn {
    width: 28px;
    height: 28px;
    background: var(--j-surface-2, var(--bg-card));
    border-color: var(--j-border-soft, var(--border));
    color: var(--j-muted, var(--text-secondary));
}
.journal-root .slab .r {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    text-transform: none;
    letter-spacing: 0;
}
.journal-root .journal-cal-hint {
    font-family: var(--j-mono, inherit);
    font-size: 10px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--j-faint, var(--text-muted));
}
.journal-root .journal-ws-bar {
    display: flex;
    justify-content: flex-end;
    padding: 8px 0 12px;
}

/* Light theme: the document reader and the Watchlists, Portfolios, and Alerts
   lists use the left-menu grey. Borders stay. Dark theme is unchanged. */
html[data-theme="light"] .lib-md-preview,
html[data-theme="light"] .lib-html-frame,
html[data-theme="light"] .lib-pdf-frame,
html[data-theme="light"] #investing-content > .lib-view .lib-card {
    background: var(--bg-sidebar);
}
html[data-theme="light"] #investing-content > .lib-view .lib-card:hover {
    background: var(--bg-hover);
}

/* Portfolio investment-case chooser. The same library paints into this host. */
.lib-pick {
    position: fixed;
    inset: 0;
    z-index: 13000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 28px;
    background: rgba(0, 0, 0, 0.55);
}
.lib-pick-panel {
    width: min(1120px, 100%);
    height: min(760px, 100%);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.45);
}
.lib-pick-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
}
.lib-pick-x {
    background: none;
    border: 0;
    color: var(--text-muted);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 2px 6px;
}
#lib-pick-host { flex: 1; min-height: 0; }
#lib-pick-host > .lib-shell { height: 100%; }
.lib-card > .lib-card-use { margin-right: 22px; }
.lib-card-use {
    margin-left: auto;
    flex-shrink: 0;
    height: 26px;
    padding: 0 10px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--neon-cyan);
    font-size: 12px;
    cursor: pointer;
}
.lib-card-use:hover { background: var(--bg-hover); }
.lib-search-hit { position: relative; }
.lib-search-hit .lib-card-use { position: absolute; right: 10px; top: 10px; margin-left: 0; }
.lib-btn-use { color: var(--neon-cyan); }
@media (max-width: 768px) {
    .lib-pick { padding: 0; align-items: stretch; }
    .lib-pick-panel {
        width: 100%;
        height: 100%;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }
}
