:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --ink: #1a1d24;
  --muted: #5c6578;
  --line: #e2e5ec;
  --user: #1f2a3a;
  --user-ink: #f5f7fb;
  --assist: #ffffff;
  --accent: #2a6f6a;
  --warn: #9a4b2e;
  --radius: 18px;
  --composer-radius: 24px;
  --max: 720px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

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

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 960px;
  margin: 0 auto;
  background:
    radial-gradient(900px 420px at 50% -20%, #e8eef2 0%, transparent 70%),
    var(--bg);
  transition: max-width 0.25s ease;
}

.app.story-open {
  max-width: 1280px;
}

.workspace {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.chat-column {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  min-height: 0;
}

.app.story-open .chat-column {
  flex: 1 1 48%;
  border-right: 1px solid var(--line);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.story-toggle[aria-pressed="true"] {
  color: var(--ink);
  border-color: var(--accent);
  background: rgba(42, 111, 106, 0.08);
}

/* Research story layer — secondary to chat */
.story-panel {
  flex: 1 1 52%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(165deg, #f7faf9 0%, #eef3f4 42%, #f4f5f7 100%);
  overflow: hidden;
}

.story-panel[hidden] {
  display: none !important;
}

.story-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem 0.55rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.72);
  flex-shrink: 0;
}

.story-kicker {
  margin: 0 0 0.2rem;
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 650;
}

.story-page-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.story-hero {
  padding: 0.85rem 1rem 0.55rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.story-hero h2 {
  margin: 0 0 0.25rem;
  font-size: 1.05rem;
}

.story-sub {
  margin: 0 0 0.45rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.story-thesis {
  margin: 0 0 0.55rem;
  font-size: 0.9rem;
  line-height: 1.45;
  max-width: 42rem;
}

.story-pillars {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0 0 0.45rem;
}

.story-pillars span {
  font-size: 0.72rem;
  color: var(--accent);
  background: rgba(42, 111, 106, 0.1);
  border: 1px solid rgba(42, 111, 106, 0.18);
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
}

.story-ext {
  margin: 0;
  font-size: 0.78rem;
}

.story-ext a { color: var(--accent); }

.story-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.55rem 1rem;
  border-bottom: 1px solid var(--line);
  max-height: 9.5rem;
  overflow: auto;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.45);
}

.story-path-label {
  flex: 1 0 100%;
  margin: 0.25rem 0 0;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.story-nav-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.28rem 0.65rem;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.story-nav-btn.happy {
  border-color: rgba(42, 111, 106, 0.35);
  color: var(--ink);
}

.story-nav-btn.active,
.story-nav-btn:hover {
  color: var(--ink);
  border-color: var(--accent);
  background: rgba(42, 111, 106, 0.08);
}

.story-body {
  flex: 1;
  overflow: auto;
  padding: 0.85rem 1.1rem 2rem;
  font-size: 0.9rem;
  line-height: 1.5;
}

.story-body h1 { font-size: 1.25rem; margin: 0 0 0.65rem; }
.story-body h2 { font-size: 1.05rem; margin: 1.1rem 0 0.45rem; }
.story-body h3 { font-size: 0.95rem; margin: 0.95rem 0 0.35rem; }
.story-body h4 { font-size: 0.88rem; margin: 0.8rem 0 0.3rem; }
.story-body p { margin: 0 0 0.7rem; }
.story-body ul, .story-body ol { margin: 0 0 0.75rem 1.1rem; padding: 0; }
.story-body li { margin: 0.2rem 0; }
.story-body blockquote {
  margin: 0 0 0.85rem;
  padding: 0.45rem 0.75rem;
  border-left: 3px solid var(--accent);
  background: rgba(42, 111, 106, 0.06);
  color: var(--ink);
}
.story-body code {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 0.8em;
  background: #eef1f5;
  padding: 0.05rem 0.3rem;
  border-radius: 4px;
}
.story-body pre {
  overflow: auto;
  background: #1a1d24;
  color: #e8ecf2;
  padding: 0.75rem;
  border-radius: 10px;
  font-size: 0.75rem;
}
.story-body a { color: var(--accent); }
.story-note {
  margin: 0 0 0.85rem;
  padding: 0.65rem 0.8rem;
  border-radius: 12px;
  background: rgba(42, 111, 106, 0.08);
  border: 1px solid rgba(42, 111, 106, 0.16);
}
.story-table-wrap {
  overflow: auto;
  margin: 0 0 0.9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
.story-body table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.78rem;
}
.story-body th, .story-body td {
  border-bottom: 1px solid var(--line);
  padding: 0.4rem 0.55rem;
  text-align: left;
  vertical-align: top;
}
.story-body th { background: #f0f3f6; font-weight: 650; }
.story-media {
  margin: 0 0 1rem;
}
.story-img, .story-video {
  display: block;
  width: 100%;
  max-height: 280px;
  object-fit: contain;
  border-radius: 12px;
  background: #0f1418;
  border: 1px solid var(--line);
}
.story-media figcaption {
  margin-top: 0.35rem;
  font-size: 0.72rem;
  color: var(--muted);
}
.story-loading, .story-error { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.1rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(8px);
  flex-shrink: 0;
}

.brand-row {
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
}

.brand {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.85rem;
}

.ghost-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.ghost-btn:hover { color: var(--ink); border-color: #c8ceda; }

.chat-shell {
  flex: 1;
  overflow: auto;
  padding: 1rem 1rem 0.5rem;
}

.message-list {
  max-width: var(--max);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  padding-bottom: 1rem;
}

.msg {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  max-width: 100%;
}

.msg.user {
  flex-direction: row-reverse;
}

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #dfe8e6;
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 0.2rem;
}

.msg.user .avatar {
  background: var(--user);
  color: var(--user-ink);
}

.bubble {
  background: var(--assist);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.75rem 0.95rem;
  max-width: min(100%, 560px);
  line-height: 1.45;
  box-shadow: 0 1px 0 rgba(26, 29, 36, 0.03);
}

.msg.user .bubble {
  background: var(--user);
  color: var(--user-ink);
  border-color: transparent;
}

.bubble p { margin: 0 0 0.55rem; }
.bubble p:last-child { margin-bottom: 0; }
.bubble em { font-style: italic; color: inherit; opacity: 0.92; }

.bubble .meta {
  margin-top: 0.55rem;
  font-size: 0.72rem;
  color: var(--muted);
}

.msg.user .bubble .meta { color: rgba(245, 247, 251, 0.65); }

.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.45rem;
}

.attach-preview span {
  font-size: 0.72rem;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.06);
}

.msg.user .attach-preview span {
  background: rgba(255, 255, 255, 0.12);
}

.raw-details {
  margin-top: 0.55rem;
  font-size: 0.72rem;
  color: var(--muted);
}

.raw-details summary {
  cursor: pointer;
  user-select: none;
}

.raw-details pre {
  margin: 0.4rem 0 0;
  padding: 0.55rem;
  background: #f0f2f6;
  border-radius: 8px;
  overflow: auto;
  max-height: 12rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 0.68rem;
  color: #334;
}

.typing {
  display: inline-flex;
  gap: 0.28rem;
  align-items: center;
  padding: 0.2rem 0;
}

.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.45;
  animation: blink 1.1s infinite ease-in-out;
}

