/* Texts app. One pane at a time on a phone (list, then conversation); side by side on a
   wider screen. Colors follow the device's light/dark setting. */
:root {
  color-scheme: light;
  --brand: #16a34a;
  --brand-dark: #12803b;
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f3f5f4;
  --border: #e3e7e5;
  --text: #17201b;
  --muted: #66716b;
  --bubble-in: #eceff0;
  --bubble-in-text: #17201b;
  --bubble-out: #16a34a;
  --bubble-out-text: #ffffff;
  --unread: #16a34a;
  --danger: #d43c3c;
  --overlay: rgba(0, 0, 0, .45);
  --shadow: 0 6px 24px rgba(0, 0, 0, .16);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --brand: #22c55e;
    --brand-dark: #16a34a;
    --bg: #0f1311;
    --surface: #151a17;
    --surface-2: #1d2420;
    --border: #2a332e;
    --text: #e7ece9;
    --muted: #93a099;
    --bubble-in: #252d29;
    --bubble-in-text: #e7ece9;
    --bubble-out: #15803d;
    --bubble-out-text: #ffffff;
    --unread: #22c55e;
    --danger: #f06262;
    --overlay: rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: currentColor; display: block; }
/* Safari's button padding can squash a shrinkable icon down to a dot */
button > svg, a > svg, label > svg { flex-shrink: 0; }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface-2); text-decoration: none; color: var(--text);
}
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.btn-danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border: 0; border-radius: 50%; background: none; color: inherit; flex: none;
}
.icon-btn:hover { background: var(--surface-2); }

/* Installed iPhone app: iOS 26+ blurs the top of the screen. What stops it (tested in
   /blurtest/, see IOS_TOP_BLUR.md) is a fixed full-screen box with a solid background-color
   holding the content, so .app is that box here; the strip gives the status bar the bars'
   colour. */
.status-fill { display: none; }
@media (display-mode: standalone) {
  .status-fill {
    display: block; position: fixed; top: 0; left: 0; right: 0; z-index: 40;
    height: var(--top); background-color: var(--surface);
  }
  .login-page .status-fill { background-color: var(--surface-2); }
  .app { overscroll-behavior: contain; background-color: var(--surface); }
}

/* ── Layout ── */
/* A fixed box sized from visualViewport (--app-height, set by app.js), so the message box
   stays above the iPhone keyboard; dvh doesn't shrink for it in installed apps */
.app { position: fixed; top: 0; left: 0; right: 0; height: var(--app-height, 100dvh); display: flex; overflow: hidden; }
/* With the keyboard up it covers the home indicator, so no bottom inset under the composer */
html.keyboard-open { --bottom: 0px; }
.pane { display: flex; flex-direction: column; min-width: 0; height: 100%; }
.list-pane { width: 100%; background: var(--surface); }
.thread-pane { width: 100%; display: none; background: var(--bg); }
.app.show-thread .list-pane { display: none; }
.app.show-thread .thread-pane { display: flex; }
@media (min-width: 820px) {
  .list-pane { width: 360px; flex: none; border-right: 1px solid var(--border); }
  .thread-pane { display: flex; flex: 1; }
  .app.show-thread .list-pane { display: flex; }
  .back-btn { display: none !important; }
}

.bar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(6px + var(--top)) 8px 6px 16px; min-height: calc(56px + var(--top));
  border-bottom: 1px solid var(--border); background: var(--surface); flex: none; position: relative;
}
.bar-title { font-size: 22px; font-weight: 700; margin: 0; flex: 1; }

