:root {
  --bg: #0a0f0d;
  --bg-rail: #0c1310;
  --bg-raise: #101815;
  --bg-code: #0b1210;
  --line: #1f2c27;
  --line-soft: #18241f;
  --line-strong: #2c3c34;
  --text: #e8f0eb;
  --muted: #9aada3;
  --faint: #71857b;
  --accent: #e8b264;
  --accent-soft: rgba(232, 178, 100, 0.14);
  --accent-ink: #1a1206;
  --mint: #7fd1b0;
  --danger: #f09283;
  --sans: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --mono: "Cascadia Code", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  --rail-w: 280px;
  --measure: 760px;
  --r: 14px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background:
    radial-gradient(1100px 520px at 12% -12%, rgba(232, 178, 100, 0.07), transparent 62%),
    var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

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

button {
  cursor: pointer;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 16px;
  top: -60px;
  z-index: 50;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  transition: top 160ms ease;
}

.skip-link:focus {
  top: 12px;
  text-decoration: none;
}

/* ---------- layout ---------- */

.app {
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

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

.scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.scroll::-webkit-scrollbar {
  width: 10px;
}

.scroll::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border: 3px solid var(--bg);
  border-radius: 999px;
}

/* ---------- sidebar ---------- */

.rail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 14px 12px 12px;
  background: var(--bg-rail);
  border-right: 1px solid var(--line);
}

.rail-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 14px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--text);
  text-decoration: none;
}

.brand:hover {
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
}

.brand-mark svg {
  width: 17px;
  height: 17px;
}

.brand-name {
  font-family: var(--serif);
  font-size: 19px;
  letter-spacing: 0.01em;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  transition: background 160ms ease, color 160ms ease, border-color 160ms ease;
}

.icon-button svg {
  width: 19px;
  height: 19px;
}

.icon-button:hover {
  background: var(--bg-raise);
  border-color: var(--line);
  color: var(--text);
}

.rail-close {
  display: none;
}

.new-chat {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 550;
  transition: background 160ms ease, border-color 160ms ease;
}

.new-chat svg {
  width: 17px;
  height: 17px;
  color: var(--muted);
}

.new-chat:hover {
  background: var(--bg-raise);
  border-color: var(--accent);
}

.history {
  flex: 1;
  min-height: 0;
  margin: 14px -6px 0;
  padding: 0 6px 8px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.history-group + .history-group {
  margin-top: 14px;
}

.history-title {
  margin: 0 0 5px;
  padding: 0 8px;
  color: var(--faint);
  font-size: 12px;
  font-weight: 500;
}

.history-row {
  display: flex;
  align-items: center;
  gap: 2px;
}

.history-item {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 0 6px 0 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  transition: background 140ms ease, color 140ms ease;
}

.history-item:hover {
  background: var(--bg-raise);
  color: var(--text);
}

.history-item[aria-current="true"] {
  background: var(--bg-raise);
  color: var(--text);
}

.history-item[aria-current="true"]::before {
  content: "";
  position: absolute;
  left: 0;
  top: 9px;
  bottom: 9px;
  width: 2px;
  border-radius: 2px;
  background: var(--accent);
}

.history-item-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13.5px;
}

.history-delete {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--faint);
  opacity: 0;
  transition: opacity 140ms ease, background 140ms ease, color 140ms ease;
}

.history-delete svg {
  width: 14px;
  height: 14px;
}

.history-row:hover .history-delete,
.history-delete:focus-visible {
  opacity: 1;
}

.history-delete:hover {
  background: var(--line);
  color: var(--danger);
}

.history-empty {
  margin: 0;
  padding: 4px 8px;
  color: var(--faint);
  font-size: 13px;
  line-height: 1.5;
}

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

.model-field {
  display: block;
  padding: 0 4px;
}

.model-field > span {
  display: block;
  margin-bottom: 5px;
  color: var(--faint);
  font-size: 12px;
}

.model-field select {
  width: 100%;
  height: 36px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-raise);
  color: var(--text);
  font-size: 13px;
}

