:root,
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --surface-muted: #edf1f3;
  --text: #172026;
  --muted: #66737d;
  --faint: #8a969e;
  --line: #dce3e8;
  --line-strong: #c8d1d8;
  --accent: #16746f;
  --accent-strong: #0f5f5b;
  --accent-soft: #dcefed;
  --user: #16746f;
  --user-text: #ffffff;
  --assistant: #ffffff;
  --danger: #b42318;
  --danger-soft: #fde8e5;
  --shadow: 0 18px 48px rgba(16, 24, 32, 0.18);
  --radius: 8px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101417;
  --surface: #171d21;
  --surface-raised: #20282d;
  --surface-muted: #242d33;
  --text: #eef3f5;
  --muted: #a1adb5;
  --faint: #74828a;
  --line: #2f3a41;
  --line-strong: #43515a;
  --accent: #56b8ae;
  --accent-strong: #82d4cc;
  --accent-soft: #183632;
  --user: #2f8d84;
  --user-text: #ffffff;
  --assistant: #20282d;
  --danger: #ff8b7f;
  --danger-soft: #3a211f;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.42);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    color-scheme: dark;
    --bg: #101417;
    --surface: #171d21;
    --surface-raised: #20282d;
    --surface-muted: #242d33;
    --text: #eef3f5;
    --muted: #a1adb5;
    --faint: #74828a;
    --line: #2f3a41;
    --line-strong: #43515a;
    --accent: #56b8ae;
    --accent-strong: #82d4cc;
    --accent-soft: #183632;
    --user: #2f8d84;
    --user-text: #ffffff;
    --assistant: #20282d;
    --danger: #ff8b7f;
    --danger-soft: #3a211f;
    --shadow: 0 18px 48px rgba(0, 0, 0, 0.42);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  background:
    radial-gradient(circle at top left, rgba(22, 116, 111, 0.12), transparent 34rem),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  margin: 0;
  overflow: hidden;
}

button,
textarea {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

button:disabled {
  cursor: default;
}

h1,
h2,
p {
  margin: 0;
}

.app-shell {
  background: var(--surface);
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  max-width: 820px;
  overflow: hidden;
  position: relative;
}

.main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
}

.topbar {
  align-items: center;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 10px;
  grid-template-columns: auto minmax(0, 1fr) auto;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  position: relative;
  z-index: 2;
}

.title-block {
  min-width: 0;
}

h1 {
  font-size: 20px;
  font-weight: 720;
  letter-spacing: 0;
}

