/* ===== File Panel (edit-in-context slice 1: viewer) ===== */
/* Third column of .app-container, beside the chat. Desktop: a bordered side
   panel; mobile: a full-screen overlay (never auto-opened there). */
.file-panel {
    /* The user-draggable width (--file-panel-width) is only set once the panel
       has actually been resized on this device; until then it keeps the
       responsive default. Both the panel and the chat column are guaranteed a
       floor, and the max-width is what enforces the chat's: the panel can grow
       only into whatever is left after the sidebar and that floor. */
    --fp-min-width: 320px;
    --fp-chat-min-width: 420px;
    display: flex;
    flex-direction: column;
    position: relative; /* anchors the resize handle on the left edge */
    width: var(--file-panel-width, clamp(320px, 38vw, 640px));
    min-width: var(--fp-min-width);
    max-width: max(var(--fp-min-width), calc(100vw - var(--sidebar-width) - var(--fp-chat-min-width)));
    height: 100vh; /* fallback */
    height: 100dvh;
    flex-shrink: 0;
    border-left: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.file-panel[hidden] {
    display: none;
}

/* Drag handle on the panel's left edge. Desktop only — on mobile the panel is
   a full-screen overlay, so there is nothing to resize. It lives inside the
   panel (absolutely positioned over its border) so it appears and disappears
   with it, no separate visibility plumbing. */
.file-panel-resize-handle {
    display: none;
}

@media (min-width: 768px) {
    .file-panel-resize-handle {
        display: block;
        position: absolute;
        top: 0;
        bottom: 0;
        left: -4px;
        width: 8px;
        cursor: col-resize;
        z-index: 50;
        touch-action: none;
    }

    .file-panel-resize-handle::before {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 2px;
        background: var(--border-color);
        transition: background 0.2s ease, width 0.2s ease;
    }

    .file-panel-resize-handle:hover::before,
    .file-panel-resize-handle.dragging::before {
        background: var(--accent);
        width: 4px;
    }
}

.file-panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

/* The shared type badge is absolutely positioned inside cards — in the panel
   header it flows inline instead. */
.file-panel-header .att-badge {
    position: static;
    flex-shrink: 0;
}

.file-panel-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-primary);
    font-family: var(--font-mono);
}

.file-panel-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    text-decoration: none;
    transition: background-color 0.15s, color 0.15s;
}

.file-panel-btn:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.file-panel-btn.active {
    color: var(--accent);
}

/* Scratchpad "active" toggle (SP-03a): a pill (not an icon square) in the panel
   header. Muted when off, accent when the assistant can see the pad. */
.scratchpad-toggle {
    width: auto;
    gap: 5px;
    padding: 0 9px;
    border: 1px solid var(--border-color);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-muted);
}

.scratchpad-toggle:hover {
    background: none;
    border-color: var(--text-muted);
    color: var(--text-secondary);
}

.scratchpad-toggle .scratchpad-toggle-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    flex-shrink: 0;
}

.scratchpad-toggle.on {
    border-color: transparent;
    background: var(--accent-subtle);
    color: var(--accent);
}

.scratchpad-toggle.on .scratchpad-toggle-dot {
    background: var(--accent);
}

/* Pinned scratchpad row at the top of the files browser. */
.chat-file-row.scratchpad-row {
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 4px;
    padding-bottom: 8px;
}

.chat-file-row.scratchpad-row .project-file-badge {
    background: var(--accent-subtle);
    color: var(--accent);
    border-color: transparent;
}

/* The row's toggle sits where a file row's size/mode controls sit — it is the
   same pill as the pad header's, just smaller so it doesn't dominate the row. */
.chat-file-row.scratchpad-row .scratchpad-toggle {
    height: 24px;
    flex-shrink: 0;
}

.fp-browser-empty {
    margin: 8px 14px;
}

.file-panel-body {
    flex: 1;
    overflow: auto;
    padding: 16px;
    font-size: var(--text-sm);
}

/* Raw text / code view: reuse the chat code-block look without its chrome. */
.file-panel-body pre.file-panel-raw {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    line-height: 1.55;
    color: var(--text-primary);
}

.file-panel-loading,
.file-panel-error,
.file-panel-empty {
    color: var(--text-secondary);
    text-align: center;
    padding: 32px 16px;
}

/* Version history viewer (FC-06b): a version rail on top, the selected
   version's rich diff below. Editorial-minimal — a thin colored edge per
   changed line + word-level highlights, keyed to theme variables. */
