:root {
  --bg: #0e1116;
  --sidebar: #141820;
  --panel: #1a1f29;
  --line: #2a3140;
  --text: #e8ecf3;
  --muted: #93a0b5;
  --accent: #3ecf8e;
  --accent-dim: #1f6b4a;
  --accent-soft: rgba(62, 207, 142, 0.12);
  --user: #245c45;
  --user-text: #eafff4;
  --assistant: #1a1f29;
  --danger: #f07178;
  --warn: #e6c07b;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --font: "Figtree", "Segoe UI", sans-serif;
  --serif: "Source Serif 4", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}

[hidden] { display: none !important; }

/* —— Gate —— */
.gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 480px at 50% -10%, #1a3d30 0%, transparent 55%),
    var(--bg);
}

.gate[hidden] { display: none !important; }

.gate-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.gate-card h1 {
  margin: 0;
  font-size: 1.55rem;
  letter-spacing: -0.03em;
  font-weight: 700;
}

.gate-card p { margin: 0; color: var(--muted); }

.gate-card input {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  font: 1rem var(--font);
  background: var(--bg);
  color: var(--text);
}

.gate-card button {
  border: 0;
  border-radius: 12px;
  padding: 12px;
  background: var(--accent);
  color: #062016;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
}

.gate-error { color: var(--danger) !important; font-size: 0.9rem; }

/* —— Shell —— */
.shell {
  height: 100%;
  display: grid;
  grid-template-columns: 280px 1fr;
}

.shell[hidden] { display: none !important; }

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-top {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.new-chat,
.upload-btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 12px;
  font: 600 0.92rem var(--font);
  cursor: pointer;
  text-align: center;
  color: var(--text);
}

.new-chat {
  background: var(--accent);
  color: #062016;
  border-color: transparent;
}

.upload-btn:hover,
.new-chat:hover { filter: brightness(1.06); }

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-item {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer;
  font-family: var(--font);
  color: var(--text);
}

.session-item:hover { background: rgba(255, 255, 255, 0.04); }

.session-item.active {
  background: var(--panel);
  border: 1px solid var(--line);
}

