:root {
  --bg: #f3f6f6; --card: #ffffff; --text: #17201f; --muted: #5f6f6d; --border: #dde5e4;
  --accent: #0f766e; --accent-ink: #ffffff; --accent-soft: #e0f2ef; --in-bubble: #ebefee; --bad: #c2410c; --unread: #e11d48;
  --shadow: 0 8px 28px rgba(15, 40, 38, 0.16);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1514; --card: #172120; --text: #e6eeec; --muted: #93a5a2; --border: #26332f; --accent: #14b8a6; --accent-ink: #04201d; --accent-soft: #123a35; --in-bubble: #22302d; --bad: #fb923c; --shadow: 0 8px 28px rgba(0, 0, 0, 0.5); }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; color: inherit; }
input, textarea { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; }
input:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 13px; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.btn { border: 1px solid var(--border); background: var(--card); border-radius: 10px; padding: 9px 14px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.wide { width: 100%; padding: 12px; }
.btn:disabled { opacity: 0.6; cursor: default; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px 0; font-weight: 600; }
.icon-btn { background: none; border: 0; font-size: 22px; line-height: 1; padding: 8px; cursor: pointer; color: var(--muted); border-radius: 10px; }
.badge { background: var(--unread); color: #fff; border-radius: 999px; font-size: 11px; font-weight: 700; padding: 1px 7px; min-width: 20px; text-align: center; }
.form-error { color: var(--bad); font-size: 14px; min-height: 1em; }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--card); border: 1px solid var(--border); border-radius: 18px; padding: 28px 22px; display: grid; gap: 12px; box-shadow: var(--shadow); }
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card p { margin: 0; }
.auth-logo { width: 56px; height: 56px; border-radius: 14px; }
.auth-card label { display: grid; gap: 5px; font-size: 13px; font-weight: 600; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); }
label.check input { width: auto; }

