:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel2: #eef1f6;
  --border: #dde2ea;
  --text: #1b2130;
  --muted: #6b7482;
  --accent: #2f6fed;
  --accent-d: #245ad0;
  --danger: #e5484d;
  --ok: #2fa25a;
  --warn: #c77700;
  --in: #eef1f6;
  --out: #2f6fed;
  --radius: 10px;
}

/* Dark theme (original black). */
[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel2: #1d212b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #8b93a3;
  --accent: #4f8cff;
  --accent-d: #3a6fd8;
  --danger: #ff5c6c;
  --ok: #35c46a;
  --warn: #ffb020;
  --in: #232833;
  --out: #274690;
}

/* Soft green theme. */
[data-theme="green"] {
  --bg: #e8f4ea;
  --panel: #f6fcf7;
  --panel2: #dcefe0;
  --border: #cae0cf;
  --text: #16281c;
  --muted: #5a6f60;
  --accent: #2f9e57;
  --accent-d: #248246;
  --danger: #c23b41;
  --ok: #2f9e57;
  --warn: #a76a08;
  --in: #dcefe0;
  --out: #34a56a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex;
  flex-direction: column;
}
.hidden { display: none !important; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.fineprint { color: var(--muted); font-size: 12px; }
.warn-text { color: var(--warn); }
.mt { margin-top: 12px; }

#app { flex: 1; min-height: 0; display: flex; }

/* buttons + inputs */
.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-size: 14px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-d); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.tiny { padding: 3px 8px; font-size: 12px; margin-left: 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 18px; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { color: var(--text); background: var(--panel2); }
.input, .composer-input, .burn-sel, select, textarea {
  width: 100%; background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; margin: 6px 0; font-size: 14px; font-family: inherit;
}
.input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
.codebox { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 10px; word-break: break-all; margin: 4px 0; }
.codebox.small { font-size: 12px; }

/* onboarding */
.onboarding { margin: auto; max-width: 440px; padding: 32px; text-align: center; }
.onboarding h1 { font-size: 24px; }
.admin-wide { max-width: 640px; }
.sep { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.admin-setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); text-align: left; }
.admin-setting label { font-size: 13px; }
.admin-setting .hint { color: var(--muted); font-size: 11px; }
.admin-setting input { width: 120px; margin: 0; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; text-align: center; }
.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.stat-num { font-size: 22px; font-weight: 600; }
.stat-label { font-size: 12px; color: var(--muted); }
.onboarding .btn { width: 100%; margin-top: 8px; }

