:root {
  --bg: #15131d;
  --surface: #1e1b2a;
  --surface2: #292538;
  --text: #ece9f5;
  --muted: #9a94b2;
  --accent: #8b7cf6;
  --border: #322d44;
  --danger: #ff6b7a;
  --speech: #ffd9a0;
  --hover: rgba(255, 255, 255, 0.04);
  --radius: 12px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --surface: #f6f5fb;
    --surface2: #eae8f4;
    --text: #1c1a26;
    --muted: #6b6682;
    --accent: #6b5ce7;
    --border: #dedbea;
    --danger: #d6283c;
    --speech: #9a5200;
    --hover: rgba(0, 0, 0, 0.04);
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3, p { margin: 0; }

#app {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: var(--app-h, 100dvh);
  transform: translateY(var(--app-top, 0px));
}

/* ---------- shared bits ---------- */
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.pad { padding: 12px 16px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.grow { flex: 1; min-width: 0; }
.error { color: var(--danger); font-size: 14px; }
.danger-text { color: var(--danger) !important; }
.link { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; text-decoration: underline; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack-sm { display: flex; flex-direction: column; gap: 8px; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; align-items: center; }
.section-title { font-size: 12px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); margin: 10px 4px 0; }
.scroll-pad { padding: 12px 14px 28px; }
.splash { height: 100%; display: grid; place-content: center; gap: 12px; text-align: center; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
label.inline { flex-direction: row; align-items: center; justify-content: space-between; }
input, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS Safari zooming on focus */
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%;
}
textarea { resize: none; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type='color'] { width: 56px; height: 38px; padding: 2px; cursor: pointer; }
input[type='search'] { -webkit-appearance: none; appearance: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-weight: 600; padding: 10px 16px; min-height: 44px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface2); color: var(--text);
}
.btn:active { filter: brightness(1.15); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.small { padding: 6px 12px; min-height: 34px; font-size: 14px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }

.icon-btn {
  width: 40px; height: 40px; flex: none; display: grid; place-items: center;
  border-radius: 50%; border: 0; background: none; color: var(--text); padding: 0;
}
.icon-btn:active { background: var(--hover); }
.icon-btn.accent { color: var(--accent); }

.avatar {
  --size: 40px;
  width: var(--size); height: var(--size); flex: none; border-radius: 50%;
  background: var(--c, var(--accent)); color: #fff;
  display: grid; place-items: center; overflow: hidden;
  font-weight: 700; font-size: calc(var(--size) * 0.38); user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.tag {
  display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 6px;
  border: 1px solid var(--border); color: var(--muted);
  font-size: 10px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; vertical-align: 1px;
}
.badge, .tab-badge {
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  display: inline-grid; place-items: center; flex: none;
}

/* ---------- lists ---------- */
.list { display: flex; flex-direction: column; }
.list.card { background: var(--surface); border-radius: 14px; overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px;
  padding: 10px 14px; background: none; border: 0; text-align: left; color: inherit; text-decoration: none;
}
button.row:hover, a.row:hover { background: var(--hover); }
button.row:active, a.row:active { background: var(--surface2); }
.row.active { background: var(--surface2); }
.row.static { cursor: default; align-items: flex-start; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; min-width: 0; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-preview.wrap { white-space: pre-wrap; }
.row-time { font-size: 12px; color: var(--muted); flex: none; }
.row-clickable { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; background: none; border: 0; padding: 0; text-align: left; color: inherit; }
.check { color: var(--accent); font-weight: 700; }
.chat-row .row-title { font-size: 16px; }

.empty { display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; padding: 56px 28px; color: var(--muted); }
.empty h3 { color: var(--text); font-size: 18px; }
.empty .btn { margin-top: 6px; }
.placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }

.profile-card { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 18px 8px 10px; text-align: center; }
.profile-card h2 { font-size: 22px; margin-top: 6px; }
.profile-card .bio, .bio { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 460px; margin: 4px 0 8px; }
.profile-card .btn { margin-top: 8px; }
.note { background: var(--surface2); border-radius: var(--radius); padding: 12px 14px; font-size: 14px; display: flex; flex-direction: column; gap: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:empty { display: none; }
.chip { background: var(--surface2); border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px; font-size: 14px; }
.picker { display: flex; flex-direction: column; gap: 4px; }
.picker-results { display: flex; flex-direction: column; margin: 0 -14px; }

.avatar-pick { display: flex; align-items: center; gap: 16px; }

/* ---------- layout shell ---------- */
.shell {
  display: flex; height: 100%; width: 100%;
  padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right);
}
.sidebar { display: flex; flex-direction: column; width: 100%; min-width: 0; background: var(--bg); }
.sidebar-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.detail { display: none; flex: 1; min-width: 0; flex-direction: column; background: var(--bg); }
.shell.has-detail .sidebar { display: none; }
.shell.has-detail .detail { display: flex; }

.topbar {
  display: flex; align-items: center; gap: 8px; flex: none; min-height: 56px;
  padding: calc(env(safe-area-inset-top) + 6px) 12px 6px 14px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 22px; }
.sidebar-body > .topbar { position: sticky; top: 0; z-index: 2; }

.tabbar {
  display: flex; flex: none; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.keyboard .tabbar, .keyboard .composer { padding-bottom: 8px; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: 11px; font-weight: 600; color: var(--muted); background: none; border: 0; }
.tab.active { color: var(--accent); }
.tab-ic { position: relative; display: grid; }
.tab-badge { position: absolute; top: -6px; left: 14px; height: 18px; min-width: 18px; font-size: 11px; }

@media (min-width: 860px) {
  .sidebar { width: 360px; flex: none; display: flex !important; border-right: 1px solid var(--border); }
  .detail { display: flex !important; }
  .back-btn { display: none !important; }
  .topbar { padding-top: 6px; }
}
@media (max-width: 859px) {
  .shell.has-detail .detail { animation: slide-in 0.2s ease-out; }
}
@keyframes slide-in { from { transform: translateX(28px); opacity: 0; } }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.head-main { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; background: none; border: 0; padding: 0; text-align: left; }
.head-avatar { display: flex; flex: none; }
.chat-title { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages-wrap { flex: 1; position: relative; min-height: 0; }
.messages { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 8px 0 12px; }
.older { text-align: center; padding: 6px 0; min-height: 8px; }
.day { display: flex; justify-content: center; margin: 16px 12px 4px; }
.day span { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface); border-radius: 999px; padding: 3px 12px; }

.msg { display: flex; gap: 12px; padding: 2px 40px 2px 12px; margin-top: 14px; position: relative; }
.msg.compact { margin-top: 0; }
.msg:hover { background: var(--hover); }
.msg-avatar { flex: none; width: 40px; height: 40px; padding: 0; border: 0; background: none; border-radius: 50%; }
.msg-gutter { flex: none; width: 40px; text-align: right; font-size: 10px; color: var(--muted); padding-top: 5px; opacity: 0; }
.msg:hover .msg-gutter { opacity: 1; }
.msg-main { flex: 1; min-width: 0; }
.msg-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 8px; }
.msg-head .tag { margin-left: -2px; }
.msg-name { font-weight: 700; }
.msg-by, .msg-head time { font-size: 12px; color: var(--muted); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; }
.msg-body a { color: var(--accent); }
.msg-body .speech { color: var(--speech); }
.msg-body .ooc { color: var(--muted); font-size: 0.93em; }
.msg-body .spoiler { background: var(--text); color: transparent; border-radius: 4px; cursor: pointer; user-select: none; }
.msg-body .spoiler a { color: transparent; }
.msg-body .spoiler.revealed { background: var(--surface2); color: inherit; user-select: auto; }
.msg-body .spoiler.revealed a { color: var(--accent); }
.edited { font-size: 11px; color: var(--muted); }
.msg-more { position: absolute; right: 6px; top: 0; width: 32px; height: 32px; display: grid; place-items: center; background: none; border: 0; color: var(--muted); border-radius: 50%; opacity: 0; }
.msg:hover .msg-more, .msg-more:focus-visible { opacity: 1; }
@media (hover: none) { .msg-more { opacity: 0.55; } }

.msg-images { display: grid; gap: 4px; margin-top: 6px; max-width: 420px; }
.msg-images.n2, .msg-images.n3, .msg-images.n4 { grid-template-columns: 1fr 1fr; }
.img-btn { padding: 0; border: 0; background: var(--surface2); border-radius: 10px; overflow: hidden; display: block; width: 100%; aspect-ratio: 1; }
.img-btn img { width: 100%; height: 100%; object-fit: cover; display: block; }
.n1 .img-btn { max-width: 360px; max-height: 440px; }
.n1 .img-btn img { object-fit: contain; }

.msg.flash { animation: flash 2s ease-out; }
@keyframes flash { 0%, 45% { background: rgba(139, 124, 246, 0.3); } 100% { background: transparent; } }

.search-field input { border-radius: 999px; background: var(--bg); }
.search-scope { padding: 8px 16px 0; font-size: 13px; color: var(--muted); }
.result { align-items: flex-start; }
.result-snippet { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
mark { background: rgba(139, 124, 246, 0.35); color: var(--text); border-radius: 3px; padding: 0 1px; }

.jump { position: absolute; right: 14px; bottom: 12px; display: flex; align-items: center; gap: 4px; background: var(--accent); color: #fff; border: 0; border-radius: 999px; padding: 8px 14px; font-weight: 600; font-size: 14px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35); }
.typing { flex: none; font-size: 12px; font-style: italic; color: var(--muted); padding: 2px 16px 4px; }
.typing:empty { display: none; }

.composer { flex: none; background: var(--surface); border-top: 1px solid var(--border); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea { flex: 1; min-width: 0; max-height: 150px; border-radius: 20px; padding: 9px 14px; line-height: 1.35; }
.persona-btn { flex: none; width: 40px; height: 40px; padding: 2px; border-radius: 50%; border: 2px solid var(--accent); background: none; display: grid; place-items: center; }
.send-btn { flex: none; width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; }
.send-btn:disabled { opacity: 0.35; }
.previews { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 8px; }
.preview { position: relative; flex: none; width: 64px; height: 64px; }
.preview img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; display: block; }
.preview-x { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); font-size: 11px; line-height: 1; display: grid; place-items: center; padding: 0; }

/* ---------- sheets, lightbox, toast ---------- */
.backdrop {
  position: fixed; left: 0; right: 0; top: var(--app-top, 0px); height: var(--app-h, 100dvh); z-index: 50;
  background: rgba(0, 0, 0, 0.55); display: flex; align-items: flex-end; justify-content: center;
  animation: fade 0.15s ease-out;
}
.backdrop.closing { opacity: 0; transition: opacity 0.15s; pointer-events: none; }
.sheet {
  background: var(--surface); width: 100%; max-width: 520px; max-height: 90%;
  display: flex; flex-direction: column; border-radius: 18px 18px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  animation: sheet-up 0.2s ease-out;
}
.keyboard .sheet { padding-bottom: 0; }
.sheet-head { display: flex; align-items: center; padding: 10px 8px 2px 18px; }
.sheet-head h2 { flex: 1; font-size: 18px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 8px 18px 20px; }
.sheet-body .list .row { padding-left: 4px; padding-right: 4px; }
@media (min-width: 600px) {
  .backdrop { align-items: center; }
  .sheet { border-radius: 18px; max-height: 85%; }
}
@keyframes fade { from { opacity: 0; } }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0.4; } }

.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, 0.94); display: flex; align-items: center; justify-content: center; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox-close { position: absolute; top: calc(env(safe-area-inset-top) + 8px); right: 10px; color: #fff; background: rgba(0, 0, 0, 0.5); }
.lightbox-open { position: absolute; bottom: calc(env(safe-area-inset-bottom) + 14px); left: 50%; transform: translateX(-50%); text-decoration: none; background: rgba(255, 255, 255, 0.15); color: #fff; border-color: transparent; }

#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 84px); transform: translate(-50%, 10px);
  max-width: min(90vw, 420px); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 80;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); color: #fff; }

/* ---------- auth ---------- */
.auth { min-height: 100%; overflow-y: auto; display: grid; place-items: center; padding: calc(env(safe-area-inset-top) + 24px) 20px 24px; }
.auth-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 16px; }
.auth-card h1 { text-align: center; font-size: 30px; }
.auth-logo { width: 72px; height: 72px; border-radius: 18px; align-self: center; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 4px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
