/* ═══════════════════════════════════════════════════════════════════════════════
   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-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;
}

/* ── 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--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;
}

.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: var(--loss);
    background: rgba(176, 82, 82, 0.15);
}

.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);
}

/* 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);
}

.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-search input {
        width: 100px;
    }
    .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;
    }
}
