:root {
  color-scheme: dark;

  --bg: #14120f;
  --bg-raised: #1c1917;
  --bg-sunken: #100e0c;
  --line: #2c2723;
  --line-soft: #221e1b;

  --text: #ece7e0;
  --text-dim: #a29a90;
  --text-faint: #6f6862;

  --accent: #e0a33f;
  --accent-soft: rgba(224, 163, 63, 0.14);
  --accent-line: rgba(224, 163, 63, 0.35);
  --online: #6fbf73;
  --offline: #b8564f;

  --radius: 14px;
  --radius-sm: 9px;
  --topbar-h: 56px;

  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 400 16px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

[hidden] { display: none !important; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- login ---------- */

.login {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 24px;
}

.login__card {
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 32px 26px 26px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 20px;
}

.login__mark {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}

.login__mark .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.35;
}
.login__mark .dot:first-child { opacity: 1; }
.login__mark .dot:nth-child(2) { opacity: 0.65; }

.login__title {
  margin: 0;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.login__hint {
  margin: -8px 0 6px;
  color: var(--text-dim);
  font-size: 14px;
}

.login__error {
  margin: 0;
  color: var(--offline);
  font-size: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field__label {
  font-size: 13px;
  color: var(--text-dim);
}

.field input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  outline: none;
}

.field input:focus-visible {
  border-color: var(--accent-line);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---------- buttons ---------- */

.btn {
  padding: 12px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn--primary {
  background: var(--accent);
  color: #241a09;
  font-weight: 600;
}
.btn--primary:hover { filter: brightness(1.06); }
.btn--primary:disabled { opacity: 0.6; cursor: default; }

.btn--ghost {
  border-color: var(--line);
  color: var(--text-dim);
  font-size: 14px;
}
.btn--ghost:hover {
  background: var(--bg-sunken);
  color: var(--text);
}

.btn--block { width: 100%; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-raised); color: var(--text); }

/* ---------- shell ---------- */

.app {
  display: flex;
  height: 100dvh;
}

.main {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--topbar-h);
  padding: 0 8px;
  padding-top: env(safe-area-inset-top);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line-soft);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}

.topbar__center {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.model-select {
  max-width: 100%;
  padding: 2px 22px 2px 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-align: center;
  text-align-last: center;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-faint) 50%),
                    linear-gradient(135deg, var(--text-faint) 50%, transparent 50%);
  background-position: right 10px center, right 5px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}
.model-select:hover { background-color: var(--bg-raised); }
.model-select option { background: var(--bg-raised); }

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-size: 11.5px;
  color: var(--text-faint);
  font-family: var(--mono);
}

.status__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
}

.status.is-online .status__dot {
  background: var(--online);
  box-shadow: 0 0 0 0 rgba(111, 191, 115, 0.5);
  animation: pulse 2.4s ease-out infinite;
}
.status.is-offline .status__dot { background: var(--offline); }

@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(111, 191, 115, 0.45); }
  70%  { box-shadow: 0 0 0 5px rgba(111, 191, 115, 0); }
  100% { box-shadow: 0 0 0 0 rgba(111, 191, 115, 0); }
}

@media (prefers-reduced-motion: reduce) {
  .status.is-online .status__dot { animation: none; }
}

/* ---------- drawer ---------- */

.drawer {
  position: fixed;
  z-index: 30;
  inset: 0 auto 0 0;
  width: 272px;
  display: flex;
  flex-direction: column;
  padding: 12px 10px calc(12px + env(safe-area-inset-bottom));
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--bg-sunken);
  border-right: 1px solid var(--line-soft);
  transform: translateX(-100%);
  transition: transform 0.22s ease;
}
.drawer.is-open { transform: none; }