.fp-history {
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: 100%;
    min-height: 0;
}
.fp-versions {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 0 0 auto;
    max-height: 40%;
    overflow-y: auto;
}
.fp-version {
    display: flex;
    flex-direction: column;
    gap: 2px;
    width: 100%;
    text-align: left;
    padding: 6px 8px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: none;
    cursor: pointer;
    color: var(--text-primary);
    font-size: var(--text-xs);
}
.fp-version:hover { background: var(--bg-tertiary); }
.fp-version.is-selected {
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    border-color: color-mix(in srgb, var(--accent) 40%, transparent);
}
.fp-version-line { display: flex; align-items: baseline; gap: 6px; }
.fp-version-who { font-weight: 600; }
.fp-version-op { color: var(--accent); text-transform: capitalize; }
.fp-version-current {
    margin-left: auto;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 0 4px;
}
.fp-version-sub { justify-content: space-between; color: var(--text-secondary); }
.fp-version-stat { display: flex; gap: 6px; font-family: var(--font-mono); }
.fp-add { color: var(--diff-add); }
.fp-del { color: var(--diff-del); }

.fp-detail {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    border-top: 1px solid var(--border-color);
    padding-top: 10px;
}
.fp-detail-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.fp-detail-title { font-size: var(--text-xs); color: var(--text-secondary); }
.fp-restore-btn {
    margin-left: auto;
    font-size: var(--text-xs);
    padding: 3px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
}
.fp-restore-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Rich diff: one line per row, wraps (UI principle), colored left edge. */
.fp-rdiff {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    line-height: 1.55;
}
.fp-rline {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    padding: 0 6px 0 8px;
    border-left: 3px solid transparent;
}
.fp-rhunk {
    color: var(--text-secondary);
    border-left-color: transparent;
    margin: 6px 0 2px;
    opacity: 0.8;
}
.fp-rctx { color: var(--text-primary); opacity: 0.75; }
.fp-rdel { border-left-color: var(--diff-del); background: color-mix(in srgb, var(--diff-del) 10%, transparent); }
.fp-radd { border-left-color: var(--diff-add); background: color-mix(in srgb, var(--diff-add) 10%, transparent); }
.fp-rdel .fp-tok { background: color-mix(in srgb, var(--diff-del) 26%, transparent); border-radius: 2px; }
.fp-radd .fp-tok { background: color-mix(in srgb, var(--diff-add) 26%, transparent); border-radius: 2px; }

/* Container file list: a text file name opens the panel (FC-04) */
.project-file-name.clickable { cursor: pointer; }
.project-file-name.clickable:hover { color: var(--accent); text-decoration: underline; }

.file-panel-error {
    color: var(--error);
}

/* ---- User editing (slice 3) ---- */

/* The editor fills the body edge-to-edge; the body drops its padding so the
   textarea's own padding lines up with the view-mode content. */
.file-panel.is-editing .file-panel-body {
    padding: 0;
    display: flex;
}

.file-panel-editor {
    flex: 1;
    width: 100%;
    padding: 16px;
    border: none;
    outline: none;
    resize: none;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    line-height: 1.55;
}

.file-panel-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.file-panel-footer[hidden] {
    display: none;
}

.file-panel-conflict {
    flex: 1;
    min-width: 0;
    font-size: var(--text-xs);
    color: var(--warning);
}

/* SP-03b: editor locked while the model's turn is in flight. */
.file-panel-lock-note {
    flex: 1;
    min-width: 0;
    font-size: var(--text-xs);
    color: var(--text-muted);
}

.file-panel.is-locked .file-panel-editor {
    opacity: 0.6;
    cursor: not-allowed;
}

/* The rendered-markdown view reuses the .message-content class (same
   renderer and typography as chat bubbles) — no panel-specific rules. */

/* Files explorer top-bar button (CF-01b): the single entry point to the chat's
   files + the unseen-activity indicator (replaces the panel's right-edge tab). */
.files-explorer-btn {
    position: relative;
}

/* Unseen-activity dot: pulses on the button until the panel is opened. */
.files-explorer-dot {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    border: 1.5px solid var(--bg-primary);
    animation: file-tab-pulse 1.6s ease-in-out infinite;
}

.files-explorer-dot[hidden] {
    display: none;
}

/* Not-loaded count (CT-06): sits at the BOTTOM-right and is muted + static, so
   it can never be mistaken for the top-right accent activity dot. Persistent
   (present while any file is off), where the dot is transient. */
