:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #151922;
  --muted: #626b79;
  --line: #d9dee7;
  --line-strong: #c6ceda;
  --primary: #166534;
  --primary-strong: #0f4a27;
  --primary-soft: #e8f5ed;
  --accent: #1d4ed8;
  --accent-soft: #e8f0ff;
  --danger: #b42318;
  --danger-soft: #fdebea;
  --warning: #915f00;
  --warning-soft: #fff4d8;
  --meta-bg: #f5f0ff;
  --meta-line: #c7b9f2;
  --meta-text: #38236d;
  --shadow: 0 18px 50px rgba(24, 32, 46, 0.08);
  --radius: 8px;
  --header-h: 68px;
  --composer-h: 120px;
}

* { box-sizing: border-box; }
html {
  min-height: 100%;
  background: var(--bg);
}
body { min-height: 100%; }

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.78), rgba(246,247,249,0.92)),
    var(--bg);
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: var(--text);
  font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

button, textarea { font: inherit; }
button { border: 0; cursor: pointer; }
button:disabled { cursor: not-allowed; }
svg { width: 18px; height: 18px; stroke-width: 2; }

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 0 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  display: grid;
  place-items: center;
  border: 1px solid #9ec5aa;
  border-radius: var(--radius);
  background: var(--primary-soft);
  color: var(--primary);
}

.brand-copy { min-width: 0; }
.brand-title {
  margin: 0;
  overflow: hidden;
  color: #101623;
  font-size: 16px;
  font-weight: 760;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.brand-subtitle {
  margin: 1px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 190px;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}
.status-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 0 0 4px var(--primary-soft);
}

.layout {
  display: grid;
  min-height: calc(100vh - var(--header-h));
}
.chat-shell {
  display: grid;
  grid-template-rows: 1fr auto;
  min-height: calc(100vh - var(--header-h));
}
.messages {
  overflow-y: auto;
  padding: 30px 18px 24px;
  scroll-behavior: smooth;
}
.messages-inner {
  width: min(980px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 14px;
}

.empty-state {
  min-height: calc(100vh - var(--header-h) - var(--composer-h) - 90px);
  display: grid;
  align-content: center;
  gap: 18px;
  padding: 20px 0;
}
.empty-title {
  max-width: 720px;
  margin: 0;
  font-size: clamp(30px, 5vw, 52px);
  line-height: 1.04;
  letter-spacing: 0;
  font-weight: 780;
}
.empty-text {
  max-width: 620px;
  margin: 0;
  color: var(--muted);
  font-size: 17px;
}
.message {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 12px;
  align-items: flex-start;
}
.message.user {
  grid-template-columns: minmax(0, 1fr) 38px;
}
.message.user .avatar {
  grid-column: 2;
  background: #edf2f7;
  color: #293344;
}
.message.user .bubble {
  grid-column: 1;
  justify-self: end;
  max-width: min(760px, 100%);
  background: #e9eef5;
  border-color: #d4dbe6;
}
.message.user .bubble-head {
  justify-content: flex-end;
}

.avatar {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--accent);
}
.message.meta .avatar {
  border-color: var(--meta-line);
  background: var(--meta-bg);
  color: var(--meta-text);
}
.bubble {
  min-width: 0;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 10px 28px rgba(24, 32, 46, 0.05);
}
.message.meta .bubble {
  border-color: var(--meta-line);
  background: var(--meta-bg);
}
.bubble-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.message.meta .bubble-head { color: var(--meta-text); }
.bubble-text {
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-text .katex {
  white-space: normal;
  word-break: normal;
}
.bubble-text .katex-display {
  margin: 0.8em 0;
  overflow-x: auto;
  overflow-y: hidden;
}
.bubble-text .katex-display > .katex {
  display: inline-block;
  max-width: 100%;
}
.typing-caret::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 1.1em;
  margin-left: 3px;
  vertical-align: -0.18em;
  background: currentColor;
  animation: blink 0.8s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.score-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0;
}
.score-card {
  min-width: 0;
  padding: 10px;
  border: 1px solid rgba(88, 65, 151, 0.22);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.54);
}
.score-label {
  color: #665b86;
  font-size: 12px;
}
.score-value {
  margin-top: 4px;
  color: var(--meta-text);
  font-size: 22px;
  font-weight: 780;
}
.meta-list {
  margin: 10px 0 0;
  padding-left: 18px;
}
.meta-list li { margin: 4px 0; }

.notice {
  display: none;
  width: min(980px, calc(100% - 36px));
  margin: 0 auto 10px;
  padding: 10px 12px;
  border: 1px solid var(--warning);
  border-radius: var(--radius);
  background: var(--warning-soft);
  color: #4d3300;
}
.notice.is-visible { display: block; }
.notice.error {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: #5f1510;
}

.composer {
  border-top: 1px solid var(--line);
  background: rgba(246, 247, 249, 0.92);
  backdrop-filter: blur(12px);
  padding: 14px 18px 18px;
}
.composer-inner {
  width: min(980px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 10px;
}
.mode-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 38px;
}
.mode-indicator {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}
.mode-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 30px;
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: #2d3646;
  font-weight: 650;
}
.mode-pill.meta {
  border-color: var(--meta-line);
  background: var(--meta-bg);
  color: var(--meta-text);
}
.meta-button {
  display: none;
  min-height: 38px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--meta-line);
  border-radius: var(--radius);
  background: var(--meta-bg);
  color: var(--meta-text);
  font-weight: 680;
  white-space: nowrap;
}
.meta-button.is-visible { display: inline-flex; }
.meta-button:hover { background: #fbf9ff; }
.meta-button:disabled { opacity: 0.62; }

.input-frame {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.chat-input {
  width: 100%;
  min-height: 36px;
  max-height: 180px;
  resize: none;
  overflow-y: hidden;
  border: 0;
  outline: 0;
  padding: 6px 2px;
  color: var(--text);
  background: transparent;
  line-height: 1.5;
}
.chat-input::placeholder { color: #7a8494; }
.chat-input.input-error { color: var(--danger); }

.send-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  background: var(--primary);
  color: white;
}
.send-button:hover { background: var(--primary-strong); }
.send-button:disabled {
  background: #d4dbe6;
  color: #7a8494;
}
.send-button.stop { background: var(--danger); }
.send-button.stop:hover { background: #8f1d14; }
.hint-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
}
.hint-row .too-long { color: var(--danger); font-weight: 700; }
.kbd {
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: #3b4656;
  font-size: 11px;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

@media (max-width: 760px) {
  :root { --header-h: 74px; }
  .topbar { padding: 0 14px; gap: 10px; }
  .brand-title { font-size: 14px; }
  .brand-subtitle, .status-strip { font-size: 11px; }
  .status-strip { min-width: auto; }
  .status-text { display: none; }
  .messages { padding: 20px 12px 18px; }
  .empty-title { font-size: 32px; }
  .empty-text { font-size: 15px; }
  .message, .message.user {
    grid-template-columns: 32px minmax(0, 1fr);
    gap: 9px;
  }
  .message.user .avatar { grid-column: 1; }
  .message.user .bubble {
    grid-column: 2;
    justify-self: stretch;
  }
  .message.user .bubble-head { justify-content: flex-start; }
  .avatar { width: 32px; height: 32px; }
  .bubble { padding: 12px; }
  .score-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .composer { padding: 10px 12px 14px; }
  .mode-actions {
    align-items: stretch;
    flex-direction: column;
  }
  .mode-indicator, .meta-button { width: 100%; }
  .meta-button { white-space: normal; }
  .hint-row { display: none; }
}