.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 0.9; transform: translateY(-2px); }
}

.composer-wrap {
  flex-shrink: 0;
  padding: 0.35rem 1rem 0.85rem;
  background: linear-gradient(180deg, transparent, var(--bg) 28%);
}

.attach-chips {
  max-width: var(--max);
  margin: 0 auto 0.4rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.attach-chips[hidden], .ground-bar[hidden] { display: none !important; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.25rem 0.55rem;
  color: var(--muted);
}

.chip button {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  font-size: 0.9rem;
  line-height: 1;
}

.ground-bar {
  max-width: var(--max);
  margin: 0 auto 0.45rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.ground-bar input {
  width: 100%;
  border: 1px solid #d7b3a4;
  background: #fff8f5;
  border-radius: 12px;
  padding: 0.55rem 0.7rem;
  font: inherit;
  color: var(--ink);
}

.jit-bar {
  max-width: var(--max);
  margin: 0 auto 0.45rem;
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 0.45rem;
}

.jit-bar input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 0.5rem 0.7rem;
  font: inherit;
  font-size: 0.9rem;
  color: var(--ink);
}

.jit-bar input:focus {
  outline: 2px solid rgba(42, 111, 106, 0.35);
  border-color: var(--accent);
}

.status-lock {
  max-width: var(--max);
  margin: 0 auto 0.4rem;
  padding: 0.55rem 0.75rem;
  border-left: 3px solid var(--accent);
  background: rgba(42, 111, 106, 0.08);
  color: var(--ink);
  font-size: 0.88rem;
}

.status-lock[hidden] { display: none !important; }

.composer {
  max-width: var(--max);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--composer-radius);
  padding: 0.45rem 0.5rem 0.45rem 0.35rem;
  box-shadow: 0 8px 28px rgba(26, 29, 36, 0.06);
}

