:root {
  --bg: #f6f4ee; --surface: #ffffff; --surface-2: #ece8dd; --text: #1d2a26; --muted: #63716b;
  --border: #dcd7ca; --primary: #1f6f5c; --primary-text: #ffffff; --accent: #e9a23b;
  --bubble: #dfeee8; --danger: #b3261e; --notice: #fff6df; --notice-border: #edd9a6;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #131a18; --surface: #1c2623; --surface-2: #24312d; --text: #e6efe9; --muted: #9aaba4;
  --border: #2e3d38; --primary: #3fa58a; --primary-text: #0c1512; --accent: #f0b65c;
  --bubble: #25423a; --danger: #ff8a80; --notice: #2c2a1c; --notice-border: #4d4625;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #131a18; --surface: #1c2623; --surface-2: #24312d; --text: #e6efe9; --muted: #9aaba4;
    --border: #2e3d38; --primary: #3fa58a; --primary-text: #0c1512; --accent: #f0b65c;
    --bubble: #25423a; --danger: #ff8a80; --notice: #2c2a1c; --notice-border: #4d4625;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, textarea, input, select { font: inherit; color: inherit; }
a { color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); font-weight: 600; }
.btn.small { min-height: 36px; padding: 0 10px; font-size: 14px; border-radius: 10px; }
.btn.wide { width: 100%; }
.icon-btn {
  flex: none; width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:active, .btn:active { opacity: .7; }
.icon-btn.send { background: var(--primary); color: var(--primary-text); border-radius: 50%; }
.icon-btn.send:disabled { opacity: .4; }
.icon-btn.stop { background: var(--danger); }

/* Sign-in */
.login {
  min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 24px; text-align: center;
}
.login h1 { margin: 6px 0 0; font-size: 32px; }
.login p { margin: 0; color: var(--muted); max-width: 30em; }
.login .btn { margin-top: 14px; }
.login-error { color: var(--danger) !important; font-weight: 600; }
.fine { font-size: 13px; margin-top: 18px !important; }

/* Layout */
.app { position: fixed; top: 0; right: 0; bottom: 0; left: 0; display: flex; }
.sidebar {
  width: 290px; flex: none; display: flex; flex-direction: column; gap: 10px;
  padding: 12px; padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--surface-2); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; padding: 2px 4px; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; }