/* ── Conversation list ── */
.search-row { padding: 8px 12px 4px; }
.search-row input {
  width: 100%; height: 40px; padding: 0 14px; border-radius: 20px;
  border: 1px solid var(--border); background: var(--surface-2); outline: none;
}
.search-row input:focus { border-color: var(--brand); }
.filter-row { padding: 4px 12px 0; }
.filter-row select {
  width: 100%; height: 38px; padding: 0 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 15px;
}
.blocked-file {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted);
  border: 1px dashed var(--border); border-radius: 12px; padding: 8px 12px; background: var(--surface);
}
.state { font-size: 12px; padding: 1px 8px; border-radius: 8px; white-space: nowrap; }
.state.here { background: color-mix(in srgb, var(--brand) 18%, transparent); color: var(--brand); }
.state.elsewhere { background: color-mix(in srgb, orange 20%, transparent); color: #b86e00; }
.state.off, .state.unknown { background: var(--surface-2); color: var(--muted); }
.field-note { font-size: 12px; color: var(--muted); margin-top: 2px; }
.dlg-body input[type=password] {
  width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font-size: 16px;
}
.banner {
  margin: 8px 12px 0; padding: 10px 12px; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid var(--border); font-size: 14px;
  display: flex; gap: 10px; align-items: center;
}
.banner span { flex: 1; }
.banner .btn { min-height: 34px; padding: 4px 12px; }
.list-tabs { display: flex; gap: 4px; padding: 8px 12px 4px; }
.tab {
  border: 0; background: none; padding: 6px 12px; border-radius: 16px; color: var(--muted); font-size: 14px;
}
.tab.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.conv-list {
  list-style: none; margin: 0; padding: 0 0 var(--bottom); overflow-y: auto; flex: 1;
  -webkit-overflow-scrolling: touch;
}
.conv {
  display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--surface-2); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 16px; color: #fff;
}
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; gap: 8px; align-items: baseline; }
.conv-name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 12px; color: var(--muted); flex: none; }
.conv-bottom { display: flex; gap: 8px; align-items: center; }
.conv-preview {
  flex: 1; font-size: 14px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-num { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 8px; padding: 0 6px; flex: none; }
.conv.unread .conv-name, .conv.unread .conv-preview { color: var(--text); font-weight: 700; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--unread); flex: none; }
.list-note { padding: 24px 16px; text-align: center; color: var(--muted); font-size: 14px; }
.more-row { padding: 12px; text-align: center; }

/* ── Conversation ── */
.empty-thread {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); gap: 4px;
}
.empty-thread img { width: 72px; height: 72px; border-radius: 18px; opacity: .85; }
.thread { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-bar { padding-left: 4px; }
.thread-title {
  flex: 1; min-width: 0; border: 0; background: none; text-align: left; padding: 4px 8px;
  display: flex; flex-direction: column; border-radius: 8px;
}
.thread-title:hover { background: var(--surface-2); }
.thread-name { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu {
  position: absolute; right: 8px; top: calc(100% - 4px); z-index: 10; min-width: 200px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 4px; display: flex; flex-direction: column;
}
.menu button { border: 0; background: none; text-align: left; padding: 10px 12px; border-radius: 6px; }
.menu button:hover { background: var(--surface-2); }

.new-to { padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--surface); position: relative; display: flex; flex-wrap: wrap; gap: 8px 16px; }
.new-to label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.new-to label:first-child { flex: 1; min-width: 220px; }
.new-to input, .new-to select {
  flex: 1; height: 38px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); outline: none; font-size: 16px;
}
.suggest {
  list-style: none; margin: 0; padding: 0; position: absolute; left: 12px; right: 12px; top: 100%; z-index: 5;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 280px; overflow-y: auto;
}
.suggest:empty { display: none; }
.suggest li { padding: 10px 12px; cursor: pointer; display: flex; justify-content: space-between; gap: 12px; }
.suggest li:hover, .suggest li.active { background: var(--surface-2); }
.suggest small { color: var(--muted); }

/* column-reverse: scrollTop 0 is the newest message, and older ones added at the top don't
   move what's on screen (scroll fix-ups in JS jump on iOS). The inner column keeps order. */
.messages {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column-reverse;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain; overflow-anchor: none;
}
.messages-inner { display: flex; flex-direction: column; gap: 2px; padding: 12px 12px 4px; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
.msg { display: flex; flex-direction: column; max-width: min(78%, 560px); }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg + .msg.gap { margin-top: 8px; }
.bubble {
  padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); border-bottom-right-radius: 6px; }
.msg.out .bubble a { color: inherit; }
.msg.failed .bubble { background: transparent; color: var(--text); border: 1.5px solid var(--danger); }
.msg.sending .bubble { opacity: .6; }
.media { display: flex; flex-direction: column; gap: 4px; margin-bottom: 2px; }
.media img, .media video { max-width: min(260px, 100%); max-height: 320px; border-radius: 14px; display: block; cursor: zoom-in; background: var(--surface-2); }
.media a.file { font-size: 14px; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }
.meta .err { color: var(--danger); }
.meta button { border: 0; background: none; color: var(--brand); padding: 0 0 0 6px; font-size: 11px; font-weight: 600; }
.load-earlier { align-self: center; margin-bottom: 8px; }

.composer {
  flex: none; border-top: 1px solid var(--border); background: var(--surface);
  padding: 6px 8px calc(6px + var(--bottom));
}
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; min-height: 40px; padding: 9px 14px; border-radius: 20px;
  border: 1px solid var(--border); background: var(--surface-2); outline: none; font-size: 16px; line-height: 1.35;
}
.composer textarea:focus { border-color: var(--brand); }
.attach-btn { color: var(--muted); }
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--brand); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; flex: none; margin: 2px 2px 0 4px;
}
.send-btn svg { width: 20px; height: 20px; }
.send-btn:disabled { opacity: .45; cursor: default; }
.attach-row { display: flex; gap: 8px; padding: 4px 4px 8px; overflow-x: auto; }
.thumb { position: relative; flex: none; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; display: block; }
.thumb button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0;
  background: var(--text); color: var(--bg); font-size: 14px; line-height: 22px; padding: 0;
}
.thumb.busy img { opacity: .5; }
.char-count { font-size: 11px; color: var(--muted); text-align: right; padding: 2px 52px 0 0; }