.drawer__list {
  flex: 1;
  overflow-y: auto;
  margin: 10px 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.chat-item:hover { background: var(--bg-raised); color: var(--text); }
.chat-item.is-active {
  background: var(--accent-soft);
  color: var(--text);
}

.chat-item__title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item__del {
  flex: none;
  border: none;
  background: transparent;
  color: var(--text-faint);
  padding: 2px 4px;
  border-radius: 5px;
  cursor: pointer;
  opacity: 0;
}
.chat-item:hover .chat-item__del,
.chat-item.is-active .chat-item__del { opacity: 1; }
.chat-item__del:hover { color: var(--offline); }
.chat-item__del svg { width: 15px; height: 15px; }

.scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.55);
}

@media (min-width: 900px) {
  .drawer { position: static; transform: none; }
  .scrim, #menu { display: none !important; }
}

/* ---------- settings sheet ---------- */

.sheet {
  position: absolute;
  z-index: 15;
  top: calc(var(--topbar-h) + env(safe-area-inset-top));
  right: 8px;
  left: 8px;
  max-width: 380px;
  margin-left: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
}

.setting {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 4px 12px;
}

.setting__name { font-size: 14.5px; }

.setting__hint {
  grid-column: 1 / -1;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
}

.setting select {
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-sunken);
  font-size: 14px;
  cursor: pointer;
}
.setting select:disabled { opacity: 0.45; cursor: default; }
.setting select option { background: var(--bg-raised); }

.setting input[type='text'] {
  grid-column: 1 / -1;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-sunken);
  font-size: 14px;
  outline: none;
}
.setting input[type='text']:focus-visible {
  border-color: var(--accent-line);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.setting input[type='text']::placeholder { color: var(--text-faint); }

.setting .btn { padding: 7px 14px; }
.setting .btn:disabled { opacity: 0.45; cursor: default; }

/* ---------- thread ---------- */

.thread {
  flex: 1;
  overflow-y: auto;
  overflow-wrap: anywhere;
  scrollbar-gutter: stable;
}

.thread > * {
  width: min(760px, 100%);
  margin-inline: auto;
  padding-inline: 16px;
}

.empty {
  padding-top: 22vh;
  color: var(--text-dim);
  text-align: center;
}
.empty h2 {
  margin: 0 0 8px;
  font-size: 21px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.02em;
}
.empty p { margin: 0; font-size: 14.5px; }

.msg { padding-block: 14px; }

.msg--user {
  display: flex;
  justify-content: flex-end;
}

.msg--user .msg__body {
  max-width: 85%;
  padding: 10px 14px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  white-space: pre-wrap;
}

.msg--assistant + .msg--user { border-top: 1px solid var(--line-soft); margin-top: 4px; }

.msg__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
}

.msg__meta::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line-soft);
}

.msg--error .msg__body {
  color: var(--offline);
  font-size: 14.5px;
}

.think {
  margin: 0 0 10px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  background: var(--bg-sunken);
}
.think summary {
  padding: 7px 11px;
  font-size: 12.5px;
  color: var(--text-faint);
  cursor: pointer;
  list-style: none;
}
.think summary::-webkit-details-marker { display: none; }
.think summary::before { content: "▸ "; }
.think[open] summary::before { content: "▾ "; }
.think__body {
  padding: 0 11px 9px;
  font-size: 13.5px;
  color: var(--text-dim);
  white-space: pre-wrap;
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--accent);
  animation: blink 1.1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.85em; }
