.thread-files-sidebar {
  --thread-files-width-ratio: 0.5;
  --thread-files-sidebar-width: clamp(
    280px,
    calc(var(--thread-files-width-ratio) * 100%),
    min(70%, calc(100% - 320px))
  );
  --thread-files-list-ratio: 0.25;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  width: var(--thread-files-sidebar-width);
  min-width: var(--thread-files-sidebar-width);
  max-width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-left: 1px solid var(--border-subtle);
  background: var(--bg-surface-1);
  color: var(--text-main);
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  align-items: stretch;
  overflow: hidden;
  transition:
    width 0.18s ease,
    min-width 0.18s ease,
    max-width 0.18s ease;
}

.thread-files-sidebar.is-resizing {
  transition: none;
  user-select: none;
}

.thread-files-sidebar.is-collapsed {
  width: 44px;
  min-width: 44px;
  max-width: 44px;
  grid-template-columns: 44px;
  box-shadow: none;
}

.thread-files-sidebar:not(.is-collapsed) {
  width: min(var(--thread-files-sidebar-width), 100%);
  min-width: min(280px, 100%);
  max-width: 100%;
  box-shadow: -16px 0 32px rgba(15, 23, 42, 0.12);
}

.thread-files-resizer {
  position: absolute;
  inset: 0 auto 0 -4px;
  width: 8px;
  cursor: col-resize;
  z-index: 2;
}

.thread-files-resizer::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 3px;
  width: 2px;
  border-radius: 0;
  background: transparent;
}

.thread-files-resizer:hover::before,
.thread-files-sidebar.is-resizing .thread-files-resizer::before {
  background: var(--accent-main);
}

.thread-files-sidebar.is-collapsed .thread-files-resizer {
  display: none;
}

.thread-files-toggle {
  grid-column: 1;
  grid-row: 1;
  width: 44px;
  min-width: 44px;
  height: 100%;
  min-height: 100%;
  border: 0;
  border-right: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  padding: 12px 0;
  cursor: pointer;
}

.thread-files-toggle:hover {
  color: var(--text-main);
  background: var(--bg-surface-hover);
}

.thread-files-toggle:focus,
.thread-files-toggle:focus-visible {
  outline: none;
  box-shadow: none;
}

.thread-files-resizer:focus,
.thread-files-resizer:focus-visible {
  outline: none;
}

.thread-files-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  min-height: 20px;
  border-radius: 999px;
  background: var(--accent-main);
  color: var(--accent-text);
  font-size: var(--font-size-body-sm);
  writing-mode: horizontal-tb;
}

.thread-files-panel {
  grid-column: 2;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.thread-files-sidebar.is-collapsed .thread-files-panel {
  display: none;
}

.thread-files-header {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--border-subtle);
}

.thread-files-header h2,
.thread-file-preview__header h3 {
  margin: 0;
  font-size: var(--font-size-title-xs);
}

.thread-files-header p,
.thread-file-preview__header p {
  margin: 2px 0 0;
  color: var(--text-dim);
  font-size: var(--font-size-body-sm);
  line-height: 1.35;
}

.thread-files-list {
  flex: 0 1 auto;
  height: auto;
  min-height: 72px;
  max-height: max(72px, calc(var(--thread-files-list-ratio) * 100%));
  min-width: 0;
  padding: 0;
  border-bottom: 1px solid var(--border-subtle);
  overflow: auto;
  scrollbar-gutter: stable;
}

.thread-files-section {
  margin: 0;
  padding: 0;
}

.thread-files-section + .thread-files-section {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border-subtle);
}

