:root {
  --bg: #f4efe7;
  --panel: rgba(255, 252, 247, 0.95);
  --ink: #1f1b18;
  --muted: #6c6258;
  --accent: #1f6f78;
  --accent-soft: #d8ebe7;
  --line: #d7c9b8;
  --danger: #9a3d2f;
  --success: #2f7a57;
  --shadow: 0 18px 40px rgba(71, 52, 28, 0.12);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(31, 111, 120, 0.12), transparent 28%),
    linear-gradient(180deg, #f7f1e6 0%, #efe4d3 100%);
}
.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: min(28rem, 100%); padding: 2rem; }
.sms-encoding-preview { color: var(--success); font-size: 0.88rem; margin-top: -0.35rem; }
.sms-encoding-preview.unicode { color: var(--danger); font-weight: 700; }

.page-shell { max-width: 1400px; margin: 0 auto; padding: 28px; }
.top-nav { display: flex; gap: 10px; padding: 10px; margin-bottom: 18px; }
.nav-spacer { flex: 1 1 auto; }
.nav-user { align-self: center; color: var(--muted); font-weight: 700; }
.nav-logout { margin: 0; }
.nav-logout .nav-link { border: 0; cursor: pointer; }
.nav-link { padding: 10px 14px; border-radius: 999px; color: var(--ink); text-decoration: none; background: #eadbc7; font-weight: 700; }
.nav-link.active { background: var(--accent); color: white; }
.hero { margin-bottom: 18px; }
.hero-actions { display: flex; justify-content: space-between; gap: 18px; align-items: end; }
.back-link { color: var(--accent); font-weight: 700; text-decoration: none; }
.eyebrow { margin: 0 0 8px; text-transform: uppercase; letter-spacing: 0.14em; color: var(--accent); font-size: 0.8rem; }
h1, h2, h3 { margin: 0; }
.subhead, .sheet-header p, .helper { color: var(--muted); }
.panel { background: var(--panel); border: 1px solid rgba(140, 109, 67, 0.16); border-radius: 20px; box-shadow: var(--shadow); }
.workspace-grid { display: grid; grid-template-columns: 320px 1fr; gap: 18px; }
.campaigns-grid { grid-template-columns: 360px 1fr; }
.sidebar { padding: 16px; display: grid; gap: 16px; align-content: start; }
.tab-chip { background: var(--accent); color: white; border-radius: 999px; padding: 10px 14px; font-weight: 700; width: fit-content; }
.stack-form { display: grid; gap: 10px; padding: 16px; border: 1px solid rgba(140, 109, 67, 0.16); border-radius: 16px; background: rgba(255,255,255,0.45); }
.import-actions { position: sticky; top: 10px; z-index: 5; display: flex; gap: 10px; flex-wrap: wrap; padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255, 252, 247, 0.96); box-shadow: var(--shadow); }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 14px 0 0; }
.pagination-links { display: flex; gap: 8px; }
.inline-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.bulk-action-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.full-width { width: 100%; margin-top: 8px; }
.detail-stack { display: grid; gap: 16px; }
.detail-card { padding: 18px; }
.message-body { white-space: pre-wrap; }
.view-toggle-panel { padding: 12px 16px; margin-bottom: 16px; }
.view-toggle-row { display: flex; justify-content: space-between; gap: 16px; align-items: center; }
.view-toggle-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.view-toggle-button { padding: 9px 14px; border-radius: 999px; background: #eadbc7; color: var(--ink); text-decoration: none; font-weight: 700; }
.view-toggle-button.active { background: var(--accent); color: white; }

.group-type-choice { display: flex; gap: 1rem; flex-wrap: wrap; }
.dynamic-rule-panel { border-top: 1px solid var(--line); padding-top: 1rem; }
.visual-rule-group { border-left: 3px solid var(--accent); background: rgba(70, 91, 126, 0.06); padding: 0.8rem; margin: 0.6rem 0; }
.rule-group-header, .visual-rule-row, .rule-preview-actions { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
.rule-group-header { justify-content: space-between; }
.rule-children { margin-left: 1rem; }
.visual-rule-row { background: white; border: 1px solid var(--line); border-radius: 6px; padding: 0.6rem; margin: 0.5rem 0; }
.visual-rule-row select { min-width: 8rem; }
.rule-preview-actions { justify-content: space-between; margin-top: 0.8rem; }
.rule-preview-list { max-height: 14rem; overflow: auto; margin-top: 0.5rem; }
.secondary-sheet-header { margin-top: 2rem; }
.group-membership-chip { display: inline-block; margin: 0.1rem; padding: 0.2rem 0.45rem; border-radius: 999px; background: rgba(70, 91, 126, 0.1); color: var(--ink); text-decoration: none; font-size: 0.85rem; }

.matrix-page-shell { max-width: none; }
.matrix-panel { padding: 1rem; }
.matrix-toolbar, .matrix-sort-row { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.matrix-toolbar { margin-bottom: 0.75rem; }
.matrix-search { flex: 1 1 18rem; }
.matrix-search input { width: 100%; }
.matrix-sort-row { min-height: 2.5rem; border-top: 1px solid var(--line); padding-top: 0.75rem; }
.matrix-sort-chips { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.matrix-sort-chip { display: inline-flex; align-items: center; gap: 0.25rem; background: rgba(70, 91, 126, 0.12); border-radius: 999px; padding: 0.3rem 0.6rem; }
.matrix-sort-chip button { border: 0; background: transparent; color: inherit; padding: 0; min-width: 1rem; }
.matrix-sort-chip.permanent { opacity: 0.75; }
.matrix-status { min-height: 1.5rem; padding: 0.3rem 0; color: var(--accent); }
.matrix-status.error { color: var(--danger); }
.membership-matrix-wrap { width: 100%; overflow: auto; max-height: calc(100vh - 19rem); border: 1px solid var(--line); }
.membership-matrix { border-collapse: separate; border-spacing: 0; min-width: 100%; background: white; }
.membership-matrix th, .membership-matrix td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0.55rem; white-space: nowrap; }
.membership-matrix thead th { position: sticky; top: 0; z-index: 4; background: var(--panel); cursor: pointer; user-select: none; }
.membership-matrix .frozen-column { position: sticky; z-index: 3; background: white; }
.membership-matrix thead .frozen-column { z-index: 6; background: var(--panel); }
.membership-matrix .frozen-first { left: 0; }
.membership-matrix .frozen-last { left: var(--matrix-first-width, 9rem); }
.membership-matrix .frozen-phone { left: calc(var(--matrix-first-width, 9rem) + var(--matrix-last-width, 10rem)); box-shadow: 3px 0 5px rgba(0, 0, 0, 0.08); }
.manual-group-header { text-align: center; }
.group-header-label { display: block; margin-bottom: 0.3rem; }
.group-header-actions { display: block; width: auto; margin: 0 auto; padding: 0.25rem 0.5rem; font-size: 0.78rem; }
.manual-group-header.dragging { opacity: 0.45; }
.membership-cell { text-align: center; }
.membership-checkbox { width: 1.15rem; height: 1.15rem; cursor: pointer; }
.group-header-editor { width: 8rem; min-width: 0; }
.column-visibility-menu { position: relative; }
.column-visibility-menu summary { list-style: none; cursor: pointer; }
.column-visibility-list { position: absolute; right: 0; z-index: 20; min-width: 14rem; max-height: 20rem; overflow: auto; background: white; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.14); padding: 0.7rem; }
.column-visibility-list label { display: flex; align-items: center; gap: 0.55rem; padding: 0.35rem; cursor: pointer; }
.column-visibility-list input[type="checkbox"] { flex: 0 0 auto; width: auto; margin: 0; padding: 0; }
.column-visibility-list label span { flex: 1 1 auto; }
.group-context-menu { position: fixed; z-index: 50; min-width: 10rem; background: white; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.18); padding: 0.35rem; }
.group-context-menu button { display: block; width: 100%; text-align: left; margin: 0.15rem 0; }
.thread-feed { display: grid; gap: 18px; }
.thread-room { padding: 18px; display: grid; gap: 16px; }
.thread-room-header { display: flex; justify-content: space-between; gap: 16px; align-items: start; }
.chat-window { display: grid; gap: 12px; padding: 14px; border: 1px solid rgba(140, 109, 67, 0.16); border-radius: 18px; background: linear-gradient(180deg, rgba(255,255,255,0.9), rgba(244,239,231,0.9)); }
.chat-row { display: flex; }
.chat-row.outbound { justify-content: flex-end; }
.chat-row.inbound { justify-content: flex-start; }
.chat-bubble { max-width: min(78%, 680px); border-radius: 20px; padding: 12px 14px; box-shadow: 0 10px 18px rgba(71, 52, 28, 0.08); }
.chat-bubble.outbound { background: linear-gradient(180deg, #2f8e97, #1f6f78); color: white; border-bottom-right-radius: 8px; }
.chat-bubble.inbound { background: #fffdf9; color: var(--ink); border: 1px solid rgba(140, 109, 67, 0.18); border-bottom-left-radius: 8px; }
.chat-meta-row { display: flex; justify-content: space-between; gap: 12px; font-size: 0.85rem; margin-bottom: 8px; opacity: 0.92; }
.message-type-tag,
.message-provider-tag {
  display: inline-flex;
  margin: 0 8px 8px 0;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
}
.message-type-tag { background: rgba(255,255,255,0.22); }
.chat-bubble.inbound .message-type-tag {
  background: rgba(31, 111, 120, 0.1);
  color: var(--accent);
}
.message-provider-tag { background: rgba(255, 255, 255, 0.16); }
.chat-bubble.inbound .message-provider-tag {
  background: rgba(140, 109, 67, 0.1);
  color: var(--muted);
}
.chat-body { white-space: pre-wrap; line-height: 1.45; }
.empty-chat { color: var(--muted); text-align: center; padding: 24px 16px; }
.composer-form { display: grid; gap: 10px; }
.checkbox-option { display: flex; align-items: center; gap: 8px; width: fit-content; }
.checkbox-option input[type="checkbox"] { flex: 0 0 auto; width: auto; margin: 0; padding: 0; }
.composer-actions { display: flex; justify-content: space-between; gap: 14px; align-items: center; }
.combined-thread-note { padding: 10px 12px; border: 1px dashed rgba(140, 109, 67, 0.3); border-radius: 12px; background: rgba(255,255,255,0.4); }
.pinned-message-card { margin-bottom: 18px; }
.thread-stack { display: grid; gap: 12px; }
.thread-card { border: 1px solid rgba(140, 109, 67, 0.16); border-radius: 16px; padding: 14px; background: rgba(255,255,255,0.45); }
.thread-message { padding: 10px 12px; border-radius: 12px; margin-top: 8px; }
.thread-message.outbound { background: rgba(31,111,120,0.08); }
.thread-message.inbound { background: rgba(140,109,67,0.08); }
.thread-meta { color: var(--muted); font-size: 0.9rem; margin-bottom: 4px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 8px 12px; font-weight: 700; }
.danger-chip { background: rgba(154, 61, 47, 0.12); color: var(--danger); }
.simple-list { margin: 0; padding-left: 20px; }
input, button, select, textarea { font: inherit; }
input, select, textarea { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: #fffdf9; }
textarea { resize: vertical; }
.inline-form input { width: min(280px, 100%); }
.bulk-action-row select { width: min(260px, 100%); }
button { border: none; background: var(--accent); color: white; padding: 12px 16px; border-radius: 12px; cursor: pointer; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.ghost-button { background: #eadbc7; color: var(--ink); }
.danger-button { background: var(--danger); padding: 9px 12px; }
.flash-stack { display: grid; gap: 10px; margin-bottom: 16px; }
.flash { padding: 12px 16px; border-radius: 14px; background: #fff; border: 1px solid var(--line); }
.flash.success { border-color: rgba(47, 122, 87, 0.4); color: var(--success); }
.flash.error { border-color: rgba(154, 61, 47, 0.4); color: var(--danger); }
.sheet-panel { padding: 16px; }
.page-actions { display: flex; justify-content: flex-end; margin-top: 18px; }
.secret-control { display: grid; gap: 8px; color: var(--muted); font-size: 0.92rem; }
.tabs-row { display: flex; gap: 8px; flex-wrap: wrap; padding-bottom: 14px; border-bottom: 1px solid rgba(140, 109, 67, 0.16); }
.tab-button { background: #eadbc7; color: var(--ink); }
.tab-button span { margin-left: 6px; color: var(--muted); }
.tab-button.active { background: var(--accent); color: white; }
.tab-panel { display: none; padding-top: 16px; }
.tab-panel.active { display: block; }
.sheet-header { display: flex; justify-content: space-between; gap: 16px; align-items: center; margin-bottom: 12px; }
.group-admin-row { display: flex; justify-content: space-between; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.count-pill { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 8px 12px; font-weight: 700; }
.sheet-frame { overflow: auto; border: 1px solid rgba(140, 109, 67, 0.16); border-radius: 16px; background: #fffdf9; }
.sheet-table { width: 100%; border-collapse: collapse; table-layout: fixed; min-width: 780px; }
.sheet-table th:nth-child(1), .sheet-table td:nth-child(1) { width: 48px; }
.sheet-table th:last-child, .sheet-table td:last-child { width: 190px; }
.edit-row input:not([type="checkbox"]) { box-sizing: border-box; width: 100%; min-width: 0; }
.edit-row .checkbox-option { white-space: nowrap; font-size: 0.82rem; }
.edit-row td:last-child .checkbox-option { display: block; }
.edit-row .ghost-button { padding: 9px 12px; }
.inline-edit-actions { display: inline-flex; gap: 8px; align-items: center; margin-top: 8px; }
.inline-edit-actions button { width: 72px; height: 40px; padding: 9px 8px; }
.sheet-table thead { background: #efe1cf; }
.sheet-table th, .sheet-table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid rgba(140, 109, 67, 0.14); }
.sheet-table th.sortable { cursor: pointer; user-select: none; position: relative; padding-right: 28px; }
.sheet-table th.sortable::after { content: '↕'; position: absolute; right: 10px; color: var(--muted); font-size: 0.85rem; }
.sheet-table th.sortable.sorted-asc::after { content: '↑'; color: var(--accent); }
.sheet-table th.sortable.sorted-desc::after { content: '↓'; color: var(--accent); }
.checkbox-col { width: 48px; text-align: center !important; }
.checkbox-col input { width: auto; }
.row-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.action-menu { position: relative; min-width: 132px; }
.action-menu summary { list-style: none; }
.action-menu summary::-webkit-details-marker { display: none; }
.action-menu-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 12px;
  background: #eadbc7;
  color: var(--ink);
  font-weight: 700;
  cursor: pointer;
}
.action-menu-button::after {
  content: '▾';
  font-size: 0.85rem;
  color: var(--muted);
}
.action-menu[open] .action-menu-button::after { content: '▴'; }
.action-menu-list {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 20;
  min-width: 180px;
  display: grid;
  gap: 6px;
  padding: 8px;
  border: 1px solid rgba(140, 109, 67, 0.16);
  border-radius: 14px;
  background: #fffdf9;
  box-shadow: var(--shadow);
}
.action-menu-link,
.action-menu-danger {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  text-align: left;
  text-decoration: none;
  font-weight: 700;
}
.action-menu-link {
  color: var(--accent);
  background: rgba(31,111,120,0.08);
}
.action-menu-danger {
  background: rgba(154, 61, 47, 0.12);
  color: var(--danger);
  padding: 10px 12px;
}
.action-menu form { margin: 0; }
.admin-user-edit-grid { display: grid; grid-template-columns: repeat(3, minmax(140px, 1fr)) auto auto auto; gap: 8px; align-items: center; }
.admin-create-form { display: none; }
dialog { border: 0; border-radius: 16px; padding: 0; max-width: min(520px, calc(100% - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(20, 28, 32, 0.45); }
.admin-create-dialog { padding: 24px; min-width: min(420px, 80vw); }
.edit-row td { background: rgba(31, 111, 120, 0.06); }
.edit-contact-form { display: grid; grid-template-columns: repeat(4, minmax(140px, 1fr)) auto auto; gap: 10px; align-items: center; }
.ghost-link { color: var(--accent); text-decoration: none; font-weight: 700; }
.empty-state { color: var(--muted); text-align: center; padding: 22px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
code { background: rgba(31,111,120,0.08); border-radius: 6px; padding: 2px 6px; }
@media (max-width: 980px) {
  .page-shell { padding: 16px; }
  .workspace-grid, .campaigns-grid { grid-template-columns: 1fr; }
  .group-admin-row, .hero-actions, .thread-room-header, .composer-actions, .view-toggle-row { flex-direction: column; align-items: stretch; }
  .bulk-action-row { flex-direction: column; align-items: stretch; }
  .edit-contact-form { grid-template-columns: 1fr; }
  .admin-user-edit-grid { grid-template-columns: 1fr; }
  .top-nav { flex-wrap: wrap; }
  .chat-bubble { max-width: 100%; }
  .action-menu-list {
    position: static;
    margin-top: 8px;
  }
}
