/* Ролка — оформление в стиле читалки sChan:
   светлая тема — тёплый пепельный и глина, тёмная — почти чёрный и песочное золото. */

:root,
:root[data-theme="light"] {
  --bg: #E6E2DD;
  --surface: #EEEBE7;
  --surface-2: #F4F2EF;
  --text: #262220;
  --text-soft: #4A4440;
  --muted: #6B625D;
  --accent: #836457;
  --accent-text: #F6F2EE;
  --accent-soft: #7A5A4E;
  --speech: #5E3F33;
  --action: #6B625D;
  --chip: rgba(131, 100, 87, 0.12);
  --line: rgba(38, 34, 32, 0.14);
  --shadow: 0 6px 18px rgba(38, 34, 32, 0.12);
  --overlay: rgba(20, 17, 15, 0.45);
  --danger: #A33A2A;
  --user-bg: rgba(131, 100, 87, 0.08);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #1B1B1A;
  --surface: #242322;
  --surface-2: #2C2B29;
  --text: #EEE8DF;
  --text-soft: #CFC6BA;
  --muted: #9A9187;
  --accent: #D3C0A3;
  --accent-text: #1B1B1A;
  --accent-soft: #D3C0A3;
  --speech: #E6D3B3;
  --action: #A69C90;
  --chip: rgba(211, 192, 163, 0.12);
  --line: rgba(238, 232, 223, 0.12);
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
  --overlay: rgba(0, 0, 0, 0.6);
  --danger: #E07A66;
  --user-bg: rgba(211, 192, 163, 0.06);
  color-scheme: dark;
}
:root[data-theme="cream"] {
  --bg: #F4EEE3;
  --surface: #FAF6EF;
  --surface-2: #FFFCF7;
  --text: #2B2622;
  --text-soft: #4D4640;
  --muted: #6E655D;
  --accent: #8A6A55;
  --accent-text: #FBF8F3;
  --accent-soft: #7A5A47;
  --speech: #61412F;
  --action: #6E655D;
  --chip: rgba(138, 106, 85, 0.12);
  --line: rgba(43, 38, 34, 0.14);
  --shadow: 0 6px 18px rgba(43, 38, 34, 0.1);
  --overlay: rgba(20, 17, 15, 0.45);
  --danger: #A33A2A;
  --user-bg: rgba(138, 106, 85, 0.08);
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #EAD9BE;
  --surface: #F1E3CC;
  --surface-2: #F6EAD6;
  --text: #3A2C1E;
  --text-soft: #54432F;
  --muted: #6B5741;
  --accent: #7C5636;
  --accent-text: #F7EEDF;
  --accent-soft: #6E4B2E;
  --speech: #5A3920;
  --action: #6B5741;
  --chip: rgba(124, 86, 54, 0.13);
  --line: rgba(58, 44, 30, 0.16);
  --shadow: 0 6px 18px rgba(58, 44, 30, 0.12);
  --overlay: rgba(30, 20, 10, 0.45);
  --danger: #9A2F1C;
  --user-bg: rgba(124, 86, 54, 0.08);
  color-scheme: light;
}
:root[data-theme="midnight"] {
  --bg: #12151C;
  --surface: #1A1F29;
  --surface-2: #212734;
  --text: #E3E6EE;
  --text-soft: #C3C8D4;
  --muted: #8C93A3;
  --accent: #A9B8DA;
  --accent-text: #12151C;
  --accent-soft: #A9B8DA;
  --speech: #CFD9F0;
  --action: #8C93A3;
  --chip: rgba(169, 184, 218, 0.12);
  --line: rgba(227, 230, 238, 0.1);
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  --overlay: rgba(0, 0, 0, 0.6);
  --danger: #E58B7A;
  --user-bg: rgba(169, 184, 218, 0.06);
  color-scheme: dark;
}

:root {
  --font-head: 'Cormorant Garamond', Georgia, serif;
  --font-ui: 'Oswald', 'Arial Narrow', sans-serif;
  --font-text: 'Literata', Georgia, serif;
  --font-plain: 'PT Sans', system-ui, sans-serif;
  --fs: 17px;
  --radius: 14px;
  --maxw: 860px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-plain);
  font-size: 16px;
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-soft); }
img { display: block; }
.hidden { display: none !important; }