.rail-links {
  display: flex;
  gap: 14px;
  padding: 10px 4px 0;
  font-size: 12.5px;
  color: var(--faint);
}

.rail-links a {
  color: var(--faint);
}

.rail-links a:hover {
  color: var(--text);
  text-decoration: none;
}

/* ---------- topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
}

.rail-open {
  display: none;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--mint);
  transition: background 200ms ease;
}

.status.is-busy .status-dot {
  background: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}

.topbar-model {
  margin-left: auto;
  overflow: hidden;
  color: var(--faint);
  font-size: 12.5px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- welcome ---------- */

.welcome {
  max-width: var(--measure);
  margin: 0 auto;
  padding: clamp(36px, 9vh, 88px) 24px 24px;
  animation: rise 260ms ease-out both;
}

.welcome h1 {
  margin: 0 0 10px;
  font-family: var(--serif);
  font-size: clamp(30px, 4vw, 44px);
  font-weight: 400;
  line-height: 1.08;
  letter-spacing: -0.015em;
}

.welcome p {
  max-width: 48ch;
  margin: 0 0 28px;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.65;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.suggestion {
  min-height: 56px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  font-size: 13.5px;
  line-height: 1.45;
  transition: background 160ms ease, border-color 160ms ease, color 160ms ease, transform 160ms ease;
}

.suggestion:hover {
  background: var(--bg-raise);
  border-color: var(--line-strong);
  color: var(--text);
  transform: translateY(-1px);
}

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

.thread {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 8px 24px 40px;
}

.msg {
  padding: 20px 0;
  border-top: 1px solid var(--line-soft);
  animation: rise 180ms ease-out both;
}

.msg:first-child {
  border-top: 0;
}

.msg-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 24px;
  margin-bottom: 7px;
}

.msg-who {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.msg-who::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.msg-user .msg-who::before {
  background: var(--line-strong);
}

.msg-actions {
  display: flex;
  gap: 4px;
  margin-left: auto;
  opacity: 0;
  transition: opacity 160ms ease;
}

.msg:hover .msg-actions,
.msg:focus-within .msg-actions {
  opacity: 1;
}

.msg-action {
  min-height: 26px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--faint);
  font-size: 12px;
  transition: color 140ms ease, background 140ms ease, border-color 140ms ease;
}

.msg-action:hover {
  background: var(--bg-raise);
  border-color: var(--line);
  color: var(--text);
}

.msg-body {
  max-width: 68ch;
  color: var(--text);
  font-size: 15.5px;
  line-height: 1.72;
  overflow-wrap: anywhere;
}

.msg-user .msg-body {
  display: inline-block;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-raise);
  white-space: pre-wrap;
}

.msg-body > :first-child {
  margin-top: 0;
}

.msg-body > :last-child {
  margin-bottom: 0;
}

.msg-body p {
  margin: 0 0 12px;
}

.msg-body h2,
.msg-body h3 {
  margin: 22px 0 8px;
  font-size: 17px;
  line-height: 1.35;
}

.msg-body h2 {
  font-family: var(--serif);
  font-size: 21px;
  font-weight: 400;
}

.msg-body ul,
.msg-body ol {
  margin: 0 0 12px;
  padding-left: 22px;
}

.msg-body li {
  margin: 3px 0;
}

.msg-body a {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-body code {
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--bg-code);
  font-family: var(--mono);
  font-size: 0.88em;
}

.msg-body blockquote {
  margin: 12px 0;
  padding: 2px 0 2px 14px;
  border-left: 2px solid var(--line-strong);
  color: var(--muted);
}

.code {
  margin: 14px 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-code);
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 8px 6px 12px;
  border-bottom: 1px solid var(--line);
  color: var(--faint);
  font-family: var(--mono);
  font-size: 11.5px;
}

.code-copy {
  min-height: 26px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 11.5px;
}

.code-copy:hover {
  border-color: var(--line-strong);
  color: var(--text);
}

.code pre {
  margin: 0;
  padding: 14px;
  overflow-x: auto;
}

.code pre code {
  padding: 0;
  border: 0;
  background: none;
  font-size: 12.5px;
  line-height: 1.6;
}

