:root {
  --bg: #14111c;
  --bg2: #1d1828;
  --bg3: #272033;
  --border: #372d48;
  --text: #ece6f4;
  --muted: #9d92b3;
  --accent: #e879a8;
  --accent2: #b58cf5;
  --danger: #f2647a;
  --user: #2d2540;
  --bot: #1f1a2b;
  --radius: 14px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
body { overscroll-behavior-y: none; }
a { color: var(--accent2); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--bg3); border-radius: 10px; padding: 8px 14px; transition: background .15s, border-color .15s; }
button:hover { border-color: var(--accent2); }
button.primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); border: none; color: #fff; font-weight: 600; }
button.danger { color: var(--danger); }
button.ghost { background: transparent; border-color: transparent; }
button.small { padding: 4px 10px; font-size: 13px; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent2); }
textarea { resize: vertical; min-height: 80px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; }
label:first-child { margin-top: 0; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; }
h1, h2, h3 { margin: 0 0 12px; font-weight: 650; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
.card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card + .card { margin-top: 14px; }

#app { display: flex; flex-direction: column; height: 100dvh; }
#topbar { display: flex; align-items: center; gap: 16px; padding: calc(10px + var(--sat)) 16px 10px; border-bottom: 1px solid var(--border); background: var(--bg2); flex: none; min-width: 0; }
#topbar .brand { font-weight: 700; color: var(--text); font-size: 16px; flex: none; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
#topbar .brand-icon { width: 22px; height: 22px; border-radius: 6px; display: block; }
/* The nav can never widen the page: if the links don't fit (narrow phone, enlarged text) it scrolls inside itself. */
#topbar nav { display: flex; gap: 4px; margin-left: auto; min-width: 0; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
#topbar nav::-webkit-scrollbar { display: none; }
#topbar nav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); font-size: 14px; white-space: nowrap; flex: none; }
#topbar nav a.active, #topbar nav a:hover { background: var(--bg3); color: var(--text); }
#view { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
.page { max-width: 900px; margin: 0 auto; padding: 20px 16px calc(20px + var(--sab)); min-width: 0; overflow-wrap: anywhere; }
.page img, .page svg { max-width: 100%; }
.page pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.loading { display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--muted); padding: 60px 20px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent2); border-radius: 50%; animation: ccspin .8s linear infinite; flex: none; }
@keyframes ccspin { to { transform: rotate(360deg); } }
.offline { position: fixed; top: calc(8px + var(--sat)); left: 50%; transform: translateX(-50%); z-index: 60; max-width: calc(100vw - 32px); background: #5a1d2b; color: #fff; border: 1px solid var(--danger); border-radius: 10px; padding: 8px 14px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.35); }
.page.narrow { max-width: 640px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; flex: 1; }

/* toast */
#toast { position: fixed; left: 50%; bottom: calc(24px + var(--sab)); transform: translateX(-50%); background: var(--bg3); border: 1px solid var(--border); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.error { border-color: var(--danger); }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 16px; }
.modal .card { width: 100%; max-width: 420px; max-height: 90vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.modal .card.wide { max-width: 680px; }
.scroll-box { max-height: 40vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; padding: 4px 10px; background: var(--bg); }
.modal .card input, .modal .card select, .modal .card textarea { margin-bottom: 10px; }

/* characters grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.char-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; cursor: pointer; transition: transform .12s, border-color .15s; display: flex; flex-direction: column; }
.char-card:hover { transform: translateY(-2px); border-color: var(--accent2); }
.avatar { width: 100%; aspect-ratio: 3/4; object-fit: cover; background: var(--bg3); display: block; }
.avatar.placeholder { display: flex; align-items: center; justify-content: center; font-size: 42px; color: var(--muted); }
.char-card .body { padding: 10px 12px 12px; }
.char-card .name { font-weight: 650; }
.char-card .tagline { color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tag { font-size: 11px; background: var(--bg3); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

/* character profile */
.profile { display: grid; grid-template-columns: 200px 1fr; gap: 20px; }
.profile .avatar { border-radius: var(--radius); aspect-ratio: 3/4; }
.profile .desc { white-space: pre-wrap; color: var(--muted); font-size: 14px; max-height: 240px; overflow: auto; }
.chat-list .item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg2); margin-top: 8px; cursor: pointer; }
.chat-list .item:hover { border-color: var(--accent2); }
.chat-list .item .title { font-weight: 600; }
.chat-list .item .meta { font-size: 12px; color: var(--muted); }
.chat-list .item .preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* editor */
.editor-grid { display: grid; grid-template-columns: 180px 1fr; gap: 20px; }
.editor-grid .avatar { border-radius: var(--radius); cursor: pointer; }
.alt-greeting { display: flex; gap: 8px; align-items: flex-start; margin-top: 8px; }
.alt-greeting textarea { min-height: 60px; }

