:root {
  --bg: #121212; --card: #1e1e1e; --card2: #262626; --line: #2c2c2c;
  --text: #f2f2f2; --dim: #9a9a9a; --accent: #3b82f6; --danger: #ef4444;
  --radius: 16px; --nav-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #0b0b0b; color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, textarea, select { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; padding: 0; }
a { color: inherit; text-decoration: none; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#app { max-width: 520px; height: 100dvh; margin: 0 auto; background: var(--bg); position: relative; display: flex; flex-direction: column; overflow: hidden; }
.screen { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px 24px; }
.screen.full { display: flex; flex-direction: column; padding: 0; overflow: hidden; }

/* top bars */
.topbar { display: flex; align-items: center; gap: 10px; padding: 12px 16px; position: sticky; top: 0; background: var(--bg); z-index: 5; }
.topbar .ttl { flex: 1; font-weight: 600; font-size: 17px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%; }
.icon:active { background: var(--card2); }
.big-title { font-size: 22px; font-weight: 700; margin: 0; }
.tabs-t { display: flex; gap: 18px; align-items: baseline; flex: 1; }
.tabs-t a { font-size: 22px; font-weight: 700; color: #5c5c5c; }
.tabs-t a.on { color: var(--text); }

/* nav */
nav.bottom { height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b); display: flex; align-items: center; justify-content: space-around; background: var(--bg); border-top: 1px solid var(--line); flex: none; }
nav.bottom a { width: 56px; height: 44px; display: grid; place-items: center; color: #8a8a8a; }
nav.bottom a.on { color: var(--text); }
nav.bottom .plus { border: 1.8px solid currentColor; border-radius: 50%; width: 34px; height: 34px; color: var(--text); }
nav.bottom .plus svg { width: 20px; height: 20px; }
nav.bottom img { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
.me-dot { width: 28px; height: 28px; border-radius: 50%; background: linear-gradient(135deg,#6d5cff,#2a1a66); display: grid; place-items: center; font-size: 13px; font-weight: 700; color: #fff; }

/* search + chips */
.search { display: flex; align-items: center; gap: 8px; background: var(--card); border-radius: 999px; padding: 0 14px; height: 44px; margin-bottom: 12px; }
.search input { flex: 1; background: none; border: 0; outline: none; min-width: 0; }
.search svg { color: var(--dim); width: 18px; height: 18px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); color: var(--dim); font-size: 13px; font-weight: 500; }
.chip.on { background: #f2f2f2; color: #111; border-color: #f2f2f2; }

/* grid */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.card { display: block; min-width: 0; }
.card .pic { aspect-ratio: 1; border-radius: var(--radius); overflow: hidden; background: var(--card); position: relative; }
.card img.av { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .new { position: absolute; top: 8px; left: 8px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; }
.card h3 { margin: 8px 0 2px; font-size: 16px; }
.card p { margin: 0; color: var(--dim); font-size: 12.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card small { display: block; margin-top: 4px; color: #6b6b6b; font-size: 12px; }

/* lists */
.row { display: flex; align-items: center; gap: 14px; padding: 9px 0; }
.row img { width: 52px; height: 52px; border-radius: 12px; object-fit: cover; flex: none; }
.row .meta { flex: 1; min-width: 0; }
.row .meta b { display: block; font-weight: 500; font-size: 16px; }
.row .meta span { display: block; color: var(--dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; color: var(--dim); padding: 56px 24px; }
.empty .ring { width: 64px; height: 64px; border: 1.5px solid #3a3a3a; border-radius: 50%; display: grid; place-items: center; margin: 0 auto 14px; color: var(--text); }
.empty b { display: block; color: var(--text); margin-bottom: 4px; }

/* character page */
.hero { display: flex; gap: 16px; align-items: flex-start; }
.hero img { width: 104px; height: 104px; border-radius: 18px; object-fit: cover; flex: none; }
.hero h1 { margin: 4px 0 2px; font-size: 22px; }
.stats { display: flex; gap: 22px; margin: 14px 0; }
.stats b { display: block; font-size: 17px; }
.stats span { color: var(--dim); font-size: 12px; }
.desc { color: #d6d6d6; }
.sec-t { margin: 20px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); }
.btn-row { display: flex; gap: 10px; margin: 16px 0; }
.btn { height: 46px; padding: 0 20px; border-radius: 999px; background: #f2f2f2; color: #111; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.grow { flex: 1; }
.btn.ghost { background: var(--card); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid #4a2020; }
.btn.round { width: 46px; padding: 0; }
.btn:disabled { opacity: .5; }
.tag { display: inline-block; background: var(--card); border-radius: 999px; padding: 3px 10px; font-size: 12px; color: var(--dim); margin: 2px 4px 2px 0; }

/* chat */
.chat-top { flex: none; }
.msgs { flex: 1; overflow-y: auto; padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.msg { display: flex; gap: 8px; max-width: 100%; }
.msg.user { justify-content: flex-end; }
.msg .av { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; flex: none; margin-top: 2px; }
.bubble { background: var(--card); border-radius: 18px; padding: 10px 14px; max-width: 88%; overflow-wrap: anywhere; }
.msg.user .bubble { background: #2a2a2a; border-bottom-right-radius: 6px; }
.msg.ai .bubble { background: #1b1b1b; border: 1px solid #262626; }
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin: 0; }
.bubble em { color: #d9d9d9; font-style: italic; }
.bubble strong { font-weight: 700; color: #fff; }
.bubble q { quotes: none; font-weight: 600; color: #fff; }
.acts { display: flex; gap: 6px; margin: 6px 0 0 38px; flex-wrap: wrap; }
.msg.user + .acts { justify-content: flex-end; margin-right: 0; }
.acts button { background: var(--card2); border-radius: 999px; padding: 5px 12px; font-size: 12.5px; color: #d0d0d0; display: inline-flex; gap: 5px; align-items: center; }
.acts svg { width: 14px; height: 14px; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: #777; animation: b 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes b { 0%, 60%, 100% { opacity: .3; transform: none; } 30% { opacity: 1; transform: translateY(-3px); } }
.composer { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px calc(6px + var(--safe-b)); background: var(--bg); }
.composer .box { flex: 1; background: var(--card); border-radius: 22px; padding: 6px 8px 6px 16px; display: flex; align-items: flex-end; }
.composer textarea { flex: 1; background: none; border: 0; outline: none; resize: none; max-height: 140px; padding: 6px 0; line-height: 1.4; min-width: 0; }
.send { width: 40px; height: 40px; border-radius: 50%; background: #2c2c2c; display: grid; place-items: center; flex: none; }
.send.on { background: #f2f2f2; color: #111; }
.disc { flex: none; text-align: center; font-size: 10.5px; color: #5c5c5c; margin: 0; padding: 0 16px calc(8px + var(--safe-b)); }

/* forms */
.field { margin: 14px 0; }
.field label { display: block; font-size: 13px; color: var(--dim); margin-bottom: 6px; }
.field input[type=text], .field input[type=password], .field textarea, .field select, .field input[type=number] {
  width: 100%; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 11px 13px; outline: none; }
.field textarea { min-height: 96px; resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: #555; }
.hint { font-size: 12.5px; color: var(--dim); margin-top: 6px; }
.switch-row { display: flex; align-items: center; gap: 12px; padding: 12px 0; }
.switch-row .t { flex: 1; }
.switch-row .t small { display: block; color: var(--dim); font-size: 12.5px; }
.sw { position: relative; width: 46px; height: 28px; flex: none; }
.sw input { opacity: 0; position: absolute; inset: 0; margin: 0; cursor: pointer; z-index: 1; }
.sw span { position: absolute; inset: 0; background: #3a3a3a; border-radius: 999px; transition: .15s; }
.sw span::after { content: ""; position: absolute; width: 22px; height: 22px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .15s; }
.sw input:checked + span { background: var(--accent); }
.sw input:checked + span::after { transform: translateX(18px); }
.avatar-pick { display: flex; align-items: center; gap: 14px; }
.avatar-pick img { width: 80px; height: 80px; border-radius: 18px; object-fit: cover; }

/* sheets */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: f .15s; }
.sheet { width: 100%; max-width: 520px; background: #181818; border-radius: 22px 22px 0 0; padding: 10px 16px calc(18px + var(--safe-b)); max-height: 88dvh; overflow-y: auto; animation: u .2s; }
.sheet .grab { width: 40px; height: 4px; background: #4a4a4a; border-radius: 4px; margin: 0 auto 14px; }
.sheet h2 { margin: 4px 0 10px; font-size: 16px; text-align: center; }
@keyframes f { from { opacity: 0; } } @keyframes u { from { transform: translateY(40px); } }
.item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 14px 4px; text-align: left; }
.item.dis { color: #5a5a5a; }
.item.danger { color: var(--danger); }
.item .badge { background: #2b2b2b; color: #aaa; font-size: 10.5px; padding: 1px 7px; border-radius: 999px; }
.sheet-card { background: var(--card); border-radius: 14px; padding: 0 14px; margin: 8px 0; }
.sheet-card .item { border-bottom: 1px solid var(--line); }
.sheet-card .item:last-child { border: 0; }

#toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%) translateY(20px); background: #2c2c2c; color: #fff; padding: 11px 18px; border-radius: 999px; font-size: 14px; opacity: 0; pointer-events: none; transition: .2s; z-index: 99; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }
.spin { width: 16px; height: 16px; border: 2px solid #777; border-top-color: #fff; border-radius: 50%; animation: r .7s linear infinite; display: inline-block; }
@keyframes r { to { transform: rotate(360deg); } }

@media (min-width: 560px) { #app { height: min(100dvh, 920px); margin-top: max(0px, calc((100dvh - 920px) / 2)); border-radius: 22px; border: 1px solid var(--line); } }

/* admin */
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: #2b2b2b; color: #aaa; vertical-align: middle; }
.pill.ok { background: #1d3a2a; color: #6fcf97; }
.pill.warn { background: #3d3418; color: #e8c46a; }
.pill.bad { background: #3d1f1f; color: #ef6a6a; }
pre.ctx { background: var(--card); border-radius: 12px; padding: 12px; font-size: 12px; overflow-x: auto; white-space: pre-wrap; word-break: break-word; color: #cfcfcf; }
a.row { color: inherit; }
.row .meta .pill { display: inline-block; overflow: visible; margin-left: 4px; }
