/* ============================================================
   panels.css — Side panels: history, TOC, chat.
   ============================================================ */

/* ---------- Version history panel ---------- */

#history-panel {
  position: fixed;
  right: 12px;
  bottom: 34px;
  z-index: 75;
  width: 380px;
  max-width: calc(100vw - 24px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
  overflow: hidden;
}

#history-panel.hidden {
  display: none;
}

.hist-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border-soft);
}

.hist-title {
  flex: 1;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
}

.hist-sub {
  font: 0.72rem var(--font-mono);
  color: var(--text-faint);
}

#history-close {
  width: 24px;
  height: 24px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-faint);
  font-size: 1rem;
  cursor: pointer;
}

#history-close:hover {
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
}

#history-list {
  max-height: 340px;
  overflow-y: auto;
  padding: 4px 0;
}

.hist-empty {
  padding: 12px 14px 14px;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text-faint);
}

.hist-item {
  padding: 6px 12px 7px;
  cursor: pointer;
}

.hist-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.hist-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.hist-time {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
}

.hist-reason {
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent-dim);
  font-size: 0.68rem;
  color: var(--accent);
}

.hist-delta {
  margin-inline-start: auto;
  font: 0.75rem var(--font-mono);
  color: var(--text-faint);
}

.hist-delta.plus {
  color: var(--success-hover);
}

.hist-delta.minus {
  color: var(--error);
}

.hist-hint {
  margin-top: 2px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 0.75rem;
  color: var(--text-faint);
}

/* Read-only snapshot preview inside the restore dialog. */
.dlg-preview {
  margin-top: 4px;
  max-height: 46vh;
  overflow-y: auto;
  padding: 10px 14px;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: var(--bg-panel);
  font-size: 0.85rem;
}

.workspace-modal-layer:has(.dlg-preview) .workspace-modal {
  width: min(640px, calc(100vw - 40px));
}

/* ---------- Table of contents panel ---------- */

#toc-panel {
  width: 250px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  margin-right: 0;
  transition: margin-right 0.18s ease;
  overflow: hidden;
  user-select: none;
}

#toc-panel.collapsed {
  margin-right: -250px;
}

#toc-close {
  margin-left: auto;
}

#toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.toc-item {
  padding: 5px 10px;
  border-radius: var(--radius);
  font-size: 0.84rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.toc-item:hover {
  background: var(--bg-raised);
  color: var(--text);
}

.toc-item.active {
  background: var(--accent-dim);
  color: var(--accent);
}

.toc-item.lvl1 { font-weight: 600; color: var(--text); }
.toc-item.lvl1.active { color: var(--accent); }
.toc-item.lvl2 { padding-inline-start: 22px; }
.toc-item.lvl3 { padding-inline-start: 34px; }
.toc-item.lvl4 { padding-inline-start: 46px; }
.toc-item.lvl5 { padding-inline-start: 58px; color: var(--text-faint); }
.toc-item.lvl6 { padding-inline-start: 70px; color: var(--text-faint); }

#toc-btn.open {
  color: var(--accent);
  background: var(--accent-dim);
}

/* ---------- AI Chat panel ---------- */

#chat-panel {
  --chat-w: 360px;
  width: var(--chat-w);
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  /* Collapsed margin must always equal the live width (resizable via divider),
     otherwise a resized panel leaks a visible sliver. */
  margin-right: calc(-1 * var(--chat-w));
  transition: margin-right 0.18s ease;
  overflow: hidden;
  user-select: none;
}

#chat-panel:not(.collapsed) {
  margin-right: 0;
}

#chat-divider {
  width: 6px;
  flex: none;
  background: var(--bg-panel);
  box-shadow: inset 1px 0 0 var(--border-soft), inset -1px 0 0 var(--border-soft);
  cursor: col-resize;
  display: none;
}

#chat-divider:hover,
#chat-divider.dragging {
  background: var(--accent-dim);
  box-shadow: inset 1px 0 0 var(--accent), inset -1px 0 0 var(--accent);
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 34px;
  flex: none;
  padding: 0 6px;
  border-bottom: 1px solid var(--border-soft);
}

.chat-title {
  flex: 1;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

.chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-faint);
  font-size: 0.8rem;
  cursor: pointer;
}

.chat-btn:hover {
  background: var(--bg-raised);
  color: var(--text);
}

.chat-btn svg {
  width: 14px;
  height: 14px;
}

