:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #16181d;
  --muted: #6b7280;
  --border: #d9dce3;
  --accent: #3b6cf6;
  --accent-text: #ffffff;
  --mine: #3b6cf6;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --theirs-text: #16181d;
  --danger: #d93a3a;
  --ok: #1f9d55;
  --shadow: 0 4px 16px rgba(0, 0, 0, .12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #20242c;
    --text: #e8eaef;
    --muted: #8b93a3;
    --border: #2a2f39;
    --accent: #5b86ff;
    --mine: #3559c7;
    --theirs: #20242c;
    --theirs-text: #e8eaef;
    --danger: #ff5c5c;
    --ok: #3ccf7a;
    --shadow: 0 4px 16px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  min-height: 38px;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.icon { background: transparent; border: none; font-size: 20px; padding: 4px 8px; min-width: 40px; }
button.icon.rec { color: var(--danger); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .4; } }
input, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  width: 100%;
  min-width: 0;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.gap { gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 8px; }

.screen { min-height: 100%; display: flex; flex-direction: column; }

/* login */
#screen-login { justify-content: center; padding: 16px; }
.login-box { width: 100%; max-width: 380px; margin: 0 auto; }
.brand { text-align: center; font-size: 30px; margin: 0 0 16px; letter-spacing: .02em; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; margin-bottom: 12px; }
.tab { flex: 1; border: none; background: transparent; }
.tab.active { background: var(--surface); box-shadow: var(--shadow); }
.form { display: flex; flex-direction: column; gap: 8px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form label input { color: var(--text); font-size: 15px; }
.form.row { flex-direction: row; }
.form.row button { flex: none; }
.hint { color: var(--muted); font-size: 12px; margin-top: -4px; }
.notice { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.api-box { margin-top: 20px; font-size: 13px; color: var(--muted); }
.api-box summary { cursor: pointer; word-break: break-all; }
.api-box form { margin-top: 8px; }

/* main */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.me-text { display: flex; flex-direction: column; min-width: 0; }
.uname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.flair {
  display: inline-block; font-size: 10px; text-transform: uppercase; letter-spacing: .05em;
  padding: 1px 6px; border-radius: 6px; border: 1px solid currentColor; margin-left: 6px; vertical-align: middle;
}
.flair.glow { text-shadow: 0 0 6px currentColor; box-shadow: 0 0 6px currentColor; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--surface-2); flex: none; image-rendering: auto; }
.avatar.big { width: 88px; height: 88px; }
.avatar.sm { width: 32px; height: 32px; }
.content { flex: 1; padding: 12px; padding-bottom: 80px; max-width: 720px; width: 100%; margin: 0 auto; }
.bottombar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.navbtn { flex: 1; border: none; border-radius: 0; background: transparent; padding: 14px 4px; color: var(--muted); position: relative; }
.navbtn.active { color: var(--accent); font-weight: 600; }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; margin-bottom: 6px;
}
.list li .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.list li .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.list li .actions button { padding: 4px 10px; min-height: 32px; font-size: 13px; }
.list li.clickable { cursor: pointer; }
.list li.clickable:active { background: var(--surface-2); }
.preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); flex: none; }
.empty { color: var(--muted); text-align: center; padding: 24px 0; }
.banner { width: 100%; aspect-ratio: 3 / 1; height: auto; border-radius: 10px; background: var(--surface-2); display: block; }
.profile-row { display: flex; gap: 14px; align-items: center; margin-top: -30px; padding: 0 10px; }
.profile-row .avatar.big { border: 3px solid var(--bg); }
.profile-row .col { margin-top: 30px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

/* chat */
#screen-chat { height: 100%; min-height: 0; }
.messages {
  flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px;
  max-width: 720px; width: 100%; margin: 0 auto;
}
.bubble {
  max-width: 82%; padding: 8px 10px; border-radius: 14px; word-wrap: break-word; white-space: pre-wrap;
  background: var(--theirs); color: var(--theirs-text); border: 1px solid var(--border); align-self: flex-start;
}
.bubble.mine { background: var(--mine); color: var(--mine-text); border-color: var(--mine); align-self: flex-end; }
.bubble canvas { display: block; max-width: 100%; height: auto; border-radius: 8px; margin: 4px 0; background: rgba(127, 127, 127, .15); }
.bubble .meta { display: block; font-size: 11px; opacity: .7; text-align: right; margin-top: 2px; }
.bubble .err { color: var(--danger); font-size: 12px; }
.bubble.mine .err { color: #ffd0d0; }
.audio-btn { background: rgba(127, 127, 127, .2); border: none; color: inherit; margin: 4px 0; }
.attachments { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 12px; max-width: 720px; width: 100%; margin: 0 auto; }
.attachments .chip {
  display: flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 8px;
  padding: 4px 4px 4px 8px; font-size: 12px; margin-bottom: 6px;
}
.attachments .chip button { min-height: 24px; padding: 0 8px; }
.composer {
  display: flex; gap: 6px; align-items: flex-end; padding: 8px; background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}
.composer textarea { flex: 1; resize: none; max-height: 120px; }

.toasts {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 90px; z-index: 50;
  display: flex; flex-direction: column; gap: 6px; width: calc(100% - 24px); max-width: 420px; pointer-events: none;
}
.toast {
  background: var(--surface); color: var(--text); border-left: 4px solid var(--danger);
  border-radius: 8px; padding: 10px 12px; box-shadow: var(--shadow); font-size: 14px; word-break: break-word;
}
.toast.info { border-left-color: var(--ok); }
.toast b { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; margin-right: 6px; }

@media (min-width: 760px) {
  .content { padding-top: 20px; }
  .bottombar { position: sticky; top: 0; order: -1; border-top: none; border-bottom: 1px solid var(--border); }
  #screen-main .topbar { position: static; }
  .toasts { bottom: 24px; }
}