/* ---------- Каркас ---------- */
.page {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 16px calc(96px + env(safe-area-inset-bottom));
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: calc(6px + env(safe-area-inset-top)) 12px 6px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar .title {
  flex: 1;
  min-width: 0;
  font-family: var(--font-head);
  font-size: 26px;
  font-weight: 500;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar .title i { color: var(--accent-soft); }
.topbar .sub {
  font-family: var(--font-ui);
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text-soft);
  cursor: pointer;
}
.icon-btn:hover { background: var(--chip); color: var(--text); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg, .btn svg { width: 20px; height: 20px; }
.icon-btn.sm { width: 32px; height: 32px; border-radius: 9px; }
.icon-btn.sm svg { width: 17px; height: 17px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-family: var(--font-ui);
  font-size: 14px;
  letter-spacing: .05em;
  text-transform: uppercase;
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--chip); }
.btn.danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.sm { min-height: 34px; padding: 0 14px; font-size: 13px; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; min-width: 0; }

.section-title {
  margin: 28px 0 12px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 10px;
}
.section-title::after { content: ''; flex: 1; height: 1px; background: var(--line); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
}
.empty .big {
  font-family: var(--font-head);
  font-size: 30px;
  color: var(--text);
  margin-bottom: 6px;
}

/* ---------- Аватарки ---------- */
.ava {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--chip);
  display: grid;
  place-items: center;
  font-family: var(--font-head);
  font-size: 22px;
  color: var(--accent-soft);
  overflow: hidden;
}
.ava img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- Главная ---------- */
.hero {
  padding: 26px 0 6px;
}
.hero h1 {
  margin: 0;
  font-family: var(--font-head);
  font-weight: 500;
  font-size: 44px;
  line-height: 1;
}
.hero h1 i { color: var(--accent-soft); }
.hero p { margin: 8px 0 0; color: var(--muted); }

.recent {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding: 2px 2px 10px;
  margin: 0 -16px;
  padding-left: 16px;
  padding-right: 16px;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
}
.recent::-webkit-scrollbar { display: none; }
.recent-item {
  scroll-snap-align: start;
  flex: none;
  width: 250px;
  display: flex;
  gap: 12px;
  padding: 12px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}
.recent-item .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recent-item .last {
  font-size: 13px;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.search {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 18px 0 10px;
}
.search input {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  outline: none;
}
.search input:focus { border-color: var(--accent); }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  padding: 4px 11px;
  border-radius: 999px;
  background: var(--chip);
  color: var(--text-soft);
  font-size: 13px;
  border: 1px solid transparent;
  cursor: pointer;
}
.tag.on { background: var(--accent); color: var(--accent-text); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.char-card {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  transition: transform .15s;
}
.char-card:hover { transform: translateY(-2px); }
.char-card .pic {
  aspect-ratio: 3 / 4;
  background: var(--chip);
  display: grid;
  place-items: center;
  font-family: var(--font-head);
  font-size: 64px;
  color: var(--accent-soft);
}
.char-card .pic img { width: 100%; height: 100%; object-fit: cover; }
.char-card .info { padding: 10px 12px 12px; }
.char-card .name {
  font-family: var(--font-head);
  font-size: 21px;
  font-weight: 600;
  line-height: 1.1;
}
.char-card .meta { font-size: 12px; color: var(--muted); margin-top: 3px; font-family: var(--font-ui); letter-spacing: .04em; }
.char-card .star {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--overlay);
  color: #F2D58B;
  display: grid;
  place-items: center;
}
.char-card .star svg { width: 15px; height: 15px; }

