:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  --serif: "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", SimSun, serif;
  --paper: #faf9f5;
  --surface: #fffefa;
  --sidebar: #f2f1e9;
  --message: #eeefe6;
  --hover: #e8eadf;
  --text: #232b26;
  --muted: #626c62;
  --line: #e0e1d6;
  --accent: #244b3b;
  --accent-strong: #153929;
  --accent-ink: #315346;
  --gold: #866736;
  --warning: #77511f;
  --warning-bg: #f7eddc;
  --error: #963f35;
  --connected: #487354;
  --scrim: #17271f4d;
  --shadow: 0 10px 27px #233a2b0b, 0 2px 5px #233a2b08;
  --content-width: 49rem;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--paper); color: var(--text); }
button, select, input, textarea { color: inherit; font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
button:focus-visible, select:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.app-shell {
  display: grid;
  grid-template-columns: clamp(13rem, 19vw, 15.75rem) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 1.55rem 1rem 1.15rem;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
}

.brand { display: flex; align-items: center; gap: .7rem; margin: 0 .25rem 1.8rem; }
.brand-mark, .assistant-mark {
  display: inline-grid;
  flex-shrink: 0;
  width: 2.25rem;
  height: 2.25rem;
  place-items: center;
  border-radius: .68rem;
  background: var(--accent);
  color: var(--surface);
  font-family: var(--serif);
  font-weight: 700;
}
.brand-name { font-size: 1rem; font-weight: 680; white-space: nowrap; }
.new-chat {
  display: flex;
  align-items: center;
  gap: .65rem;
  min-height: 2.7rem;
  padding: .48rem .72rem;
  border: 1px solid var(--line);
  border-radius: .7rem;
  background: var(--surface);
  color: var(--accent-strong);
  font-size: .875rem;
  font-weight: 650;
  text-align: left;
  transition: background-color .16s ease-out;
}
.new-chat:hover, .history-item:hover { background: var(--hover); }
.history-section { display: flex; flex: 1; flex-direction: column; min-height: 0; margin-top: 1.75rem; }
.history-section h2 { margin: 0 .6rem .5rem; color: var(--muted); font-size: .75rem; font-weight: 650; }
.history { display: grid; align-content: start; gap: .18rem; min-height: 0; overflow: auto; padding: .14rem; }
.history-item {
  min-width: 0;
  min-height: 2.45rem;
  padding: .45rem .6rem;
  overflow: hidden;
  border: 0;
  border-radius: .5rem;
  background: transparent;
  font-size: .875rem;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-item[aria-current] { background: var(--hover); color: var(--accent-strong); font-weight: 650; }
.history-item:focus-visible { outline-offset: -2px; }
.database-link { display: flex; justify-content: space-between; gap: .5rem; padding: .6rem; border-radius: .5rem; color: var(--accent-strong); font-size: .875rem; text-decoration: none; }
.database-link:hover { background: var(--hover); }
.database-link:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.sidebar-footer { margin: .8rem .6rem 0; color: var(--muted); font-size: .75rem; }
.drawer-backdrop, .menu-button { display: none; }

.main { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  min-height: 4.15rem;
  padding: .7rem clamp(1rem, 3vw, 2.3rem);
}
.topbar-heading { display: flex; align-items: center; gap: .55rem; }
.topbar-title { color: var(--accent-strong); font-size: .875rem; font-weight: 680; white-space: nowrap; }
.connection-button {
  display: flex;
  align-items: center;
  gap: .45rem;
  min-height: 2.1rem;
  padding: .4rem .65rem;
  border: 0;
  border-radius: .55rem;
  background: transparent;
  color: var(--muted);
  font-size: .75rem;
  white-space: nowrap;
}
.connection-button:hover, .close-button:hover, .menu-button:hover { background: var(--hover); }
.status-dot { width: .43rem; height: .43rem; border-radius: 50%; background: var(--muted); }
.status-dot[data-status="online"] { background: var(--connected); }
.status-dot[data-status="unauthorized"], .status-dot[data-status="offline"] { background: var(--gold); }

.stage { display: flex; flex-direction: column; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.welcome {
  display: flex;
  flex: 1 0 auto;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.4rem clamp(1.1rem, 3vw, 2rem) 2.8rem;
  text-align: center;
}
.welcome-picture { width: min(22rem, 90%); height: clamp(6.4rem, 19vh, 11rem); margin-bottom: .65rem; }
.welcome-picture img { display: block; width: 100%; height: 100%; object-fit: contain; }
.welcome h1 {
  margin: 0;
  color: var(--accent-strong);
  font: 700 clamp(1.85rem, 3.65vw, 3rem)/1.23 var(--serif);
  text-wrap: balance;
}
.welcome-description { margin: .85rem 0 0; color: var(--muted); font-size: .875rem; line-height: 1.65; text-wrap: balance; }
.examples {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: .65rem;
  width: min(100%, 41.75rem);
  margin-top: 2.15rem;
}
.example {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: .35rem;
  min-width: 0;
  padding: .72rem .8rem;
  border: 1px solid var(--line);
  border-radius: .75rem;
  background: transparent;
  font-size: .875rem;
  line-height: 1.5;
  text-align: left;
  transition: background-color .16s ease-out, border-color .16s ease-out;
}
.example:hover { border-color: var(--accent-ink); background: var(--surface); }
.example-topic { display: flex; justify-content: space-between; gap: .3rem; color: var(--accent-ink); font-weight: 670; }
.example-arrow { color: var(--gold); }
.example-text { color: var(--muted); }

.transcript { width: min(100%, var(--content-width)); margin: 0 auto; padding: 1.1rem 1.25rem 1.85rem; }
.turn { display: grid; gap: 1.1rem; margin: 0 0 1.65rem; }
.turn:last-child { margin-bottom: 0; }
.user-message { display: grid; justify-items: end; gap: .4rem; min-width: 0; }
.speaker-label { padding-inline: .25rem; color: var(--muted); font-size: .75rem; }
.user-text, .hint-text, .source-answer { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.user-text { max-width: min(36rem, 90%); padding: .62rem .9rem; border-radius: 1.15rem 1.15rem .32rem 1.15rem; background: var(--message); line-height: 1.65; }
.assistant-message { display: flex; align-items: flex-start; gap: .7rem; min-width: 0; }
.assistant-mark { width: 1.9rem; height: 1.9rem; border-radius: .57rem; font-size: .875rem; }
.assistant-content { display: grid; justify-items: start; min-width: 0; gap: .52rem; }
.assistant-label { margin: .3rem 0 0; color: var(--accent-strong); font-size: .875rem; font-weight: 680; }
.hint-text { line-height: 1.7; }
.response-note, .session-note, .source-status { margin: 0; color: var(--muted); font-size: .875rem; line-height: 1.55; }
.session-note { color: var(--warning); }
.response-error { color: var(--error); }
.response-actions, .source-links { display: flex; flex-wrap: wrap; align-items: center; gap: .38rem; }
.source-label { padding-right: .1rem; color: var(--muted); font-size: .75rem; }
.source-link, .retry-button {
  min-height: 1.8rem;
  padding: .2rem .55rem;
  border: 1px solid var(--line);
  border-radius: .38rem;
  background: var(--surface);
  color: var(--accent-strong);
  font-size: .75rem;
  overflow-wrap: anywhere;
}
.source-link:hover, .retry-button:hover { background: var(--hover); }
.diagnostics { color: var(--muted); font-size: .75rem; line-height: 1.6; }
.diagnostics summary { width: fit-content; cursor: pointer; }
.diagnostics p { margin: .35rem 0 0 .85rem; }

.composer-area { padding: .45rem 1.25rem calc(.55rem + env(safe-area-inset-bottom)); }
.composer-width { width: min(100%, calc(var(--content-width) - 2.5rem)); margin: 0 auto; }
.composer {
  padding: .7rem .72rem .55rem;
  border: 1px solid var(--line);
  border-radius: 1.1rem;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent-ink); }
.composer textarea {
  display: block;
  width: 100%;
  min-height: 3.15rem;
  max-height: 8rem;
  resize: vertical;
  padding: .1rem .25rem .35rem;
  border: 0;
  outline: 0;
  background: transparent;
  line-height: 1.45;
}
.composer textarea:focus-visible { outline: 0; }
.composer textarea::placeholder { color: var(--muted); }
.composer-actions, .composer-send { display: flex; align-items: center; justify-content: space-between; gap: .48rem; }
.role-control { display: inline-flex; align-items: center; gap: .3rem; padding: 0 .3rem; color: var(--muted); }
.role-control select { min-height: 2rem; padding: .22rem .08rem; appearance: none; border: 0; border-radius: .25rem; background: transparent; font-size: .75rem; cursor: pointer; }
.role-control > .icon { pointer-events: none; }
.role-control:focus-within { color: var(--accent-strong); }
.send-button {
  display: grid;
  width: 2.34rem;
  height: 2.34rem;
  place-items: center;
  border: 0;
  border-radius: .68rem;
  background: var(--accent);
  color: var(--surface);
  transition: background-color .16s ease-out;
}
.send-button:hover { background: var(--accent-strong); }
.send-button:disabled { background: var(--message); color: var(--muted); }
.character-count, .composer-help { color: var(--muted); font-size: .75rem; }
.composer-help { margin: .58rem 0 0; line-height: 1.4; text-align: center; }
.connection-notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  margin-bottom: .55rem;
  padding: .52rem .72rem;
  border-radius: .6rem;
  background: var(--warning-bg);
  color: var(--warning);
  font-size: .875rem;
  line-height: 1.5;
}
.text-button { flex-shrink: 0; padding: .2rem .1rem; border: 0; background: transparent; color: inherit; font-weight: 680; text-decoration: underline; text-underline-offset: .2em; }

.dialog { width: min(calc(100% - 2rem), 27rem); max-height: calc(100% - 2rem); padding: 0; border: 1px solid var(--line); border-radius: 1rem; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.dialog::backdrop { background: var(--scrim); }
.dialog-header { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .75rem .95rem .7rem 1.25rem; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: 0; color: var(--accent-strong); font-size: 1rem; }
.close-button, .menu-button { width: 2.2rem; height: 2.2rem; padding: 0; border: 0; border-radius: .45rem; background: transparent; color: var(--accent-ink); }
.close-button { display: grid; flex-shrink: 0; place-items: center; }
.dialog-body { padding: 1rem 1.25rem 1.2rem; }
.field-label { display: block; margin-bottom: .38rem; font-size: .875rem; font-weight: 680; }
.token-input { width: 100%; min-height: 2.45rem; padding: .45rem .6rem; border: 1px solid var(--line); border-radius: .45rem; background: var(--paper); }
.field-help, .settings-feedback { margin: .5rem 0 0; color: var(--muted); font-size: .75rem; line-height: 1.6; }
.settings-feedback[data-error] { color: var(--error); }
.connection-details { display: grid; gap: .45rem; margin: .85rem 0 0; font-size: .875rem; }
.connection-details div { display: flex; justify-content: space-between; gap: .75rem; }
.connection-details dt { color: var(--muted); }
.connection-details dd { margin: 0; text-align: right; }
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 1.05rem; }
.primary-button { min-height: 2.4rem; padding: .4rem .8rem; border: 0; border-radius: .5rem; background: var(--accent); color: var(--surface); font-size: .875rem; font-weight: 680; }
.primary-button:hover { background: var(--accent-strong); }
.primary-button:disabled { opacity: .7; }
.source-dialog { width: min(calc(100% - 2rem), 38rem); }
.source-question { margin: 0 0 .45rem; color: var(--accent-strong); font-size: 1rem; line-height: 1.5; overflow-wrap: anywhere; }
.source-meta { margin: 0 0 .9rem; color: var(--muted); font-size: .75rem; line-height: 1.55; overflow-wrap: anywhere; }
.source-answer { line-height: 1.75; }
.source-content .retry-button { margin-top: .65rem; }

.icon { position: relative; display: inline-block; flex: 0 0 auto; width: .95rem; height: .95rem; }
.icon::before, .icon::after { position: absolute; content: ""; }
.icon-plus::before, .icon-plus::after, .icon-close::before, .icon-close::after { top: calc(50% - .75px); left: 6%; width: 88%; height: 1.5px; border-radius: 1px; background: currentColor; }
.icon-plus::after { transform: rotate(90deg); }
.icon-close::before { transform: rotate(45deg); }
.icon-close::after { transform: rotate(-45deg); }
.icon-menu::before { top: 22%; left: 2%; width: 96%; height: 1.5px; border-radius: 1px; background: currentColor; box-shadow: 0 4px currentColor, 0 8px currentColor; }
.icon-chevron { width: .7rem; height: .7rem; }
.icon-chevron::before { top: 15%; left: 24%; width: 45%; height: 45%; transform: rotate(45deg); border: solid currentColor; border-width: 0 1.4px 1.4px 0; }
.icon-person::before { top: 0; left: 32%; width: 38%; height: 38%; border: 1.3px solid currentColor; border-radius: 50%; }
.icon-person::after { top: 49%; left: 14%; width: 75%; height: 41%; border: 1.3px solid currentColor; border-radius: 50% 50% 25% 25%; }
.icon-send::before { top: 14%; left: 28%; width: 48%; height: 48%; transform: rotate(45deg); border: solid currentColor; border-width: 1.8px 0 0 1.8px; }
.icon-send::after { top: 17%; left: calc(50% - .9px); width: 1.8px; height: 73%; border-radius: 1px; background: currentColor; }

@media (max-width: 730px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 3; width: min(16.5rem, 85vw); visibility: hidden; transform: translateX(-100%); transition: transform .2s ease-out, visibility .2s; }
  .sidebar.is-open { visibility: visible; transform: translateX(0); }
  .drawer-backdrop { position: fixed; inset: 0; z-index: 2; display: block; width: 100%; border: 0; background: var(--scrim); }
  .topbar { min-height: 3.4rem; padding: .35rem .7rem; }
  .menu-button { display: grid; place-items: center; }
  .welcome { padding-bottom: 1.75rem; }
}

@media (max-width: 530px) {
  .welcome { padding-top: .5rem; }
  .welcome-picture { height: clamp(5.75rem, 16vh, 8rem); }
  .examples { grid-template-columns: minmax(0, 1fr); gap: .38rem; max-width: 25rem; margin-top: 1.2rem; }
  .example { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .08rem .45rem; padding: .4rem .65rem; }
  .example-topic { display: contents; }
  .example-arrow { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
  .example-text { grid-column: 1; grid-row: 2; }
  .transcript { padding-inline: .85rem; }
  .composer-area { padding-inline: .7rem; }
  .composer-help { display: none; }
}

@media (max-height: 590px) {
  .welcome-picture { height: min(5rem, 15vh); margin-bottom: .35rem; }
  .welcome { padding-bottom: 1rem; }
  .examples { margin-top: .9rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}

.logout-button { padding: .6rem; border: 0; border-radius: .5rem; background: transparent; color: var(--accent-strong); font-size: .875rem; text-align: left; }
.logout-button:hover { background: var(--hover); }
.login-page { display: grid; min-height: 100vh; min-height: 100dvh; place-items: center; padding: 1.5rem; }
.login-panel { width: min(100%, 23rem); padding: 2rem; border: 1px solid var(--line); border-radius: 1rem; background: var(--surface); box-shadow: var(--shadow); }
.login-panel .brand { margin: 0 0 2rem; }
.login-panel h1 { margin: 0 0 1.5rem; color: var(--accent-strong); font: 700 1.8rem/1.25 var(--serif); }
.login-feedback { min-height: 1.25rem; margin: .5rem 0; color: var(--error); font-size: .875rem; line-height: 1.45; }
.login-submit { width: 100%; }
