:root {
  --bg: #f0f2f5; --panel: #ffffff; --line: #e3e6ea; --text: #1c2328; --muted: #667781;
  --accent: #128c7e; --accent-ink: #ffffff; --me: #d9fdd3; --them: #ffffff; --chat-bg: #efeae2;
  --danger: #c0392b; --read: #34b7f1;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a; --panel: #111b21; --line: #26343d; --text: #e9edef; --muted: #8696a0;
    --accent: #00a884; --me: #005c4b; --them: #202c33; --chat-bg: #0b141a;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: system-ui, "Segoe UI", Arial, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; }

/* login */
#login { display: grid; place-items: center; height: 100%; padding: 16px; }
#login form { background: var(--panel); padding: 28px; border-radius: 14px; width: min(360px, 100%); display: grid; gap: 12px; box-shadow: 0 4px 20px rgba(0,0,0,.08); }
#login h1 { margin: 0 0 6px; font-size: 22px; }

/* app shell */
#app { display: grid; grid-template-rows: auto 1fr; height: 100%; }
header { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
header .brand { font-weight: 700; }
header .spacer { flex: 1; }
.main { display: grid; grid-template-columns: 340px 1fr; min-height: 0; }

/* sidebar */
.sidebar { background: var(--panel); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar .tools { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--line); }
.sidebar .tools input { flex: 1; min-width: 0; }
.chatlist { overflow-y: auto; flex: 1; }
.chatitem { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 11px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.chatitem:hover, .chatitem.active { background: var(--bg); }
.chatitem .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatitem .time { font-size: 12px; color: var(--muted); }
.chatitem .last { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; font-size: 12px; padding: 1px 7px; justify-self: end; }

/* conversation */
.conv { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.conv .head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.conv .head .title { font-weight: 600; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 14px 6%; display: flex; flex-direction: column; gap: 4px; }
.bubble { max-width: min(75%, 560px); padding: 6px 9px 4px; border-radius: 9px; background: var(--them); align-self: flex-start; box-shadow: 0 1px .5px rgba(0,0,0,.13); white-space: pre-wrap; word-wrap: break-word; }
.bubble.me { background: var(--me); align-self: flex-end; }
.bubble .who { font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.bubble .meta { font-size: 11px; color: var(--muted); text-align: end; margin-top: 2px; display: flex; gap: 6px; justify-content: flex-end; }
.bubble .tick.read { color: var(--read); }
.bubble.deleted .text { font-style: italic; color: var(--muted); }
.bubble img, .bubble video { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.bubble audio { max-width: 260px; }
.loadmore { align-self: center; margin-bottom: 8px; }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 20px; }
.composer { display: flex; gap: 8px; padding: 10px; background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; height: 44px; max-height: 140px; }
.readonly-note { padding: 12px; text-align: center; background: var(--panel); border-top: 1px solid var(--line); color: var(--muted); }

dialog { border: none; border-radius: 12px; padding: 20px; background: var(--panel); color: var(--text); width: min(420px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { display: grid; gap: 10px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 760px) {
  .main { grid-template-columns: 1fr; }
  .main.in-chat .sidebar { display: none; }
  .main:not(.in-chat) .conv { display: none; }
  .back { display: inline-block; }
  .messages { padding: 10px; }
  .bubble { max-width: 88%; }
}

/* MFA */
#mfaForm { background: var(--panel); padding: 28px; border-radius: 14px; width: min(360px, 100%); display: grid; gap: 12px; box-shadow: 0 4px 20px rgba(0,0,0,.08); }
#mfaForm h1 { margin: 0; font-size: 22px; }
.code-input { font-size: 22px; letter-spacing: 6px; text-align: center; font-variant-numeric: tabular-nums; }
.code-input::placeholder { letter-spacing: normal; font-size: 15px; }
.mfa-tabs { display: flex; gap: 6px; }
.mfa-tabs button { flex: 1; }
.mfa-tabs button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; }
dialog.wide { width: min(560px, 94vw); }
.sec { display: grid; gap: 14px; }
.sec-block { border: 1px solid var(--line); border-radius: 10px; padding: 14px; display: grid; gap: 8px; }
.sec-block p { margin: 0; font-size: 13px; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tag.on { background: var(--accent); color: var(--accent-ink); }
.notice { background: #fff3cd; color: #664d03; padding: 10px; border-radius: 8px; }
.qr { display: block; width: 200px; height: 200px; background: #fff; padding: 6px; border-radius: 8px; margin: 6px auto; }
.secret { display: block; text-align: center; font-size: 13px; word-break: break-all; padding: 6px; background: var(--bg); border-radius: 6px; }
.setup { display: grid; gap: 8px; margin-top: 8px; }
.setup ol { margin: 0; padding-inline-start: 20px; font-size: 13px; }

/* admin */
.admin { max-width: 1100px; margin: 0 auto; padding: 16px; display: grid; gap: 18px; }
.card { background: var(--panel); border-radius: 12px; padding: 16px; border: 1px solid var(--line); overflow-x: auto; }
.card h2 { margin: 0 0 12px; font-size: 18px; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: start; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.inline-form input { min-width: 140px; }
.perm-cell { white-space: nowrap; }
.perm-cell label { margin-inline-end: 8px; font-size: 13px; }
.tag { font-size: 12px; padding: 2px 7px; border-radius: 6px; background: var(--bg); }
pre.json { background: var(--bg); padding: 10px; border-radius: 8px; max-height: 240px; overflow: auto; direction: ltr; text-align: left; }
.ltr { direction: ltr; unicode-bidi: plaintext; }