.fab {
  position: fixed;
  right: max(18px, calc(50% - var(--maxw) / 2 + 18px));
  bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 30;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.fab svg { width: 26px; height: 26px; }

/* ---------- Страница персонажа ---------- */
.char-hero {
  display: flex;
  gap: 18px;
  align-items: flex-end;
  padding: 22px 0 8px;
}
.char-hero .pic {
  flex: none;
  width: 132px;
  aspect-ratio: 3 / 4;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--chip);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  font-family: var(--font-head);
  font-size: 60px;
  color: var(--accent-soft);
}
.char-hero .pic img { width: 100%; height: 100%; object-fit: cover; }
.char-hero h1 {
  margin: 0 0 8px;
  font-family: var(--font-head);
  font-weight: 500;
  font-size: 38px;
  line-height: 1.05;
}
.desc-box {
  margin-top: 14px;
  padding: 14px 16px;
  font-family: var(--font-text);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-soft);
  white-space: pre-wrap;
  max-height: 180px;
  overflow: hidden;
  position: relative;
}
.desc-box.open { max-height: none; }
.desc-box:not(.open)::after {
  content: '';
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 60px;
  background: linear-gradient(transparent, var(--surface));
}
.list { display: flex; flex-direction: column; gap: 10px; }
.list-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 12px 12px 16px;
  cursor: pointer;
}
.list-item .t { font-weight: 700; }
.list-item .s {
  font-size: 13px;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- Формы ---------- */
.form { display: flex; flex-direction: column; gap: 16px; margin-top: 16px; }
.field label, .field .label {
  display: block;
  margin-bottom: 6px;
  font-family: var(--font-ui);
  font-size: 13px;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--muted);
}
.field .hint { font-size: 13px; color: var(--muted); margin-top: 5px; }
.field input[type=text], .field input[type=number], .field input[type=url], .field input:not([type]),
.field select, .field textarea {
  width: 100%;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  outline: none;
}
.field textarea {
  min-height: 90px;
  resize: vertical;
  font-family: var(--font-text);
  font-size: 15px;
  line-height: 1.55;
}
.field textarea.tall { min-height: 180px; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }
.range-row { display: flex; gap: 12px; align-items: center; }
.range-row input[type=range] { flex: 1; accent-color: var(--accent); }
.range-row input[type=number] { width: 96px !important; flex: none; }
.switch {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 4px 0;
}
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
details.fold {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 0 16px;
}
details.fold > summary {
  padding: 14px 0;
  cursor: pointer;
  font-family: var(--font-head);
  font-size: 22px;
  font-weight: 600;
  list-style: none;
  display: flex;
  justify-content: space-between;
}
details.fold > summary::after { content: '+'; color: var(--muted); }
details.fold[open] > summary::after { content: '−'; }
details.fold > .form { margin: 0 0 18px; }
.hint b { color: var(--text-soft); font-weight: 700; }
.rec {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 7px;
  padding: 4px 11px 4px 9px;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  border-radius: 999px;
  background: var(--chip);
  color: var(--accent-soft);
  font-size: 13px;
  cursor: pointer;
}
.rec svg { width: 14px; height: 14px; }
.rec:hover { background: var(--accent); color: var(--accent-text); }
.field.flash { animation: flash .7s; border-radius: 12px; }
@keyframes flash { 0% { background: var(--chip); box-shadow: 0 0 0 6px var(--chip); } 100% { background: transparent; box-shadow: 0 0 0 6px transparent; } }
.profiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.profile {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
}
.profile b { font-family: var(--font-head); font-size: 21px; font-weight: 600; }
.profile span { font-size: 13px; color: var(--muted); line-height: 1.35; }
.profile.best { border-color: var(--accent); }
.profile.best b::after { content: ' ★'; color: var(--accent-soft); font-size: 15px; }
.profile:hover { background: var(--chip); }
.explain { padding: 0 16px; box-shadow: none; }
.explain > summary {
  padding: 13px 0;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}
.explain > summary svg { width: 20px; height: 20px; color: var(--accent-soft); flex: none; }
.explain p { margin: 0 0 10px; font-size: 14px; line-height: 1.5; color: var(--text-soft); }
details.sub { border-top: 1px dashed var(--line); padding-top: 4px; }
details.sub > summary { cursor: pointer; padding: 8px 0; color: var(--muted); font-size: 14px; }
details.sub > .form { margin-top: 8px; }
.avatar-edit { display: flex; gap: 16px; align-items: center; }
.avatar-edit .pic {
  width: 96px;
  aspect-ratio: 3 / 4;
  border-radius: 12px;
  overflow: hidden;
  background: var(--chip);
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}
.avatar-edit .pic img { width: 100%; height: 100%; object-fit: cover; }
.greet-item { position: relative; }
.greet-item .icon-btn { position: absolute; top: 4px; right: 4px; }

