:root {
  /* App chrome: red / white */
  --bg: #f6f6f7;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6f76;
  --border: #e7e2e3;
  --accent: #c8102e;        /* buttons, badges (white text on it: 5.9:1) */
  --accent-ink: #b00e28;    /* red text on white */
  --accent-fg: #ffffff;
  --accent-soft: #fdecee;
  --nav-bg: #c8102e;
  --nav-fg: #ffffff;
  --nav-muted: rgba(255, 255, 255, 0.78);
  --nav-active: rgba(255, 255, 255, 0.2);
  --danger: #b3261e;
  --success: #1e7e34;
  --radius: 8px;

  /* Message pane: WhatsApp's own palette */
  --thread-bg: #efeae2;
  --wa-text: #111b21;
  --wa-meta: #667781;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --bubble-note: #fff5c4;
  --wa-day: #ffffff;
  --wa-tick-read: #53bdeb;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --panel: #1a1b1e;
    --text: #ececee;
    --muted: #9a9ca3;
    --border: #2d2e33;
    --accent: #d42c43;
    --accent-ink: #ff7a8a;
    --accent-fg: #ffffff;
    --accent-soft: #3a141b;
    --nav-bg: #7d1122;
    --nav-fg: #ffffff;
    --nav-muted: rgba(255, 255, 255, 0.72);
    --nav-active: rgba(255, 255, 255, 0.16);
    --danger: #ff6b6b;
    --success: #5cc97a;

    --thread-bg: #0b141a;
    --wa-text: #e9edef;
    --wa-meta: #8696a0;
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --bubble-note: #4a3f16;
    --wa-day: #182229;
    --wa-tick-read: #53bdeb;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
.muted { color: var(--muted); }
.center { display: grid; place-items: center; min-height: 100vh; padding: 16px; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 7px 12px;
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.55; cursor: default; }
input, select, textarea {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 7px 10px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Login */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  border-top: 4px solid var(--accent);
  padding: 28px;
  width: 100%;
  max-width: 380px;
}
.card h1 { margin: 0 0 4px; font-size: 20px; color: var(--accent-ink); }
.card p { margin: 0 0 18px; }
.card form { display: grid; gap: 10px; }
.card .error { color: var(--danger); margin: 6px 0 0; }

/* Shell */
.shell {
  display: grid;
  grid-template-columns: 200px 340px 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
/* Each pane scrolls on its own; without min-height: 0 a long list stretches the grid and the whole page scrolls. */
.shell > * { min-height: 0; }
.nav, .page { overflow-y: auto; overscroll-behavior: contain; }
.list-body, .messages { overscroll-behavior: contain; }
.nav {
  background: var(--nav-bg);
  color: var(--nav-fg);
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  gap: 2px;
}
.nav .brand { font-weight: 700; padding: 4px 10px 14px; }
.nav a {
  display: flex;
  justify-content: space-between;
  padding: 7px 10px;
  border-radius: var(--radius);
  color: var(--nav-fg);
  text-decoration: none;
}
.nav a:hover { background: var(--nav-active); }
.nav a.active { background: var(--nav-active); font-weight: 600; box-shadow: inset 3px 0 0 var(--nav-fg); }
.nav a:focus-visible { outline: 2px solid var(--nav-fg); outline-offset: -2px; }
.nav .section { color: var(--nav-muted); font-size: 12px; text-transform: uppercase; padding: 14px 10px 4px; }
.nav .spacer { flex: 1; }
.nav .me { color: var(--nav-muted); padding: 8px 10px; font-size: 13px; overflow-wrap: anywhere; }

.list { border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-width: 0; }
.list-head { padding: 12px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; }
.list-head input { flex: 1; }
.list-body { overflow-y: auto; flex: 1; min-height: 0; }
.conv {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { color: var(--muted); font-size: 12px; }
.conv .preview { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .meta { display: flex; gap: 6px; align-items: center; justify-content: flex-end; }
.badge { background: var(--accent); color: var(--accent-fg); border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 700; }
.tag { border: 1px solid var(--border); border-radius: 999px; font-size: 11px; padding: 0 7px; color: var(--muted); white-space: nowrap; }

.thread { display: flex; flex-direction: column; min-width: 0; background: var(--thread-bg); color: var(--wa-text); }
.thread-head, .composer { color: var(--text); }
.thread-head {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.thread-head .who { flex: 1; min-width: 140px; }
.thread-head .who b { display: block; }
.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(560px, 80%); padding: 6px 9px 4px; border-radius: 8px; border-top-left-radius: 0; background: var(--bubble-in); color: var(--wa-text); align-self: flex-start; box-shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13); }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-top-left-radius: 8px; border-top-right-radius: 0; }
.msg.note { background: var(--bubble-note); align-self: center; border: 1px dashed var(--wa-meta); border-radius: 8px; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .foot { font-size: 11px; color: var(--wa-meta); text-align: right; margin-top: 2px; }
.msg .tick.read { color: var(--wa-tick-read); }
.msg .tick.failed { color: var(--danger); }
.msg a { color: #027eb5; }
@media (prefers-color-scheme: dark) { .msg a { color: #53bdeb; } }
.msg .err { color: var(--danger); font-size: 12px; }
.msg img { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.msg audio, .msg video { max-width: 100%; }
.day { align-self: center; font-size: 12px; color: var(--wa-meta); background: var(--wa-day); box-shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13); padding: 2px 10px; border-radius: 999px; margin: 6px 0; }

.composer { background: var(--panel); border-top: 1px solid var(--border); padding: 10px 12px; display: grid; gap: 8px; }
.composer .modes { display: flex; gap: 6px; align-items: center; }
.composer .modes button { border: none; background: none; padding: 3px 8px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.composer .modes button.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 40px; max-height: 180px; }
.composer.note textarea { background: var(--bubble-note); }
.composer .recording { align-items: center; min-height: 40px; flex-wrap: wrap; }
.composer .recording audio { height: 36px; max-width: 100%; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: rec-pulse 1s ease-in-out infinite; }
.rec-time { font-variant-numeric: tabular-nums; color: var(--muted); }
@keyframes rec-pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .rec-dot { animation: none; } }
.window-warn { font-size: 13px; color: var(--muted); }

.empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 24px; text-align: center; }

/* Pages (team, setup) */
.page { grid-column: 2 / 4; overflow-y: auto; padding: 24px; }
.page h2 { margin-top: 0; }
.page .panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px; margin-bottom: 18px; max-width: 860px; }
.page form.inline { display: flex; gap: 8px; flex-wrap: wrap; }
.page form.inline input { flex: 1; min-width: 180px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
.check { display: flex; gap: 8px; padding: 4px 0; }
.ok { color: var(--success); }
.bad { color: var(--danger); }
code.copy { background: var(--bg); padding: 4px 8px; border-radius: 6px; cursor: pointer; overflow-wrap: anywhere; }

dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); padding: 20px; width: min(440px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { display: grid; gap: 10px; }
dialog h3 { margin: 0 0 4px; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 9px 16px; border-radius: 8px; z-index: 10; max-width: calc(100vw - 32px); }

.back { display: none; }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .nav { flex-direction: row; flex-wrap: wrap; border-right: none; border-bottom: 1px solid var(--border); padding: 8px; align-items: center; }
  .nav .brand { padding: 4px 8px; }
  .nav .section, .nav .spacer, .nav .me { display: none; }
  .nav a { padding: 5px 8px; gap: 6px; }
  .shell.has-thread .list { display: none; }
  .shell:not(.has-thread) .thread { display: none; }
  .back { display: inline-block; }
  .page { grid-column: 1; padding: 16px; }
}