.chat-item { display: flex; align-items: center; border-radius: 10px; }
.chat-item.active { background: var(--surface); }
.chat-item a {
  flex: 1; min-width: 0; padding: 11px 8px; color: var(--text); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item button {
  flex: none; width: 40px; height: 40px; border: 0; background: transparent; color: var(--muted);
  font-size: 20px; line-height: 1; cursor: pointer; border-radius: 10px;
}
.chat-empty { color: var(--muted); font-size: 14px; padding: 8px; }
.side-foot { border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.who { font-weight: 600; }
.quota { color: var(--muted); font-size: 13px; }
.side-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 4px; padding: 4px 6px;
  padding-top: calc(4px + env(safe-area-inset-top)); border-bottom: 1px solid var(--border); background: var(--bg);
}
.title { flex: 1; min-width: 0; text-align: center; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.messages { max-width: 760px; margin: 0 auto; padding: 16px 14px 24px; display: flex; flex-direction: column; gap: 16px; }

.notice {
  background: var(--notice); border: 1px solid var(--notice-border); border-radius: 14px;
  padding: 12px 14px; font-size: 15px;
}
.notice strong { display: block; margin-bottom: 2px; }
.msg.user { align-self: flex-end; max-width: 86%; background: var(--bubble); border-radius: 18px 18px 4px 18px; padding: 10px 14px; }
.msg.user .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { align-self: stretch; overflow-wrap: anywhere; }
.msg .photos { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg .photos img { width: 120px; height: 120px; object-fit: cover; border-radius: 10px; cursor: zoom-in; background: var(--surface-2); }
.msg.error { color: var(--danger); background: transparent; border: 1px solid var(--danger); border-radius: 12px; padding: 10px 14px; }

.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 .7em; }
.md ul, .md ol { padding-left: 1.4em; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.05em; margin: 1em 0 .4em; }
.md code { background: var(--surface-2); border-radius: 5px; padding: 1px 5px; font-size: .92em; }
.md pre { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 10px; color: var(--muted); }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.figure { margin: .4em 0 .8em; color: var(--text); }
.figure svg { display: block; width: 100%; max-width: 440px; height: auto; max-height: 420px; }
.figure.drawing { color: var(--muted); font-size: 14px; }
figure.pic { margin: .4em 0 .8em; }
figure.pic img { display: block; max-width: 100%; max-height: 360px; border-radius: 12px; cursor: zoom-in; background: var(--surface-2); }
figure.pic figcaption { margin-top: 4px; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.typing::after { content: "▍"; color: var(--primary); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.update {
  display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--notice); border-bottom: 1px solid var(--notice-border); font-size: 14px;
}
.update:not([hidden]) + .topbar { padding-top: 4px; }

/* Mode picker, tests */
.modes { display: flex; flex-direction: column; gap: 8px; }
.mode-card, .option {
  display: flex; flex-direction: column; gap: 2px; text-align: left; width: 100%; cursor: pointer;
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text);
}
.mode-card strong { font-size: 16px; }
.mode-card span { color: var(--muted); font-size: 14px; }
.mode-card.active, .option.selected { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); background: var(--bubble); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel h2 { margin: 0; font-size: 17px; }
.panel input[type=text], .panel select, .panel textarea {
  width: 100%; min-height: 44px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg); color: var(--text);
}
.panel textarea { min-height: 96px; resize: vertical; }
.rows { display: flex; flex-direction: column; gap: 8px; }
.row-item { display: flex; flex-direction: column; gap: 6px; padding: 10px 0; border-top: 1px solid var(--border); }
.row-item:first-child { border-top: 0; padding-top: 0; }
.row-item .sub { color: var(--muted); font-size: 13px; }
.row-item .actions { display: flex; gap: 8px; flex-wrap: wrap; }
a.row-item { color: var(--text); text-decoration: none; }
.progress { color: var(--muted); font-size: 14px; }
.quiz-nav { display: flex; justify-content: space-between; gap: 8px; }
.waiting { text-align: center; color: var(--muted); padding: 40px 10px; }
.waiting::after { content: "▍"; color: var(--primary); animation: blink 1s steps(2) infinite; }
.score { font-size: 28px; font-weight: 700; }
.verdict { font-weight: 700; }
.verdict.correct { color: var(--primary); } .verdict.partial { color: var(--accent); } .verdict.wrong { color: var(--danger); }
.answer-line { font-size: 14px; color: var(--muted); }
.answer-line b { color: var(--text); font-weight: 600; }

/* Composer */
.composer {
  position: relative; max-width: 760px; width: 100%; margin: 0 auto;
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom));
}
.composer .row {
  display: flex; align-items: flex-end; gap: 4px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 24px;
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: transparent;
  padding: 10px 4px; max-height: 160px; line-height: 1.4; overflow-y: hidden;
}
.note { margin: 6px 4px 0; font-size: 12px; line-height: 1.3; color: var(--muted); text-align: center; }
.previews { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 4px 6px; }
.previews:empty { display: none; }
.preview { position: relative; width: 64px; height: 64px; }
.preview img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; }
.preview.loading img { opacity: .4; }
.preview button {
  position: absolute; top: -8px; right: -8px; width: 26px; height: 26px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); line-height: 1; cursor: pointer;
}
.attach-menu {
  position: absolute; bottom: 100%; left: 10px; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18); z-index: 5;
}
.attach-menu button { border: 0; background: transparent; padding: 14px 18px; text-align: left; cursor: pointer; }
.attach-menu button + button { border-top: 1px solid var(--border); }

.viewer {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 30; background: rgba(0, 0, 0, .9);
  display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.viewer img { max-width: 100%; max-height: 100%; }

/* Phone: the sidebar becomes a drawer */
@media (max-width: 800px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 20; width: 84%; max-width: 320px;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .app.drawer .sidebar { transform: none; }
  .app.drawer .scrim { display: block; position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 15; background: rgba(0, 0, 0, .4); }
}
@media (min-width: 801px) {
  #menu { visibility: hidden; }
}
