:root {
  color-scheme: light;
  --app-height: 100dvh;
  --bg: #f8f7f3;
  --sidebar: #f1f0eb;
  --surface: #fffefa;
  --surface-soft: #f5f3ed;
  --surface-hover: #eae8e1;
  --line: rgba(45, 47, 51, 0.1);
  --line-strong: rgba(45, 47, 51, 0.17);
  --text: #34363a;
  --text-strong: #202226;
  --muted: #777a7f;
  --muted-soft: #999b9f;
  --accent: #d6a426;
  --accent-soft: rgba(214, 164, 38, 0.12);
  --accent-ink: #332706;
  --success: #3d8b63;
  --danger: #bd4f4b;
  --shadow: 0 12px 34px rgba(39, 37, 30, 0.09);
  --font-body: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-family: var(--font-body);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111315;
  --sidebar: #17191c;
  --surface: #1d2024;
  --surface-soft: #191c20;
  --surface-hover: #25292e;
  --line: rgba(242, 244, 247, 0.1);
  --line-strong: rgba(242, 244, 247, 0.18);
  --text: #e6e8eb;
  --text-strong: #f5f6f7;
  --muted: #999fa7;
  --muted-soft: #717780;
  --accent: #e3b441;
  --accent-soft: rgba(227, 180, 65, 0.12);
  --accent-ink: #211a07;
  --success: #66bd8b;
  --danger: #ee7b73;
  --shadow: 0 14px 38px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

body {
  font-family: var(--font-body);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
textarea { font: inherit; }

button { cursor: pointer; }

button:focus-visible,
textarea:focus-visible,
summary:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.login-page {
  overflow: auto;
  background:
    radial-gradient(circle at 50% -15%, rgba(214, 164, 38, .12), transparent 40%),
    var(--bg);
}

.login-shell {
  display: grid;
  min-height: 100dvh;
  padding: max(28px, env(safe-area-inset-top)) 20px max(28px, env(safe-area-inset-bottom));
  place-items: center;
}

.login-panel {
  width: min(100%, 390px);
  padding: 36px 34px 30px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: rgba(255, 254, 250, .9);
  box-shadow: var(--shadow);
}

.login-brand {
  display: grid;
  width: 44px;
  height: 44px;
  margin-bottom: 22px;
  place-items: center;
  border: 1px solid rgba(214, 164, 38, .46);
  border-radius: 14px;
  background: var(--accent-soft);
  color: #9b720b;
  font-size: 16px;
  font-weight: 850;
}

.login-eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .14em;
}

.login-panel h1 {
  margin: 0;
  color: var(--text-strong);
  font-size: 25px;
  line-height: 1.35;
  letter-spacing: -.025em;
}

.login-intro {
  margin: 10px 0 26px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.65;
}

.login-form { display: grid; gap: 9px; }
.login-form label {
  margin-top: 5px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
}

.login-form input {
  width: 100%;
  min-height: 48px;
  padding: 0 13px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  outline: none;
  background: var(--surface);
  color: var(--text-strong);
  transition: border-color .16s ease, box-shadow .16s ease;
}

.login-form input:focus {
  border-color: rgba(177, 130, 16, .58);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.login-error {
  min-height: 19px;
  margin: 3px 0 0;
  color: var(--danger);
  font-size: 12px;
}

.login-form button {
  min-height: 48px;
  margin-top: 2px;
  border: 0;
  border-radius: 12px;
  background: var(--text-strong);
  color: var(--surface);
  font-weight: 700;
}

.login-form button:hover { opacity: .9; }
.login-note {
  margin: 22px 0 0;
  color: var(--muted-soft);
  font-size: 11px;
  text-align: center;
}

@media (max-width: 480px) {
  .login-shell { padding-inline: 16px; }
  .login-panel {
    padding: 30px 24px 26px;
    border-radius: 18px;
  }
}

::selection { background: rgba(214, 164, 38, 0.23); }

* {
  scrollbar-color: var(--line-strong) transparent;
  scrollbar-width: thin;
}

.app-shell {
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  width: 100%;
  height: var(--app-height);
  min-height: 0;
}

.history-drawer {
  z-index: 20;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  padding: 12px 10px 10px;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
}

.drawer-header,
.drawer-header > div,
.chat-header,
.header-actions,
.runtime-state,
.composer,
.runtime-progress > summary {
  display: flex;
  align-items: center;
}

.drawer-header {
  justify-content: space-between;
  min-height: 40px;
  padding: 0 7px 8px;
}

.drawer-header > div { gap: 9px; }

.brand-mark {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 1px solid rgba(214, 164, 38, 0.43);
  border-radius: 9px;
  color: #a27608;
  background: var(--accent-soft);
  font-size: 10px;
  font-weight: 850;
  letter-spacing: 0.02em;
}

html[data-theme="dark"] .brand-mark { color: var(--accent); }
.drawer-header strong { color: var(--text-strong); font-size: 13px; letter-spacing: -0.01em; }

.new-chat-button {
  min-height: 42px;
  margin: 3px 2px 14px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.43);
  color: var(--text-strong);
  font-size: 13px;
  font-weight: 650;
  text-align: left;
  transition: background 0.16s ease, border-color 0.16s ease;
}

html[data-theme="dark"] .new-chat-button { background: var(--surface); }

.new-chat-button:hover,
.conversation-item:hover,
.conversation-item.active { background: var(--surface-hover); }

.conversation-item.active { color: var(--text-strong); }

.history-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 9px 7px;
  color: var(--muted);
  font-size: 11px;
}