.thread-files-list button.thread-files-section__toggle {
  box-sizing: border-box;
  width: 100%;
  min-height: 22px;
  margin: 0;
  padding: 2px 8px;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  font: inherit;
  color: inherit;
  text-align: left;
  appearance: none;
  -webkit-appearance: none;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.thread-files-section__toggle:hover {
  background: var(--bg-surface-hover);
}

.thread-files-section__toggle:focus,
.thread-files-section__toggle:focus-visible {
  outline: none;
  background: var(--bg-surface-hover);
}

.thread-files-section__chevron {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  transition: transform 0.15s ease;
}

.thread-files-section.is-collapsed .thread-files-section__chevron {
  transform: rotate(-90deg);
}

.thread-files-section__chevron svg {
  width: 12px;
  height: 12px;
}

.thread-files-section__title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: var(--font-size-caption);
  font-weight: 600;
  line-height: 16px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: left;
}

.thread-files-section__count {
  flex: 0 0 auto;
  min-width: 0;
  height: auto;
  padding: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text-dim);
  font-size: var(--font-size-caption);
  line-height: 16px;
  font-variant-numeric: tabular-nums;
  opacity: 0.75;
}

.thread-files-section.is-collapsed .thread-files-section__body {
  display: none;
}

.thread-files-section__empty {
  padding: 2px 8px 4px 26px;
  color: var(--text-dim);
  font-size: var(--font-size-body-sm);
  line-height: 16px;
}

.thread-files-section__items {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin: 0;
  padding: 0;
}