.lore-entry { border: 1px solid var(--border); border-radius: 10px; padding: 10px; margin-top: 8px; background: var(--bg); }
.lore-entry textarea { min-height: 60px; }
.warn-badge { font-size: 10px; color: var(--danger); border: 1px solid var(--danger); border-radius: 999px; padding: 0 6px; vertical-align: middle; font-weight: 500; }

/* personas */
.persona-row { display: flex; gap: 12px; align-items: center; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg2); margin-top: 8px; cursor: pointer; }
.persona-row:hover { border-color: var(--accent2); }
.persona-row.selected { border-color: var(--accent2); background: var(--bg3); }
.pav { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; background: var(--bg3); flex: none; }
.pav.placeholder { display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }
.pav.big { width: 84px; height: 84px; cursor: pointer; font-size: 30px; }
.pav.xs { width: 26px; height: 26px; font-size: 12px; }
.persona-row .desc { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pill { font-size: 11px; border: 1px solid var(--border); border-radius: 999px; padding: 0 7px; color: var(--muted); margin-left: 6px; vertical-align: middle; font-weight: 500; }
.pill.default { color: var(--accent); border-color: var(--accent); }
.persona-chip { display: flex; align-items: center; gap: 6px; height: 44px; padding: 0 10px; border-radius: 10px; background: var(--bg3); border: 1px solid var(--border); max-width: 150px; flex: none; }
.persona-chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.cast-bar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; padding: 6px 12px; border-bottom: 1px solid var(--border); background: var(--bg2); font-size: 12px; color: var(--muted); flex: none; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--bg3); border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px; font-size: 12px; color: var(--text); cursor: default; }
.chip button { background: none; border: none; padding: 0 2px; color: var(--muted); font-size: 12px; }
.chip button:hover { color: var(--danger); }
.msg .bubble { position: relative; }
.msg-menu { position: absolute; right: 8px; bottom: 34px; background: var(--bg3); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; z-index: 15; min-width: 190px; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.msg-menu button { display: block; width: 100%; text-align: left; background: transparent; border: none; border-radius: 0; padding: 9px 12px; color: var(--text); font-size: 13px; }
.msg-menu button:hover { background: var(--bg2); }
input[type=range] { padding: 0; height: 28px; accent-color: var(--accent); }
.prompt-row { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); margin-top: 8px; cursor: pointer; }
.prompt-row.active { border-color: var(--accent2); }
.link-row { padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); margin-top: 8px; }
/* ---------- phones ---------- */
@media (max-width: 640px) {
  .persona-chip { max-width: 96px; padding: 0 8px; height: 40px; }
  .persona-chip span.muted { display: none; }
  .composer { padding: 6px 8px calc(8px + var(--sab)); }
  .composer .box { padding: 4px 6px 6px; }
  .composer textarea { min-height: 44px; font-size: 16px; }
  .composer .sparkle, .composer .send { width: 42px; height: 42px; }
  .persona-chip { max-width: 150px; height: 40px; }
  .modal { padding: 8px; align-items: flex-end; }
  .modal .card, .modal .card.wide { max-width: 100%; max-height: 92vh; padding: 14px; border-radius: 16px 16px 0 0; }
  .persona-row { flex-wrap: wrap; }
  .persona-row > .row { width: 100%; justify-content: flex-end; }
  .msg .actions button { padding: 6px 9px; font-size: 15px; }
  .msg .actions .pager button { font-size: 20px; padding: 4px 10px; }
  .msg-menu { right: 4px; left: 4px; bottom: 40px; min-width: 0; }
  .msg-menu button { padding: 12px 14px; font-size: 14px; }
  .menu { right: 8px; left: 8px; top: 52px; }
  .menu button { padding: 12px 14px; }
  .chat-head { padding: 6px 8px; gap: 8px; }
  .chat-head .mini { width: 34px; height: 34px; }
  .chat-head select { max-width: 104px; font-size: 12px; }
  .cast-bar { padding: 5px 8px; }
  .page-head h1 { width: 100%; }
  .page-head { gap: 8px; }
  .page-head button { padding: 7px 10px; font-size: 13px; }
  .scroll-box { max-height: 45vh; }
  .prompt-row { flex-wrap: wrap; }
  .lore-entry .row { flex-wrap: wrap; }
  input[type=range] { min-width: 0; }
  .qr svg { width: 160px; height: 160px; }
}
.persona { display: flex; gap: 12px; align-items: flex-start; }
.persona .badge { font-size: 11px; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 1px 8px; margin-left: 8px; }