.session-item .row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.session-item .t {
  flex: 1;
  min-width: 0;
  font-weight: 650;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .s {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-empty {
  padding: 16px 12px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.session-menu {
  position: fixed;
  z-index: 1000;
  min-width: 160px;
  background: #1a2030;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  padding: 4px;
}

.session-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: #ffb4b4;
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
}

.session-menu button:hover {
  background: rgba(255, 92, 92, 0.15);
}

.status-pill {
  flex-shrink: 0;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  white-space: nowrap;
}

.status-pill.running {
  color: #062016;
  background: var(--warn);
  border-color: transparent;
  animation: pulse 1.4s ease infinite;
}

.status-pill.finished {
  color: #062016;
  background: var(--accent);
  border-color: transparent;
}

.status-pill.error {
  color: #fff;
  background: #8f2f2f;
  border-color: transparent;
}

.status-pill.cancelled {
  color: var(--warn);
  border-color: rgba(230, 192, 123, 0.45);
  background: rgba(230, 192, 123, 0.1);
}

.status-pill.idle {
  color: var(--muted);
}

.sidebar-foot {
  padding: 12px 14px 16px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.75rem;
}

.linkish {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font: 0.75rem var(--font);
  text-decoration: underline;
  color: var(--muted);
}

.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
  position: relative;
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(20, 24, 32, 0.9);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.download-btn {
  flex-shrink: 0;
  text-decoration: none;
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--accent);
  color: #062016;
  font-weight: 700;
  font-size: 0.9rem;
}

.download-btn[hidden] { display: none !important; }
.download-btn:hover { filter: brightness(1.05); }

.log-btn {
  flex-shrink: 0;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
}

.log-btn:hover {
  color: #e8eef8;
  background: rgba(255, 255, 255, 0.08);
}

.log-modal-card {
  max-width: min(720px, 94vw);
  width: 100%;
}

.log-modal-body {
  max-height: min(55vh, 480px);
  overflow: auto;
  background: #0c0f14;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.45;
  color: #c5d0e0;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0 0 12px;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.muted { color: var(--muted); margin: 4px 0 0; font-size: 0.85rem; }

.thread {
  overflow-y: auto;
  padding: 24px clamp(16px, 4vw, 48px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.welcome {
  margin: auto;
  max-width: 36rem;
  text-align: center;
  padding: 24px 8px 48px;
}

.welcome h3 {
  margin: 0 0 8px;
  font-size: 1.65rem;
  letter-spacing: -0.03em;
  font-weight: 700;
  font-family: var(--serif);
}

.welcome > p {
  margin: 0 0 20px;
  color: var(--muted);
  line-height: 1.55;
}

.suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.suggestions button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 14px;
  padding: 12px 14px;
  font: 0.92rem var(--font);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.suggestions button:hover {
  border-color: var(--accent-dim);
  background: var(--accent-soft);
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(760px, 100%);
  width: 100%;
  align-self: center;
}

.msg.user { align-items: flex-end; }
.msg.assistant,
.msg.system { align-items: flex-start; }

.msg .role {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0 4px;
}

.msg .bubble {
  padding: 14px 18px;
  border-radius: 18px;
  line-height: 1.55;
  word-break: break-word;
  box-shadow: var(--shadow);
}

.msg.user .bubble {
  background: var(--user);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
  font-weight: 500;
}

.msg.assistant .bubble {
  background: var(--assistant);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
  font-family: var(--serif);
  font-size: 1.05rem;
  line-height: 1.65;
  color: #dce3ef;
}

.msg.system .bubble {
  background: transparent;
  box-shadow: none;
  color: var(--muted);
  border: 1px dashed var(--line);
  font-size: 0.9rem;
  font-family: var(--font);
  white-space: pre-wrap;
}

.msg.error {
  align-items: flex-start;
  max-width: min(720px, 100%);
}

.msg.error .bubble {
  background: rgba(255, 92, 92, 0.1);
  border: 1px solid rgba(255, 92, 92, 0.45);
  border-radius: 14px;
  border-bottom-left-radius: 6px;
  color: #ffd0d0;
  font-family: var(--font);
  white-space: pre-wrap;
  font-size: 0.98rem;
  line-height: 1.5;
}

.msg.error .role {
  color: #ff8e8e;
}

.retry-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.retry-bar button {
  border: 1px solid rgba(255, 92, 92, 0.4);
  background: rgba(255, 92, 92, 0.15);
  color: #ffe0e0;
  border-radius: 10px;
  padding: 8px 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.retry-bar button:hover {
  filter: brightness(1.1);
}

.retry-bar button.primary {
  background: var(--accent);
  border-color: transparent;
  color: #062016;
}

/* Markdown in assistant answers */
.msg.assistant .bubble.md > *:first-child { margin-top: 0; }
.msg.assistant .bubble.md > *:last-child { margin-bottom: 0; }

.msg.assistant .bubble.md h1,
.msg.assistant .bubble.md h2,
.msg.assistant .bubble.md h3,
.msg.assistant .bubble.md h4 {
  font-family: var(--font);
  font-weight: 750;
  letter-spacing: -0.02em;
  color: #f3f6fb;
  margin: 1.1em 0 0.45em;
  line-height: 1.25;
}

.msg.assistant .bubble.md h1 { font-size: 1.35rem; }
.msg.assistant .bubble.md h2 { font-size: 1.2rem; }
.msg.assistant .bubble.md h3 { font-size: 1.08rem; color: var(--accent); }

.msg.assistant .bubble.md p {
  margin: 0.65em 0;
}

.msg.assistant .bubble.md ul,
.msg.assistant .bubble.md ol {
  margin: 0.55em 0;
  padding-left: 1.35em;
}

.msg.assistant .bubble.md li {
  margin: 0.28em 0;
}

.msg.assistant .bubble.md strong {
  font-weight: 750;
  color: #fff;
  font-family: var(--font);
}

.msg.assistant .bubble.md code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.1em 0.35em;
}

.msg.assistant .bubble.md pre {
  background: #0c0f14;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.8em 0;
}

.msg.assistant .bubble.md pre code {
  background: none;
  border: 0;
  padding: 0;
  font-size: 0.84rem;
}

.msg.assistant .bubble.md blockquote {
  margin: 0.8em 0;
  padding: 0.2em 0 0.2em 0.9em;
  border-left: 3px solid var(--accent-dim);
  color: var(--muted);
}

/* Live activity while working */
.activity {
  width: 100%;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #171c26 0%, #12161e 100%);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.activity.done {
  opacity: 0.72;
}

.activity-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

.activity.done .spinner {
  animation: none;
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 2px #12161e;
}

@keyframes spin { to { transform: rotate(360deg); } }

.activity-status {
  font-size: 0.92rem;
  font-weight: 650;
  color: var(--text);
}

.activity-log {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 180px;
  overflow-y: auto;
}

.activity-log li {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 8px;
  align-items: start;
  font-size: 0.82rem;
  color: var(--muted);
  font-family: var(--mono);
  line-height: 1.35;
}

.activity-log li .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-top: 4px;
  background: var(--line);
}

.activity-log li.running { color: var(--warn); }
.activity-log li.running .dot {
  background: var(--warn);
  box-shadow: 0 0 0 3px rgba(230, 192, 123, 0.2);
  animation: pulse 1.2s ease infinite;
}

.activity-log li.completed { color: #9fd9bc; }
.activity-log li.completed .dot { background: var(--accent); }

.activity-log li.error { color: var(--danger); }
.activity-log li.error .dot { background: var(--danger); }

.activity-log li.info { color: var(--muted); }
.activity-log li.info .dot { background: #5b6b82; }

@keyframes pulse {
  50% { box-shadow: 0 0 0 5px rgba(230, 192, 123, 0.05); }
}

.activity-log .label { min-width: 0; }
.activity-log .state {
  text-transform: lowercase;
  opacity: 0.85;
  white-space: nowrap;
}

.composer {
  padding: 12px clamp(16px, 4vw, 48px) 20px;
  background: linear-gradient(180deg, transparent, rgba(14, 17, 22, 0.95) 35%);
}

.run-actions {
  max-width: 760px;
  margin: 0 auto 10px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.run-actions[hidden] { display: none !important; }

.btn.danger {
  background: #8f2f2f;
  color: #ffe8e8;
}

.btn.danger:hover { filter: brightness(1.08); }

.composer-row {
  max-width: 760px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 10px;
  box-shadow: var(--shadow);
}

.composer-row:focus-within {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px rgba(62, 207, 142, 0.15);
}

textarea {
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: 1rem/1.45 var(--font);
  padding: 8px 10px;
  max-height: 160px;
  min-height: 40px;
}

#sendBtn {
  border: 0;
  border-radius: 12px;
  height: 40px;
  padding: 0 16px;
  background: var(--accent);
  color: #062016;
  font-weight: 700;
  cursor: pointer;
}

#sendBtn:disabled { opacity: 0.45; cursor: not-allowed; }

.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(14, 17, 22, 0.88);
  border: 2px dashed var(--accent);
  pointer-events: none;
}

.drop-overlay[hidden] { display: none !important; }

.drop-overlay p {
  text-align: center;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0;
}

.drop-overlay span {
  display: block;
  margin-top: 6px;
  font-size: 0.9rem;
  font-weight: 400;
  color: var(--muted);
}

.modal {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.55);
  padding: 16px;
}

.modal[hidden] { display: none !important; }

.modal-card {
  width: min(420px, 100%);
  background: var(--panel);
  border-radius: 16px;
  padding: 20px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.modal-card h3 { margin: 0 0 6px; font-weight: 700; }
.modal-file {
  margin: 0 0 14px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.8rem;
  word-break: break-all;
}

.modal-card input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  font: 1rem var(--font);
  margin-bottom: 14px;
  background: var(--bg);
  color: var(--text);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--accent);
  color: #062016;
  font-weight: 700;
  cursor: pointer;
}

.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}

@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    max-height: 38vh;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
}