.thread-file-tree {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.thread-files-list button.thread-file-folder__toggle {
  box-sizing: border-box;
  width: 100%;
  min-height: 22px;
  margin: 0;
  padding: 0 8px 0 calc(10px + var(--tree-depth, 0) * 14px);
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  font: inherit;
  color: inherit;
  text-align: left;
  appearance: none;
  -webkit-appearance: none;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.thread-file-folder__toggle:hover {
  background: var(--bg-surface-hover);
}

.thread-file-folder__toggle:focus,
.thread-file-folder__toggle:focus-visible {
  outline: none;
  background: var(--bg-surface-hover);
}

.thread-file-folder__chevron {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  transition: transform 0.15s ease;
}

.thread-file-folder.is-collapsed .thread-file-folder__chevron {
  transform: rotate(-90deg);
}

.thread-file-folder.is-collapsed .thread-file-folder__body {
  display: none;
}

.thread-file-folder__name {
  flex: 1 1 auto;
  min-width: 0;
  font-family: inherit;
  font-size: var(--font-size-body-md);
  font-weight: 500;
  line-height: 22px;
  color: var(--text-main);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-file-folder__count {
  flex: 0 0 auto;
  min-width: 4.5em;
  text-align: right;
  color: var(--text-dim);
  font-size: var(--font-size-caption);
  line-height: 22px;
  font-variant-numeric: tabular-nums;
  opacity: 0.82;
}

.thread-files-empty {
  padding: 6px 8px;
  color: var(--text-dim);
  font-size: var(--font-size-body-md);
  line-height: 16px;
}

.thread-file-preview__image {
  display: block;
  max-width: 100%;
  max-height: 320px;
  margin: 0 auto;
  border-radius: 10px;
  object-fit: contain;
}

.thread-files-list button.thread-file-row {
  box-sizing: border-box;
  width: 100%;
  min-height: 0;
  height: 22px;
  margin: 0;
  padding: 0 8px 0 calc(10px + var(--tree-depth, 0) * 14px);
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  font: inherit;
  color: inherit;
  text-align: left;
  appearance: none;
  -webkit-appearance: none;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
}

.thread-files-list button.thread-file-row:hover:not(.is-active) {
  background: var(--bg-surface-hover);
}

.thread-files-list button.thread-file-row.is-active {
  background: var(--bg-surface-accent-selected);
  box-shadow: inset 3px 0 0 var(--accent-main);
}

.thread-files-list button.thread-file-row.is-active .thread-file-row__path {
  font-weight: 500;
  color: var(--text-main);
}

.thread-file-row:focus,
.thread-file-row:focus-visible {
  outline: none;
}

.thread-files-list button.thread-file-row:focus-visible:not(.is-active),
.thread-files-list button.thread-file-row:hover:focus-visible:not(.is-active) {
  background: var(--bg-surface-hover);
}

.thread-files-list button.thread-file-row.is-active:focus-visible {
  background: var(--bg-surface-accent-selected);
  box-shadow: inset 3px 0 0 var(--accent-main);
}

.thread-file-row__path {
  flex: 1 1 auto;
  min-width: 0;
  font-family: inherit;
  color: var(--text-main);
  font-size: var(--font-size-body-md);
  font-weight: 400;
  line-height: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-file-row__meta {
  flex: 0 0 auto;
  min-width: 4.5em;
  text-align: right;
  color: var(--text-dim);
  font-size: var(--font-size-caption);
  line-height: 22px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  opacity: 0.82;
}

.thread-file-preview {
  min-height: 96px;
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  padding: 10px 12px 0;
  gap: 10px;
  overflow: hidden;
}

.thread-file-preview.hidden {
  display: none;
}

.thread-file-preview__header {
  display: grid;
  gap: 10px;
}

.thread-file-preview__actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.thread-file-preview__content.document-viewer-text {
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  white-space: normal;
  padding: 0;
  background: var(--doc-bg);
  color: var(--doc-text);
  border-color: var(--doc-border);
  color-scheme: light;
}

.thread-file-preview__content.document-viewer-text .document-viewer-markdown {
  flex: 1;
  min-height: 0;
}

.thread-file-preview__content.document-viewer-text .document-viewer-pre,
.thread-file-preview__content.document-viewer-text .thread-file-preview__code {
  margin: 0;
  border: 0;
  border-radius: 0;
  flex: 1 1 auto;
  min-height: 0;
  background: var(--doc-paper);
  color: var(--doc-text);
}

.thread-file-preview__content.document-viewer-text {
  display: flex;
  flex-direction: column;
}

.thread-file-preview__content .document-viewer-frame {
  min-height: 0;
  flex: 1;
  height: 100%;
}

.thread-file-preview__content .document-viewer-image-wrap {
  min-height: 0;
}

.thread-file-preview__content .document-viewer-docx-canvas {
  min-height: 0;
  overflow: auto;
}

.thread-file-preview__content {
  min-height: 0;
  flex: 1;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  overflow: auto;
  scrollbar-gutter: stable;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: var(--font-size-body-sm);
  background: var(--bg-surface-2);
  color: var(--text-main);
}

.thread-file-preview__content--markdown {
  padding: 0;
  border: 0;
  border-radius: 0;
  white-space: normal;
  background: transparent;
}

.thread-file-preview__content--binary {
  flex: 0 0 auto;
  min-height: auto;
  overflow: visible;
}

.thread-file-preview__message {
  box-sizing: border-box;
  width: 100%;
}

.thread-file-preview__content--code {
  padding: 0;
  border: 0;
  border-radius: 0;
  white-space: normal;
  background: transparent;
}

.thread-file-preview__code code.hljs {
  background: transparent;
}

.thread-file-preview__code {
  margin: 0;
  padding: 16px 18px;
  overflow: auto;
  border-radius: 0;
  border: 0;
  background: var(--doc-paper, var(--bg-surface-2));
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

.thread-file-preview__code code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

@media (max-width: 480px) {
  .app {
    --thread-files-rail: 36px;
  }

  .main {
    padding-right: 36px;
  }

  .chat-input-area {
    right: 36px;
  }

  .app:has(.thread-files-sidebar:not(.is-collapsed)) .main {
    padding-right: var(--thread-files-rail, 36px);
  }

  .app:has(.thread-files-sidebar:not(.is-collapsed)) .chat-input-area {
    right: var(--thread-files-rail, 36px);
  }

  .thread-files-sidebar.is-collapsed {
    width: 36px;
    min-width: 36px;
    max-width: 36px;
    grid-template-columns: 36px;
  }

  .thread-files-toggle {
    width: 36px;
    min-width: 36px;
  }
}