.chat-provider-btn {
  width: auto;
  max-width: 100px;
  padding: 0 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-provider-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.chat-model-btn {
  width: auto;
  max-width: 120px;
  padding: 0 8px;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-faint);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-model-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.chat-model-refresh-btn {
  width: 26px;
  padding: 0;
}

.chat-model-refresh-btn svg {
  width: 12px;
  height: 12px;
}

.chat-model-refresh-btn:hover {
  color: var(--accent);
}

.chat-model-refresh-btn:active svg {
  transform: rotate(180deg);
  transition: transform 0.3s ease;
}

/* Custom dropdowns */
.chat-model-dropdown,
.chat-provider-dropdown {
  position: absolute;
  top: 34px;
  z-index: 5;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  padding: 3px;
}

.chat-model-dropdown {
  right: 80px;
  min-width: 140px;
  max-width: 280px;
  max-height: 220px;
  overflow-y: auto;
}

.chat-provider-dropdown {
  right: 6px;
  min-width: 120px;
}

.chat-model-dropdown.hidden,
.chat-provider-dropdown.hidden {
  display: none;
}

.chat-model-search {
  width: 100%;
  padding: 5px 8px;
  margin-bottom: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.78rem;
  outline: none;
  box-sizing: border-box;
}

.chat-model-search:focus {
  border-color: var(--accent);
}

.chat-model-search::placeholder {
  color: var(--text-faint);
}

.chat-prov-opt {
  padding: 5px 10px;
  border-radius: var(--radius);
  font-size: 0.82rem;
  color: var(--text-muted);
  cursor: pointer;
}

.chat-prov-opt:hover {
  background: var(--bg-panel);
  color: var(--text);
}

.chat-prov-opt.sel {
  background: var(--accent-dim);
  color: var(--accent);
}

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-soft);
}

.chat-msg:last-child {
  border-bottom: none;
}

.chat-msg-head {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-role {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-faint);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.chat-msg.chat-user .chat-role {
  color: var(--accent);
}

.chat-meta {
  font-size: 0.68rem;
  color: var(--text-faint);
  margin-inline-start: 6px;
}

.chat-act {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
}

.chat-act svg {
  width: 12px;
  height: 12px;
}

.chat-act:hover {
  background: var(--bg-raised);
  color: var(--text);
}

.chat-msg-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.chat-msg:hover .chat-msg-actions {
  opacity: 1;
}

.chat-msg.chat-folded .chat-msg-body {
  max-height: 60px;
  overflow: hidden;
  position: relative;
}

.chat-msg.chat-folded .chat-msg-body::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 30px;
  background: linear-gradient(transparent, var(--bg-panel));
  pointer-events: none;
}

.chat-msg-body {
  font-size: 0.875rem;
  line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
}

.chat-msg.chat-assistant .chat-msg-body {
  white-space: normal;
}

.chat-msg.chat-assistant .chat-msg-body > *:first-child {
  margin-top: 0;
}

.chat-msg.chat-assistant .chat-msg-body > *:last-child {
  margin-bottom: 0;
}

.chat-msg.chat-assistant .chat-msg-body p {
  margin: 0 0 0.7em;
  line-height: 1.65;
}

.chat-msg.chat-assistant .chat-msg-body p:last-child {
  margin-bottom: 0;
}

.chat-msg.chat-assistant .chat-msg-body h1,
.chat-msg.chat-assistant .chat-msg-body h2,
.chat-msg.chat-assistant .chat-msg-body h3,
.chat-msg.chat-assistant .chat-msg-body h4,
.chat-msg.chat-assistant .chat-msg-body h5,
.chat-msg.chat-assistant .chat-msg-body h6 {
  margin: 0.8em 0 0.4em;
  font-weight: 650;
  line-height: 1.3;
  color: var(--text);
}

.chat-msg.chat-assistant .chat-msg-body h1 { font-size: 1.15em; }
.chat-msg.chat-assistant .chat-msg-body h2 { font-size: 1.08em; }
.chat-msg.chat-assistant .chat-msg-body h3 { font-size: 1.02em; }
.chat-msg.chat-assistant .chat-msg-body h4,
.chat-msg.chat-assistant .chat-msg-body h5,
.chat-msg.chat-assistant .chat-msg-body h6 { font-size: 0.95em; color: var(--text-muted); }

.chat-msg.chat-assistant .chat-msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.8em 0;
}

.chat-msg.chat-assistant .chat-msg-body table {
  border-collapse: collapse;
  margin: 0.6em 0;
  width: 100%;
  font-size: 0.85em;
}

.chat-msg.chat-assistant .chat-msg-body th,
.chat-msg.chat-assistant .chat-msg-body td {
  border: 1px solid var(--border);
  padding: 4px 10px;
  text-align: left;
}

.chat-msg.chat-assistant .chat-msg-body th {
  background: var(--bg-code);
  font-weight: 600;
}

.chat-msg.chat-assistant .chat-msg-body code {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  background: var(--bg-code);
  border: 1px solid var(--border-soft);
  border-radius: 3px;
  padding: 1px 5px;
  color: var(--code-fg);
  direction: ltr;
  unicode-bidi: embed;
}

.chat-msg.chat-assistant .chat-msg-body pre {
  margin: 0.6em 0;
  padding: 10px 12px;
  background: var(--bg-code);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow-x: auto;
  direction: ltr;
  text-align: left;
}

