:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --card-bg: #ffffff;
  --text: #1c1c1e;
  --muted: #6b6b70;
  --border: #e0e0e3;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --error: #d92d20;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16161a;
    --card-bg: #202024;
    --text: #f2f2f3;
    --muted: #9a9aa2;
    --border: #333338;
    --accent: #6366f1;
    --accent-text: #ffffff;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.muted { color: var(--muted); font-weight: normal; font-size: 0.85em; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
  font-weight: 600;
  font-size: 0.9rem;
}

input, textarea, select {
  font: inherit;
  padding: 0.6rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

button {
  font: inherit;
  font-weight: 600;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

button:disabled { opacity: 0.6; cursor: default; }

.generate-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
}

.reference-toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: none;
  color: var(--muted);
  border: 1px dashed var(--border);
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 999px;
  margin-bottom: 1rem;
}

.reference-toggle-btn:hover,
.reference-toggle-btn.active {
  color: var(--accent);
  border-color: var(--accent);
}

.reference-toggle-count {
  display: inline-block;
  min-width: 1.1rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.72rem;
  font-weight: 700;
  text-align: center;
}

.reference-section {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1rem 0;
  margin-bottom: 1rem;
}

.reference-upload-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: -0.5rem 0 0.6rem;
  font-size: 0.85rem;
}

.reference-files-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0 0 1rem;
}

.reference-files-list:empty { margin: 0; }

.reference-file-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.5rem;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.8rem;
  max-width: 100%;
}

.reference-file-chip .file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}

.reference-file-chip button {
  background: none;
  color: var(--muted);
  padding: 0;
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1;
  flex-shrink: 0;
}

.reference-file-chip button:hover { color: var(--error); }

.link-btn {
  background: none;
  color: var(--accent);
  padding: 0;
  font-weight: 600;
}

.secondary-btn {
  background: none;
  color: var(--error);
  border: 1px solid var(--error);
}

.secondary-btn:hover { background: var(--error); color: #ffffff; }

.error {
  color: var(--error);
  font-weight: 600;
  margin-top: 0.75rem;
}

/* Login page */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 360px;
}

.login-card h1 { margin: 0 0 0.25rem; font-size: 1.4rem; }

/* Dashboard layout — fixed to the viewport height so the sidebar and the
   transcript each scroll independently, and the composer (prompt form) at
   the bottom of .main never gets pushed off-screen as a chat grows. */
.layout {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 280px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.sidebar-header h2 { margin: 0; font-size: 1rem; }

.new-chat-btn {
  width: 100%;
  margin-bottom: 1rem;
}

.new-folder-form {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.new-folder-form input {
  flex: 1;
  min-width: 0;
  padding: 0.4rem 0.6rem;
  font-size: 0.85rem;
}

.new-folder-form button {
  padding: 0.4rem 0.7rem;
}

.folder { margin-bottom: 0.75rem; }

.folder-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 0.5rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.folder-header.drop-target,
.history-list.drop-target {
  background: var(--bg);
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}

.item-actions {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  flex-shrink: 0;
}

.rename-folder-btn,
.rename-item-btn {
  background: none;
  color: var(--muted);
  padding: 0 0.2rem;
  font-size: 0.8rem;
  line-height: 1.4;
}

.rename-folder-btn:hover,
.rename-item-btn:hover { color: var(--accent); }

.delete-folder-btn {
  background: none;
  color: var(--muted);
  padding: 0 0.25rem;
  font-weight: 700;
  font-size: 0.9rem;
  line-height: 1;
}

.delete-folder-btn:hover { color: var(--error); }

.inline-edit-input {
  font-size: 0.85rem;
  padding: 0.15rem 0.4rem;
  width: 100%;
  min-width: 0;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.5rem;
  min-height: 10px; /* keep empty folders/unfiled a valid, visible drop target */
  border-radius: 8px;
}

.history-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.6rem 0.5rem;
  border-radius: 8px;
  cursor: grab;
  margin-bottom: 0.25rem;
}

.history-item:active { cursor: grabbing; }
.history-item:hover { background: var(--bg); }
.history-item.dragging { opacity: 0.4; }

.history-item-main { min-width: 0; flex: 1; }

.history-item .prompt {
  font-size: 0.85rem;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item .meta {
  font-size: 0.7rem;
  color: var(--muted);
}

.delete-item-btn {
  background: none;
  color: var(--muted);
  padding: 0 0.2rem;
  font-weight: 700;
  font-size: 0.9rem;
  line-height: 1.4;
  flex-shrink: 0;
}

.delete-item-btn.confirm-armed,
.delete-folder-btn.confirm-armed {
  color: #ffffff;
  background: var(--error);
  border-radius: 4px;
  padding: 0 0.35rem;
}

.delete-item-btn:hover { color: var(--error); }

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0; /* let this actually shrink to fit .layout's 100vh instead of growing past it */
  padding: 2rem 2rem 0;
  max-width: 1200px;
}

.main h1 { margin: 0 0 1rem; flex-shrink: 0; }

/* Scrolls independently of the composer below it. `justify-content: flex-end`
   would visually bottom-anchor short content too, but in several browsers it
   makes overflowed content unreachable by scrolling — a real regression, not
   just a style nitpick. `margin-top: auto` on the transcript gets the same
   "short chat sits right above the composer, no dead space" look without
   that trap: scrolling to a long chat's earlier turns keeps working normally. */
.main-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.transcript { margin-top: auto; }
.transcript:empty { margin: 0; }

/* Always right after the scrollable transcript — visible without scrolling
   the whole page, but never fixed in place over content like a modal. */
.composer {
  flex-shrink: 0;
  margin: 1.5rem 0 2rem;
}

.turn { margin-bottom: 1.5rem; }

.turn-user {
  padding: 0 0.25rem 1rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.turn-user .turn-role {
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  display: block;
  margin-bottom: 0.3rem;
}

.turn-user .turn-timestamp {
  font-weight: normal;
  text-transform: none;
  letter-spacing: normal;
}

.turn-user .turn-content { color: var(--text); white-space: pre-wrap; }

.turn-user .turn-reference-note {
  margin-top: 0.4rem;
  font-size: 0.8rem;
  font-style: italic;
}

.turn-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.turn-title { margin: 0; }
.turn-meta { margin: 0.2rem 0 0; }

.script-output {
  background: var(--bg);
  color: var(--text);
  padding: 1.25rem;
  border-radius: 8px;
  white-space: pre-wrap;
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.6;
  margin-top: 0.75rem;
}
