/* Permit CRM — calm, dense, readable. Light and dark via prefers-color-scheme. No inline styles (CSP). */
:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel2: #f0f2f5; --line: #e2e5ea; --line2: #d3d8df;
  --text: #16191d; --muted: #667080; --accent: #2563eb; --accent-ink: #ffffff; --accent-soft: #e6eefe;
  --ok: #15803d; --ok-soft: #e7f5ec; --warn: #b45309; --warn-soft: #fdf3e2; --bad: #b91c1c; --bad-soft: #fdecec;
  --out: #2563eb; --out-ink: #ffffff; --in: #eceff3; --in-ink: #16191d; --dry: #7c8594;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216; --panel: #161a20; --panel2: #1c2129; --line: #262c35; --line2: #333b46;
    --text: #e8ebef; --muted: #939cab; --accent: #5b8cff; --accent-ink: #0b1020; --accent-soft: #1b2744;
    --ok: #4ade80; --ok-soft: #13291c; --warn: #fbbf24; --warn-soft: #2c2412; --bad: #f87171; --bad-soft: #2d1616;
    --out: #3b6ef0; --out-ink: #ffffff; --in: #232932; --in-ink: #e8ebef; --dry: #8a93a3;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
body { overflow: hidden; }
h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 18px; } h3 { font-size: 14px; margin: 18px 0 8px; }
a { color: var(--accent); text-decoration: none; } a:hover { text-decoration: underline; }
.muted { color: var(--muted); } .small { font-size: 12px; } .strong { font-weight: 600; }
.tabular, table.data td, .badge, .time { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-size: 12px; }
.grow { flex: 1; }
.error { color: var(--bad); min-height: 1.2em; margin: 4px 0; }

.btn { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line2); border-radius: 8px; padding: 7px 12px; cursor: pointer; line-height: 1.2; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.good { color: var(--ok); border-color: var(--ok); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 12px; border-radius: 6px; }
.btn.block { width: 100%; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line2); border-radius: 8px; padding: 7px 9px; min-width: 0; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; width: 100%; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--line2); color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: .02em; }
.pill.ok { color: var(--ok); border-color: var(--ok); } .pill.warn { color: var(--warn); border-color: var(--warn); } .pill.bad { color: var(--bad); border-color: var(--bad); } .pill.accent { color: var(--accent); border-color: var(--accent); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; text-align: center; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; overflow: auto; height: 100%; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px; }
.login-brand { font-size: 20px; font-weight: 700; }
.login-card p { margin: 0; }