.chat-msg.chat-assistant .chat-msg-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
}

.chat-msg.chat-assistant .chat-msg-body .code-block {
  margin: 0.6em 0;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow: hidden;
  direction: ltr;
  text-align: left;
}

.chat-msg.chat-assistant .chat-msg-body .code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 10px;
  background: var(--bg-code);
  border-bottom: 1px solid var(--border-soft);
  font: 0.75rem var(--font-mono);
  color: var(--text-faint);
}

.chat-msg.chat-assistant .chat-msg-body .code-copy {
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: transparent;
  color: var(--text-muted);
  font: 0.7rem var(--font-ui);
  cursor: pointer;
}

.chat-msg.chat-assistant .chat-msg-body .code-copy:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.chat-msg.chat-assistant .chat-msg-body .code-block pre {
  margin: 0;
  padding: 10px 12px;
  background: var(--bg-code);
  border: none;
  border-radius: 0;
  overflow-x: auto;
  max-height: 300px;
}

.chat-msg.chat-assistant .chat-msg-body .code-block code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
}

.chat-msg.chat-user .chat-msg-body {
  padding: 8px 12px;
  border-radius: var(--radius-lg);
  background: var(--accent-dim);
  user-select: text;
  -webkit-user-select: text;
}

.chat-msg.chat-error .chat-msg-body {
  color: var(--error);
  font-size: 0.8rem;
}

.chat-typing .chat-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.chat-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: chatDot 1.2s infinite ease-in-out;
}

.chat-dots i:nth-child(2) { animation-delay: 0.15s; }
.chat-dots i:nth-child(3) { animation-delay: 0.3s; }

@keyframes chatDot {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

.chat-input-row {
  position: relative;
  display: flex;
  align-items: flex-end;
  padding: 8px 10px;
  border-top: 1px solid var(--border-soft);
}

.chat-input {
  flex: 1;
  min-height: 36px;
  max-height: 120px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg);
  color: var(--text);
  font: 0.875rem/1.6 var(--font-ui);
  padding: 7px 48px 7px 10px;
  outline: none;
  overflow-y: auto;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12);
}

.chat-input:focus {
  border-color: var(--accent);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 2px var(--accent-dim);
}

.chat-input::placeholder {
  color: var(--text-faint);
}

.chat-input:disabled {
  opacity: 0.5;
}

.chat-send-btn {
  position: absolute;
  right: 18px;
  bottom: 14px;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--code-bg-dark);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.chat-send-btn:hover {
  filter: brightness(1.15);
  transform: scale(1.05);
}

.chat-send-btn:active {
  transform: scale(0.95);
}

.chat-send-btn:disabled {
  opacity: 0.4;
  cursor: default;
  transform: none;
}

.chat-send-btn svg {
  width: 18px;
  height: 18px;
}

.chat-status {
  flex: none;
  padding: 3px 12px 6px;
  font-size: 0.68rem;
  color: var(--text-faint);
  text-align: center;
}

/* Data Storage tab styles */
.chat-storage-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 20px;
  padding: 12px;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  background: var(--bg);
}

.chat-storage-head {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
}

.chat-storage-sub {
  font-size: 0.75rem;
  color: var(--text-faint);
}

.chat-storage-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 6px 10px;
  background: var(--bg-panel);
  border-radius: var(--radius);
}

.chat-storage-btn {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text);
  font-size: 0.82rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.chat-storage-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.chat-storage-note {
  font-size: 0.75rem;
  color: var(--text-faint);
  font-style: italic;
  margin-top: 8px;
}

.chat-storage-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* Memory Viewer */
.chat-memory-viewer {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  max-height: 300px;
  overflow-y: auto;
}

.chat-memory-viewer.hidden {
  display: none;
}

.chat-memory-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-memory-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-memory-info strong {
  color: var(--text);
}

.chat-memory-messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-memory-messages-head {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chat-memory-msg {
  padding: 8px;
  border-radius: var(--radius);
  background: var(--bg);
  border: 1px solid var(--border-soft);
}

.chat-memory-user .chat-memory-role {
  color: var(--accent);
}

.chat-memory-assistant .chat-memory-role {
  color: var(--success);
}

.chat-memory-role {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-faint);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.chat-memory-text {
  font-size: 0.8rem;
  color: var(--text);
  line-height: 1.4;
  word-break: break-word;
}

.chat-memory-empty {
  font-size: 0.8rem;
  color: var(--text-faint);
  font-style: italic;
  text-align: center;
  padding: 20px;
}

/* Message edit/delete actions */
.chat-memory-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border-soft);
}

.chat-memory-edit-btn,
.chat-memory-delete-btn {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text-muted);
  font-size: 0.72rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.chat-memory-edit-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.chat-memory-delete-btn:hover {
  border-color: var(--error);
  color: var(--error);
  background: rgba(224, 108, 117, 0.1);
}