/* ---------- Чат ---------- */
.chat-view {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}
.chat-view .topbar { position: relative; }
.chat-head { flex: 1; min-width: 0; display: flex; gap: 10px; align-items: center; cursor: pointer; }
.chat-head .ava { width: 38px; height: 38px; }
.chat-head .name { font-family: var(--font-head); font-size: 22px; font-weight: 600; line-height: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ctx-meter {
  height: 3px;
  border-radius: 2px;
  background: var(--line);
  margin-top: 5px;
  overflow: hidden;
  max-width: 160px;
}
.ctx-meter i { display: block; height: 100%; background: var(--accent); }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.messages-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 12px 12px 20px;
}
.msg {
  display: flex;
  gap: 12px;
  padding: 14px 12px;
  border-radius: var(--radius);
  position: relative;
}
.msg + .msg { margin-top: 4px; }
.msg.user { background: var(--user-bg); }
.msg .ava { width: 40px; height: 40px; margin-top: 2px; }
.msg .body { flex: 1; min-width: 0; }
.msg .who {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: var(--font-ui);
  font-size: 13px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--accent-soft);
}
.msg .who .time { color: var(--muted); font-size: 11px; letter-spacing: .04em; }
.msg .usage {
  margin-top: 2px;
  font-family: var(--font-ui);
  font-size: 11px;
  letter-spacing: .03em;
  color: var(--muted);
  opacity: .8;
}
.msg .text {
  margin-top: 4px;
  font-family: var(--font-text);
  font-size: var(--fs);
  line-height: 1.62;
  overflow-wrap: anywhere;
}
.msg .text p { margin: 0 0 .7em; }
.msg .text p:last-child { margin-bottom: 0; }
.msg .text em { color: var(--action); }
.msg .text strong { font-weight: 700; }
.msg .text .q { color: var(--speech); }
.msg .text code { font-family: ui-monospace, monospace; font-size: .88em; background: var(--chip); padding: 1px 5px; border-radius: 5px; }
.msg .text pre { background: var(--chip); padding: 10px 12px; border-radius: 10px; overflow-x: auto; white-space: pre-wrap; }
.msg .text hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.msg .text blockquote { margin: 0 0 .7em; padding-left: 12px; border-left: 3px solid var(--line); color: var(--text-soft); }
.msg.summarized .body { opacity: .62; }
.msg .cursor::after {
  content: '▍';
  color: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg .thinking {
  margin-top: 6px;
  font-size: 13px;
  color: var(--muted);
}
.msg .thinking summary { cursor: pointer; font-family: var(--font-ui); letter-spacing: .05em; text-transform: uppercase; font-size: 12px; }
.msg .thinking div { white-space: pre-wrap; padding: 8px 10px; margin-top: 6px; border-left: 2px solid var(--line); }
.msg .swipes {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--muted);
}
.msg .actions {
  display: none;
  flex-wrap: wrap;
  gap: 2px;
  margin-top: 8px;
}
.msg.show-actions .actions { display: flex; }
@media (hover: hover) {
  .msg:hover .actions { display: flex; }
}
.msg .edit-area {
  width: 100%;
  min-height: 120px;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--accent);
  background: var(--surface);
  font-family: var(--font-text);
  font-size: var(--fs);
  line-height: 1.55;
  resize: vertical;
  outline: none;
}
.msg.error .text { color: var(--danger); font-family: var(--font-plain); font-size: 15px; }
.summary-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 0;
  font-family: var(--font-ui);
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}
.summary-divider::before, .summary-divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.load-more { display: flex; justify-content: center; padding: 6px 0 12px; }

.quick-bar {
  flex: none;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.quick-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  gap: 6px;
  padding: 7px 10px 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.quick-inner::-webkit-scrollbar { display: none; }
.qbtn {
  flex: none;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-soft);
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}
.qbtn:hover { background: var(--chip); color: var(--text); }
.qbtn.npc { border-color: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--accent-soft); }
.quick-bar:not(.hidden) + .composer { border-top: 0; }
.msg.npc .ava { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.msg.npc .who { color: var(--text-soft); }
.npc-item { padding: 10px 12px; box-shadow: none; display: flex; flex-direction: column; gap: 8px; }
.npc-item input, .npc-item select, .npc-item textarea {
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  outline: none;
  min-width: 0;
}
.npc-item textarea { min-height: 60px; resize: vertical; font-family: var(--font-text); font-size: 14px; }
.event-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.event-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}
.event-btn span { font-size: 22px; }
.event-btn b { font-size: 14px; }
.event-btn small { color: var(--muted); font-size: 12px; }
.event-btn.surprise { grid-column: 1 / -1; border-color: var(--accent); }
.event-btn:hover { background: var(--chip); }
.dice-tabs { margin: 0; }
.dice-result {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 14px;
  padding: 18px;
  border-radius: 16px;
  background: var(--surface);
  animation: pop .4s ease-out;
}
.dice-result b { font-family: var(--font-head); font-size: 64px; line-height: 1; }
.dice-result span { font-family: var(--font-ui); text-transform: uppercase; letter-spacing: .08em; }
.dice-result.crit-win b, .dice-result.win b { color: #6fae6a; }
.dice-result.crit-fail b, .dice-result.fail b { color: var(--danger); }
.dice-result.part b { color: var(--accent-soft); }
@keyframes pop { 0% { transform: scale(.6) rotate(-12deg); opacity: 0; } 70% { transform: scale(1.08) rotate(3deg); } 100% { transform: none; opacity: 1; } }
.composer {
  flex: none;
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
}
.composer-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  gap: 6px;
  align-items: flex-end;
}
.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  max-height: 40vh;
  padding: 11px 16px;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: var(--surface);
  resize: none;
  outline: none;
  font-family: var(--font-text);
  font-size: 16px;
  line-height: 1.4;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.send-btn svg { width: 20px; height: 20px; }
