:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1d1c1a;
  --muted: #6d6a64;
  --border: #e2dfd8;
  --accent: #6b4eff;
  --accent-contrast: #ffffff;
  --accent-soft: #ece8ff;
  --danger: #c0392b;
  --success: #1e8e5a;
  --bubble-user: #6b4eff;
  --bubble-user-text: #ffffff;
  --bubble-char: #ffffff;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 16px rgba(0, 0, 0, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316;
    --surface: #1c1c21;
    --surface-2: #26262d;
    --text: #ececef;
    --muted: #9a9aa3;
    --border: #2f2f37;
    --accent: #8f7bff;
    --accent-soft: #2a2545;
    --bubble-user: #6b4eff;
    --bubble-char: #24242b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .6em; }
h1 { font-size: 1.7rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* --- layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand span { color: var(--accent); }
.topbar nav { display: flex; gap: 14px; flex-wrap: wrap; flex: 1; }
.topbar nav a { color: var(--muted); font-weight: 500; }
.topbar nav a.active, .topbar nav a:hover { color: var(--text); text-decoration: none; }
.topbar form { margin: 0; }
.container { max-width: 980px; margin: 0 auto; padding: 24px 20px 60px; }
.narrow { max-width: 460px; }

/* --- messages */
.flash { list-style: none; padding: 0; margin: 0 0 16px; }
.flash li { padding: 10px 14px; border-radius: 10px; margin-bottom: 8px; background: var(--accent-soft); }
.flash li.error { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); }
.flash li.success { background: color-mix(in srgb, var(--success) 15%, var(--surface)); }

/* --- cards */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.char-card { display: flex; flex-direction: column; gap: 10px; }
.char-card .avatar { width: 100%; aspect-ratio: 1; border-radius: 12px; object-fit: cover; background: var(--surface-2); }
.char-card .meta { flex: 1; }
.badge {
  display: inline-block; font-size: .72rem; font-weight: 600; letter-spacing: .02em;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted);
}
.badge.adult { background: color-mix(in srgb, var(--danger) 18%, var(--surface)); color: var(--danger); }
.badge.romance { background: var(--accent-soft); color: var(--accent); }

/* --- forms */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-contrast); font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: none;
}
.btn:hover { filter: brightness(1.07); text-decoration: none; }
.btn.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--border); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
form p, .field { margin: 0 0 14px; }
label { display: block; font-weight: 600; margin-bottom: 4px; font-size: .92rem; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=file],
select, textarea {
  width: 100%; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); border-color: var(--accent); }
input[type=checkbox] { width: 18px; height: 18px; vertical-align: middle; margin-right: 6px; accent-color: var(--accent); }
.helptext, .field .help { display: block; color: var(--muted); font-size: .82rem; margin-top: 3px; }
.errorlist { list-style: none; padding: 0; margin: 4px 0; color: var(--danger); font-size: .85rem; }
.form-grid { display: grid; gap: 0 18px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px 4px; margin: 0 0 18px; background: var(--surface); }
legend { font-weight: 700; padding: 0 6px; }
.checkbox-row label { display: inline; font-weight: 500; }
.lock { font-weight: 400; font-size: .8rem; color: var(--muted); display: inline-flex; align-items: center; margin-left: 8px; }
.lock input { width: 14px; height: 14px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* --- tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--text); border-color: var(--accent); font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--text); }

/* --- lists */
.list { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: flex; gap: 14px; align-items: center; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text);
}
.list-item:hover { text-decoration: none; border-color: var(--accent); }
.list-item .avatar-sm { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; background: var(--surface-2); flex: none; }
.list-item .grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
table.simple { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.simple td, table.simple th { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table-wrap { overflow-x: auto; }

/* --- chat */
body.chat-page { overflow: hidden; }
.chat-layout { display: flex; flex-direction: column; height: calc(100dvh - 53px); max-width: 820px; margin: 0 auto; }
.chat-header {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.chat-header .avatar-sm { width: 42px; height: 42px; border-radius: 50%; object-fit: cover; }
.chat-header .grow { flex: 1; min-width: 0; }
.chat-header .status { font-size: .8rem; color: var(--muted); min-height: 1.2em; }
.chat-header .status.active { color: var(--accent); }
.chat-scroll { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.load-more { align-self: center; margin-bottom: 8px; }
.msg { display: flex; flex-direction: column; max-width: min(78%, 560px); }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.character { align-self: flex-start; }
.bubble {
  padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
  background: var(--bubble-char); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.msg.user .bubble { background: var(--bubble-user); color: var(--bubble-user-text); border-color: transparent; border-bottom-right-radius: 5px; }
.msg.character .bubble { border-bottom-left-radius: 5px; }
.bubble.media { padding: 4px; }
.bubble img, .bubble video { display: block; border-radius: 12px; max-width: 100%; max-height: 420px; }
.bubble audio { display: block; width: 260px; max-width: 100%; }
.bubble .caption { padding: 6px 8px 2px; }
.bubble details { padding: 4px 8px 0; font-size: .85rem; }
.msg-meta { font-size: .72rem; color: var(--muted); margin: 2px 6px 4px; }
.msg.gap { margin-top: 10px; }
.notice { align-self: center; font-size: .82rem; color: var(--muted); background: var(--surface-2); padding: 4px 12px; border-radius: 999px; margin: 6px 0; text-align: center; }
.notice.error { color: var(--danger); }
.composer { display: flex; gap: 8px; padding: 10px 16px 14px; border-top: 1px solid var(--border); background: var(--surface); }
.composer textarea { flex: 1; min-height: 44px; max-height: 160px; border-radius: 22px; padding: 11px 16px; resize: none; }
.composer .btn { border-radius: 22px; padding: 0 18px; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; margin-right: 6px; }
.conn.on { background: var(--success); }

/* --- landing */
.hero { text-align: center; padding: 60px 0 30px; }
.hero h1 { font-size: 2.4rem; }
.hero p { font-size: 1.1rem; color: var(--muted); max-width: 560px; margin: 0 auto 24px; }
.features { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-top: 30px; }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; gap: 10px; }
  .container { padding: 16px 12px 40px; }
  .msg { max-width: 88%; }
  .chat-layout { height: calc(100dvh - 49px); }
}