.composer.drag-hot {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(42, 111, 106, 0.12);
}

.icon-btn, .send-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  background: transparent;
  color: var(--muted);
}

.icon-btn:hover { color: var(--ink); background: #f0f2f6; }

.send-btn {
  background: var(--accent);
  color: #fff;
}

.send-btn:hover:not(:disabled) { filter: brightness(1.05); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

#textInput {
  flex: 1;
  border: 0;
  resize: none;
  outline: none;
  font: inherit;
  font-size: 0.98rem;
  line-height: 1.4;
  max-height: 160px;
  padding: 0.55rem 0.25rem;
  background: transparent;
  color: var(--ink);
}

.composer-hint {
  max-width: var(--max);
  margin: 0.4rem auto 0;
  text-align: center;
  font-size: 0.7rem;
  color: var(--muted);
}

.app.drop-target::after {
  content: "Drop files to attach";
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(244, 245, 247, 0.82);
  color: var(--accent);
  font-size: 1.1rem;
  font-weight: 600;
  pointer-events: none;
  z-index: 50;
}

/* Past sessions drawer */
.app.sessions-open {
  max-width: 1100px;
}

.sessions-drawer {
  flex: 0 0 280px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.88);
  overflow: hidden;
}

.sessions-drawer[hidden] {
  display: none !important;
}

.sessions-drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.85rem 0.9rem 0.55rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.sessions-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
}

.sessions-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0.55rem 1rem;
}

.session-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.65rem 0.6rem;
  border-radius: 12px;
  border: 1px solid transparent;
  margin-bottom: 0.35rem;
}

.session-row:hover {
  border-color: var(--line);
  background: rgba(42, 111, 106, 0.05);
}

.session-row.active {
  border-color: rgba(42, 111, 106, 0.35);
  background: rgba(42, 111, 106, 0.08);
}

.session-row-title {
  font-size: 0.88rem;
  font-weight: 650;
  color: var(--ink);
  line-height: 1.3;
}

.session-row-meta {
  font-size: 0.72rem;
  color: var(--muted);
}

.session-row-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.session-row-actions button {
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.28rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

.session-row-actions button.primary {
  border-color: rgba(42, 111, 106, 0.45);
  color: var(--accent);
  background: rgba(42, 111, 106, 0.08);
}

.session-row-actions button:hover {
  border-color: var(--accent);
}

.sessions-empty {
  margin: 1rem 0.9rem;
  color: var(--muted);
  font-size: 0.85rem;
}

@media (max-width: 900px) {
  .app.story-open {
    max-width: 100%;
  }
  .app.story-open .workspace {
    flex-direction: column;
  }
  .app.story-open .chat-column {
    flex: 0 0 42%;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .story-panel {
    flex: 1 1 58%;
  }
  .app.sessions-open .workspace {
    flex-direction: column;
  }
  .sessions-drawer {
    flex: 0 0 auto;
    max-height: 40%;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
}

@media (max-width: 640px) {
  .ground-bar { grid-template-columns: 1fr; }
  .jit-bar { grid-template-columns: 1fr; }
  .bubble { max-width: 92%; }
  .brand-sub { display: none; }
  .app.story-open .chat-column { flex: 0 0 38%; }
}