h2 {
  font-size: 16px;
  font-weight: 720;
  letter-spacing: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar p,
.drawer-head p {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button,
.new-chat-button,
.send-button,
.action-button,
.theme-button,
.chat-delete {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: inline-flex;
  justify-content: center;
  min-height: 38px;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease, opacity 120ms ease;
}

.icon-button {
  background: var(--surface-muted);
  color: var(--text);
  font-size: 13px;
  font-weight: 680;
  min-width: 42px;
  padding: 0 10px;
}

.new-chat-button {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 760;
  min-width: 92px;
  padding: 0 13px;
}

.icon-button:hover {
  border-color: var(--line-strong);
}

.close-button {
  background: transparent;
  color: var(--muted);
  min-width: 58px;
}

.compact-new {
  font-size: 13px;
  min-width: 48px;
  padding: 0 10px;
}

.messages {
  background: var(--bg);
  background:
    linear-gradient(to bottom, color-mix(in srgb, var(--surface-muted) 55%, transparent), transparent 140px),
    var(--bg);
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 18px 12px 22px;
  scroll-behavior: smooth;
}

.empty {
  align-self: center;
  color: var(--muted);
  display: grid;
  gap: 8px;
  justify-items: center;
  margin-top: 18vh;
  max-width: 280px;
  text-align: center;
}

.empty-title {
  color: var(--text);
  font-size: 17px;
  font-weight: 720;
}

.empty-detail {
  font-size: 13px;
  line-height: 1.45;
}

.message {
  display: grid;
  gap: 6px;
  max-width: 92%;
}

.message.user {
  align-self: flex-end;
  justify-items: end;
}

.message.assistant {
  align-self: flex-start;
  justify-items: start;
}

.message-role {
  color: var(--faint);
  font-size: 11px;
  font-weight: 680;
  padding: 0 3px;
  text-transform: uppercase;
}

.message-body {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.52;
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 12px 13px;
  white-space: normal;
}

.message.user .message-body {
  background: var(--user);
  border-color: var(--user);
  border-color: color-mix(in srgb, var(--user) 80%, var(--line));
  color: var(--user-text);
}

.message.assistant .message-body {
  background: var(--assistant);
}

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

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

.message-body p,
.message-body ul,
.message-body ol,
.message-body blockquote,
.message-body pre,
.message-body table,
.message-body .katex-display {
  margin: 0 0 11px;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4 {
  font-size: 1em;
  font-weight: 760;
  line-height: 1.3;
  margin: 14px 0 8px;
}

.message-body ul,
.message-body ol {
  padding-left: 21px;
}

.message-body li + li {
  margin-top: 4px;
}

.message-body blockquote {
  border-left: 3px solid var(--line-strong);
  color: var(--muted);
  padding-left: 10px;
}

.message-body a {
  color: var(--accent-strong);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.message.user .message-body a {
  color: #ffffff;
}

.message-body table {
  border-collapse: collapse;
  display: block;
  max-width: 100%;
  overflow-x: auto;
}

.message-body th,
.message-body td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
  vertical-align: top;
}

.message-body th {
  background: var(--surface-muted);
  font-weight: 760;
}

.message-body .katex-display {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 2px;
}

.message-body .katex {
  font-size: 1.04em;
}

.message-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 32px;
}

.action-button {
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  min-height: 30px;
  padding: 5px 9px;
}

.action-button:hover {
  background: var(--surface-muted);
  color: var(--text);
}

.action-button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.action-button.danger {
  color: var(--danger);
}

.branch {
  align-items: center;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 76%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  display: inline-flex;
  font-size: 12px;
  font-weight: 650;
  gap: 6px;
  min-height: 30px;
  padding: 2px 5px;
}

.branch .action-button {
  background: transparent;
  border-color: transparent;
  min-height: 24px;
  min-width: 24px;
  padding: 0;
}

.edit-box {
  display: grid;
  gap: 9px;
  width: min(100%, 88vw);
}

.edit-box textarea {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  color: var(--text);
  min-height: 112px;
  outline: none;
  padding: 11px;
  resize: vertical;
  width: 100%;
}

.edit-box textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.composer {
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-top: 1px solid var(--line);
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
}

.composer-box {
  align-items: end;
  background: var(--surface-raised);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(0, 1fr) auto;
  padding: 7px;
}

.composer-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

.composer textarea {
  background: transparent;
  border: 0;
  color: var(--text);
  max-height: 150px;
  min-height: 36px;
  outline: none;
  overflow-y: auto;
  padding: 8px 6px;
  resize: none;
  width: 100%;
}

.composer textarea::placeholder {
  color: var(--faint);
}

.send-button {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 760;
  min-height: 38px;
  min-width: 66px;
  padding: 0 14px;
}

.send-button:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.send-button:disabled,
.action-button:disabled {
  opacity: 0.48;
}

.send-button.busy::after {
  animation: pulse 1s infinite;
  content: ".";
}

.drawer-backdrop {
  background: rgba(4, 10, 14, 0.36);
  inset: 0;
  opacity: 0;
  pointer-events: none;
  position: fixed;
  transition: opacity 160ms ease;
  z-index: 9;
}

.drawer {
  background: var(--surface);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  left: 0;
  max-width: 380px;
  min-width: 0;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
  position: fixed;
  top: 0;
  transform: translateX(-104%);
  transition: transform 180ms ease;
  width: min(88vw, 380px);
  z-index: 10;
}

.app-shell.drawer-open .drawer {
  transform: translateX(0);
}

.app-shell.drawer-open .drawer-backdrop {
  opacity: 1;
  pointer-events: auto;
}

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

.drawer-actions {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.theme-switch {
  background: var(--surface-muted);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: grid;
  gap: 3px;
  grid-template-columns: repeat(3, 1fr);
  margin: 0 14px 12px;
  padding: 3px;
}

.theme-button {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  min-height: 32px;
}

.theme-button.active {
  background: var(--surface);
  border-color: var(--line);
  color: var(--text);
}

.chat-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  overflow-y: auto;
  padding: 0 10px 14px;
}

.chat-row {
  align-items: stretch;
  border-radius: var(--radius);
  display: grid;
  gap: 4px;
  grid-template-columns: minmax(0, 1fr);
  position: relative;
}

.chat-row.active {
  background: var(--accent-soft);
}

.chat-item {
  background: transparent;
  border: 0;
  border-radius: var(--radius);
  color: var(--text);
  display: grid;
  gap: 4px;
  min-height: 58px;
  min-width: 0;
  padding: 10px 64px 10px 10px;
  text-align: left;
  width: 100%;
}

.chat-row:not(.active) .chat-item:hover {
  background: var(--surface-muted);
}

.chat-title {
  font-size: 14px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-time {
  color: var(--muted);
  font-size: 12px;
}

.chat-delete {
  align-self: start;
  background: transparent;
  border-color: transparent;
  color: var(--faint);
  font-size: 11px;
  font-weight: 760;
  min-height: 30px;
  min-width: 54px;
  padding: 0 8px;
  position: absolute;
  right: 4px;
  top: 14px;
}

.chat-delete:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

code {
  background: var(--surface-muted);
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
  padding: 1px 4px;
}

.message.user code {
  background: rgba(255, 255, 255, 0.18);
  color: #ffffff;
}

pre {
  background: #151b1f;
  border-radius: var(--radius);
  color: #f3f7f8;
  margin: 8px 0;
  max-width: 100%;
  overflow-x: auto;
  padding: 12px;
  white-space: pre;
}

pre code {
  background: transparent;
  color: inherit;
  padding: 0;
}

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

@media (min-width: 821px) {
  .app-shell {
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
}
