:root {
  --bg: #0f0d12;
  --panel: #17141c;
  --panel-2: #1f1a26;
  --line: #2c2535;
  --text: #ece6f2;
  --muted: #9a90a6;
  --accent: #d6385a;
  --accent-2: #ff6b8a;
  --dialog: #f3c47a;
  --action: #b9a8d0;
  --user-bubble: #2a2033;
  --radius: 10px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; background: var(--bg); color: var(--text);
  font: 15px/1.55 system-ui, "Segoe UI", sans-serif;
}
button, .btn, input, textarea, select { font: inherit; color: inherit; }
button, .btn {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
button:hover, .btn:hover { border-color: #463b52; background: #251f2d; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-2); }
button.danger { color: #ff8b8b; }
button.link { background: none; border: none; color: var(--accent-2); padding: 0 4px; font-size: 13px; }
button.icon { background: none; border: none; font-size: 18px; padding: 4px 8px; }
.small { font-size: 12px; }
.muted { color: var(--muted); }
.grow { flex: 1; }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }

/* sidebar */
#sidebar {
  width: 280px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.brand { font-size: 20px; font-weight: 700; padding: 16px 16px 6px; letter-spacing: .3px; }
.logo { filter: drop-shadow(0 0 6px #d6385a88); }
.status { display: flex; align-items: center; gap: 8px; padding: 0 16px 12px; font-size: 12px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #777; flex-shrink: 0; }
.status.ready .dot { background: #4ade80; box-shadow: 0 0 6px #4ade80; }
.status.starting .dot { background: #facc15; animation: pulse 1s infinite alternate; }
.status.error .dot { background: #f87171; }
@keyframes pulse { to { opacity: .3; } }
.side-actions { display: flex; gap: 8px; padding: 0 16px 8px; }
.side-actions > * { flex: 1; justify-content: center; font-size: 13px; }
.section-title { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; }
#char-list { flex: 0 1 auto; max-height: 45vh; }
#chat-section { display: flex; flex-direction: column; min-height: 0; flex: 1; }
#chat-section[hidden] { display: none; }
.chats { flex: 1; }
.list li {
  display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 8px; cursor: pointer;
}
.list li:hover { background: var(--panel-2); }
.list li.active { background: #2a1f2e; box-shadow: inset 2px 0 0 var(--accent); }
.list .meta { min-width: 0; flex: 1; }
.list .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .del { visibility: hidden; background: none; border: none; color: var(--muted); padding: 2px 6px; }
.list li:hover .del { visibility: visible; }
.side-footer { margin-top: auto; padding: 12px 16px; border-top: 1px solid var(--line); }
.side-footer button { width: 100%; justify-content: center; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%; object-fit: cover; flex-shrink: 0;
  background: linear-gradient(135deg, #5b2340, #2a1a3a); display: inline-grid; place-items: center;
  font-weight: 700; color: #fff; font-size: 15px;
}
.avatar.big { width: 84px; height: 84px; font-size: 30px; }

/* main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-header {
  display: flex; align-items: center; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--line);
  background: var(--panel);
}
#btn-menu { display: none; }
#head-name { font-weight: 700; font-size: 16px; }
#head-sub { font-size: 12px; }
.head-titles { flex: 1; min-width: 0; }

#messages { flex: 1; overflow-y: auto; padding: 24px 0 12px; scroll-behavior: auto; }
.msg { display: flex; gap: 12px; max-width: 860px; margin: 0 auto; padding: 8px 20px; }
.msg .avatar { margin-top: 2px; }
.msg .body { flex: 1; min-width: 0; }
.msg .who { font-weight: 700; font-size: 13px; color: var(--accent-2); display: flex; align-items: center; gap: 8px; }
.msg.user .who { color: #9ecbff; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user .text { background: var(--user-bubble); padding: 8px 12px; border-radius: var(--radius); display: inline-block; max-width: 100%; }
.msg .text em { color: var(--action); }
.msg .text .dq { color: var(--dialog); }
.msg .text .dq em { color: inherit; }
.msg .tools { display: flex; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity .12s; }
.msg:hover .tools, .msg.last .tools { opacity: 1; }
.msg .tools button { background: none; border: none; color: var(--muted); padding: 2px 6px; font-size: 13px; }
.msg .tools button:hover { color: var(--text); }
.swipes { display: inline-flex; align-items: center; gap: 2px; font-size: 12px; color: var(--muted); font-weight: 400; }
.swipes button { background: none; border: none; padding: 0 4px; color: var(--muted); }
.think { margin: 4px 0 8px; font-size: 13px; color: var(--muted); border-left: 2px solid var(--line); padding-left: 10px; }
.think summary { cursor: pointer; user-select: none; }
.think div { white-space: pre-wrap; margin-top: 4px; }
.cursor::after { content: "▍"; color: var(--accent-2); animation: pulse .6s infinite alternate; }
.msg textarea.edit { width: 100%; min-height: 120px; background: var(--panel-2); border: 1px solid var(--accent); border-radius: 8px; padding: 8px; resize: vertical; }
.edit-actions { display: flex; gap: 8px; margin-top: 6px; }
.picture { margin: 8px 0 2px; max-width: min(420px, 100%); }
.picture img { display: block; width: 100%; border-radius: var(--radius); border: 1px solid var(--line); cursor: zoom-in; }
.picture figcaption { display: flex; align-items: center; gap: 2px; font-size: 11px; margin-top: 3px; }
.picture figcaption span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-left: 4px; }
.picture figcaption button { background: none; border: none; color: var(--muted); padding: 1px 6px; font-size: 13px; }
.picture figcaption button:hover { color: var(--text); }
.picture.painting {
  aspect-ratio: 832 / 1216; max-width: 260px; display: grid; place-items: center; text-align: center;
  border: 1px dashed #4a3d57; border-radius: var(--radius); color: var(--muted); font-size: 13px;
  background: linear-gradient(110deg, #1c1722 30%, #2a2033 50%, #1c1722 70%); background-size: 250% 100%;
  animation: shimmer 1.6s linear infinite;
}
@keyframes shimmer { to { background-position: -150% 0; } }
dialog select { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-weight: 400; font-size: 14px; }
dialog label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.row.wrap { flex-wrap: wrap; }
.refs-block { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.refs-head { font-size: 13px; font-weight: 600; }
.ref-list { display: flex; gap: 8px; flex-wrap: wrap; }
.ref-thumb { position: relative; width: 92px; height: 92px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.ref-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ref-thumb button { position: absolute; top: 3px; right: 3px; padding: 0 6px; font-size: 12px; background: #000b; border: none; }
.ref-add { width: 92px; height: 92px; border: 1px dashed #4a3d57; border-radius: 8px; display: grid; place-items: center; text-align: center; font-size: 12px; color: var(--muted); cursor: pointer; }
.ref-add:hover { border-color: var(--accent); color: var(--text); }
.ref-add.busy { cursor: progress; opacity: .6; }
dialog label.slider { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
dialog label.slider input { flex: 1; padding: 0; }
.notice { max-width: 860px; margin: 8px auto; padding: 8px 20px; font-size: 12px; color: var(--muted); text-align: center; }
.notice.err { color: #f87171; }
.empty { max-width: 560px; margin: 12vh auto; text-align: center; color: var(--muted); padding: 0 20px; }
.empty h1 { color: var(--text); font-size: 26px; }

#composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 18px 14px; }
#composer[hidden] { display: none; }
.composer-tools { display: flex; align-items: center; gap: 8px; max-width: 860px; margin: 0 auto 8px; font-size: 13px; }
.composer-tools button { padding: 3px 10px; font-size: 13px; }
.composer-tools #speed { margin-left: auto; font-size: 12px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; color: var(--muted); }
.composer-row { display: flex; gap: 10px; max-width: 860px; margin: 0 auto; align-items: flex-end; }
#input {
  flex: 1; resize: none; max-height: 40vh; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 12px; outline: none;
}
#input:focus { border-color: #5a4868; }
#btn-send { padding: 10px 18px; }
#btn-send.stop { background: #444; border-color: #555; }

/* dialogs */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
  width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0;
}
dialog::backdrop { background: #000a; backdrop-filter: blur(2px); }
dialog form { padding: 20px 24px; display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0 0 4px; }
dialog h3 { margin: 10px 0 0; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
dialog input, dialog textarea {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-weight: 400; font-size: 14px;
}
dialog textarea { resize: vertical; }
dialog details summary { cursor: pointer; color: var(--muted); margin-bottom: 8px; }
dialog details[open] { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 16px; align-items: center; }
.avatar-edit { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.dlg-actions { display: flex; gap: 8px; margin-top: 8px; }

/* ---------- visual novel stage ---------- */
body.vn #messages { display: none; }
body.vn.vn-log #messages { display: block; }
body.vn.vn-log #stage { display: none; }
#stage[hidden] { display: none; }
#stage {
  flex: 1; position: relative; overflow: hidden; min-height: 0;
  background: radial-gradient(ellipse at 50% 30%, #2b2033, #0c0a0f);
}
.stage-bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity 1.2s ease; }
.stage-bg.show { opacity: 1; }
.stage-chips { position: absolute; top: 14px; left: 14px; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; z-index: 3; }
.stage-chip {
  background: #ff7ac8e6; color: #fff; font-weight: 700; font-size: 20px; padding: 4px 14px; border-radius: 4px;
  border: 2px solid #ffd0ec; text-shadow: 0 1px 2px #a0306a; box-shadow: 0 2px 8px #0005;
}
.stage-chip.small { font-size: 14px; padding: 2px 10px; }
.stage-tools { position: absolute; top: 14px; right: 14px; z-index: 3; display: flex; gap: 6px; align-items: center; }
.stage-status { background: #000a; color: #fff; font-size: 12px; padding: 4px 10px; border-radius: 12px; }
.stage-tools button { background: #000a; border: none; color: #fff; font-size: 12px; padding: 4px 10px; border-radius: 12px; opacity: 0; transition: opacity .2s; }
#stage:hover .stage-tools button { opacity: .85; }
.stage-tools button:hover { opacity: 1 !important; background: #000d; }
.sprite-wrap { position: absolute; left: 0; right: 0; bottom: 0; top: 4%; display: flex; justify-content: center; align-items: flex-end; pointer-events: none; z-index: 1; }
.sprite-motion { position: relative; height: 100%; aspect-ratio: 832 / 1216; transform-origin: 50% 100%; animation: breathe 4.6s ease-in-out infinite; }
.sprite { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; object-position: bottom; filter: drop-shadow(0 6px 14px #0007); transition: opacity .35s ease; }
.sprite.blink { opacity: 0; transition: none; }
.sprite-wrap.gone .sprite-motion { opacity: 0; transform: translateY(6%); transition: opacity .6s, transform .6s; }
.sprite-wrap .sprite-motion { transition: opacity .6s, transform .6s; }
@keyframes breathe { 0%, 100% { transform: scale(1, 1); } 50% { transform: scale(1.004, 1.012); } }
.react-happy { animation: hop .5s ease-out, breathe 4.6s ease-in-out .5s infinite; }
.react-surprised { animation: jump .35s ease-out, breathe 4.6s ease-in-out .35s infinite; }
.react-angry { animation: shake .45s linear, breathe 4.6s ease-in-out .45s infinite; }
.react-embarrassed { animation: fidget .6s ease-in-out, breathe 4.6s ease-in-out .6s infinite; }
.react-sad { animation: droop 1s ease-in-out, breathe 4.6s ease-in-out 1s infinite; }
.react-flirty, .react-aroused { animation: sway 1.4s ease-in-out, breathe 4.6s ease-in-out 1.4s infinite; }
@keyframes hop { 30% { transform: translateY(-4%); } 60% { transform: translateY(0); } 80% { transform: translateY(-1.2%); } }
@keyframes jump { 40% { transform: translateY(-3%) scale(1.02); } }
@keyframes shake { 20%, 60% { transform: translateX(-1.4%); } 40%, 80% { transform: translateX(1.4%); } }
@keyframes fidget { 25% { transform: translateX(-.8%) rotate(-.6deg); } 75% { transform: translateX(.8%) rotate(.6deg); } }
@keyframes droop { 50% { transform: translateY(1.5%) scale(.99); } }
@keyframes sway { 30% { transform: rotate(-1deg); } 70% { transform: rotate(1deg); } }
.stage-cg[hidden] { display: none; } /* display:grid below would otherwise override [hidden] and dim the whole stage */
.stage-cg { position: absolute; inset: 0; z-index: 2; background: #000c; display: grid; place-items: center; cursor: zoom-out; animation: fadein .4s; }
.stage-cg img { max-width: 100%; max-height: 100%; object-fit: contain; }
@keyframes fadein { from { opacity: 0; } }
.vn-box {
  position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); width: min(1000px, calc(100% - 28px));
  z-index: 4; display: flex; flex-direction: column;
}
.vn-name {
  align-self: flex-start; min-width: 200px; background: linear-gradient(90deg, #ff4fb4, #ff7ac8); color: #fff; font-weight: 800;
  font-size: 22px; padding: 4px 26px; border-top: 3px solid #ffd0ec; text-shadow: 0 1px 3px #a0306a; margin-left: 4px;
}
.vn-name:empty { display: none; }
.vn-text {
  background: #fbe6f1e0; color: #3b2f38; border-top: 5px solid #ff4fb4; padding: 14px 22px; font-size: 17px; line-height: 1.55;
  max-height: 28vh; min-height: 88px; overflow-y: auto; white-space: pre-wrap; box-shadow: 0 6px 24px #0006; backdrop-filter: blur(3px);
}
.vn-text em { color: #7a5a8c; }
.vn-text .dq { color: #2a1d28; font-weight: 500; }
.vn-text.user { color: #2b4a6b; }
#btn-vn.on, #btn-log.on { filter: drop-shadow(0 0 6px var(--accent)); }

/* ---------- accounts ---------- */
.account-row { display: flex; align-items: center; gap: 4px; margin-top: 8px; flex-wrap: wrap; }
.account-row #account-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pw-warning { background: #3a1d12; border: 1px solid #a5522a; color: #ffc59e; font-size: 12px; padding: 6px 8px; border-radius: 8px; margin-bottom: 8px; }
#login-screen { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: radial-gradient(ellipse at 50% 30%, #2b2033, var(--bg)); }
#login-screen[hidden] { display: none; }
.login-card { width: min(360px, calc(100vw - 32px)); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 60px #0008; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
.login-card input { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; font-weight: 400; }
.login-card button { justify-content: center; padding: 10px; }
.login-brand { font-size: 24px; font-weight: 800; text-align: center; margin-bottom: 4px; }
.login-error { color: #f87171; font-size: 13px; min-height: 1em; }
.login-notice { margin: 0; text-align: center; }
body.signed-out #sidebar, body.signed-out #main { visibility: hidden; }
dialog.wide { width: min(1100px, calc(100vw - 32px)); height: min(760px, calc(100vh - 48px)); }
.admin-wrap { display: flex; flex-direction: column; gap: 12px; padding: 18px 22px; height: 100%; }
.admin-head { display: flex; align-items: center; gap: 16px; }
.admin-head h2 { margin: 0; }
.tabs { display: flex; gap: 4px; }
.tab.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.new-user { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.new-user input, .new-user select, #convo-user, #char-owner { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th, .admin-table td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); }
.admin-table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.admin-table td button { padding: 3px 8px; font-size: 12px; }
section[data-panel] { flex: 1; min-height: 0; overflow: auto; }
.convo-layout { display: flex; gap: 14px; height: 100%; min-height: 0; }
.convo-side { width: 280px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.convo-side .list { overflow-y: auto; padding: 0; }
.convo-view { flex: 1; overflow-y: auto; border-left: 1px solid var(--line); padding-left: 14px; }
.convo-view .t-msg { margin: 8px 0; white-space: pre-wrap; }
.convo-view .t-msg b { color: var(--accent-2); }
.convo-view .t-msg.user b { color: #9ecbff; }
.convo-view .t-meta { font-size: 11px; color: var(--muted); }
.convo-view img { max-width: 220px; border-radius: 8px; display: block; margin-top: 4px; }
.tag-deleted { color: #f87171; font-size: 11px; margin-left: 6px; }

@media (max-width: 760px) {
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; }
  body.menu-open #sidebar { transform: none; box-shadow: 0 0 40px #000; }
  #btn-menu { display: block; }
  .msg { padding: 8px 12px; }
}
