:root {
  color-scheme: light dark;
  --brand: light-dark(#1b4ed8, #346bf1);
  --brand-text: light-dark(#1b4ed8, #4b7cf3);
  --screen: light-dark(#ffffff, #1c1c1e);
  --sidebar: light-dark(#f5f5f7, #232325);
  --bubble: light-dark(#f0f0f3, #2c2c2e);
  --text: light-dark(#1d1d1f, #f5f5f7);
  --text-secondary: light-dark(#6e6e73, #a1a1a6);
  --text-tertiary: light-dark(#8e8e93, #7c7c80);
  --border: light-dark(rgb(0 0 0 / 0.08), rgb(255 255 255 / 0.1));
  --selected: light-dark(rgb(27 78 216 / 0.1), rgb(52 107 241 / 0.22));
  --danger: light-dark(#c4271c, #ff6961);
  --code: light-dark(#f6f6f8, #161618);
  --radius: 12px;
  accent-color: var(--brand);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--screen);
  color: var(--text);
}

button { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: minmax(240px, 300px) 1fr;
  height: 100dvh;
}

/* MARK: Sidebar */

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

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
}

.wordmark {
  font-weight: 600;
  font-size: 17px;
  letter-spacing: -0.01em;
  margin-inline-end: auto;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
}
.icon-button:hover { background: var(--border); color: var(--text); }
.icon-button:disabled { opacity: 0.4; cursor: default; }
.icon-button svg { width: 18px; height: 18px; }

.text-button {
  padding: 4px 8px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
}
.text-button:hover { background: var(--border); color: var(--text); }

/* Apple's Sign in with Apple button: black on light, white on dark, the logo and title in the system font. */
.apple-sign-in {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 240px;
  height: 44px;
  margin-top: 6px;
  padding: 0 20px;
  border: 0;
  border-radius: 8px;
  background: light-dark(#000, #fff);
  color: light-dark(#fff, #000);
  font: 500 18px/1 system-ui, -apple-system, sans-serif;
  letter-spacing: -0.01em;
  cursor: pointer;
}
.apple-sign-in svg { width: 17px; height: 17px; margin-top: -3px; }
.apple-sign-in:focus-visible { outline: 3px solid var(--brand); outline-offset: 3px; }

#search {
  margin: 0 12px 8px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--screen);
  color: var(--text);
  font: inherit;
}

.demo-note {
  margin: 0 14px 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

#list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.list-heading {
  margin: 14px 8px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-tertiary);
}

.list-item {
  display: grid;
  gap: 1px;
  padding: 7px 10px;
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
}
.list-item:hover { background: var(--border); }
.list-item[aria-current="page"] { background: var(--selected); }

.list-title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-weight: 500;
}

.list-detail {
  font-size: 12px;
  color: var(--text-tertiary);
}

.list-empty {
  margin: 24px 10px;
  color: var(--text-secondary);
}

/* MARK: Chat and page */

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

.detail .bar { border-bottom: 1px solid var(--border); }
.detail .back { display: none; }

#detail-title {
  margin: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 15px;
  font-weight: 600;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px 48px;
  padding-bottom: max(48px, env(safe-area-inset-bottom));
}

.transcript, .page {
  max-width: 760px;
  margin: 0 auto;
}

.message { margin-block-end: 18px; }
.message.user { display: flex; flex-direction: column; align-items: flex-end; }

.bubble {
  max-width: min(85%, 600px);
  padding: 9px 14px;
  border-radius: 20px;
  background: var(--bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message-meta {
  margin: 4px 2px 0;
  font-size: 12px;
  color: var(--text-tertiary);
}

.note {
  color: var(--text-secondary);
  font-style: italic;
}

.error-text {
  padding: 8px 12px;
  border-radius: 10px;
  background: light-dark(#fdecea, #3a1d1b);
  color: var(--danger);
}

.summary {
  margin-block-end: 24px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
}
.summary summary { cursor: pointer; font-weight: 500; }

.read-only, .placeholder {
  text-align: center;
  font-size: 13px;
  color: var(--text-tertiary);
}
.placeholder { margin-top: 30vh; }

.page-meta {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-tertiary);
}

/* MARK: Composer */

.sidebar-footer {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

.composer {
  padding: 0 20px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.composer-box {
  max-width: 760px;
  margin: 0 auto;
  padding: 10px 10px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--screen);
  box-shadow: 0 2px 12px light-dark(rgb(0 0 0 / 0.06), rgb(0 0 0 / 0.3));
}
.composer-box:focus-within { border-color: light-dark(rgb(27 78 216 / 0.45), rgb(75 124 243 / 0.6)); }

#draft {
  display: block;
  width: 100%;
  min-height: 24px;
  max-height: 220px;
  padding: 2px 0;
  border: 0;
  outline: none;
  resize: none;
  background: none;
  color: var(--text);
  font: inherit;
  line-height: 1.45;
}

.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 6px;
}

#model-picker {
  min-width: 0;
  max-width: min(60%, 320px);
  padding: 4px 6px;
  border: 0;
  border-radius: 8px;
  background: var(--sidebar);
  color: var(--text-secondary);
  font: inherit;
  font-size: 13px;
}

.send-button {
  display: grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--brand);
  color: white;
  cursor: pointer;
}
.send-button svg { width: 18px; height: 18px; }
.send-button[data-mode="send"] .stop-icon,
.send-button[data-mode="stop"] .send-icon { display: none; }

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 60%;
  text-align: center;
}
.welcome-title { margin: 0; font-size: 22px; font-weight: 600; }
.welcome-detail { margin: 0; max-width: 360px; color: var(--text-secondary); }

.error-banner {
  position: fixed;
  inset: auto 16px max(16px, env(safe-area-inset-bottom)) auto;
  max-width: min(420px, calc(100vw - 32px));
  margin: 0;
  padding: 10px 14px;
  border-radius: 12px;
  background: light-dark(#fdecea, #3a1d1b);
  color: var(--danger);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.15);
}

/* MARK: Keys and models */

.settings {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--screen);
  color: var(--text);
}
.settings::backdrop { background: rgb(0 0 0 / 0.35); }
.settings h2 { margin: 0 0 6px; font-size: 18px; }
.settings-note { margin: 0 0 16px; font-size: 13px; color: var(--text-secondary); }

.key-list { display: grid; gap: 12px; }
.key-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  align-items: baseline;
}
.key-name { font-weight: 500; }
.key-link { font-size: 13px; color: var(--brand-text); }
.key-row input {
  grid-column: 1 / -1;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--sidebar);
  color: var(--text);
  font: inherit;
}
.key-error { grid-column: 1 / -1; font-size: 12px; color: var(--danger); }
.key-error:empty { display: none; }

