:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --text: #11181c;
  --muted: #687076;
  --border: #e2e5e9;
  --mine: #1f6feb;
  --mine-text: #ffffff;
  --theirs: #e9edf1;
  --theirs-text: #11181c;
  --peer: #8250df;          /* another person (not a session): my side, purple — Roei 11.10 */
  --peer-text: #ffffff;
  --accent: #1f6feb;
  --danger: #cf222e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --panel-2: #0d1117;
    --text: #e6edf3;
    --muted: #8b949e;
    --border: #30363d;
    --mine: #2f6feb;
    --mine-text: #ffffff;
    --theirs: #21262d;
    --theirs-text: #e6edf3;
    --peer: #8957e5;
    --peer-text: #ffffff;
    --accent: #58a6ff;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85rem; }
.spacer { flex: 1; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.95rem;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn.small { padding: 5px 9px; font-size: 0.85rem; }
.icon-btn { border: none; background: transparent; color: var(--muted); font-size: 1.2rem; cursor: pointer; padding: 4px 8px; }

/* ---- login gate ---- */
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; }
.gate-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; max-width: 360px; width: 100%; text-align: center; }
.gate-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.login-step { display: grid; gap: 6px; margin-top: 14px; text-align: start; }
.login-step label { font-size: 0.85rem; color: var(--muted); }
.login-step input { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font: inherit; font-size: 16px; direction: ltr; text-align: start; }
.login-err { margin: 12px 0 0; color: var(--danger); font-size: 0.9rem; }
.gate-card .btn { margin-top: 16px; width: 100%; }

/* ---- app shell ---- */
/* --app-h follows the visible area (app.js, visualViewport), so the compose bar is never under
   the phone keyboard (Roei 11.10); 100dvh is the fallback. */
.app { display: grid; grid-template-columns: 320px 1fr; height: var(--app-h, 100dvh); }
.sidebar { border-inline-end: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.side-head .title { font-weight: 700; }
.whoami { font-size: 0.8rem; color: var(--muted); background: none; border: 1px solid var(--border); border-radius: 8px; padding: 3px 8px; cursor: pointer; font-family: inherit; }
.side-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 16px; flex-wrap: wrap; }
.show-hidden { font-size: 0.8rem; color: var(--muted); display: flex; align-items: center; gap: 4px; }
.add-form { display: grid; gap: 6px; padding: 0 16px 10px; }
.add-form input { padding: 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel-2); color: var(--text); }

