:root {
  color-scheme: light;
  --ink: #1e2329;
  --muted: #657180;
  --line: #d7dde4;
  --paper: #f7f8f5;
  --panel: #ffffff;
  --accent: #2563eb;
  --accent-2: #0f766e;
  --warn: #b7791f;
  --bad: #b42318;
  --good: #16803c;
  --shadow: 0 18px 50px rgba(30, 35, 41, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", sans-serif;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(37, 99, 235, 0.06), transparent 260px),
    var(--paper);
}

button,
textarea,
input {
  font: inherit;
}

.app-shell {
  width: min(1240px, calc(100vw - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 22px 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 16px;
}

.topbar {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-2);
  font-size: 13px;
  font-weight: 700;
}

h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: 0;
}

.status-pill {
  flex: 0 0 auto;
  height: 36px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 14px;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.nav-link,
.material-link {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--panel);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
}

.nav-link:hover,
.material-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}

.status-pill.ready .status-dot {
  background: var(--good);
}

.status-pill.error .status-dot {
  background: var(--bad);
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 18px;
}

.chat-panel,
.side-panel {
  min-width: 0;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.chat-panel {
  min-height: calc(100vh - 132px);
  display: grid;
  grid-template-rows: 1fr auto;
  overflow: hidden;
}

.messages {
  min-height: 0;
  overflow: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message {
  align-self: stretch;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
}

.message.user {
  grid-template-columns: minmax(0, 1fr) 34px;
}

.message.user .avatar {
  grid-column: 2;
  background: #2f5d50;
}

.message.user .bubble {
  grid-row: 1;
  border-color: rgba(15, 118, 110, 0.26);
  background: #f2fbf8;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: white;
  font-size: 14px;
  font-weight: 800;
}

.bubble {
  height: fit-content;
  min-width: 0;
  max-width: 760px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--panel);
  line-height: 1.62;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble p {
  margin: 0;
}

.meta-line {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.composer {
  border-top: 1px solid var(--line);
  padding: 14px;
  background: rgba(247, 248, 245, 0.82);
}

.mode-control {
  width: fit-content;
  display: inline-grid;
  grid-template-columns: repeat(2, minmax(84px, auto));
  gap: 4px;
  margin-bottom: 10px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.mode-help {
  max-width: 760px;
  margin: -2px 0 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.mode-button {
  height: 32px;
  border: 0;
  border-radius: 6px;
  padding: 0 12px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-weight: 700;
}

.mode-button.active,
.mode-button[aria-pressed="true"] {
  color: white;
  background: var(--accent);
}

textarea {
  display: block;
  width: 100%;
  min-height: 88px;
  max-height: 180px;
  resize: vertical;
  border: 1px solid #c4ccd6;
  border-radius: 8px;
  padding: 12px 13px;
  color: var(--ink);
  background: white;
  outline: none;
}

textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.composer-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: stretch;
}

.sample-row {
  min-width: 0;
  --sample-row-height: 38px;
  margin-top: 10px;
  display: grid;
  grid-auto-rows: var(--sample-row-height);
  gap: 8px;
  max-height: calc(var(--sample-row-height) * 5 + 8px * 4);
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 2px;
}

.sample-button,
.send-button {
  border-radius: 8px;
  border: 1px solid var(--line);
  white-space: nowrap;
  cursor: pointer;
}

.sample-button {
  height: 38px;
  width: 100%;
  padding: 0 11px;
  color: #384454;
  background: white;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sample-button:hover {
  border-color: var(--accent-2);
  color: var(--accent-2);
}

.send-button {
  min-width: 88px;
  height: auto;
  padding: 0 18px;
  border-color: var(--accent);
  color: white;
  background: var(--accent);
  font-weight: 700;
}

.send-button:disabled {
  cursor: wait;
  opacity: 0.68;
}

.side-panel {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
}

.memory-map {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #e9edf0;
}

.metric-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.metric {
  min-height: 82px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: white;
}

.metric span {
  display: block;
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 17px;
  line-height: 1.3;
}

.notes {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.notes h2 {
  margin: 0 0 6px;
  font-size: 14px;
}

.notes p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.58;
}

.materials-shell {
  width: min(1120px, calc(100vw - 32px));
}

.materials-page {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 14px;
}

.materials-toolbar {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr)) minmax(260px, 1.6fr);
  gap: 10px;
  align-items: stretch;
}

.material-stat,
.material-search {
  min-width: 0;
  min-height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.9);
}

.material-stat span,
.material-search span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.material-stat strong {
  display: block;
  margin-top: 8px;
  font-size: 22px;
  line-height: 1.2;
}

.material-search input {
  width: 100%;
  height: 36px;
  margin-top: 8px;
  border: 1px solid #c4ccd6;
  border-radius: 8px;
  padding: 0 10px;
  color: var(--ink);
  background: white;
  outline: none;
}

.material-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.materials-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 24px;
}

.material-item {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.material-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
  padding: 14px 16px 10px;
}

.material-title {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.material-meta {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.source-tag {
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(15, 118, 110, 0.25);
  border-radius: 8px;
  padding: 0 9px;
  color: var(--accent-2);
  background: #f2fbf8;
  font-size: 13px;
  font-weight: 800;
}

.material-summary,
.material-full {
  margin: 0;
  padding: 0 16px 14px;
  line-height: 1.68;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.material-summary {
  color: #3a4553;
}

.material-full {
  display: none;
  border-top: 1px solid var(--line);
  padding-top: 14px;
  color: var(--ink);
}

.material-item.expanded .material-summary {
  display: none;
}

.material-item.expanded .material-full {
  display: block;
}

.material-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px 16px;
}

.material-toggle {
  min-height: 36px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0 12px;
  color: white;
  background: var(--accent);
  font-size: 14px;
  font-weight: 800;
}

.loading-state {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.9);
}

@media (max-width: 900px) {
  .app-shell {
    width: min(100vw - 20px, 720px);
    padding: 12px 0;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    padding-bottom: 12px;
  }

  .top-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .chat-panel {
    min-height: 68vh;
  }

  .materials-toolbar {
    grid-template-columns: 1fr 1fr;
  }

  .material-search {
    grid-column: 1 / -1;
  }
}

@media (max-width: 520px) {
  h1 {
    font-size: 24px;
  }

  .messages {
    padding: 12px;
  }

  .message,
  .message.user {
    grid-template-columns: 30px minmax(0, 1fr);
  }

  .message.user .avatar {
    grid-column: 1;
  }

  .message.user .bubble {
    grid-column: 2;
  }

  .avatar {
    width: 30px;
    height: 30px;
    border-radius: 8px;
  }

  .composer-input {
    grid-template-columns: minmax(0, 1fr) 72px;
  }

  .send-button {
    width: 100%;
    min-width: 0;
    padding: 0 10px;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

  .materials-shell {
    width: min(100vw - 20px, 720px);
  }

  .materials-toolbar {
    grid-template-columns: 1fr;
  }

  .material-search {
    grid-column: auto;
  }

  .material-header {
    grid-template-columns: 1fr;
  }
}