/* ── Dialogs ── */
.dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
/* Overlays draw their colour as an image: a background-color here can stick in the iPhone
   status bar for the rest of the session if the viewport changes while one is open */
.dialog::backdrop { background: linear-gradient(var(--overlay), var(--overlay)); }
.dlg-head { display: flex; align-items: center; padding: 12px 8px 12px 18px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dlg-head h2 { flex: 1; margin: 0; font-size: 18px; }
.dlg-body { padding: 14px 18px 18px; }
.dlg-body h3 { font-size: 15px; margin: 20px 0 8px; }
.dlg-body h3:first-child { margin-top: 0; }
.dlg-body p { margin: 6px 0; font-size: 14px; color: var(--muted); }
.dlg-body label { display: block; font-size: 14px; color: var(--muted); margin: 10px 0 4px; }
.dlg-body input[type=text], .dlg-body input[type=tel], .dlg-body input[type=date], .dlg-body select, .dlg-body textarea {
  width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font-size: 16px;
}
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 120px; }
.card { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin: 8px 0; }
.num-row { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.num-row:last-child { border-bottom: 0; }
.num-row .grow { flex: 1; min-width: 0; }
.tag { font-size: 12px; padding: 1px 8px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.ok { color: var(--brand); }
.bad { color: var(--danger); }
.hook { display: flex; gap: 6px; align-items: center; margin: 4px 0 10px; }
.hook code {
  flex: 1; min-width: 0; font-size: 12px; padding: 6px 8px; border-radius: 6px; background: var(--surface-2);
  overflow-x: auto; white-space: nowrap; border: 1px solid var(--border);
}
.preview-table { width: 100%; overflow-x: auto; margin: 8px 0; border: 1px solid var(--border); border-radius: 8px; }
.preview-table table { border-collapse: collapse; font-size: 12px; min-width: 100%; }
.preview-table th, .preview-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.map-grid { display: grid; grid-template-columns: 130px 1fr; gap: 6px 10px; align-items: center; }
.map-grid label { margin: 0; }
.result { font-size: 14px; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); margin-top: 10px; white-space: pre-wrap; }

.viewer {
  position: fixed; inset: 0; z-index: 50; background: linear-gradient(rgba(0, 0, 0, .92), rgba(0, 0, 0, .92));
  display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--bottom)); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 20px; font-size: 14px;
  max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}

/* ── Login ── */
.login-page { overflow-y: auto; display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 16px; background: var(--surface-2); }
.login-card {
  width: min(360px, 100%); background: var(--surface); border-radius: 16px; padding: 28px 24px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: stretch; text-align: center;
}
.login-icon { width: 64px; height: 64px; border-radius: 16px; align-self: center; }
.login-card h1 { margin: 10px 0 16px; font-size: 24px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; text-align: left; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
.login-card input {
  height: 44px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font-size: 16px;
}
.login-card .btn-primary { min-height: 46px; margin-top: 4px; }
.login-msg { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.login-hint { color: var(--muted); font-size: 13px; margin: 14px 0 0; }

/* ── Contacts tab ── */
.filter-row.off { display: none; }
.contact-tools { display: flex; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.contact-tools .btn { min-height: 36px; padding: 4px 14px; }

/* ── Group texts ── */
.avatar.group {
  background: var(--brand) center / 26px no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z'/%3E%3C/svg%3E");
}
.msg .sender { font-size: 12px; color: var(--muted); margin: 0 10px 2px; font-weight: 600; }
.new-to .chips { display: contents; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 14px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text); font-size: 14px; white-space: nowrap;
}
.chip button { border: 0; background: none; color: var(--muted); font-size: 16px; line-height: 1; padding: 0 4px; }
.new-to label:first-child { flex-wrap: wrap; }
.new-to label:first-child input { min-width: 140px; }
#group-note { width: 100%; }

/* ── Small things ── */
/* On screen and focusable (iOS won't focus a hidden field), but invisible and untouchable */
.focus-bounce {
  position: fixed; top: 0; left: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0;
  font-size: 16px; pointer-events: none;
}
#update-banner { border-color: var(--brand); }
.bubble a[href^="tel:"] { text-decoration: underline; }