/* settings */
.models-list .model { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.models-list .model:last-child { border-bottom: none; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: #5fd39a; }
.dot.no { background: var(--danger); }
.qr { background: #fff; border-radius: 12px; padding: 10px; display: inline-block; }
.qr svg { display: block; width: 200px; height: 200px; }
code { background: var(--bg3); padding: 2px 6px; border-radius: 6px; font-size: 13px; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--bg2); flex: none; }
.chat-head .mini { width: 38px; height: 38px; border-radius: 50%; object-fit: cover; background: var(--bg3); flex: none; }
.chat-head .who { min-width: 0; flex: 1; }
.chat-head .who .name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .who .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head select { width: auto; max-width: 180px; padding: 6px 8px; font-size: 13px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 14px; -webkit-overflow-scrolling: touch; }
.msg { display: flex; gap: 10px; max-width: 860px; width: 100%; margin: 0 auto; }
.msg .mini { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; background: var(--bg3); flex: none; margin-top: 2px; }
.msg .mini.placeholder { display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }
.msg .bubble { flex: 1; min-width: 0; background: var(--bot); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.msg.user .bubble { background: var(--user); }
.msg .head { display: flex; align-items: baseline; gap: 8px; font-size: 13px; margin-bottom: 4px; }
.msg .head .name { font-weight: 650; }
.msg .head .model { color: var(--muted); font-size: 11px; }
.msg .content { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg .content i { color: var(--accent2); font-style: italic; }
.msg .content .q { color: #ffd9ea; }
.msg .content pre { background: var(--bg); padding: 8px 10px; border-radius: 8px; overflow: auto; white-space: pre; }
.msg .content.streaming::after { content: "▍"; color: var(--accent); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }
.typing { color: var(--muted); font-style: italic; }
.typing .dots::after { content: ""; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.msg .actions { display: flex; gap: 2px; margin-top: 6px; align-items: center; opacity: .55; transition: opacity .15s; flex-wrap: wrap; }
.msg:hover .actions, .msg:focus-within .actions { opacity: 1; }
.msg .actions button { background: transparent; border: none; padding: 3px 7px; color: var(--muted); font-size: 13px; border-radius: 6px; }
.msg .actions button:hover { background: var(--bg3); color: var(--text); }
.msg .actions .swipe { font-size: 12px; color: var(--muted); margin: 0 2px; }
.msg .edit textarea { min-height: 100px; margin-bottom: 8px; }
.notice { text-align: center; font-size: 12px; color: var(--muted); max-width: 860px; margin: 0 auto; padding: 0 12px; }
.notice.error { color: var(--danger); }
.composer { flex: none; background: var(--bg2); padding: 8px 12px calc(10px + var(--sab)); }
.composer .box { max-width: 860px; margin: 0 auto; border: 1px solid var(--accent2); border-radius: 14px; background: var(--bg3); padding: 6px 8px 6px; box-shadow: 0 0 0 1px rgba(181,140,245,.15); }
.composer textarea { width: 100%; min-height: 40px; max-height: 40vh; resize: none; padding: 8px 8px 4px; background: transparent; border: none; border-radius: 0; font-size: 15px; }
.composer textarea:focus { border: none; }
.composer .bar { display: flex; align-items: center; gap: 8px; padding: 2px 0 0; }
.composer .sparkle { width: 38px; height: 38px; padding: 0; font-size: 20px; color: var(--accent2); flex: none; border-radius: 10px; }
.composer .sparkle:hover { background: var(--bg2); color: var(--accent); }
.composer .send { width: 38px; height: 38px; min-width: 0; padding: 0; font-size: 18px; border-radius: 10px; flex: none; }
.composer .stop { height: 38px; min-width: 60px; color: var(--danger); flex: none; }
.persona-chip { height: 36px; }
.msg .actions .pager { margin-left: auto; display: inline-flex; align-items: center; gap: 2px; }
.msg .actions .pager button { font-size: 18px; line-height: 1; padding: 2px 8px; }
.msg .actions .pager button:disabled { opacity: .3; }
.msg .actions button[data-act="continue"] { font-size: 15px; }

/* menu */
.menu { position: absolute; right: 12px; top: 56px; background: var(--bg3); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; z-index: 20; min-width: 200px; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.menu button { display: block; width: 100%; text-align: left; background: transparent; border: none; border-radius: 0; padding: 10px 14px; }
.menu button:hover { background: var(--bg2); }

@media (max-width: 640px) {
  #topbar { padding-left: 12px; padding-right: 12px; gap: 8px; }
  #topbar .brand span { display: none; } /* icon only: leaves the width to the four links */
  #topbar nav { flex: 1; justify-content: space-between; }
  #topbar nav a { padding: 6px 8px; font-size: 13px; }
  .page { padding: 14px 12px calc(14px + var(--sab)); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
  .profile, .editor-grid { grid-template-columns: 1fr; }
  .profile .avatar, .editor-grid .avatar { max-width: 200px; }
  .chat-head select { max-width: 120px; }
  .msg .mini { width: 32px; height: 32px; }
  .msg .actions { opacity: .9; }
  .msg .bubble { padding: 8px 11px; }
  .messages { padding: 12px 8px; gap: 10px; }
  body.in-chat #topbar { display: none; }
}