.thread-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.thread-list li { padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; display: flex; align-items: center; gap: 8px; }
.thread-list li:hover { background: var(--panel-2); }
.thread-list li.active { background: var(--panel-2); border-inline-start: 3px solid var(--accent); }
.thread-list li.retired, .thread-list li.hidden-row { opacity: 0.55; }
.thread-list .t-main { flex: 1; min-width: 0; }
.thread-list .t-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-list .t-sub { font-size: 0.75rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-list .t-actions { display: flex; gap: 2px; }
.tag { font-size: 0.65rem; padding: 1px 6px; border-radius: 999px; background: var(--border); color: var(--muted); }

/* ---- conversation ---- */
.conversation { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.conv-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
.conv-head .title { font-weight: 700; }
.source-badge { font-size: 0.7rem; color: var(--muted); }
.notice { margin: 10px 16px 0; padding: 10px 12px; border-radius: 8px; background: var(--panel); border: 1px solid var(--border); color: var(--muted); font-size: 0.85rem; }

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 78%; padding: 8px 12px; border-radius: 14px; word-wrap: break-word; white-space: pre-wrap; line-height: 1.4; }
.msg .who { font-size: 0.7rem; color: var(--muted); margin-bottom: 2px; }
.msg .time { font-size: 0.65rem; color: var(--muted); margin-top: 3px; text-align: start; }
/* Each writer's own color in --c (app.js colorFor; Yarden 11.10). People: solid bubble. Sessions: a
   light tint of their color with a colored edge, so long replies stay easy to read. */
.msg.theirs { align-self: flex-start; background: color-mix(in srgb, var(--c, var(--muted)) 14%, var(--panel)); color: var(--theirs-text); border-inline-start: 3px solid var(--c, transparent); }
.msg.theirs .who { color: color-mix(in srgb, var(--c, var(--muted)) 70%, var(--text)); font-weight: 600; }
.msg.mine { align-self: flex-end; background: var(--c, var(--mine)); color: var(--mine-text); }
.msg.mine .who, .msg.mine .time { color: rgba(255,255,255,0.8); }
.msg.peer { align-self: flex-end; background: var(--c, var(--peer)); color: var(--peer-text); }
.msg.peer .who, .msg.peer .time { color: rgba(255,255,255,0.85); }
.msg.pending { opacity: 0.6; }
/* A light person color (Gilad's תכלת, Roei 12.10): dark text on the solid bubble. */
.msg.mine.light, .msg.peer.light { color: #0b2540; }
.msg.mine.light .who, .msg.mine.light .time, .msg.peer.light .who, .msg.peer.light .time { color: rgba(11,37,64,0.75); }
.msg.light a { color: #0b4f8a; }
/* Replies (Roei 11.10): the quoted message inside a bubble, and the bar above the compose box. */
.msg .quote { display: flex; flex-direction: column; gap: 1px; border-inline-start: 3px solid var(--qc, var(--accent)); background: rgba(127,127,127,0.16); border-radius: 6px; padding: 4px 8px; margin-bottom: 4px; font-size: 0.8rem; white-space: normal; max-height: 3.6em; overflow: hidden; }
.msg .quote b { font-size: 0.75rem; }
.msg .quote { cursor: pointer; }
.msg.flash { animation: msg-flash 1.5s ease-out; }
@keyframes msg-flash { 0%, 30% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 3px transparent; } }
.reply-bar { display: flex; align-items: center; gap: 8px; padding: 6px 16px; border-top: 1px solid var(--border); background: var(--panel-2); }
.reply-bar .reply-quote { flex: 1; min-width: 0; display: flex; flex-direction: column; border-inline-start: 3px solid var(--accent); padding-inline-start: 8px; font-size: 0.8rem; }
.reply-bar .reply-quote span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg { touch-action: pan-y; }
/* Delete button on your own messages (Yarden 11.10). */
.msg { position: relative; }
.msg-del { position: absolute; top: 2px; inset-inline-start: -30px; border: none; background: transparent; cursor: pointer; font-size: 0.9rem; opacity: 0.45; padding: 4px; }
.msg-del:hover { opacity: 1; }
/* Polls under a session's question (Itay 11.10). */
.poll { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; white-space: normal; }
.poll-opt { display: flex; justify-content: space-between; align-items: center; gap: 8px; text-align: start; font: inherit; font-size: 0.9rem; padding: 8px 10px; border-radius: 10px; cursor: pointer; border: 1px solid color-mix(in srgb, var(--c, var(--accent)) 45%, transparent); background: var(--panel); color: var(--text); }
.poll-opt:hover { background: var(--panel-2); }
.poll-opt.chosen { border-color: var(--c, var(--accent)); box-shadow: inset 0 0 0 1px var(--c, var(--accent)); }
.poll-voters { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }

.compose { display: flex; gap: 8px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--panel); }
.compose textarea { flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font: inherit; background: var(--panel-2); color: var(--text); max-height: 120px; }

/* ---- mobile ---- */
.only-mobile { display: none; }
@media (max-width: 720px) {
  .app { grid-template-columns: minmax(0, 1fr); }   /* not plain 1fr: a long word widened the grid past the screen */
  .sidebar { display: flex; }
  .conversation { display: none; }
  .app.viewing .sidebar { display: none; }
  .app.viewing .conversation { display: flex; }
  .only-mobile { display: inline-block; }
  .msg { max-width: 86%; }
  /* No gap above the keyboard (Yarden 11.10, after the 5cm try): the app already shrinks to the
     visible area, so only the phone's own safe-area inset is kept. */
  .compose { padding-bottom: calc(10px + 1cm + env(safe-area-inset-bottom, 0px)); }   /* keyboard closed: 1cm higher (Yarden 11.10) */
  .kbd-open .compose { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }  /* keyboard open: flush above it */
}

/* image attachments */
.compose .attach { padding: 0 12px; font-size: 1.2rem; background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; }
.attach-previews { display: flex; gap: 8px; padding: 8px 16px 0; background: var(--panel); border-top: 1px solid var(--border); overflow-x: auto; }
.attach-previews .pv { position: relative; flex: none; }
.attach-previews img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; display: block; }
.attach-previews button { position: absolute; top: -6px; inset-inline-end: -6px; width: 22px; height: 22px; border-radius: 50%; border: none; background: #000a; color: #fff; font-size: 13px; line-height: 22px; padding: 0; cursor: pointer; }
.msg img.att { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin-top: 6px; cursor: zoom-in; }
.msg a.dl { display: inline-block; margin-top: 4px; font-size: 13px; color: inherit; opacity: .75; text-decoration: none; padding: 2px 8px; border: 1px solid currentColor; border-radius: 999px; }
.msg a.dl:hover { opacity: 1; }

/* Password/code fields are plain text inputs so the phone keyboard keeps its language switcher
   (Roei 11.10: email stays English, password in any language); characters still shown as dots. */
.masked { -webkit-text-security: disc; text-security: disc; }

/* Account panel over the app (Yarden 11.10) */
.account { background: rgba(0,0,0,0.45); z-index: 10; }
.account .gate-card .btn { margin-top: 10px; }
.login-err.ok { color: #1a7f37; }

/* Unread (Roei 11.10, WhatsApp-style) */
.thread-list .badge { min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px; background: #25d366; color: #fff; font-size: 0.75rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }
.unread-divider { align-self: center; margin: 6px 0; padding: 4px 14px; border-radius: 999px; background: var(--panel); color: var(--muted); font-size: 0.8rem; border: 1px solid var(--border); }

.link-btn { background: none; border: none; color: var(--accent); font: inherit; font-size: 0.85rem; margin-top: 12px; cursor: pointer; }

/* User management (Roei & Yarden, 11.10) */
.admin { margin-top: 22px; padding-top: 14px; border-top: 1px solid var(--border); text-align: start; }
.admin h2 { font-size: 1.05rem; margin: 0 0 6px; }
.admin-users { list-style: none; padding: 0; margin: 8px 0; }
.admin-users li { display: flex; align-items: center; justify-content: space-between; gap: 6px; font-size: 0.85rem; padding: 4px 0; border-bottom: 1px solid var(--border); }
.admin select { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel-2); color: var(--text); font: inherit; font-size: 16px; }
.account .gate-card { max-height: calc(var(--app-h, 100dvh) - 32px); overflow-y: auto; }

/* "typing…" under the thread name (Yarden 11.10) */
.conv-titles { display: flex; flex-direction: column; min-width: 0; }
.conv-titles .typing { font-size: 0.75rem; color: #1a7f37; min-height: 1em; line-height: 1.1; }

/* Yarden 11.10: a message with a long unbreakable string (hash, path, URL) made the conversation
   wider than the phone screen — text cut off on the left and the send button pushed off-screen.
   Let grid/flex children shrink, wrap anything, and never scroll the page sideways. */
html, body { overflow-x: hidden; }
.conversation, .sidebar { min-width: 0; }
.messages { overflow-x: hidden; }
.msg { overflow-wrap: anywhere; min-width: 0; }
.compose { min-width: 0; }
.compose textarea { min-width: 0; }

/* Jump-to-bottom button (Yarden 11.10, WhatsApp-style) */
.conversation { position: relative; }
.to-bottom { position: absolute; inset-inline-start: 14px; bottom: 86px; width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--border); background: var(--panel); color: var(--muted); font-size: 1.4rem; line-height: 1; box-shadow: 0 2px 8px rgba(0,0,0,0.18); cursor: pointer; z-index: 5; display: flex; align-items: center; justify-content: center; }
.to-bottom-count { position: absolute; top: -6px; inset-inline-end: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 999px; background: #25d366; color: #fff; font-size: 0.7rem; font-weight: 700; display: flex; align-items: center; justify-content: center; }

/* A long thread title is cut with "…" instead of being clipped (review 11.10). */
.conv-titles .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* Chat formatting inside messages (Yarden 11.10) */
.msg code { font-family: ui-monospace, Menlo, monospace; font-size: 0.85em; background: rgba(127,127,127,0.18); padding: 1px 4px; border-radius: 4px; }
.msg pre { font-family: ui-monospace, Menlo, monospace; font-size: 0.8rem; background: rgba(127,127,127,0.15); padding: 8px; border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; margin: 6px 0; direction: ltr; text-align: left; }

/* In-page yes/no (askYesNo, Yarden 11.10). */
.ask-back { position: fixed; inset: 0; background: rgba(0,0,0,0.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.ask-box { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 14px; padding: 20px; max-width: 340px; width: 100%; text-align: center; }
.ask-box p { margin: 0 0 16px; line-height: 1.5; }
.ask-row { display: flex; gap: 10px; justify-content: center; }
.ask-row .btn { min-width: 90px; }
/* Permission popup (Yarden 12.10): wider, with the request details. */
.perm-box { max-width: 520px; text-align: start; }
.perm-box h3 { margin: 0 0 8px; }
.perm-box .perm-tool { font-weight: 600; margin-bottom: 8px; }
.perm-preview { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px; max-height: 40vh; overflow: auto; white-space: pre-wrap; word-break: break-word; direction: ltr; text-align: left; font-size: 0.8rem; margin: 0 0 8px; }
.perm-box .perm-when { font-size: 0.75rem; color: var(--muted); margin-bottom: 12px; }
.perm-box .perm-err { color: #cf222e; margin: 8px 0 0; min-height: 1em; }
