/* ============================================================================
   R10.4 — File tree sidebar + inline file preview modal
   Read-only project-file browser for the workspace. Toggled from the topbar.
   Palette + typography inherited from styles.css. No new design language.
   ============================================================================ */

/* Topbar toggle button — styled as a sibling of the existing .chat-icon-btn row */
.ft-topbar-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ft-topbar-toggle[aria-pressed="true"] {
  background: var(--purple-dim);
  color: var(--purple);
}

/* ---- Sidebar shell ---- */
.file-tree-sidebar {
  position: fixed;
  top: 56px;               /* sit under the topbar — topbar is ~56px tall */
  bottom: 0;
  left: 0;
  width: 220px;
  background: #f9fafb;
  border-right: 1px solid var(--border);
  z-index: 40;             /* above panels, below modals (which are 1000+) */
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-md);
  transform: translateX(-100%);
  transition: transform 0.18s ease-out;
}
.file-tree-sidebar[data-open="true"] {
  transform: translateX(0);
}
.file-tree-sidebar[hidden] {
  display: none;
}

.ft-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-family: 'Space Grotesk', 'Inter', sans-serif;
}
.ft-header-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.ft-refresh-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.72rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.12s;
}
.ft-refresh-btn:hover {
  color: var(--purple);
  border-color: var(--purple-dim);
  background: var(--purple-dim);
}
.ft-refresh-btn:disabled {
  opacity: 0.5;
  cursor: progress;
}

.ft-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 4px 12px;
  font-size: 0.85rem;
}

.ft-empty,
.ft-error {
  padding: 14px 12px;
  font-size: 0.82rem;
  color: var(--text-light);
  text-align: center;
}
.ft-error { color: #b91c1c; }

/* ---- Tree nodes ---- */
.ft-tree, .ft-tree ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.ft-tree ul {
  padding-left: 14px;
  border-left: 1px dashed var(--border-light);
  margin-left: 10px;
}
.ft-node {
  display: block;
}

/* <details>/<summary> for dirs — accessible collapse */
.ft-dir > summary {
  list-style: none;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}
.ft-dir > summary::-webkit-details-marker { display: none; }
.ft-dir > summary::before {
  content: '▸';
  font-size: 0.7rem;
  color: var(--text-light);
  transition: transform 0.12s;
  display: inline-block;
  width: 10px;
}
.ft-dir[open] > summary::before {
  transform: rotate(90deg);
}
.ft-dir > summary:hover {
  background: var(--purple-dim);
}
.ft-dir > summary:focus-visible {
  outline: 2px solid var(--purple);
  outline-offset: 1px;
}

/* R13.1.C — row wrap so quick-action buttons sit beside the file button. */
.ft-row {
  display: flex;
  align-items: center;
  gap: 2px;
  position: relative;
}
.ft-row .ft-file { flex: 1 1 auto; min-width: 0; }
.ft-actions {
  display: flex;
  gap: 2px;
  flex: 0 0 auto;
  padding-right: 4px;
  opacity: 0;
  transition: opacity 120ms ease;
}
.ft-row:hover .ft-actions,
.ft-row:focus-within .ft-actions {
  opacity: 1;
}
/* Mobile: always show actions since hover doesn't exist reliably */
@media (hover: none) {
  .ft-actions { opacity: 1; }
}
.ft-action {
  background: transparent;
  border: 0;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
  line-height: 1;
  transition: background 100ms ease;
  min-width: 28px;
  min-height: 28px;
}
.ft-action:hover { background: var(--purple-dim, #f3f4f6); }
.ft-action:focus-visible { outline: 2px solid #f97316; outline-offset: 2px; }

/* Files — buttons so they're clearly clickable */
.ft-file {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 4px 8px 4px 24px; /* 24px so file aligns past the caret column */
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font: inherit;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ft-file:hover,
.ft-file:focus-visible {
  background: var(--purple-dim);
  outline: none;
}
.ft-file[aria-current="true"] {
  background: var(--purple-light);
  color: var(--purple);
  font-weight: 600;
}

.ft-icon {
  flex: 0 0 auto;
  font-size: 0.95rem;
  line-height: 1;
}
.ft-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ft-size {
  flex: 0 0 auto;
  font-size: 0.7rem;
  color: var(--text-light);
  margin-left: 4px;
}

/* ---- Preview modal ---- */
.ft-preview-overlay {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.45);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.ft-preview-overlay[hidden] { display: none; }

.ft-preview-modal {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 860px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.ft-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.ft-preview-title {
  font-family: 'Space Grotesk', 'Inter', sans-serif;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ft-preview-meta {
  font-size: 0.72rem;
  color: var(--text-light);
  font-weight: 500;
}
.ft-preview-close {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.85rem;
  flex: 0 0 auto;
}
.ft-preview-close:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.ft-preview-body {
  flex: 1;
  overflow: auto;
  background: #fafbfc;
}
.ft-preview-pre {
  margin: 0;
  padding: 16px 18px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.82rem;
  line-height: 1.5;
  color: #111827;
  white-space: pre;
  tab-size: 2;
}
.ft-preview-notice {
  padding: 28px 22px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
}
.ft-preview-notice strong { display: block; color: var(--text); margin-bottom: 4px; }

/* ---- Mobile ---- */
@media (max-width: 768px) {
  .file-tree-sidebar {
    width: 78%;
    max-width: 300px;
    top: 0;
    z-index: 1100; /* above the mobile topbar when shown */
  }
  .ft-preview-modal {
    max-width: 100%;
    max-height: 92vh;
  }
}

/* Scrim under the sidebar on mobile so taps outside close it */
.ft-scrim {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.35);
  z-index: 39;
}
.ft-scrim[hidden] { display: none; }
@media (min-width: 769px) {
  .ft-scrim { display: none !important; } /* desktop: no scrim, sidebar is a panel */
}
