:root {
  --bg: #f4f2ee; --card: #ffffff; --ink: #1d232b; --muted: #6b7380; --line: #e2ded6;
  --accent: #c0392b; --accent-ink: #fff; --blue: #1f5fa8; --me: #a14d00; --me-bg: #fff3e6;
  --bot-bg: #eef3fa; --ok: #2e7d32; --warn: #b26a00; --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #15181c; --card: #1f2329; --ink: #e8e6e3; --muted: #9aa3ad; --line: #333941;
    --accent: #e0574a; --blue: #7fb0ec; --me: #ffb36b; --me-bg: #3a2a18; --bot-bg: #1d2a3a; --shadow: none; }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
h1 { font-size: 1.6rem; margin: 0 0 .3rem; }
h2 { font-size: 1.15rem; margin: 0 0 .6rem; }
a { color: var(--blue); }
.muted { color: var(--muted); }
.error { color: var(--accent); min-height: 1.2em; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); margin: 0 0 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.between { justify-content: space-between; }
button, .button, select, input, textarea { font: inherit; color: inherit; }
button, .button { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; min-height: 42px; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.ghost { background: transparent; }
.small { padding: 5px 10px; min-height: 34px; font-size: .9rem; }
.tiny { padding: 2px 8px; min-height: 26px; font-size: .8rem; }
input, select, textarea { border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; background: var(--card); width: 100%; }
label { display: block; font-size: .92rem; color: var(--muted); }
label > input, label > select { margin-top: 4px; color: var(--ink); }
.chip { font-size: .85rem; padding: 4px 10px; border-radius: 99px; background: var(--bg); border: 1px solid var(--line); white-space: nowrap; }
.chip.over { background: var(--accent); color: #fff; border-color: var(--accent); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: 12px; }

/* header + tabs */
.top { position: sticky; top: 0; z-index: 5; display: flex; gap: 8px; align-items: center; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--card); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; font-size: 1.15rem; margin-right: auto; color: var(--accent); }
.top select { width: auto; padding: 6px 8px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; padding: 8px 12px 0; scrollbar-width: none; }
.tabs button { border: none; border-bottom: 3px solid transparent; border-radius: 0; background: none; padding: 8px 10px; min-height: 40px; white-space: nowrap; color: var(--muted); }
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.banner { margin: 8px 12px 0; padding: 10px 12px; border-radius: 10px; background: #fdecea; color: #8a1c12; border: 1px solid #f5c2bc; }
@media (prefers-color-scheme: dark) { .banner { background: #3a1d1a; color: #ffb4a9; border-color: #5c2a24; } }
.view { padding: 12px; max-width: 1400px; margin: 0 auto; }

/* modes */
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
@media (min-width: 720px) { .modes { grid-template-columns: repeat(6, 1fr); } }
.modes button.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.modes .accent { border-style: dashed; }
.setup { display: grid; gap: 10px; }
.setup-pane { display: grid; gap: 10px; }
.resume label { flex: 1; }

/* workspace: chat + pdf */
.workspace { display: grid; gap: 12px; }
.ws-tabs { display: flex; gap: 6px; }
.ws-tabs button.active { border-color: var(--accent); color: var(--accent); }
.chat-pane, .pdf-pane { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0; }
.chat-pane { height: calc(100dvh - 190px); min-height: 420px; }
.pdf-pane { height: calc(100dvh - 190px); min-height: 420px; }
.workspace[data-pane="chat"] .pdf-pane, .workspace[data-pane="pdf"] .chat-pane { display: none; }
.workspace.no-pdf .pdf-pane, .workspace.no-pdf .ws-tabs { display: none !important; }
.workspace.no-pdf .chat-pane { display: flex !important; }
@media (min-width: 1000px) {
  .workspace { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .workspace.no-pdf { grid-template-columns: minmax(0, 1fr); max-width: 900px; margin: 0 auto; width: 100%; }
  .ws-tabs { display: none; }
  .workspace[data-pane] .pdf-pane, .workspace[data-pane] .chat-pane { display: flex; }
  .ws-tabs { grid-column: 1 / -1; }
}
.chat-head, .pdf-head { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdf-pane iframe { flex: 1; border: 0; width: 100%; border-radius: 0 0 var(--radius) var(--radius); background: #888; }
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; }
.msg.user { align-self: flex-end; }
.msg .bubble { padding: 10px 12px; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--me-bg); white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--bot-bg); }
.msg.user .msg-tools { display: none; }
.msg-tools { text-align: right; margin-top: 2px; }
.msg.streaming .bubble { white-space: pre-wrap; }
.msg.status .bubble { background: none; color: var(--muted); font-style: italic; padding: 2px 4px; }
.bubble table, .md table, .itog table { border-collapse: collapse; width: 100%; font-size: .93rem; display: block; overflow-x: auto; }
.bubble th, .bubble td, .md th, .md td, .itog th, .itog td { border: 1px solid var(--line); padding: 4px 6px; text-align: left; vertical-align: top; }
.bubble p, .md p { margin: .35em 0; }
.bubble pre, .md pre { white-space: pre-wrap; background: rgba(127,127,127,.12); padding: 8px; border-radius: 8px; }
.composer { border-top: 1px solid var(--line); padding: 8px; display: grid; gap: 6px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer textarea { resize: vertical; min-height: 48px; max-height: 40vh; }
.composer-actions { display: flex; justify-content: space-between; gap: 8px; }
.file-btn { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; color: var(--ink); }
.attach-preview { display: flex; align-items: center; gap: 8px; }
.attach-preview img { max-height: 70px; border-radius: 6px; }
.itog { padding: 12px; border-top: 1px solid var(--line); max-height: 50vh; overflow-y: auto; }
.itog .saved { color: var(--ok); font-weight: 600; }

/* voice */
.voice-card { display: grid; gap: 12px; max-width: 900px; margin: 0 auto; }
.switch { display: flex; gap: 8px; align-items: center; color: var(--ink); }
.switch input { width: auto; }
.voice-main { display: flex; gap: 18px; align-items: center; }
.voice-btn { width: 120px; height: 120px; border-radius: 50%; font-size: 1.2rem; font-weight: 700; background: var(--accent); color: #fff; border: none; flex: none; box-shadow: 0 4px 14px rgba(192,57,43,.35); }
.voice-btn.live { background: #333; box-shadow: 0 0 0 0 rgba(192,57,43,.6); animation: pulse 1.6s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(192,57,43,.55); } 70% { box-shadow: 0 0 0 18px rgba(192,57,43,0); } 100% { box-shadow: 0 0 0 0 rgba(192,57,43,0); } }
@media (prefers-reduced-motion: reduce) { .voice-btn.live { animation: none; } }
.voice-meta { flex: 1; display: grid; gap: 6px; }
.voice-status { font-weight: 600; }
.voice-timer { font-variant-numeric: tabular-nums; font-size: 1.6rem; }
.meter { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; border: 1px solid var(--line); }
.meter span { display: block; height: 100%; width: 0; background: var(--ok); transition: width .1s; }
.ptt { width: 100%; min-height: 72px; font-size: 1.1rem; font-weight: 600; user-select: none; -webkit-user-select: none; touch-action: none; }
.ptt.down { background: var(--accent); color: #fff; }
.legend { font-size: .85rem; display: flex; gap: 14px; flex-wrap: wrap; }
.legend .me { color: var(--me); } .legend .bot { color: var(--blue); }
.subs { min-height: 160px; max-height: 45vh; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding: 8px; background: var(--bg); border-radius: 10px; }
.sub { padding: 6px 10px; border-radius: 10px; max-width: 92%; overflow-wrap: anywhere; }
.sub.me { background: var(--me-bg); color: var(--me); align-self: flex-end; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .95rem; }
.sub.bot { background: var(--bot-bg); align-self: flex-start; }
.sub.pending { opacity: .65; }

/* diary */
.cat { margin-bottom: 12px; }
.cat h3 { font-size: 1rem; margin: 8px 0 4px; }
.mistake { display: grid; grid-template-columns: 1fr auto; gap: 6px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.mistake .w { text-decoration: line-through; color: var(--accent); }
.mistake .c { color: var(--ok); font-weight: 600; }
.mistake .r { color: var(--muted); font-size: .9rem; }
.count { font-variant-numeric: tabular-nums; background: var(--bg); border-radius: 99px; padding: 0 8px; font-size: .85rem; }
.grid-form { display: grid; gap: 8px; margin-top: 8px; }
@media (min-width: 720px) { .grid-form { grid-template-columns: 1.2fr 1fr 1fr 1.4fr auto; } }
.journal { list-style: none; margin: 0; padding: 0; }
.journal li { padding: 8px 0; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.journal .when { color: var(--muted); font-size: .85rem; }
.edit-row { display: grid; gap: 6px; grid-column: 1 / -1; }

/* anki */
.cards { width: 100%; border-collapse: collapse; }
.cards td, .cards th { border-bottom: 1px solid var(--line); padding: 6px; text-align: left; vertical-align: top; }
@media (max-width: 640px) {
  .cards thead { display: none; }
  .cards tr { display: grid; grid-template-columns: 1fr auto; padding: 6px 0; border-bottom: 1px solid var(--line); }
  .cards td { border: none; padding: 2px 4px; }
  .cards td:nth-child(1) { font-weight: 600; }
  .cards td:nth-child(4) { grid-row: 1; grid-column: 2; }
}
.md h1, .md h2, .md h3 { font-size: 1.05rem; }
@media (max-width: 520px) {
  .top { gap: 6px; padding-left: 10px; padding-right: 10px; }
  .brand { font-size: 1.05rem; }
  .top select { padding: 5px 4px; font-size: .9rem; }
  .chip { font-size: .78rem; padding: 3px 8px; }
  .chip .long { display: none; }
  .top .small { padding: 4px 8px; font-size: .82rem; }
  .tabs { gap: 0; padding: 6px 6px 0; }
  .tabs button { padding: 8px 7px; font-size: .95rem; }
  .view { padding: 10px; }
  .voice-btn { width: 96px; height: 96px; }
}
/* engine labels, walkie-talkie */
.engine-tag { display: inline-block; font-size: .72rem; color: var(--muted); border: 1px solid var(--line); border-radius: 99px; padding: 0 7px; margin-right: 6px; }
.msg-tools { display: flex; justify-content: flex-end; align-items: center; gap: 4px; }
.engine-choice { display: grid; gap: 6px; }
.engine-choice label { display: flex; gap: 8px; align-items: center; }
.engine-choice input { width: auto; }
.talk { touch-action: none; }
.talk.recording { background: var(--accent); color: #fff; }
.talk.busy { opacity: .6; }
.small-print { font-size: .82rem; margin: 0; }
.sub .engine-tag { margin-left: 6px; }