.settings-heading { margin: 18px 0 6px; font-size: 15px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.setting-row select, .setting-block textarea {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--sidebar);
  color: var(--text);
  font: inherit;
}
.setting-block { display: grid; gap: 4px; }
.setting-block textarea { resize: vertical; min-height: 64px; }
.setting-block small { font-size: 12px; color: var(--text-tertiary); }
.model-filter {
  width: 100%;
  margin-bottom: 8px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--sidebar);
  color: var(--text);
  font: inherit;
}
.model-list { display: grid; gap: 6px; }
.model-group { border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; }
.model-group summary { cursor: pointer; font-weight: 500; padding: 2px 0; }
.model-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 3px 0; }
.model-toggle { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 14px; }
.model-toggle span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.star-button {
  flex: none;
  width: 28px;
  height: 28px;
  padding: 4px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--text-tertiary);
  cursor: pointer;
}
.star-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linejoin: round; }
.star-button[aria-pressed="true"] { color: var(--brand); }
.star-button[aria-pressed="true"] svg { fill: currentColor; }
.star-button:disabled { opacity: 0.3; cursor: default; }
.settings-problem { margin: 14px 0 0; font-size: 13px; color: var(--danger); }
.settings-problem:empty { display: none; }

.settings-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.primary-button {
  padding: 7px 16px;
  border: 0;
  border-radius: 9px;
  background: var(--brand);
  color: white;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

/* MARK: Markdown */

.markdown { overflow-wrap: anywhere; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown a { color: var(--brand-text); }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.25; margin: 1.2em 0 0.5em; }
.markdown h1 { font-size: 1.4em; }
.markdown h2 { font-size: 1.2em; }
.markdown h3 { font-size: 1.05em; }
.markdown ul, .markdown ol { padding-inline-start: 1.4em; }
.markdown li + li { margin-top: 0.2em; }
.markdown code {
  padding: 0.1em 0.35em;
  border-radius: 5px;
  background: var(--code);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
}
.markdown pre {
  padding: 12px 14px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--code);
}
.markdown pre code { padding: 0; background: none; }
.markdown blockquote {
  margin-inline: 0;
  padding-inline-start: 12px;
  border-inline-start: 3px solid var(--border);
  color: var(--text-secondary);
}
.markdown table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}
.markdown th, .markdown td {
  padding: 6px 10px;
  border: 1px solid var(--border);
  text-align: start;
}
.markdown th { background: var(--code); }
.markdown img { max-width: 100%; }

/* MARK: Gate */

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: var(--screen);
}

.gate-card {
  display: grid;
  justify-items: center;
  gap: 14px;
  max-width: 420px;
  text-align: center;
}
.gate-card .wordmark { margin: 0; font-size: 24px; }
.gate-card p { margin: 0; color: var(--text-secondary); }
.gate-card a { color: var(--brand-text); }

.gate-setup, .gate-sign-in { display: none; gap: 10px; justify-items: center; }
.gate[data-kind="setup"] .gate-setup,
.gate[data-kind="sign-in"] .gate-sign-in { display: grid; }
.gate[data-kind="error"] .gate-message { color: var(--danger); }
.gate-message:empty { display: none; }

/* MARK: Phone */

@media (width < 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-inline-end: 0; }
  .app[data-view="list"] .detail,
  .app[data-view="detail"] .sidebar { display: none; }
  .detail .back { display: grid; }
  .content { padding-inline: 14px; }
  .composer { padding-inline: 10px; }
  .bubble { max-width: 90%; }
}
