:root {
  color-scheme: light;
  --paper: #ffffff;
  --surface: #ffffff;
  --raised: #f2f4f7;
  --ink: #272d38;
  --muted: #667080;
  --line: #e8ecf2;
  --control-line: #cbd2de;
  --sidebar: #f6f7f9;
  --sidebar-surface: #ffffff;
  --sidebar-line: #e1e6ee;
  --sidebar-ink: #343c4b;
  --sidebar-muted: #667080;
  --accent: #4d63aa;
  --accent-hover: #3d518f;
  --accent-light: #6177ae;
  --accent-wash: #eef2fa;
  --error: #9f2d35;
  --shadow: 0 1px 3px rgb(36 51 82 / 3%), 0 8px 28px rgb(36 51 82 / 4%);
  --rail-width: 244px;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
  font-family: var(--sans);
  background: var(--paper);
  color: var(--ink);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 280px; }
button, input, select, textarea { color: inherit; font: inherit; }
button { cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
button, a, input, select, textarea, [tabindex] { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
a { color: var(--accent); }
button { touch-action: manipulation; }
.symbols { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon, .brand-mark svg { width: 20px; height: 20px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.muted { color: var(--muted); }
.brand { display: inline-flex; align-items: center; gap: 11px; flex: 0 0 auto; color: var(--sidebar-ink); font-size: 15px; font-weight: 650; text-decoration: none; white-space: nowrap; }
.brand-mark { width: 34px; height: 34px; display: inline-grid; place-items: center; color: var(--accent); background: var(--accent-wash); border: 1px solid #e1e7f3; border-radius: 10px; }
.brand-mark svg { width: 19px; height: 19px; }
.brand-separator { color: var(--accent-light); padding: 0 2px; }
.chapter { display: inline-flex; align-items: center; gap: 9px; color: var(--accent); font-size: 12px; font-weight: 650; letter-spacing: .1em; }
.chapter > span { height: 5px; width: 5px; border-radius: 50%; background: currentColor; }
.gate h1, .welcome h1 { margin: 18px 0 17px; color: var(--ink); font: 500 clamp(36px, 4.7vw, 58px)/1.2 var(--sans); letter-spacing: -.025em; }
.title-dot { color: var(--accent); }
.hero-copy { max-width: 34em; margin: 0; color: var(--muted); font-size: 16px; line-height: 1.75; }

.gate { min-height: 100dvh; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr auto; }
.gate-header { padding: 22px 30px; background: var(--paper); border-bottom: 1px solid var(--line); }
.gate-main { display: grid; align-items: center; justify-items: center; padding: clamp(20px, 5vw, 64px); }
.gate-card { width: min(100%, 520px); padding: clamp(22px, 5vw, 46px); }
.gate-card h1 { font-size: clamp(32px, 5.2vw, 54px); }
.gate-divider { height: 1px; margin: 30px 0 23px; background: var(--line); }
.gate-card p[role="status"] { margin: 0; font-size: 14px; }
.gate-form { display: grid; gap: 11px; }
.gate-form label, .admin-form label { font-size: 14px; font-weight: 630; }
.gate-form input, .admin-form input { min-width: 0; min-height: 46px; padding: 10px 13px; border: 1px solid var(--control-line); border-radius: 9px; background: var(--paper); }
.gate-form .primary-button { justify-content: space-between; margin-top: 3px; }
.primary-button, .plain-button, .danger-button { min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 8px 15px; border: 1px solid transparent; border-radius: 8px; font-size: 14px; font-weight: 650; }
.primary-button { color: var(--surface); background: var(--accent); }
.primary-button:hover:not(:disabled), .send-button:hover:not(:disabled) { background: var(--accent-hover); }
.plain-button { color: var(--ink); background: var(--surface); border-color: var(--control-line); }
.plain-button:hover:not(:disabled) { background: var(--raised); }
#session-retry { margin-top: 10px; }
.danger-button { color: var(--surface); background: var(--error); }
.inline-error { margin: 0; color: var(--error); font-size: 13px; line-height: 1.5; }
.gate-footer { padding: 18px 20px 23px; text-align: center; color: var(--muted); font-size: 12px; line-height: 1.55; }

.workspace { display: grid; grid-template-columns: var(--rail-width) minmax(0, 1fr); height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { min-height: 0; display: flex; flex-direction: column; padding: 22px 14px 14px; background: var(--sidebar); color: var(--sidebar-ink); border-right: 1px solid var(--line); }
.sidebar-heading { display: flex; justify-content: space-between; align-items: center; padding: 0 6px; }
.sidebar :focus-visible, .gate-header :focus-visible, .admin-header :focus-visible { outline-color: var(--accent-light); }
.new-chat { display: flex; align-items: center; gap: 9px; min-height: 44px; margin-top: 30px; padding: 8px 11px; border: 1px solid var(--sidebar-line); border-radius: 9px; background: var(--sidebar-surface); color: var(--sidebar-ink); font-size: 14px; font-weight: 650; text-align: left; }
.new-chat .icon { color: var(--accent-light); }
.new-chat:hover:not(:disabled) { border-color: var(--accent-light); }
.history { margin-top: 27px; flex: 1 1 auto; min-height: 0; overflow: auto; padding: 4px; }
.history h2 { margin: 0 0 9px 6px; color: var(--sidebar-muted); font-size: 12px; letter-spacing: .06em; font-weight: 650; }
.history-empty { margin: 0 0 0 6px; color: var(--sidebar-muted); font-size: 12px; line-height: 1.6; }
.chat-list { margin: 0; padding: 0; list-style: none; }
.chat-list button { display: block; width: 100%; min-height: 40px; overflow: hidden; padding: 7px 8px; border: 1px solid transparent; border-radius: 8px; text-align: left; text-overflow: ellipsis; white-space: nowrap; background: transparent; font-size: 13px; }
.chat-list button:hover, .chat-list button[aria-current="true"] { background: #eaf0f7; }
.chat-list button[aria-current="true"] { color: #344977; }
.sidebar-footer { display: flex; align-items: center; gap: 9px; padding: 14px 4px 2px; border-top: 1px solid var(--sidebar-line); }
.account-dot { flex: 0 0 auto; width: 7px; height: 7px; margin: 0 5px; border-radius: 50%; background: var(--accent-light); }
.account-copy { min-width: 0; display: grid; gap: 3px; }
.account-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 630; }
.account-copy small { color: var(--sidebar-muted); font-size: 11px; }
.text-button { min-height: 38px; margin-left: auto; padding: 4px 8px; border: 0; border-radius: 6px; background: transparent; color: var(--accent); white-space: nowrap; font-size: 13px; }
.text-button:hover:not(:disabled) { background: var(--accent-wash); }
.sidebar .text-button, .sidebar .icon-button { color: var(--sidebar-ink); }
.sidebar .text-button:hover:not(:disabled), .sidebar .icon-button:hover:not(:disabled) { background: var(--sidebar-surface); border-color: var(--sidebar-line); }
.close-sidebar, .open-sidebar, .sidebar-overlay { display: none !important; }

.chat-main { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; }
.chat-header { min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 13px clamp(14px, 3.4vw, 40px); border-bottom: 1px solid var(--line); background: var(--surface); }
.model-control, .model-group, .header-actions { display: flex; align-items: center; gap: 8px; }
.model-control, .model-group { min-width: 0; }
.model-group label { color: var(--muted); font-size: 12px; }
.model-group select {
  appearance: none;
  width: min(36vw, 190px);
  min-height: 40px;
  border: 1px solid var(--control-line);
  border-radius: 8px;
  padding: 6px 26px 6px 12px;
  background: var(--paper);
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 15px center, right 11px center;
  background-size: 4px 4px;
  background-repeat: no-repeat;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
}
.model-group select:hover:not(:disabled) { border-color: var(--accent); }
.model-group select:disabled { cursor: default; }
.header-actions { flex: 0 0 auto; gap: 4px; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 40px; min-height: 40px; padding: 7px 8px; border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--muted); font-size: 13px; }
.icon-button:hover:not(:disabled) { background: var(--raised); border-color: var(--line); color: var(--ink); }
.icon-button .icon { width: 18px; height: 18px; }
.chat-scroll { min-height: 0; overflow: auto; overflow-wrap: anywhere; overscroll-behavior: contain; }
.chat-scroll:focus-visible { outline-offset: -3px; }
.welcome, .messages, .composer-wrap { width: min(100%, 820px); margin-inline: auto; }
.welcome { padding: clamp(30px, 13dvh, 130px) clamp(20px, 4vw, 45px) 30px; text-align: center; }
.welcome .hero-copy { margin-inline: auto; }
.examples { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 11px; margin-top: clamp(22px, 5dvh, 45px); }
.example { min-width: 0; min-height: 90px; display: grid; grid-template-columns: 1fr auto; align-items: center; align-content: center; gap: 8px; padding: 13px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-align: left; color: var(--ink); font-size: 13px; font-weight: 450; line-height: 1.5; }
.example:hover { border-color: var(--accent); }
.example-category { grid-column: 1 / -1; color: var(--muted); font-size: 11px; font-weight: 650; letter-spacing: .09em; }
.example .icon { width: 15px; height: 15px; color: var(--accent); }
.messages { display: grid; gap: 22px; align-content: start; padding: 32px clamp(15px, 4vw, 45px) 28px; }
.message { min-width: 0; }
.message > h2 { margin: 0 0 9px; color: var(--ink); font-size: 13px; font-weight: 700; }
.user-message { width: fit-content; max-width: min(91%, 590px); justify-self: end; }
.user-message > h2 { text-align: right; color: var(--muted); }
.user-message .user-text { margin: 0; padding: 11px 15px; background: var(--raised); border-radius: 14px 5px 14px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.assistant-message { padding: 11px 0 20px; border-bottom: 1px solid var(--line); }
.message-copy { font-size: 15px; line-height: 1.8; }
.message-copy > :first-child { margin-top: 0; }
.message-copy > :last-child { margin-bottom: 0; }
.message-copy h1, .message-copy h2, .message-copy h3, .message-copy h4, .message-copy h5, .message-copy h6 { margin: 1em 0 .5em; color: var(--ink); font-weight: 700; line-height: 1.4; }
.message-copy h1 { font-size: 1.35em; }
.message-copy h2 { font-size: 1.22em; }
.message-copy h3 { font-size: 1.12em; }
.message-copy p, .message-copy ul, .message-copy ol, .message-copy blockquote { margin: .65em 0; }
.message-copy ul, .message-copy ol { padding-inline-start: 1.6em; }
.message-copy blockquote { padding-inline-start: 12px; border-inline-start: 2px solid var(--control-line); color: var(--muted); }
.message-copy a { text-underline-offset: 3px; }
.message-copy pre { max-width: 100%; overflow: auto; margin: 12px 0; padding: 13px 15px; border: 1px solid var(--line); border-radius: 9px; background: var(--raised); font-size: 13px; white-space: pre; overflow-wrap: normal; }
.message-copy :not(pre) > code { padding: 2px 4px; border-radius: 4px; background: var(--raised); }
.message-copy table { border-collapse: collapse; width: max-content; max-width: 100%; }
.message-copy th, .message-copy td { padding: 5px 9px; border: 1px solid var(--line); vertical-align: top; }
.table-scroll { max-width: 100%; overflow: auto; }
.message-copy .math-source[data-display="true"] { display: block; max-width: 100%; overflow-x: auto; overflow-y: hidden; padding: .25em 0; }
.message-copy .katex-display { margin: .6em 0; }
.message-state { margin: 8px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.message-state.error { color: var(--error); }
.streaming .message-state::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.5s ease-in-out infinite alternate; vertical-align: 1px; }
@keyframes pulse { to { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .streaming .message-state::before { animation: none; } }

.composer-area { min-width: 0; padding: 10px clamp(15px, 4vw, 45px) max(12px, env(safe-area-inset-bottom)); background: var(--paper); }
.composer-wrap { max-width: 730px; }
.model-info, .privacy { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.model-info { margin: 0 3px 8px; }
.app-notice { margin-bottom: 8px; padding: 8px 11px; border-radius: 8px; background: var(--accent-wash); color: var(--accent); font-size: 13px; line-height: 1.5; }
.app-notice.error { color: var(--error); background: #fbecee; }
.composer { display: grid; overflow: hidden; border: 1px solid var(--control-line); border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
.composer:focus-within { border-color: #7d90c3; box-shadow: 0 0 0 3px rgb(77 99 170 / 7%), var(--shadow); }
.composer textarea { width: 100%; min-height: 61px; max-height: 30dvh; resize: vertical; padding: 13px 15px 4px; border: 0; background: transparent; font-size: 16px; line-height: 1.5; }
.composer textarea:focus-visible { outline: 0; }
.composer-bottom { display: flex; align-items: center; gap: 8px; min-height: 49px; padding: 4px 8px 8px 15px; }
.keyboard-hint, .message-count { color: var(--muted); font-size: 11px; }
.message-count { margin-left: auto; }
.send-button, .stop-button { min-width: 39px; min-height: 39px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; margin-left: auto; padding: 7px; border: 1px solid transparent; border-radius: 11px; background: var(--accent); color: var(--surface); }
.send-button:disabled { opacity: 1; background: var(--raised); color: var(--muted); }
.send-button:focus-visible, .stop-button:focus-visible { outline-offset: -3px; outline-color: currentColor; }
.message-count:not([hidden]) + .send-button { margin-left: 0; }
.stop-button { padding-inline: 11px; border-color: var(--control-line); background: var(--surface); color: var(--ink); font-size: 13px; font-weight: 650; }
.stop-button:hover { background: var(--raised); }
.stop-button span { width: 9px; height: 9px; background: currentColor; border-radius: 2px; }
.privacy { margin: 9px 0 0; text-align: center; }
.confirm-dialog { width: min(calc(100% - 28px), 420px); padding: 22px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); background: var(--surface); box-shadow: var(--shadow); }
.confirm-dialog::backdrop { background: rgb(24 25 30 / 46%); }
.confirm-dialog h2 { margin: 0 0 8px; font-size: 19px; }
.confirm-dialog p { margin: 0; color: var(--muted); font-size: 14px; }
.dialog-actions { display: flex; justify-content: end; gap: 8px; margin-top: 20px; }

.admin-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 16px max(16px, calc((100vw - 1050px) / 2)); background: var(--surface); border-bottom: 1px solid var(--line); }
.admin-header .plain-button { border-color: var(--sidebar-line); background: var(--sidebar-surface); color: var(--sidebar-ink); }
.admin-header .plain-button:hover { border-color: var(--accent-light); }
.admin-main { max-width: 1082px; margin-inline: auto; padding: 34px 16px 55px; }
.admin-main h1 { margin: 0 0 9px; color: var(--ink); font: 500 36px/1.25 var(--sans); letter-spacing: -.025em; }
.admin-main h2 { margin: 0 0 14px; color: var(--ink); font-size: 19px; }
.admin-main h3 { margin: 0 0 6px; color: var(--ink); font-size: 15px; }
.admin-intro { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.admin-panel { min-width: 0; padding: clamp(15px, 3vw, 25px); margin: 0 0 16px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.admin-auth { max-width: 470px; }
.admin-form { display: grid; gap: 10px; }
.admin-form > .primary-button { justify-self: start; }
.admin-field { display: grid; gap: 5px; min-width: 0; }
.admin-fields { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.admin-form fieldset { display: flex; flex-wrap: wrap; gap: 6px 18px; margin: 0; padding: 3px 0; border: 0; }
.admin-form legend { margin-bottom: 4px; padding: 0; font-size: 14px; font-weight: 630; }
.admin-form fieldset label { display: inline-flex; align-items: center; gap: 5px; font-weight: 500; }
.admin-form input[type="checkbox"] { accent-color: var(--accent); min-height: 22px; }
.budget-grid { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.budget-card { padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper); }
.budget-card p { margin: 3px 0 0; font-size: 13px; }
.admin-table-wrap { overflow: auto; }
.admin-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 13px; }
.admin-table th, .admin-table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
.admin-table th { color: var(--muted); }
.admin-table td:first-child { min-width: 100px; max-width: 210px; overflow-wrap: anywhere; white-space: normal; }
.admin-table small { color: var(--muted); }
.admin-table .plain-button { min-height: 38px; }
.admin-panel-header { display: flex; justify-content: space-between; align-items: start; gap: 8px; }
.new-codes { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.codes-output { width: 100%; min-height: 90px; resize: vertical; padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); font: 14px/1.45 monospace; }

@media (max-width: 740px) {
  .gate { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr auto; }
  .gate-header { grid-area: auto; padding: 14px 18px; }
  .gate-card h1 { font-size: clamp(30px, 8vw, 48px); }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; z-index: 3; inset: 0 auto 0 0; width: min(310px, calc(100vw - 45px)); visibility: hidden; transform: translateX(-100%); }
  .sidebar.is-open { visibility: visible; transform: translateX(0); }
  .sidebar-overlay:not([hidden]) { display: block !important; position: fixed; z-index: 2; inset: 0; width: 100%; padding: 0; border: 0; background: rgb(24 25 30 / 46%); }
  .close-sidebar, .open-sidebar { display: inline-flex !important; }
  .chat-header { padding: 8px 10px; }
  .welcome { padding-top: clamp(24px, 8dvh, 68px); }
  .action-label { display: none; }
}

@media (max-width: 510px) {
  .welcome h1 { font-size: clamp(31px, 9.4vw, 46px); }
  .hero-copy { font-size: 14px; }
  .examples { grid-template-columns: minmax(0, 1fr); gap: 6px; }
  .example { min-height: 42px; grid-template-columns: auto 1fr auto; gap: 9px; padding: 8px 10px; }
  .example-category { grid-column: auto; min-width: 2.3em; }
  .model-group label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .model-group select { width: min(43vw, 160px); }
  .keyboard-hint { display: none; }
  .privacy { font-size: 11px; }
  .admin-fields { grid-template-columns: minmax(0, 1fr); }
}

@media (forced-colors: active) {
  .model-group select { appearance: auto; background-image: none; }
  .composer:focus-within { outline: 2px solid Highlight; }
  .chat-list button[aria-current="true"] { border-color: Highlight; }
}