.send-btn.stop { background: var(--danger); color: #fff; }

/* ---------- Окна ---------- */
.modal-back {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fade .15s;
}
.modal {
  width: 100%;
  max-width: 620px;
  max-height: calc(100dvh - 32px);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-radius: 20px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 {
  flex: 1;
  margin: 0;
  font-family: var(--font-head);
  font-weight: 600;
  font-size: 25px;
}
.modal-body { padding: 4px 20px 20px; overflow-y: auto; }
.modal-foot {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
@media (max-width: 640px) {
  .modal-back { align-items: flex-end; padding: 0; }
  .modal { max-width: none; border-radius: 20px 20px 0 0; max-height: 92dvh; animation: up .2s ease-out; }
}
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.tabs {
  display: flex;
  gap: 4px;
  margin: 12px 0 4px;
  padding: 4px;
  border-radius: 999px;
  background: var(--chip);
}
.tabs button {
  flex: 1;
  padding: 7px 8px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  font-family: var(--font-ui);
  font-size: 13px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-soft);
  cursor: pointer;
}
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,.08); }

.menu-list { display: flex; flex-direction: column; padding: 8px 0; }
.menu-list button {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 13px 8px;
  border: 0;
  background: transparent;
  text-align: left;
  font-size: 16px;
  border-radius: 10px;
  cursor: pointer;
}
.menu-list button:hover { background: var(--chip); }
.menu-list button svg { width: 20px; height: 20px; color: var(--muted); flex: none; }
.menu-list button.danger { color: var(--danger); }
.menu-list button.danger svg { color: var(--danger); }

.summary-item { padding: 14px 16px; }
.summary-item .head { display: flex; align-items: center; gap: 6px; font-family: var(--font-ui); font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.summary-item .content { margin-top: 6px; font-family: var(--font-text); font-size: 15px; line-height: 1.55; white-space: pre-wrap; }
.summary-item textarea { width: 100%; min-height: 160px; margin-top: 8px; padding: 10px; border-radius: 10px; border: 1px solid var(--accent); background: var(--surface); font-family: var(--font-text); font-size: 15px; line-height: 1.5; }
.prompt-msg { margin-bottom: 10px; padding: 10px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--line); }
.prompt-msg .r { font-family: var(--font-ui); font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-soft); }
.prompt-msg pre { margin: 6px 0 0; white-space: pre-wrap; font-family: var(--font-plain); font-size: 13px; color: var(--text-soft); max-height: 300px; overflow-y: auto; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin: 12px 0; }
.stat { padding: 10px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); }
.stat b { display: block; font-family: var(--font-head); font-size: 26px; font-weight: 600; line-height: 1.1; }
.stat span { font-size: 12px; color: var(--muted); }

.toast-wrap {
  position: fixed;
  left: 50%;
  top: calc(12px + env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 460px);
  pointer-events: none;
}
.toast {
  padding: 11px 16px;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-size: 14px;
  animation: fade .2s;
  pointer-events: auto;
}
.toast.err { background: var(--danger); color: #fff; }

.spinner {
  width: 22px;
  height: 22px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 30px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 520px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .hero h1 { font-size: 38px; }
  .char-hero .pic { width: 110px; }
  .char-hero h1 { font-size: 32px; }
  .msg { padding: 12px 6px; gap: 10px; }
  .msg .ava { width: 34px; height: 34px; }
  .messages-inner { padding: 8px 6px 16px; }
}