.md h1, .md h2, .md h3 {
  margin: 1.2em 0 0.5em;
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.3;
}
.md h1 { font-size: 1.32em; }
.md h2 { font-size: 1.18em; }
.md h3 { font-size: 1.05em; }
.md ul, .md ol { margin: 0 0 0.85em; padding-left: 1.35em; }
.md li { margin-bottom: 0.25em; }
.md blockquote {
  margin: 0 0 0.85em;
  padding-left: 0.9em;
  border-left: 2px solid var(--accent-line);
  color: var(--text-dim);
}
.md hr { border: none; border-top: 1px solid var(--line); margin: 1.3em 0; }
.md a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
.md code {
  padding: 0.14em 0.38em;
  background: var(--bg-sunken);
  border: 1px solid var(--line-soft);
  border-radius: 5px;
  font-family: var(--mono);
  font-size: 0.87em;
}
.md pre {
  position: relative;
  margin: 0 0 0.85em;
  padding: 12px 13px;
  background: var(--bg-sunken);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  overflow-x: auto;
}
.md pre code {
  padding: 0;
  border: none;
  background: none;
  font-size: 13px;
  line-height: 1.5;
}
.md pre .copy {
  position: absolute;
  top: 7px;
  right: 7px;
  padding: 3px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-raised);
  color: var(--text-faint);
  font-family: var(--sans);
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.md pre:hover .copy, .md pre .copy:focus-visible { opacity: 1; }
.md table {
  width: 100%;
  margin: 0 0 0.85em;
  border-collapse: collapse;
  font-size: 0.92em;
}
.md th, .md td {
  padding: 6px 9px;
  border: 1px solid var(--line);
  text-align: left;
}
.md th { background: var(--bg-sunken); }

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

.composer {
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line-soft);
  background: var(--bg);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: min(760px, 100%);
  margin: 0 auto 8px;
}
.chips:empty { display: none; }

.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 5px 8px 5px 11px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--text-dim);
}

.chip__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chip__size {
  flex: none;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
}

.chip__del {
  flex: none;
  padding: 0 2px;
  border: none;
  background: none;
  color: var(--text-faint);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}
.chip__del:hover { color: var(--offline); }

.attach {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  margin-bottom: 2px;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
}
.attach:hover { color: var(--text); background: var(--bg-sunken); }
.attach svg { width: 18px; height: 18px; }

.composer__box {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: min(760px, 100%);
  margin-inline: auto;
  padding: 6px 6px 6px 6px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 22px;
  transition: border-color 0.15s;
}
.composer__box:focus-within { border-color: var(--accent-line); }

.composer__box textarea {
  flex: 1;
  max-height: 45dvh;
  padding: 8px 0;
  border: none;
  background: none;
  outline: none;
  resize: none;
  line-height: 1.45;
}
.composer__box textarea::placeholder { color: var(--text-faint); }

.send {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #241a09;
  cursor: pointer;
}
.send:disabled { background: var(--line); color: var(--text-faint); cursor: default; }
.send svg { width: 19px; height: 19px; stroke-width: 2.1; }
.send--stop { background: var(--text); color: var(--bg); }
.send--stop svg { fill: currentColor; stroke: none; }

.composer__note {
  width: min(760px, 100%);
  margin: 6px auto 0;
  min-height: 15px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  text-align: center;
}
.composer__note.is-warning { color: var(--accent); }

.msg__meta .ctx { color: var(--text-faint); }
.msg__meta .ctx.is-tight { color: var(--accent); }

.speak-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  margin-left: 8px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
}
.speak-btn:hover { color: var(--accent); background: var(--bg-raised); }
.speak-btn.is-busy { color: var(--accent); }
.speak-btn svg { width: 15px; height: 15px; }

/* ---------- shared note ---------- */

.sheet--note {
  /* A pasted document needs room, but the panel still hangs off the topbar. */
  max-width: 720px;
  gap: 12px;
  max-height: min(70vh, 560px);
}

.note__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.note__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
}

.note__meta {
  margin: 0;
  min-width: 0;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  text-align: right;
}

.note__text {
  flex: 1;
  min-height: 180px;
  resize: vertical;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-sunken);
  color: inherit;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  outline: none;
}
.note__text:focus-visible {
  border-color: var(--accent-line);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.note__text::placeholder { color: var(--text-faint); }

.note__actions {
  display: flex;
  gap: 8px;
}
.note__actions .btn { flex: 1; padding: 8px 12px; }
.note__actions .btn:disabled { opacity: 0.45; cursor: default; }

.note__note {
  margin: 0;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
}
