:root {
  --bg: #fbfaf8;
  --surface: #ffffff;
  --sunken: #f2f0ec;
  --line: #e2ded6;
  --text: #1d1b18;
  --muted: #6a655d;
  --accent: #0f5c6e;
  --accent-text: #ffffff;
  --good: #1f6b3a;
  --good-bg: #e3f1e7;
  --warn: #8a5300;
  --warn-bg: #fbeed3;
  --bad: #9b2226;
  --bad-bg: #fbe6e4;
  /* Colours in code, on --sunken. Each is at least 4.5 times as light or dark as it. */
  --code-comment: #6a655d;
  --code-string: #2c6a1f;
  --code-number: #9a4400;
  --code-keyword: #8a2a6c;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --surface: #1f1e1c;
    --sunken: #262422;
    --line: #37342f;
    --text: #ece8e1;
    --muted: #a39d92;
    --accent: #6cc3d6;
    --accent-text: #0c1c20;
    --good: #8fd3a5;
    --good-bg: #1c3324;
    --warn: #f0c36b;
    --warn-bg: #3a2c10;
    --bad: #f2a29c;
    --bad-bg: #3d1b1a;
    --code-comment: #a39d92;
    --code-string: #a3d38c;
    --code-number: #f0b26a;
    --code-keyword: #e49ad0;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.column { width: 100%; max-width: 780px; margin: 0 auto; padding: 0 16px; }

/* ---- top bar ---- */

.bar {
  display: flex;
  flex-wrap: wrap; /* on a phone the name has the first line and the rest the second */
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: 0.01em;
  white-space: nowrap; /* the name is never broken in two */
}

/* A caliper's two jaws. */
.mark {
  width: 18px;
  height: 18px;
  border: 2px solid var(--accent);
  border-top: 0;
  border-radius: 0 0 3px 3px;
  position: relative;
}
.mark::after {
  content: "";
  position: absolute;
  left: -5px;
  right: -5px;
  top: 5px;
  border-top: 2px solid var(--accent);
}

.facts {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.chip {
  font-size: 12.5px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--sunken);
  color: var(--muted);
  white-space: nowrap;
}
.chip.good { background: var(--good-bg); color: var(--good); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }

/* ---- the frame: the list of chats on the left, the conversation beside it ---- */

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

.panel {
  flex: none;
  width: 272px;
  display: flex;
  flex-direction: column;
  padding: 14px 10px 10px;
  border-right: 1px solid var(--line);
  background: var(--surface);
  overflow-y: auto;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 30px;
  margin: 0 8px 6px;
}
.panel-head h2 {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.panel .note { margin: 8px 8px 0; }
.panel .about { margin-top: auto; padding-top: 14px; } /* at the foot of the panel */

.chats { list-style: none; margin: 0; padding: 0; }
.chats li { padding: 7px 8px; border-radius: 8px; }
.chats li + li { margin-top: 2px; }
.chats li.current { background: var(--sunken); }
/* The title: two lines at most, then an ellipsis. */
.chats .name {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text);
  font-size: 14.5px;
  line-height: 1.35;
  text-align: left;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.chats .name:hover { color: var(--accent); }
.chats li.current .name { font-weight: 650; }
/* Under the title: when the chat was last added to, and what can be done with it. */
.chats .row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 10px;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12.5px;
}
.chats .row button {
  padding: 2px 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 12.5px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.chats .row button:hover { color: var(--accent); }
.chats .row button:disabled { opacity: 0.45; cursor: default; }

/* Behind the list of chats when it lies over the page, on a small screen. */
.shade { position: fixed; z-index: 19; inset: 0; background: rgb(0 0 0 / 0.4); }

/* ---- conversation ---- */

main { flex: 1; overflow-y: auto; padding: 24px 0 8px; }

.welcome { padding: 8vh 0 0; }
.welcome h2 { margin: 0 0 6px; font-size: 24px; font-weight: 650; text-wrap: balance; }
.welcome p { margin: 0 0 20px; color: var(--muted); max-width: 60ch; }

.examples { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.example {
  text-align: left;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.example:hover { border-color: var(--accent); }

.turn { margin: 0 0 22px; }

.question {
  margin-left: auto;
  width: fit-content;
  max-width: 85%;
  padding: 9px 14px;
  border-radius: var(--radius);
  margin-bottom: 14px;
  background: var(--sunken);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.answer { overflow-wrap: anywhere; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer p, .answer ul, .answer ol, .answer .code, .answer blockquote, .answer .table { margin: 0 0 12px; }
.answer h3, .answer h4, .answer h5, .answer h6 { margin: 20px 0 8px; line-height: 1.3; }
.answer h3 { font-size: 19px; }
.answer h4 { font-size: 17px; }
.answer h5, .answer h6 { font-size: 16px; }
.answer ul, .answer ol { padding-left: 24px; }
.answer li { margin: 2px 0; }
.answer li > ul, .answer li > ol { margin: 2px 0 4px; }
.answer li > p { margin: 6px 0 0; }
.answer li > .code { margin: 8px 0 10px; }
.answer a { color: var(--accent); }
.answer hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.answer blockquote { padding: 2px 0 2px 14px; border-left: 3px solid var(--line); color: var(--muted); }
/* Code in a sentence. A long piece breaks anywhere rather than make the page wider. */
.answer code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--sunken);
  padding: 1px 5px;
  border-radius: 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* A code block: a header line (language, Copy), then the code. The code keeps every
   space and never wraps; a long line scrolls sideways inside the block, never the page. */
.answer .code {
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--sunken);
  overflow: hidden; /* keeps the corners round */
}
.code-head {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 4px 6px 4px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.3;
}
.code-lang { font-weight: 600; letter-spacing: 0.02em; overflow-wrap: anywhere; }
button.code-copy {
  margin-left: auto;
  min-width: 68px;
  padding: 2px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 12.5px;
  line-height: 20px;
  white-space: nowrap;
}
button.code-copy:hover { border-color: var(--accent); }
.answer .code pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  white-space: pre;
  tab-size: 4;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.55;
}
.answer .code pre:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.answer .code pre code {
  font-size: inherit;
  background: none;
  padding: 0;
  border-radius: 0;
  white-space: inherit;
  overflow-wrap: normal;
}
.code-comment { color: var(--code-comment); font-style: italic; }
.code-string { color: var(--code-string); }
.code-number { color: var(--code-number); }
.code-keyword { color: var(--code-keyword); }

/* Read out by screen readers, not shown. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.table { overflow-x: auto; }
.answer table { border-collapse: collapse; font-size: 15px; }
.answer th, .answer td { padding: 6px 12px; border: 1px solid var(--line); text-align: left; vertical-align: top; }
.answer th { background: var(--sunken); font-weight: 600; }

.cites { white-space: nowrap; }
.cite {
  display: inline-block;
  min-width: 18px;
  margin: 0 1px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--sunken);
  color: var(--accent);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  line-height: 18px;
  text-align: center;
  text-decoration: none;
  vertical-align: 1px;
}
.cite:hover { background: var(--accent); color: var(--accent-text); }

.status { margin: 8px 0 0; color: var(--muted); font-size: 14px; min-height: 22px; }
.status.working::after { content: " …"; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }
@media (prefers-reduced-motion: reduce) { .status.working::after { animation: none; } }

.error {
  margin: 8px 0 0;
  padding: 9px 14px;
  border-radius: var(--radius);
  background: var(--bad-bg);
  color: var(--bad);
}

.sources { margin: 12px 0 0; padding: 0; list-style: none; font-size: 14px; }
.sources summary { cursor: pointer; padding: 3px 0; color: var(--muted); }
.sources summary strong { color: var(--text); font-weight: 600; }
.sources .passage {
  margin: 4px 0 8px 22px;
  padding: 8px 12px;
  border-left: 3px solid var(--line);
  background: var(--surface);
  white-space: pre-wrap;
}
.sources li:target summary strong, .sources li.lit summary strong { color: var(--accent); }

/* ---- composer ---- */

.composer { padding: 10px 0 12px; border-top: 1px solid var(--line); background: var(--surface); }

textarea {
  display: block;
  width: 100%;
  max-height: 40vh;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  resize: none;
}
textarea:focus-visible, input:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.controls { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }

select, input {
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

button { font: inherit; cursor: pointer; }
button.primary {
  padding: 7px 18px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.primary:disabled { opacity: 0.45; cursor: default; }
button.quiet {
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}
button.quiet:hover { border-color: var(--accent); }
button.again { padding: 2px 9px; font-size: 13px; }
button.plain {
  padding: 5px 2px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
button.quiet:disabled { opacity: 0.45; cursor: default; }

.note { margin-top: 8px; margin-bottom: 0; color: var(--muted); font-size: 12.5px; }

/* What happened to a document that was just added. One line per document. */
.upload { font-size: 14px; white-space: pre-line; overflow-wrap: anywhere; }
.upload.working::after { content: " …"; animation: pulse 1.2s ease-in-out infinite; }
.upload.good { color: var(--good); }
.upload.bad { color: var(--bad); }
@media (prefers-reduced-motion: reduce) { .upload.working::after { animation: none; } }

/* ---- API key dialog ---- */

dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h2 { margin: 0 0 6px; font-size: 18px; }
dialog p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
dialog input { width: 100%; font-size: 16px; }
dialog.wide { width: min(620px, calc(100vw - 32px)); }

/* ---- the list of added documents ---- */

.documents { list-style: none; margin: 0 0 12px; padding: 0; max-height: 45vh; overflow-y: auto; }
.documents li {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 12px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.documents .name { flex: 1; min-width: 12ch; overflow-wrap: anywhere; }
.documents .about { color: var(--muted); font-size: 13px; white-space: nowrap; }

/* Above a chat that someone else started and passed on. */
.shared { margin: 0 0 16px; color: var(--muted); font-size: 14px; }

/* ---- screens of different widths ---- */

/* Wide: the list of chats always stands on the left, so nothing opens or closes it. */
@media (min-width: 900px) {
  #chats-open, #chats-close, .shade { display: none !important; }
}

/* Narrow: no room beside the conversation. "Chats" slides the list in over the page. */
@media (max-width: 899px) {
  .panel {
    position: fixed;
    z-index: 20;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(320px, 86vw);
    box-shadow: 0 0 28px rgb(0 0 0 / 0.28);
    visibility: hidden; /* so that nothing in it can be reached while it is away */
    transform: translateX(-104%);
    transition: transform 0.18s ease, visibility 0s linear 0.18s;
  }
  body.chats-shown .panel {
    visibility: visible;
    transform: none;
    transition: transform 0.18s ease;
  }
}
@media (prefers-reduced-motion: reduce) {
  .panel, body.chats-shown .panel { transition: none; }
}

@media (max-width: 560px) {
  .welcome { padding-top: 3vh; }
  /* The top bar in two lines: the name, "Chats" and "New chat", then the badge. */
  .bar { gap: 8px; }
  .facts { display: contents; }
  .brand { order: 1; margin-right: auto; }
  .brand h1 { font-size: 16px; }
  #chats-open, #new-chat { order: 2; padding: 6px 10px; }
  .chip { order: 3; white-space: normal; }
  /* Under the question box: no label breaks in two. What has no room moves to a second line. */
  .composer .controls { flex-wrap: wrap; }
  .composer .controls button { white-space: nowrap; }
  .composer .controls .spacer { display: none; }
  #send { margin-left: auto; }
}

[hidden] { display: none !important; }
