:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #17202a;
  --muted: #667085;
  --line: #dde3ea;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --warn: #b45309;
  --danger: #b42318;
  --shadow: 0 18px 40px rgba(18, 38, 63, 0.08);
}

[hidden],
body[data-authenticated="false"] .view,
body[data-authenticated="false"] .nav-tabs,
body[data-authenticated="false"] .event-log {
  display: none !important;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  display: grid;
  grid-template-columns: 280px 1fr;
}

button,
input,
select,
textarea {
  font: inherit;
}

.shell-sidebar {
  background: #15202b;
  color: #eef4f8;
  min-height: 100vh;
  padding: 24px 18px;
  position: sticky;
  top: 0;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 32px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: var(--accent);
  font-weight: 800;
}

.brand p,
.brand small {
  margin: 0;
}

.brand p {
  font-weight: 800;
}

.brand small {
  color: #aebbc7;
}

.nav-tabs {
  display: grid;
  gap: 8px;
}

.nav-tab {
  border: 0;
  border-radius: 8px;
  padding: 12px;
  color: #d8e2ea;
  background: transparent;
  text-align: right;
  cursor: pointer;
}

.nav-tab:hover,
.nav-tab.is-active {
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
}

.workspace {
  padding: 28px;
  min-width: 0;
}

.workspace[aria-busy="true"] .connection-pill::after {
  content: " · تحديث";
}

.topbar,
.settings-panel,
.section-head,
.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar {
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 30px;
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
}

.connection-pill,
.status-badge {
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 13px;
  background: #e8eef3;
  color: var(--muted);
  white-space: nowrap;
}

.connection-pill.is-online {
  background: #d1fae5;
  color: #065f46;
}

.session-status {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  flex-direction: column;
}

.role-pill,
.role-notice {
  color: var(--muted);
  font-size: 0.82rem;
}

.role-notice {
  margin: 0;
}

.role-notice.is-denied {
  color: var(--danger);
}

.role-pill.is-stale {
  color: #b45309;
  font-weight: 700;
}

.connection-pill.is-stale {
  background: #fef3c7;
  color: #92400e;
}

.settings-panel,
.panel,
.event-log {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.settings-panel {
  padding: 16px;
  margin-bottom: 20px;
  display: grid;
  grid-template-columns: repeat(4, minmax(180px, 1fr));
}

.invite-panel {
  margin-bottom: 1rem;
}

.invite-panel[hidden] {
  display: none;
}

.invite-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr)) auto;
  gap: 0.75rem;
  align-items: end;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  white-space: nowrap;
}

.toggle-label input {
  width: auto;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 11px;
  color: var(--ink);
  background: #ffffff;
}

textarea {
  resize: vertical;
}

.button,
.text-button {
  cursor: pointer;
}

.button {
  border: 0;
  border-radius: 8px;
  padding: 11px 14px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 750;
  align-self: end;
}

.button:hover {
  background: var(--accent-strong);
}

.command-form[aria-busy="true"] {
  opacity: 0.72;
}

.command-form[aria-busy="true"] .button {
  cursor: wait;
}

.button.ghost {
  color: var(--accent);
  background: #e6f5f2;
}

.button.danger {
  background: var(--danger);
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 4px;
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.metric span {
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 28px;
}

.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.05fr);
  gap: 16px;
}

.lead-intake-layout {
  display: grid;
  gap: 16px;
}

.lead-intake-form {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: end;
}

.lead-intake-form .button {
  justify-self: start;
}

.lead-list-panel {
  margin: 0;
}

.lead-table {
  min-width: 0;
  table-layout: fixed;
}

.lead-table th,
.lead-table td {
  overflow-wrap: anywhere;
}

.panel {
  padding: 18px;
  min-width: 0;
}

.orders-table-panel {
  margin-top: 16px;
}

.attention-panel {
  margin-top: 16px;
}

code {
  direction: ltr;
  unicode-bidi: isolate;
}

.delivery-form {
  margin-top: 16px;
  grid-template-columns: repeat(4, minmax(0, 1fr)) auto;
  align-items: end;
}

.panel-title {
  margin-bottom: 14px;
}

.panel-title span {
  color: var(--muted);
  font-size: 13px;
}