.reasoning {
  margin: 0 0 12px;
  border: 1px solid var(--line-soft);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.012);
}

.reasoning summary {
  padding: 8px 12px;
  color: var(--faint);
  font-size: 12.5px;
  cursor: pointer;
  user-select: none;
}

.reasoning-body {
  max-height: 260px;
  margin: 0;
  padding: 0 12px 12px;
  overflow-y: auto;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.thinking {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--faint);
  font-size: 13.5px;
}

.thinking-dots {
  display: inline-flex;
  gap: 3px;
}

.thinking-dots i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  animation: blink 1.2s ease-in-out infinite;
}

.thinking-dots i:nth-child(2) {
  animation-delay: 0.16s;
}

.thinking-dots i:nth-child(3) {
  animation-delay: 0.32s;
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  margin-left: 2px;
  border-radius: 1px;
  background: var(--accent);
  vertical-align: -0.15em;
  animation: blink 1s steps(1) infinite;
}

.msg-error {
  color: var(--danger);
  font-size: 13.5px;
}

.msg-error-card .msg-body {
  max-width: 68ch;
  padding: 12px 14px;
  border: 1px solid rgba(240, 146, 131, 0.35);
  border-radius: 11px;
  background: rgba(240, 146, 131, 0.08);
}

.msg-error-card .msg-body p {
  margin: 0 0 10px;
}

.error-retry {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  border: 1px solid rgba(240, 146, 131, 0.45);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 12.5px;
}

.error-retry:hover {
  border-color: var(--danger);
  background: rgba(240, 146, 131, 0.14);
}

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

.composer {
  padding: 10px 24px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: linear-gradient(to bottom, rgba(10, 15, 13, 0.6), var(--bg) 40%);
}

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  background: var(--bg-raise);
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.composer-inner:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#input {
  flex: 1;
  min-height: 36px;
  max-height: 200px;
  padding: 7px 0;
  border: 0;
  background: transparent;
  color: var(--text);
  line-height: 1.55;
  outline: none;
  resize: none;
  scrollbar-width: thin;
}

#input::placeholder {
  color: var(--faint);
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.send {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  transition: opacity 160ms ease, transform 160ms ease;
}

.send svg {
  width: 18px;
  height: 18px;
}

.send:hover:not(:disabled) {
  transform: translateY(-1px);
}

.send:disabled {
  opacity: 0.35;
  cursor: default;
}

.stop {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.stop:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.fineprint {
  max-width: var(--measure);
  margin: 8px auto 0;
  color: var(--faint);
  font-size: 11.5px;
  text-align: center;
}

.fineprint a {
  color: var(--faint);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 60;
  max-width: min(420px, calc(100vw - 32px));
  padding: 10px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--bg-raise);
  color: var(--text);
  font-size: 13px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translate(-50%, 8px);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
  }

  .rail {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(86vw, 320px);
    transform: translateX(-102%);
    transition: transform 220ms ease;
    box-shadow: 24px 0 48px rgba(0, 0, 0, 0.35);
  }

  .rail.is-open {
    transform: translateX(0);
  }

  .rail-close,
  .rail-open {
    display: grid;
  }

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

  .scrim[hidden] {
    display: none;
  }

  .topbar {
    padding: 0 10px;
  }

  .topbar-model {
    font-size: 12px;
  }

  .welcome {
    padding-top: 40px;
    padding-left: 16px;
    padding-right: 16px;
  }

  .welcome h1 {
    font-size: clamp(28px, 8vw, 36px);
  }

  .suggestions {
    grid-template-columns: minmax(0, 1fr);
  }

  .thread {
    padding: 4px 16px 28px;
  }

  .msg {
    padding: 16px 0;
  }

  .msg-body {
    font-size: 15px;
  }

  .composer {
    padding-left: 12px;
    padding-right: 12px;
  }

  .msg-actions {
    opacity: 1;
  }
}

@media (hover: none) {
  .msg-actions {
    opacity: 1;
  }

  .history-delete {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.45;
    transform: scale(0.82);
  }
}

@keyframes blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.2;
  }
}