.files-explorer-count {
    position: absolute;
    bottom: 0;
    right: 0;
    min-width: 15px;
    height: 15px;
    padding: 0 3px;
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: var(--text-muted);
    border: 1.5px solid var(--bg-primary);
    color: var(--bg-primary);
    font-size: 9px;
    font-weight: 700;
    /* Digit glyphs sit optically high in their line box; a 0 line-height lets
       flexbox center the ink rather than the (taller) line box. */
    line-height: 0;
}

.files-explorer-count[hidden] {
    display: none;
}

/* Hover/focus popover naming the not-loaded files (CT-06). Read-only — the
   button itself is the click target, so the popover needs no interactivity. */
.fp-context-popover {
    z-index: 200;
    min-width: 200px;
    max-width: 300px;
    padding: 8px 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg, 0 6px 24px rgba(0, 0, 0, 0.28));
    font-size: var(--text-xs);
    pointer-events: none; /* purely informational; never eats a click */
}

.fp-context-popover-title {
    padding: 2px 12px 6px;
    color: var(--text-muted);
    font-weight: 600;
    letter-spacing: 0.03em;
    text-transform: uppercase;
}

.fp-context-popover-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    padding: 3px 12px;
}

.fp-cp-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-primary);
}

.fp-cp-note {
    flex-shrink: 0;
    color: var(--text-muted);
}

/* Tools-off warning atop the context view (CT-06): the one case where an
   unchecked file is truly unreachable rather than merely unloaded. */
.fp-tools-off-note {
    margin: 4px 14px 8px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: var(--warning-subtle, rgba(230, 160, 30, 0.12));
    border: 1px solid var(--warning-border, rgba(230, 160, 30, 0.35));
    color: var(--text-secondary);
    font-size: var(--text-xs);
    line-height: 1.4;
}

@keyframes file-tab-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.35); opacity: 0.55; }
}

/* Browser (file list) view inside the panel (CF-01b). Rows reuse the container
   file-list parts (.chat-file-row / badge / size / download). */
.fp-browser {
    display: flex;
    flex-direction: column;
    padding: 4px 0;
}

/* Source dividers in the browser (CT-04): "Workspace · Lore", "This chat".
   They answer a question the flat list couldn't — where this chat's context
   is actually coming from. */
.fp-browser-section {
    padding: 10px 14px 4px;
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* An unloaded/muted row stays legible — it is still a real file you may want to
   open or download — but reads as inactive at a glance. Matches the container
   page's .project-file-item.is-context-off treatment. */
.chat-file-row.is-context-off .chat-file-name,
.chat-file-row.is-context-off .project-file-badge {
    opacity: 0.55;
}

.chat-file-row.is-context-off .chat-file-name {
    font-style: italic;
}

/* A row whose state came from a per-chat override rather than the container
   default. The marker is deliberately quiet: the reset button beside it is the
   loud part, and it only exists on overridden rows. */
.chat-file-row.is-overridden {
    box-shadow: inset 2px 0 0 var(--accent);
}

.fp-reset-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border: none;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--accent);
    font-size: var(--text-sm);
    line-height: 1;
    cursor: pointer;
    transition: background 0.2s;
}

.fp-reset-btn:hover {
    background: var(--border-color);
}

.fp-reset-btn[hidden] {
    display: none;
}

/* Chat working-file inject mode (CT-04): one button cycling auto → pin → mute.
   Sits in the same left column as the knowledge rows' checkbox so every row in
   the list has its control in the same place. */
.fp-mode-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    border: none;
    border-radius: var(--radius-sm);
    background: none;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.2s;
}

.fp-mode-btn:hover {
    background: var(--border-color);
}

.fp-mode-btn:disabled {
    cursor: progress;
    opacity: 0.5;
}

/* Auto is the default and should recede; pin and mute are deliberate states and
   should read as such. */
.fp-mode-btn.mode-auto {
    color: var(--text-muted);
    font-size: 13px;
}

.fp-mode-btn.mode-pin,
.fp-mode-btn.mode-mute {
    font-size: 11px;
}

/* Knowledge rows sit one notch in from their section header. */
.chat-file-row.knowledge-row {
    padding-left: 14px;
}

/* Mobile: the panel becomes a full-screen overlay above the chat (opened only
   by explicit tap — app.js never auto-opens it at this width). */
@media (max-width: 768px) {
    .file-panel {
        position: fixed;
        inset: 0;
        width: 100%;
        /* Drop the desktop resize floors — a full-screen overlay shares the
           width with nothing, and the max-width would cap it at 320px here. */
        min-width: 0;
        max-width: none;
        z-index: 110;
        border-left: none;
    }
}