/* app shell */
#appView { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.top { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--card); border-bottom: 1px solid var(--border); }
.num-pick { display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; column-gap: 8px; align-items: center; text-align: left; background: none; border: 0; padding: 6px 8px; border-radius: 12px; cursor: pointer; min-width: 0; }
.num-pick:hover { background: var(--bg); }
.num-pick-label { font-weight: 800; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.num-pick-sub { grid-row: 2; font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.num-pick .badge { grid-row: 1; grid-column: 2; justify-self: start; }
.num-pick .caret { grid-row: 2; grid-column: 2; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 2px; }
.num-menu { position: absolute; top: 100%; left: 8px; right: 8px; max-width: 380px; max-height: 70vh; overflow: auto; background: var(--card); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); z-index: 20; padding: 6px; }
.num-opt { display: flex; align-items: center; gap: 10px; width: 100%; background: none; border: 0; text-align: left; padding: 10px; border-radius: 10px; cursor: pointer; }
.num-opt:hover, .num-opt.on { background: var(--accent-soft); }
.num-opt-main { flex: 1; min-width: 0; }
.num-opt-main b { display: block; }
.num-opt-main span { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.banner { background: #fef3c7; color: #78350f; padding: 8px 14px; font-size: 14px; }

.panes { flex: 1; display: grid; grid-template-columns: 1fr; min-height: 0; }
.list-pane { display: flex; flex-direction: column; min-height: 0; background: var(--card); }
.list-tools { display: flex; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.conv-list { overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 42px 1fr auto; gap: 10px; align-items: center; width: 100%; background: none; border: 0; border-bottom: 1px solid var(--border); padding: 11px 12px; text-align: left; cursor: pointer; }
.conv:hover { background: var(--bg); }
.conv.on { background: var(--accent-soft); }
.avatar { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; color: #fff; font-size: 15px; }
.conv-main { min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-when { font-size: 12px; color: var(--muted); white-space: nowrap; }
.conv-last { font-size: 13.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .conv-name, .conv.unread .conv-last { color: var(--text); font-weight: 800; }
.conv-empty { padding: 30px 16px; text-align: center; color: var(--muted); }
.search-hit-num { font-size: 11.5px; color: var(--accent); font-weight: 700; }

.thread-pane { display: none; min-height: 0; background: var(--bg); }
.thread-empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 20px; text-align: center; }
.thread { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; background: var(--card); border-bottom: 1px solid var(--border); }
.thread-who { flex: 1; min-width: 0; }
.who-btn { background: none; border: 0; text-align: left; cursor: pointer; padding: 2px 4px; display: grid; }
#whoName { font-weight: 800; font-size: 16px; }
.who-sub { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.new-to { font-size: 16px; }
.thread-from { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.msgs { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0 4px; font-weight: 600; }
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.bubble.in { align-self: flex-start; background: var(--in-bubble); border-bottom-left-radius: 6px; }
.bubble.out { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 6px; }
.bubble.failed { background: transparent; color: var(--bad); border: 1px solid var(--bad); }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 12px; margin: 2px 0; }
.bubble a { color: inherit; }
.meta { font-size: 11px; color: var(--muted); margin: 0 6px 6px; }
.meta.out { align-self: flex-end; } .meta.in { align-self: flex-start; }
.load-more { align-self: center; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); background: var(--card); border-top: 1px solid var(--border); }
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; padding: 10px 14px; }
.send-btn { width: 42px; height: 42px; flex: 0 0 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 18px; cursor: pointer; }
.send-btn:disabled { opacity: 0.5; }
.composer-meta { font-size: 11.5px; color: var(--muted); padding: 0 16px 6px; background: var(--card); min-height: 0; }
.composer-meta:empty { display: none; }

/* phone: one pane at a time */
body.in-thread .list-pane { display: none; }
body.in-thread .thread-pane { display: block; }
@media (min-width: 800px) {
  .panes { grid-template-columns: minmax(300px, 380px) 1fr; }
  .list-pane { border-right: 1px solid var(--border); }
  .thread-pane, body.in-thread .thread-pane { display: block; }
  body.in-thread .list-pane { display: flex; }
  .back { display: none; }
}

/* sheets (settings / admin) */
.sheet { position: fixed; inset: 0; background: rgba(8, 20, 18, 0.45); z-index: 50; overflow-y: auto; padding: 20px 12px; }
.sheet-card { background: var(--card); border-radius: 18px; max-width: 520px; margin: 0 auto; padding: 18px 18px 24px; box-shadow: var(--shadow); }
.sheet-card.wide { max-width: 760px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head h2 { margin: 0; }
.sheet h3 { margin: 22px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.stack { display: grid; gap: 8px; }
.toggle-list { display: grid; gap: 6px; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; }
.toggle-row span b { display: block; } .toggle-row span small { color: var(--muted); }
.switch { position: relative; width: 46px; height: 28px; flex: 0 0 46px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch i { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: background 0.15s; pointer-events: none; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; background: #fff; border-radius: 50%; transition: transform 0.15s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }
.switch input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }
.codes { background: var(--bg); border: 1px dashed var(--border); border-radius: 10px; padding: 12px; font-size: 16px; letter-spacing: 0.05em; }
.tabs { display: flex; gap: 4px; margin: 14px 0; border-bottom: 1px solid var(--border); }
.tab { background: none; border: 0; padding: 9px 12px; cursor: pointer; font-weight: 700; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.on { color: var(--accent); border-bottom-color: var(--accent); }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.card-row { border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-top: 10px; display: grid; gap: 6px; }
.card-row-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.pill { font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); }
.pill.ok { color: #15803d; border-color: rgba(21,128,61,0.4); background: rgba(21,128,61,0.08); }
.pill.warn { color: var(--bad); border-color: rgba(194,65,12,0.4); background: rgba(194,65,12,0.08); }
.user-form { border: 1px solid var(--accent); border-radius: 14px; padding: 14px; margin: 12px 0; display: grid; gap: 10px; }
.user-form .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (max-width: 520px) { .user-form .grid2 { grid-template-columns: 1fr; } }
.num-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 6px; }
.num-checks label { display: flex; gap: 8px; align-items: center; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; }
.num-checks input, .user-form label.check input { width: auto; }
.label-input { max-width: 240px; padding: 7px 10px; }
.act { font-size: 13.5px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.act b { font-weight: 700; } .act time { color: var(--muted); font-size: 12px; float: right; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* 2026-10-08: long words/links no longer push the page sideways; picture attach */
.panes { grid-template-columns: minmax(0, 1fr); }
@media (min-width: 800px) { .panes { grid-template-columns: minmax(300px, 380px) minmax(0, 1fr); } }
.thread-pane, .thread, .msgs, .list-pane { min-width: 0; }
.msgs { overflow-x: hidden; }
.bubble { min-width: 0; word-break: break-word; }
.toggle-row span small { display: block; font-size: 12.5px; }
.attach-btn { width: 42px; height: 42px; flex: 0 0 42px; border-radius: 50%; border: 1px solid var(--border); background: var(--card); font-size: 18px; cursor: pointer; }
.attach-preview { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--card); border-top: 1px solid var(--border); }
.attach-preview img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.attach-preview span { flex: 1; }
.num-opt-hidden { color: var(--muted); font-size: 13px; justify-content: center; border-top: 1px solid var(--border); border-radius: 0 0 10px 10px; margin-top: 4px; }
/* 2026-10-08: a long last message stretched its row past the screen on the conversation list */
.conv { grid-template-columns: 42px minmax(0, 1fr) auto; max-width: 100%; }
.conv-main, .conv-top, .conv-name, .conv-last { min-width: 0; }
.conv-last, .search-hit-num { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-list, .list-pane, .list-tools { min-width: 0; max-width: 100%; overflow-x: hidden; }
.list-tools input { min-width: 0; }
.top, .num-pick { min-width: 0; max-width: 100%; }
.num-pick { overflow: hidden; }

/* message menu + toast + send-from picker (2026-10-08) */
.bubble { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.bubble.picked { outline: 2px solid var(--accent); outline-offset: 2px; }
.msg-menu { position: fixed; z-index: 60; background: var(--card); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 4px; display: grid; min-width: 170px; }
.msg-menu button { background: none; border: 0; text-align: left; padding: 10px 12px; border-radius: 8px; font-size: 15px; cursor: pointer; }
.msg-menu button:hover, .msg-menu button:focus-visible { background: var(--accent-soft); outline: none; }
.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 12px); background: #17201f; color: #fff; padding: 8px 16px; border-radius: 999px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 0.15s, transform 0.15s; z-index: 70; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }
.from-sel { width: auto; max-width: 150px; padding: 4px 6px; font-size: 13px; font-weight: 700; border-radius: 8px; }

.repeat { display: inline-block; margin-left: 8px; font-size: 11px; font-weight: 800; padding: 0 6px; border-radius: 999px; background: rgba(0, 0, 0, 0.08); vertical-align: 1px; }
.bubble.out .repeat { background: rgba(255, 255, 255, 0.25); }

/* instant send (2026-10-08) */
.bubble.pending { opacity: 0.75; }
.sending { animation: sendingPulse 1.2s ease-in-out infinite; }
@keyframes sendingPulse { 50% { opacity: 0.45; } }
@media (prefers-reduced-motion: reduce) { .sending { animation: none; } }
.meta .retry { font-size: 11px; padding: 0; }