/* layout */
.layout { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 14px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.brand { font-weight: 600; display: inline-flex; align-items: center; gap: 7px; }
.brand-icon { width: 24px; height: 24px; display: block; border-radius: 6px; }
.theme-row { display: flex; gap: 8px; margin: 6px 0; }
.theme-opt { flex: 1; cursor: pointer; border: 2px solid var(--border); border-radius: 8px; padding: 10px 8px; text-align: center; font-size: 13px; background: var(--panel2); }
.theme-opt.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.theme-swatch { display: inline-block; width: 100%; height: 22px; border-radius: 5px; margin-bottom: 6px; border: 1px solid rgba(128,128,128,.35); }
.sw-light { background: #f4f6f9; }
.sw-dark { background: #0f1115; }
.sw-green { background: #e8f4ea; }
.me { margin-left: auto; text-align: right; cursor: pointer; line-height: 1.2; }
.me-nick { display: block; font-size: 13px; }
.me-id { display: block; font-size: 11px; color: var(--muted); }
.conn-badge { font-size: 12px; padding: 3px 8px; border-radius: 20px; }
.conn-badge.on { color: var(--ok); }
.conn-badge.off { color: var(--muted); }

/* identity switcher (top-right squares) */
.id-switcher { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; max-width: 180px; justify-content: flex-end; }
.id-square { width: 30px; height: 30px; border-radius: 7px; border: 1px solid var(--border); background: var(--panel2); color: var(--text); cursor: pointer; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; padding: 0; }
.id-square:hover { border-color: var(--accent); }
.id-square.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.id-square.eph { border-style: dashed; }
.id-square.add { color: var(--muted); font-weight: 400; }

/* identity chooser (entry screen) */
.id-list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.id-choice { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel2); color: var(--text); cursor: pointer; text-align: left; width: 100%; }
.id-choice:hover { border-color: var(--accent); }
.id-choice-nick { font-weight: 600; }
.id-choice-id { font-size: 11px; color: var(--muted); }

.body { flex: 1; display: flex; min-height: 0; }
.sidebar { width: 300px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.pane { flex: 1; display: flex; min-width: 0; }

.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab { flex: 1; background: transparent; border: none; color: var(--muted); padding: 12px; cursor: pointer; font-size: 14px; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.sidebar .btn.small { margin: 10px; }
.list { flex: 1; overflow-y: auto; }
.empty { padding: 20px; color: var(--muted); font-size: 13px; }

.row { display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.row:hover { background: var(--panel2); }
.row.sel { background: var(--panel2); border-left: 3px solid var(--accent); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 14px; display: flex; align-items: center; gap: 6px; }
.row-sub { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--muted); opacity: .5; }
.dot.grp { width: auto; height: auto; border-radius: 6px; background: var(--panel2); padding: 2px 6px; font-size: 11px; }
.verified { color: var(--ok); font-size: 12px; }
.unverified { color: var(--warn); font-size: 12px; }
.owner-tag { color: var(--accent); font-size: 11px; }
.badge { background: var(--danger); color: #fff; font-size: 10px; padding: 1px 6px; border-radius: 10px; }

/* chat */
.empty-pane { margin: auto; color: var(--muted); }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head { display: flex; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
.chat-title { font-weight: 600; }
.chat-sub { font-size: 11px; color: var(--muted); }
.chat-actions { margin-left: auto; display: flex; gap: 8px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; }
.msg.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 4px; }
.msg.burned { opacity: .7; }
.msg-body { white-space: pre-wrap; word-break: break-word; }
.burned-body { font-style: italic; color: var(--muted); }
.msg-img { max-width: 260px; max-height: 320px; border-radius: 8px; display: block; }
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 4px; display: flex; flex-wrap: wrap; gap: 2px; }
.msg-sender { color: var(--accent); margin-right: 4px; }
.burn { color: var(--warn); }
/* Blue outgoing bubble needs light text for contrast. */
.msg.out .msg-body { color: #fff; }
.msg.out .msg-meta, .msg.out .msg-state, .msg.out .burn { color: rgba(255,255,255,.85); }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--border); background: var(--panel); }
.composer-input { flex: 1; resize: none; max-height: 120px; margin: 0; }
.burn-sel { width: auto; margin: 0; }

/* member/app lists */
.member-list, .app-list { max-height: 240px; overflow-y: auto; }
.member-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; gap: 8px; }

/* modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,20,30,.45); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); width: 100%; max-width: 460px; max-height: 86vh; overflow-y: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px; }
.modal-body h4 { margin: 14px 0 6px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }

/* toast */
.toast { position: fixed; bottom: 76px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--panel2); border: 1px solid var(--border); color: var(--text); padding: 10px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--danger); }
.toast.warn { border-color: var(--warn); }

/* footer */
.app-footer { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; padding: 6px 16px; background: var(--panel); border-top: 1px solid var(--border); font-size: 11px; color: var(--muted); }

/* responsive */
/* Back button: hidden on desktop, shown on mobile single-view. */
.back-btn { display: none; font-size: 26px; line-height: 1; padding: 0 8px 4px; }
.chat-head-main { min-width: 0; flex: 1; }
.chat-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Phones (narrow width) OR any short viewport (landscape phone): show ONE view
   at a time — the list, or the open chat with a back button. */
@media (max-width: 820px), (max-height: 520px) {
  .body { flex-direction: column; position: relative; }
  .sidebar { width: 100%; max-height: none; flex: 1; border-right: none; }
  .pane { display: none; }
  .body.chat-open .sidebar { display: none; }
  .body.chat-open .pane { display: flex; flex: 1; min-height: 0; }
  .back-btn { display: inline-flex; }
  .msg { max-width: 88%; }
  .me-id { display: none; }
}

/* Landscape phones are very short: reclaim vertical space. */
@media (max-height: 520px) {
  .app-footer { display: none; }
  .topbar { padding: 6px 12px; }
  .messages { padding: 8px 12px; gap: 6px; }
  .composer { padding: 6px 10px; }
}