.command-form {
  display: grid;
  gap: 13px;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 560px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 10px;
  text-align: right;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

td {
  font-size: 14px;
}

.muted-text {
  color: var(--muted);
}

.safety-note {
  border-radius: 999px;
  padding: 8px 12px;
  background: #fff7ed;
  color: var(--warn);
  font-size: 13px;
  font-weight: 700;
}

.timeline,
.guardrails {
  margin: 0;
  padding: 0 20px 0 0;
}

.guardrails {
  list-style: none;
  padding: 0;
}

.safety-gate {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}

.safety-gate.is-disabled strong {
  color: var(--accent);
}

.safety-gate.is-enabled {
  border-color: #fdba74;
  background: #fff7ed;
}

.safety-gate.is-enabled strong,
.has-warning {
  color: var(--warn) !important;
}

.policy-note {
  padding: 0 4px;
}

.timeline li,
.guardrails li {
  margin-bottom: 12px;
  color: #344054;
}

.section-head {
  margin: 6px 0 16px;
}

.table-search {
  width: min(360px, 100%);
}

.event-log {
  margin-top: 16px;
  padding: 18px;
}

#eventLog {
  margin: 0;
  max-height: 260px;
  overflow: auto;
  direction: ltr;
  text-align: left;
  white-space: pre-wrap;
  color: #263238;
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .shell-sidebar {
    position: static;
    min-height: auto;
  }

  .nav-tabs {
    grid-template-columns: repeat(5, minmax(110px, 1fr));
    overflow-x: auto;
  }

  .settings-panel,
  .invite-fields,
  .split-grid,
  .lead-intake-form,
  .metric-grid,
  .delivery-form {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  .workspace {
    padding: 18px;
  }

  .topbar,
  .section-head,
  .panel-title {
    align-items: flex-start;
    flex-direction: column;
  }

  h1 {
    font-size: 24px;
  }
}

.whatsapp-contact-form {
  margin-bottom: 24px;
}

.whatsapp-mode-options {
  border: 0;
  padding: 0;
  margin: 12px 0;
  display: grid;
  gap: 12px;
}

.whatsapp-mode-card {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  border: 1px solid #d5deea;
  border-radius: 12px;
  padding: 16px;
  cursor: pointer;
  line-height: 1.7;
}

.whatsapp-mode-card input[type="radio"] {
  width: 20px;
  height: 20px;
  flex: 0 0 20px;
  margin-top: 5px;
}

.whatsapp-mode-card:has(input:checked) {
  border-color: #0f766e;
  background: #effaf8;
}

.whatsapp-mode-card:focus-within {
  outline: 2px solid #0f766e;
  outline-offset: 2px;
}

.conversation-workspace {
  display: grid;
  grid-template-columns: minmax(240px, 30%) minmax(0, 1fr);
  align-items: stretch;
  gap: 18px;
  margin-bottom: 24px;
}

.conversation-sidebar,
.conversation-detail,
.conversation-empty {
  min-width: 0;
  margin: 0;
}

.conversation-sidebar {
  padding: 18px;
}

.conversation-sidebar .panel-title {
  flex-wrap: wrap;
  gap: 8px;
}

.conversation-sidebar .panel-title h2 {
  font-size: 19px;
}

.conversation-sidebar > .muted-text {
  font-size: 12px;
  line-height: 1.7;
}

.conversation-list {
  list-style: none;
  padding: 0;
  margin: 18px 0 0;
  display: grid;
  gap: 8px;
  max-height: 560px;
  overflow-y: auto;
}

.conversation-list-item {
  width: 100%;
  display: grid;
  gap: 7px;
  text-align: start;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
}

.conversation-list-item:hover {
  background: #f4faf8;
}

.conversation-list-item.is-selected {
  border-color: var(--accent);
  background: #e7f5f1;
  box-shadow: inset -3px 0 0 var(--accent);
}

.conversation-list-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -3px;
}

.conversation-list-item strong,
.conversation-list-preview {
  overflow-wrap: anywhere;
}

.conversation-list-preview {
  font-size: 13px;
  line-height: 1.7;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.conversation-list-time,
.conversation-list-count,
.conversation-list-empty {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.conversation-empty {
  display: grid;
  place-items: center;
  min-height: 420px;
  text-align: center;
  line-height: 1.8;
  background: #fcfdfd;
}

.conversation-empty-mark {
  display: inline-grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 18px;
  background: #e7f5f1;
  color: var(--accent);
  font-size: 30px;
}

.conversation-detail[hidden],
.conversation-empty[hidden] {
  display: none;
}

.conversation-detail {
  scroll-margin-top: 20px;
}

.conversation-detail .panel-title {
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 12px;
}

.conversation-detail .panel-title h2 {
  font-size: 20px;
  overflow-wrap: anywhere;
}

.conversation-detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.conversation-detail-actions .button {
  padding: 9px 12px;
  font-size: 13px;
}

.conversation-history-note {
  font-size: 12px;
}

.conversation-message-list {
  list-style: none;
  margin: 18px 0 0;
  padding: 20px;
  display: flex;
  flex-direction: column;
  direction: ltr;
  gap: 14px;
  height: min(58vh, 580px);
  min-height: 300px;
  overflow-y: auto;
  background: #f4f7f6;
  border: 1px solid var(--line);
  border-radius: 14px;
}

.conversation-message {
  direction: rtl;
  align-self: flex-start;
  border: 1px solid var(--line);
  border-radius: 14px 14px 14px 4px;
  padding: 14px 16px;
  background: var(--surface);
  min-width: 0;
  max-width: 88%;
  flex-shrink: 0;
}

.conversation-message.is-outbound {
  align-self: flex-end;
  border-color: #c8e6dc;
  border-radius: 14px 14px 4px 14px;
  background: #e7f5ef;
}

.conversation-message-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
}

.conversation-message-body {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.8;
}

.message-row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.messaging-secondary {
  margin-top: 18px;
}

.messaging-secondary > summary {
  color: var(--accent-strong);
  cursor: pointer;
  font-weight: 700;
  line-height: 1.7;
}

.messaging-secondary[open] > summary {
  margin-bottom: 22px;
}

.messaging-secondary .whatsapp-contact-form {
  padding: 0;
  margin: 0;
  border: 0;
  box-shadow: none;
}

@media (max-width: 1000px) {
  .conversation-workspace {
    grid-template-columns: 1fr;
  }

  .conversation-list {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    max-height: 250px;
  }

  .conversation-empty {
    min-height: 260px;
  }
}

@media (max-width: 600px) {
  .conversation-message-list {
    padding: 12px;
  }

  .conversation-message {
    max-width: 96%;
    padding: 12px;
  }
}