.text-button,
.icon-button {
  min-width: 42px;
  min-height: 42px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.text-button {
  min-width: auto;
  min-height: 30px;
  padding: 0 5px;
  font-size: 11px;
}

.icon-button {
  display: grid;
  place-items: center;
  border-radius: 10px;
  font-size: 19px;
  transition: background 0.16s ease, color 0.16s ease;
}

.icon-button:hover { background: var(--surface-hover); color: var(--text-strong); }
.drawer-close,
.menu-button,
.mobile-new-chat { display: none; }
.header-actions { gap: 2px; }
.theme-toggle { display: grid; font-size: 17px; }
.theme-icon { line-height: 1; }

.conversation-list {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.conversation-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  align-items: center;
  margin-bottom: 2px;
  border-radius: 9px;
}

.conversation-item {
  display: grid;
  gap: 3px;
  width: 100%;
  min-height: 50px;
  margin-bottom: 0;
  padding: 8px 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--text);
  text-align: left;
  transition: background 0.14s ease;
}

.conversation-delete {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.14s ease, background 0.14s ease, color 0.14s ease;
}

.conversation-row:hover .conversation-delete,
.conversation-delete:focus-visible {
  opacity: 1;
}

.conversation-delete:hover {
  background: rgba(185, 54, 54, 0.09);
  color: #a63d3d;
}

@media (max-width: 900px) {
  .conversation-delete { opacity: 0.72; }
}

.conversation-title {
  overflow: hidden;
  font-size: 12.5px;
  font-weight: 560;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-meta,
.history-empty {
  margin: 0;
  color: var(--muted);
  font-size: 10.5px;
}

.history-empty { padding: 12px 10px; }

.technical-details {
  padding: 9px 8px 0;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 10.5px;
}

.technical-details summary { min-height: 30px; cursor: pointer; }

.technical-details pre {
  overflow: auto;
  max-height: 210px;
  margin: 7px 0 0;
  padding: 10px;
  border-radius: 9px;
  background: var(--surface);
  color: var(--muted);
  font-size: 9.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.drawer-overlay { display: none; }

.chat-view {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.chat-header {
  z-index: 5;
  min-height: 50px;
  justify-content: space-between;
  padding: 0 17px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 91%, transparent);
  backdrop-filter: blur(15px);
}

.chat-title { display: grid; gap: 1px; }
.chat-title strong { color: var(--text-strong); font-size: 13px; font-weight: 670; letter-spacing: -0.01em; }

.runtime-state {
  gap: 6px;
  color: var(--muted);
  font-size: 10.5px;
}

.runtime-state i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--success);
}

.runtime-state.error i { background: var(--danger); }

.messages {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  padding: 36px max(28px, calc((100% - 820px) / 2)) 34px;
}

.message {
  display: flex;
  width: 100%;
  margin: 0 auto 27px;
}

.message.user { justify-content: flex-end; }

.bubble {
  max-width: min(740px, 88%);
  overflow-wrap: anywhere;
  color: var(--text);
  font-size: 16px;
  line-height: 1.78;
  letter-spacing: 0.005em;
}

.message.user .bubble {
  padding: 10px 15px;
  border-radius: 18px 18px 5px 18px;
  background: #ebe8df;
  color: var(--text-strong);
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
}