/* Shell */
.app { height: 100%; display: flex; flex-direction: column; }
.banner { text-align: center; font-weight: 700; font-size: 12px; letter-spacing: .06em; padding: 5px 10px; text-transform: uppercase; }
.banner.dry { background: var(--panel2); color: var(--muted); border-bottom: 1px solid var(--line); }
.banner.off { background: var(--warn-soft); color: var(--warn); }
.banner.live { background: var(--bad); color: #fff; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; }
.tabs { display: flex; gap: 2px; }
.tab { font: inherit; background: transparent; border: 0; color: var(--muted); padding: 6px 10px; border-radius: 6px; cursor: pointer; font-weight: 600; }
.tab.active { color: var(--text); background: var(--panel2); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }

/* Inbox */
.inbox { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(280px, 340px) minmax(0, 1fr) minmax(280px, 340px); }
.col { min-height: 0; display: flex; flex-direction: column; }
.list-col { border-right: 1px solid var(--line); background: var(--panel); }
.card-col { border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; }
.list-tools { padding: 10px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.list-tools input[type=search] { width: 100%; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.filters select { width: 100%; font-size: 12px; padding: 5px 6px; }
.contact-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.contact-item { display: grid; grid-template-columns: 10px 1fr auto; gap: 2px 8px; padding: 9px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.contact-item:hover { background: var(--panel2); }
.contact-item.selected { background: var(--accent-soft); }
.contact-item .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; }
.contact-item.unread .dot { background: var(--accent); }
.contact-item .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-item.unread .name { font-weight: 700; }
.contact-item .time { color: var(--muted); font-size: 11px; }
.contact-item .preview { grid-column: 2 / 4; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-item .meta { grid-column: 2 / 4; display: flex; gap: 4px; flex-wrap: wrap; }
.empty { margin: auto; color: var(--muted); padding: 30px; text-align: center; }

.thread-col { background: var(--bg); }
.thread { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.thread-title { min-width: 0; flex: 1; }
.thread-title div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 3px; }
.day-sep { align-self: center; color: var(--muted); font-size: 11px; margin: 12px 0 6px; font-weight: 600; }
.bubble-wrap { display: flex; flex-direction: column; max-width: min(78%, 520px); }
.bubble-wrap.out { align-self: flex-end; align-items: flex-end; }
.bubble-wrap.in { align-self: flex-start; align-items: flex-start; }
.bubble { padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.out .bubble { background: var(--out); color: var(--out-ink); border-bottom-right-radius: 5px; }
.in .bubble { background: var(--in); color: var(--in-ink); border-bottom-left-radius: 5px; }
.out.dryrun .bubble { background: transparent; color: var(--text); border: 1px dashed var(--dry); }
.out.blocked .bubble, .out.failed .bubble, .out.undelivered .bubble { background: var(--bad-soft); color: var(--bad); border: 1px solid var(--bad); }
.bubble-meta { font-size: 11px; color: var(--muted); margin: 2px 4px 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.bubble-meta .bad { color: var(--bad); }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 6px; }
.composer textarea { min-height: 44px; max-height: 180px; }
.composer-row { display: flex; align-items: center; gap: 8px; }
.compose-notice { padding: 6px 9px; border-radius: 7px; background: var(--warn-soft); color: var(--warn); }
.compose-notice.bad { background: var(--bad-soft); color: var(--bad); }

.card { padding: 14px; }
.card-section { padding: 10px 0; border-bottom: 1px solid var(--line); }
.card-section:last-child { border-bottom: 0; }
.card-name { font-size: 16px; font-weight: 700; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 4px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.mismatch { color: var(--warn); font-weight: 600; }
.hold-box { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 8px 10px; display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.phone-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 2px; }
.card select, .card textarea { width: 100%; }
.card textarea { min-height: 110px; }
.save-state { font-size: 11px; color: var(--muted); height: 14px; }

/* Pages */
.page { flex: 1; overflow-y: auto; padding: 16px 20px 40px; max-width: 1100px; width: 100%; margin: 0 auto; }
.page-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.panel h3 { margin-top: 0; }
.table-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th, table.data td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data th { color: var(--muted); font-weight: 600; font-size: 12px; }
table.data td.num, table.data th.num { text-align: right; }
table.data tr.warn td { color: var(--warn); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.grid-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; margin-top: 12px; align-items: end; }
.form-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.tpl { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
.tpl-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.tpl-body { white-space: pre-wrap; font-size: 13px; }
.tpl-rendered { white-space: pre-wrap; font-size: 13px; background: var(--panel2); border-radius: 6px; padding: 8px 10px; margin-top: 6px; }
.tpl textarea { min-height: 90px; margin-top: 6px; }

.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 8px; font-size: 13px; max-width: 90vw; z-index: 50; box-shadow: var(--shadow); }
.toast.bad { background: var(--bad); color: #fff; }

.mobile-only { display: none; }
@media (max-width: 1100px) {
  .inbox { grid-template-columns: minmax(260px, 320px) minmax(0, 1fr); }
  .card-col { display: none; }
  .inbox[data-pane=card] .card-col { display: flex; position: fixed; inset: 0; z-index: 20; }
  #showCard { display: inline-block; }
  #backToThread { display: inline-block; margin-bottom: 8px; }
}
@media (max-width: 720px) {
  .topbar { gap: 8px; padding: 8px 10px; }
  .brand, #whoami { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .inbox .list-col, .inbox .thread-col { display: none; }
  .inbox[data-pane=list] .list-col { display: flex; }
  .inbox[data-pane=thread] .thread-col { display: flex; }
  .mobile-only { display: inline-block; }
  .bubble-wrap { max-width: 88%; }
  .page { padding: 12px; }
  input, select, textarea { font-size: 16px; }
}
