/* Family Chat — phone-first, system fonts only, no external assets. */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f6f4ef; --surface: #ffffff; --surface-2: #efede6; --text: #1c1c1a;
  --muted: #6f6d66; --border: #e2ded2; --accent: #2f7d5b; --accent-text: #ffffff;
  --bubble-me: #d9efe2; --bubble-them: #ffffff; --danger: #c0392b;
  --radius: 16px; --tap: 48px;
}
[data-theme="dark"] {
  --bg: #171916; --surface: #222622; --surface-2: #2c302c; --text: #f0efe9;
  --muted: #a9a79e; --border: #3a3f3a; --accent: #4caf83; --accent-text: #0c1a13;
  --bubble-me: #274d38; --bubble-them: #2c302c; --danger: #e57373;
}
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.45;
  min-height: 100vh;
}
#app { max-width: 480px; margin: 0 auto; min-height: 100vh; background: var(--bg);
  display: flex; flex-direction: column; position: relative; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer;
  min-height: var(--tap); }
input, textarea, select { font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; padding: 12px; width: 100%; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
a { color: var(--accent); }
img, video { max-width: 100%; display: block; }
svg { flex-shrink: 0; }

/* Header */
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20; min-height: 56px; }
.topbar h1 { font-size: 20px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { width: var(--tap); height: var(--tap); border-radius: 50%; display: inline-flex;
  align-items: center; justify-content: center; font-size: 20px; flex-shrink: 0; }
.icon-btn:active { background: var(--surface-2); }
.back-btn { font-size: 22px; width: var(--tap); }

/* Generic layout */
.page { flex: 1; display: flex; flex-direction: column; padding: 16px 12px 24px; gap: 12px;
  overflow-y: auto; }
.page.center { justify-content: center; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 13px; }
.btn { display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--accent); color: var(--accent-text); border-radius: 999px;
  padding: 14px 20px; font-weight: 700; font-size: 17px; width: 100%; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
label.field { display: block; font-weight: 600; margin-bottom: 4px; }
.field-wrap { display: flex; flex-direction: column; gap: 4px; }
h2 { font-size: 22px; }
h3 { font-size: 17px; margin-bottom: 6px; }

/* Avatar */
.avatar { width: 48px; height: 48px; border-radius: 50%; display: inline-flex; align-items: center;
  justify-content: center; font-size: 24px; color: #fff; flex-shrink: 0; overflow: hidden;
  background: var(--accent); }
.avatar.lg { width: 84px; height: 84px; font-size: 40px; }
.avatar.sm { width: 32px; height: 32px; font-size: 16px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-row { display: flex; align-items: center; gap: 12px; }
.emoji-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.emoji-grid button { font-size: 28px; min-height: var(--tap); border-radius: 12px;
  border: 2px solid transparent; }
.emoji-grid button.sel { border-color: var(--accent); background: var(--surface-2); }
.color-row { display: flex; gap: 10px; flex-wrap: wrap; }
.color-dot { width: 44px; height: 44px; border-radius: 50%; border: 3px solid transparent; }
.color-dot.sel { border-color: var(--text); }

/* Chat list */
.search-box { position: sticky; top: 0; z-index: 5; }
.chat-row { display: flex; gap: 12px; align-items: center; padding: 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); width: 100%; text-align: left; }
.chat-row .meta { flex: 1; min-width: 0; }
.chat-row .name { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .last { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px;
  min-width: 24px; height: 24px; padding: 0 7px; display: inline-flex; align-items: center;
  justify-content: center; font-size: 13px; font-weight: 700; flex-shrink: 0; }
.time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.fab { position: fixed; bottom: 20px; right: max(20px, calc(50% - 240px + 20px)); width: 60px; height: 60px;
  border-radius: 50%; background: var(--accent); color: var(--accent-text); font-size: 32px;
  display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 14px rgba(0,0,0,.25); z-index: 30; }

/* Messages */
.msgs { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 82%; padding: 10px 12px; border-radius: 16px; background: var(--bubble-them);
  border: 1px solid var(--border); align-self: flex-start; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.me { background: var(--bubble-me); align-self: flex-end; border-color: transparent; }
.msg .sender { font-size: 13px; font-weight: 700; margin-bottom: 2px; }
.msg .text { white-space: pre-wrap; }
.msg .meta-line { display: flex; gap: 6px; align-items: center; justify-content: flex-end;
  font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg .meta-line .ticks { font-size: 14px; }
.msg.deleted .text { color: var(--muted); font-style: italic; }
.msg .fwd { font-size: 12px; color: var(--muted); font-style: italic; margin-bottom: 2px; }
.reply-quote { border-left: 3px solid var(--accent); padding: 4px 8px; background: var(--surface-2);
  border-radius: 8px; font-size: 13px; margin-bottom: 6px; color: var(--muted); }
.reactions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.reaction { background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 2px 8px; font-size: 14px; min-height: 28px; }
.reaction.mine { border-color: var(--accent); background: var(--bubble-me); }
.msg-media { border-radius: 12px; overflow: hidden; margin-bottom: 6px; cursor: pointer; }
.msg-media img, .msg-media video { width: 100%; max-height: 320px; object-fit: cover; }
.file-chip { display: flex; gap: 8px; align-items: center; background: var(--surface-2);
  border-radius: 10px; padding: 10px; margin-bottom: 6px; }
.pin-flag, .star-flag { font-size: 12px; color: var(--muted); }
.typing { color: var(--muted); font-size: 13px; font-style: italic; padding: 0 14px 6px; min-height: 20px; }
.seen-by { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* Composer */
.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 8px 10px 12px; }
.reply-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: var(--surface-2); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; font-size: 14px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-row textarea { flex: 1; resize: none; max-height: 120px; min-height: var(--tap); }
.send-btn { width: 52px; height: 52px; border-radius: 50%; background: var(--accent);
  color: var(--accent-text); font-size: 22px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.attach-btn { width: var(--tap); height: var(--tap); border-radius: 50%; flex-shrink: 0; font-size: 20px; }

/* Sheets, modals, toast */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--surface); border-radius: 20px 20px 0 0; width: 100%; max-width: 480px;
  padding: 12px 16px calc(20px + env(safe-area-inset-bottom)); max-height: 80vh; overflow-y: auto; }
.sheet .grab { width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 0 auto 12px; }
.sheet-btn { display: flex; gap: 12px; align-items: center; width: 100%; text-align: left;
  padding: 6px 4px; font-size: 17px; }
.sheet-btn.danger { color: var(--danger); }
.emoji-row { display: flex; gap: 4px; justify-content: space-between; padding: 8px 0; }
.emoji-row button { font-size: 28px; min-height: var(--tap); flex: 1; border-radius: 12px; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 60;
  display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal { background: var(--surface); border-radius: 20px; padding: 20px; width: 100%; max-width: 440px;
  max-height: 85vh; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 24px; z-index: 99;
  background: #222; color: #fff; padding: 12px 18px; border-radius: 999px; font-size: 15px;
  max-width: 90vw; text-align: center; display: none; box-shadow: 0 4px 16px rgba(0,0,0,.3); }
#toast.show { display: block; }
[data-theme="dark"] #toast { background: #eee; color: #111; }

/* Poll */
.poll { background: var(--surface-2); border-radius: 12px; padding: 10px; margin-bottom: 6px; }
.poll-q { font-weight: 700; margin-bottom: 8px; }
.poll-opt { display: flex; justify-content: space-between; gap: 8px; width: 100%; text-align: left;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px;
  background: var(--surface); min-height: 40px; }
.poll-opt.voted { border-color: var(--accent); background: var(--bubble-me); font-weight: 700; }
.poll-bar { height: 6px; border-radius: 3px; background: var(--accent); opacity: .35; margin-top: 4px; }

/* Lists */
.person-row, .member-row { display: flex; gap: 12px; align-items: center; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  width: 100%; text-align: left; }
.person-row .meta, .member-row .meta { flex: 1; min-width: 0; }
.person-row .name, .member-row .name { font-weight: 700; }
.check { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--border);
  display: inline-flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.check.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.toggle-row { display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px 4px; border-bottom: 1px solid var(--border); }
.toggle-row:last-child { border-bottom: none; }
.toggle { width: 56px; height: 32px; border-radius: 999px; background: var(--surface-2);
  border: 1px solid var(--border); position: relative; flex-shrink: 0; min-height: 32px; }
.toggle::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px;
  border-radius: 50%; background: var(--muted); transition: left .15s; }
.toggle.on { background: var(--accent); }
.toggle.on::after { left: 27px; background: #fff; }
.code-box { font-size: 28px; letter-spacing: 6px; font-weight: 800; text-align: center;
  background: var(--surface-2); border-radius: 12px; padding: 14px; }
.invite-link { word-break: break-all; background: var(--surface-2); border-radius: 10px; padding: 10px; font-size: 14px; }

/* Viewer */
.viewer { position: fixed; inset: 0; background: #000; z-index: 80; display: flex;
  flex-direction: column; }
.viewer-bar { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; color: #fff; }
.viewer-body { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.viewer-body img, .viewer-body video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-body iframe { width: 100%; height: 100%; border: none; background: #fff; }

/* Retry / error */
.retry-screen { text-align: center; padding: 40px 20px; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.error-box { background: var(--surface); border: 1px solid var(--danger); border-radius: 12px;
  padding: 12px; font-size: 14px; word-break: break-word; text-align: left; width: 100%; }

/* Large text mode */
.large-text body, body.large-text { font-size: 19px; }
body.large-text .msg .text, body.large-text .chat-row .last { font-size: 17px; }
body.large-text h1 { font-size: 22px; }
body.large-text h2 { font-size: 25px; }

/* Minimal mode: hide everything except chats + send box */
body.minimal .topbar .icon-btn:not(.keep), body.minimal .fab,
body.minimal .search-box, body.minimal .attach-btn { display: none; }
body.minimal .composer-row textarea { border-radius: 999px; }