html[data-theme="dark"] .message.user .bubble { background: #292b2e; }

.message.assistant .bubble {
  width: min(740px, 100%);
  color: var(--text);
}

.runtime-progress {
  width: min(740px, 100%);
  margin: -11px auto 20px;
  color: var(--muted);
  font-size: 11.5px;
}

.runtime-progress > summary {
  width: fit-content;
  min-height: 30px;
  gap: 8px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.runtime-progress > summary::-webkit-details-marker { display: none; }

.progress-spinner {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  border: 1.5px solid var(--line-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.82s linear infinite;
}

.runtime-progress.completed .progress-spinner,
.runtime-progress.failed .progress-spinner {
  border: 0;
  animation: none;
}

.runtime-progress.completed .progress-spinner::before {
  content: "✓";
  color: var(--success);
  font-weight: 850;
}

.runtime-progress.failed .progress-spinner::before { content: "!"; color: var(--danger); font-weight: 850; }

@keyframes spin { to { transform: rotate(360deg); } }

.runtime-audit {
  display: grid;
  gap: 15px;
  margin-top: 7px;
  padding: 14px 15px;
  border-radius: 12px;
  background: var(--surface-soft);
  box-shadow: inset 0 0 0 1px var(--line);
}

.runtime-audit h3 {
  margin: 0 0 7px;
  color: var(--text-strong);
  font-size: 10.5px;
  font-weight: 680;
  letter-spacing: 0.02em;
}

.activity-list,
.rationale-list {
  display: grid;
  gap: 7px;
  margin: 0;
  padding-left: 18px;
}

.activity-list li strong { display: block; color: var(--text); font-size: 11.5px; }
.activity-list li span { display: block; margin-top: 2px; color: var(--muted); font-size: 10.5px; }
.tool-list,
.audit-sources { display: grid; gap: 7px; }

.tool-row,
.audit-card {
  padding: 9px 10px;
  border-radius: 9px;
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
}

.audit-card strong { color: var(--text-strong); }
.audit-card p { margin: 4px 0 0; line-height: 1.55; }
.audit-muted { margin: 0; color: var(--muted); font-size: 10.5px; }
.wrap-anywhere { overflow-wrap: anywhere; }
.record-details { margin-top: 8px; }
.record-details summary,
.record-card summary { min-height: 28px; cursor: pointer; color: #96700d; }
html[data-theme="dark"] .record-details summary { color: var(--accent); }
.record-list { display: grid; gap: 7px; margin-top: 7px; }
.record-card { padding: 7px 9px; border-radius: 8px; background: var(--surface-soft); box-shadow: inset 0 0 0 1px var(--line); }
.record-card > summary { overflow: hidden; color: var(--text); text-overflow: ellipsis; white-space: nowrap; }
.record-card dl { display: grid; grid-template-columns: minmax(80px, .32fr) minmax(0, 1fr); gap: 5px 9px; margin: 8px 0 0; }
.record-card dt { color: var(--muted); }
.record-card dd { min-width: 0; margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }

.composer-wrap {
  padding: 11px max(24px, calc((100% - 820px) / 2)) max(14px, env(safe-area-inset-bottom));
  background: linear-gradient(180deg, color-mix(in srgb, var(--bg) 4%, transparent), var(--bg) 24%);
}

.composer {
  gap: 10px;
  min-height: 56px;
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

.composer:focus-within {
  border-color: rgba(214, 164, 38, 0.52);
  box-shadow: 0 12px 38px rgba(39, 37, 30, 0.12), 0 0 0 3px var(--accent-soft);
}

textarea {
  display: block;
  flex: 1;
  min-width: 0;
  min-height: 25px;
  max-height: 180px;
  resize: none;
  overflow-y: auto;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text-strong);
  font-size: 15px;
  line-height: 1.55;
}

textarea::placeholder { color: var(--muted-soft); }

.send-button {
  display: grid;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 20px;
  font-weight: 900;
  transition: transform 0.14s ease, opacity 0.14s ease;
}

.send-button:hover:not(:disabled) { transform: translateY(-1px); }
.send-button:disabled { cursor: wait; opacity: .43; }
.composer-meta {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 30px;
  padding: 3px 4px 0;
}

.reasoning-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 28px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  transition: color .14s ease, background .14s ease;
}

.reasoning-toggle:hover { color: var(--text-strong); background: var(--surface-soft); }
.reasoning-toggle[aria-pressed="true"] { color: #856007; background: var(--accent-soft); }
html[data-theme="dark"] .reasoning-toggle[aria-pressed="true"] { color: var(--accent); }

.composer-hint {
  position: absolute;
  left: 50%;
  margin: 0;
  color: var(--muted-soft);
  font-size: 9.5px;
  text-align: center;
  transform: translateX(-50%);
}

.markdown-body > :first-child { margin-top: 0; }
.markdown-body > :last-child { margin-bottom: 0; }
.markdown-body p { margin: 0 0 13px; }
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 24px 0 10px;
  color: var(--text-strong);
  font-weight: 720;
  line-height: 1.36;
  letter-spacing: -0.018em;
}
.markdown-body h1 { font-size: 1.42em; }
.markdown-body h2 { font-size: 1.25em; }
.markdown-body h3 { font-size: 1.1em; }
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 { font-size: 1em; }
.markdown-body strong { color: var(--text-strong); font-weight: 720; }
.markdown-body ul,
.markdown-body ol { margin: 9px 0 14px; padding-left: 23px; }
.markdown-body li + li { margin-top: 5px; }
.markdown-body blockquote {
  margin: 15px 0;
  padding: 3px 0 3px 15px;
  border-left: 3px solid var(--accent);
  color: #666a70;
}
html[data-theme="dark"] .markdown-body blockquote { color: #b5bac0; }
.markdown-body code {
  padding: 2px 5px;
  border-radius: 5px;
  background: var(--surface-soft);
  color: #835f05;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: .88em;
}
html[data-theme="dark"] .markdown-body code { color: #e9c96f; }
.markdown-body pre {
  max-width: 100%;
  overflow-x: auto;
  margin: 15px 0;
  padding: 14px;
  border-radius: 11px;
  background: #24262a;
  color: #f0f1f2;
}
.markdown-body pre code { padding: 0; background: transparent; color: inherit; white-space: pre; }
.markdown-body a { color: #8b670c; text-decoration-thickness: 1px; text-underline-offset: 3px; }
html[data-theme="dark"] .markdown-body a { color: #e0ba59; }
.table-scroll { width: 100%; overflow-x: auto; margin: 15px 0; border-radius: 10px; box-shadow: inset 0 0 0 1px var(--line); }
.markdown-body table { min-width: 480px; width: 100%; border-collapse: collapse; font-size: 13px; line-height: 1.55; }
.markdown-body th,
.markdown-body td { padding: 9px 11px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.markdown-body tr:last-child td { border-bottom: 0; }
.markdown-body th { background: var(--surface-soft); color: var(--text-strong); font-weight: 680; }

.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;
}

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .history-drawer {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(318px, 86vw);
    height: var(--app-height);
    transform: translateX(-102%);
    transition: transform .2s ease;
    box-shadow: 18px 0 58px rgba(43, 41, 34, .18);
  }
  html[data-theme="dark"] .history-drawer { box-shadow: 18px 0 58px rgba(0, 0, 0, .45); }
  .history-drawer.open { transform: translateX(0); }
  .drawer-close,
  .menu-button,
  .mobile-new-chat { display: grid; }
  .drawer-overlay {
    position: fixed;
    z-index: 15;
    inset: 0;
    display: block;
    visibility: hidden;
    opacity: 0;
    border: 0;
    background: rgba(35, 35, 32, .28);
    backdrop-filter: blur(1px);
    transition: opacity .2s ease, visibility .2s;
  }
  .drawer-overlay.visible { visibility: visible; opacity: 1; }
  .chat-header { padding: 0 10px; }
}

@media (max-width: 600px) {
  .chat-header {
    min-height: 49px;
    padding-right: 7px;
    background: var(--bg);
  }
  .chat-title strong { font-size: 12.5px; }
  .runtime-state { font-size: 10px; }
  .icon-button { min-width: 42px; min-height: 42px; }
  .messages { padding: 24px 16px 20px; }
  .message { margin-bottom: 24px; }
  .bubble { max-width: 92%; font-size: 16px; line-height: 1.72; }
  .message.user .bubble { padding: 9px 13px; font-size: 14.5px; }
  .message.assistant .bubble { width: 100%; max-width: 100%; }
  .runtime-progress { width: 100%; margin-top: -9px; margin-bottom: 18px; }
  .runtime-progress > summary,
  .record-details summary,
  .record-card summary { min-height: 42px; }
  .runtime-audit { padding: 12px; }
  .record-card dl { grid-template-columns: 1fr; }
  .record-card dd { margin-bottom: 5px; }
  .composer-wrap {
    padding: 8px 9px max(9px, env(safe-area-inset-bottom));
    background: var(--bg);
  }
  .composer {
    min-height: 54px;
    padding-left: 14px;
    border-radius: 18px;
    box-shadow: 0 8px 26px rgba(39, 37, 30, .1);
  }
  .composer-meta { min-height: 31px; padding-left: 2px; }
  .composer-hint { display: none; }
  textarea { font-size: 16px; }
  .send-button { width: 42px; height: 42px; }
  .markdown-body h1 { font-size: 1.34em; }
  .markdown-body h2 { font-size: 1.2em; }
  .markdown-body h3 { font-size: 1.08em; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
  .progress-spinner { animation-duration: 1.6s; }
}
