:root {
  --bg: #F6F1E9;
  --surface: #FFFDF9;
  --ink: #1F1B16;
  --muted: #6E655A;
  --line: #E4DCCF;
  --accent: #B5532E;
  --accent-ink: #FFFFFF;
  --accent-soft: #F3E1D6;
  --user-bubble: #E9E0D2;
  --danger: #A8322A;
  --radius: 18px;
  --fs: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1B1814; --surface: #25211C; --ink: #F2ECE3; --muted: #A99F92;
    --line: #3A342C; --accent: #E07A50; --accent-ink: #1B1814; --accent-soft: #3B2A21;
    --user-bubble: #322C25; --danger: #F08A80; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1B1814; --surface: #25211C; --ink: #F2ECE3; --muted: #A99F92;
  --line: #3A342C; --accent: #E07A50; --accent-ink: #1B1814; --accent-soft: #3B2A21;
  --user-bubble: #322C25; --danger: #F08A80; color-scheme: dark;
}
html.big { --fs: 21px; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 400 var(--fs)/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; }

/* Кнопки */
.btn {
  border: 0; border-radius: 14px; padding: 16px 20px; min-height: 58px;
  font-weight: 600; cursor: pointer;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-wide { width: 100%; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer; font-size: 1.3em;
  min-width: 52px; min-height: 52px; border-radius: 14px; color: var(--ink);
}
.icon-btn:active, .btn:active { transform: scale(.97); }
.icon-btn.round { border-radius: 50%; background: var(--surface); border: 1px solid var(--line); flex: none; }
.icon-btn.send { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.icon-btn.mic.on { background: var(--danger); color: #fff; border-color: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 10px color-mix(in srgb, var(--danger) 20%, transparent); } }

/* Вход */
.login-wrap { width: 100%; max-width: 460px; margin: auto; padding: 32px 16px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.brand-mark { color: var(--accent); font-size: 2em; }
.brand h1 { font-size: 1.8em; margin: 0; }
.lead { color: var(--muted); margin: 0 0 20px; font-size: 1.1em; }
.user-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
.user-tile {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px 12px; text-align: center; cursor: pointer; font-weight: 600; font-size: 1.1em;
}
.user-tile .av { display: block; font-size: 2.2em; margin-bottom: 6px; }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.login-who { font-size: 1.4em; font-weight: 700; margin: 0 0 8px; }
.label { color: var(--muted); }
#login-pass {
  padding: 16px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface);
  font-size: 1.2em; min-height: 58px;
}
#login-pass:focus { outline: none; border-color: var(--accent); }
.error { color: var(--danger); min-height: 1.5em; margin: 0; }

/* Шапка */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.hello { flex: 1; margin: 0 4px; font-size: 1.35em; }
.chat-title { flex: 1; margin: 0; font-size: 1.1em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-select { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 8px; font-size: .8em; max-width: 40%; }

/* Главная */
.home-main { width: 100%; max-width: 720px; margin: 0 auto; padding: 16px; }
.preset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.preset {
  display: flex; align-items: center; gap: 14px; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; min-height: 84px; cursor: pointer;
}
.preset .pi { font-size: 2em; flex: none; width: 52px; height: 52px; display: grid; place-items: center; background: var(--accent-soft); border-radius: 14px; }
.preset .pt { font-weight: 700; font-size: 1.1em; display: block; }
.preset .ps { color: var(--muted); font-size: .88em; display: block; }
.section-title { font-size: 1em; color: var(--muted); margin: 28px 4px 8px; font-weight: 600; }
.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.history li { display: flex; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.history .hopen { flex: 1; display: flex; gap: 12px; align-items: center; border: 0; background: none; text-align: left; padding: 14px; min-height: 60px; cursor: pointer; min-width: 0; }
.history .htitle { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.history .hdate { color: var(--muted); font-size: .8em; display: block; }
.history .hdel { color: var(--muted); font-size: 1em; }

/* Чат */
.chat-screen { height: 100dvh; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 16px 12px 8px; display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 780px; margin: 0 auto; }
.msg { max-width: 92%; padding: 12px 16px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.notice { align-self: center; color: var(--muted); font-size: .9em; background: none; text-align: center; }
.msg.err { align-self: center; color: var(--danger); border: 1px solid var(--danger); background: none; }
.msg p { margin: 0 0 .6em; } .msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 0 0 .6em; padding-left: 1.3em; }
.msg h3, .msg h4 { margin: .4em 0; font-size: 1.05em; }
.msg pre { background: var(--bg); padding: 10px; border-radius: 10px; overflow-x: auto; font-size: .85em; }
.msg code { font-family: ui-monospace, monospace; font-size: .9em; }
.msg a { color: var(--accent); }
.msg img.thumb { max-width: 180px; max-height: 180px; border-radius: 10px; display: block; margin-bottom: 6px; }
.msg .file-chip { display: inline-block; background: var(--bg); border-radius: 8px; padding: 4px 10px; font-size: .85em; margin-bottom: 6px; }
.msg-actions { display: flex; gap: 4px; margin-top: 6px; }
.msg-actions button { white-space: nowrap; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 6px 12px; font-size: .85em; cursor: pointer; min-height: 40px; }
.typing { color: var(--muted); font-style: italic; }

.attachments { display: flex; gap: 8px; padding: 0 12px; flex-wrap: wrap; max-width: 780px; margin: 0 auto; width: 100%; }
.attachments:empty { display: none; }
.att { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 6px; font-size: .8em; display: flex; align-items: center; gap: 6px; }
.att img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; }
.att button { border: 0; background: var(--danger); color: #fff; border-radius: 50%; width: 28px; height: 28px; cursor: pointer; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg);
  width: 100%; max-width: 780px; margin: 0 auto;
}
#input {
  flex: 1; min-width: 0; resize: none; max-height: 40vh; padding: 13px 14px; min-height: 52px;
  border-radius: 16px; border: 2px solid var(--line); background: var(--surface); line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--accent); }

.btn.small { min-height: 44px; padding: 8px 14px; font-size: .85em; }
.pass-row { display: flex; gap: 8px; align-items: stretch; }
.pass-row #login-pass { flex: 1; min-width: 0; }
.pass-toggle { flex: none; border: 2px solid var(--line); }
