:root {
  --color-bg: #f8faf9;
  --color-surface: #ffffff;
  --color-surface-elevated: #ffffff;
  --color-border: #e2e8f0;
  --color-border-subtle: #f1f5f9;
  --color-text: #0f172a;
  --color-muted: #475569;
  --color-link: #156548;
  --color-primary: #1f8a62;
  --color-primary-hover-bg: #176b4c;
  --color-primary-hover: #176b4c;
  --color-link-hover: #176b4c;
  --color-primary-soft: rgba(31, 138, 98, 0.12);
  --color-primary-text: #156548;
  --color-on-primary: #ffffff;
  --color-danger: #b45309;
  --color-danger-text: #92400e;
  --color-success: #15803d;
  --color-success-text: #166534;
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.08);
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --sidebar-width: 252px;
  --header-height: 60px;
  --focus-ring: 0 0 0 3px color-mix(in srgb, var(--color-primary) 35%, transparent);
}

[data-theme='dark'] {
  --color-bg: #0b0f0e;
  --color-surface: #13201c;
  --color-surface-elevated: #1a2e28;
  --color-border: #2a5c4a;
  --color-border-subtle: #152620;
  --color-text: #ecfdf5;
  --color-muted: #a8b8c8;
  --color-link: #6ee7c7;
  --color-primary: #2dd4a8;
  --color-primary-hover-bg: #1a9b7a;
  --color-primary-hover: #5eead4;
  --color-link-hover: #5eead4;
  --color-primary-soft: rgba(45, 212, 168, 0.15);
  --color-primary-text: #6ee7c7;
  --color-on-primary: #062018;
  --color-danger-text: #fcd34d;
  --color-success-text: #86efac;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100dvh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--color-link); text-decoration: none; }
a:not(.btn):not(.brand-link):not(.toolbar-link):hover {
  color: var(--color-link-hover);
  text-decoration: underline;
}

button:not(.nav-item):not(.theme-toggle):not(.btn-logout):not(.secondary):hover,
.btn:hover,
a.btn-primary:hover {
  background: var(--color-primary-hover-bg);
  color: var(--color-on-primary);
  text-decoration: none;
}

.nav-item:hover,
.nav-item:focus-visible {
  background: var(--color-primary-soft);
  color: var(--color-text);
}

button.theme-toggle:hover {
  background: color-mix(in srgb, var(--color-primary) 12%, transparent);
  color: var(--color-text);
  border-color: color-mix(in srgb, var(--color-primary) 40%, var(--color-border));
}

/* ── App layout ── */

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100dvh;
}

.app-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.app-content {
  flex: 1;
  padding: 1.5rem;
  max-width: 1280px;
  width: 100%;
}

/* ── Sidebar ── */

.sidebar {
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100dvh;
  overflow: hidden;
}

.sidebar-brand {
  padding: 1.125rem 1rem;
  border-bottom: 1px solid var(--color-border-subtle);
  flex-shrink: 0;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0.625rem 1rem;
}

.nav-group {
  margin-bottom: 1rem;
}

.nav-group-label {
  display: block;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--color-muted);
  padding: 0.25rem 0.75rem 0.5rem;
}

.nav-item {
  width: 100%;
  display: block;
  padding: 0.5rem 0.75rem;
  margin: 0 0 1px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-muted);
  font-size: 0.875rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}

.nav-item.active {
  background: var(--color-primary-soft);
  color: var(--color-primary-text);
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--color-primary);
}

.nav-item.active:hover,
.nav-item.active:focus-visible {
  background: var(--color-primary-soft);
  color: var(--color-primary-text);
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 90;
  border: none;
  cursor: pointer;
}

.sidebar-backdrop.visible {
  display: block;
}

/* ── Header ── */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: var(--header-height);
  padding: 0 1.5rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-start {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.header-end {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.page-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-toggle {
  display: none;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  margin: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
  flex-shrink: 0;
}

.sidebar-toggle svg {
  width: 1.125rem;
  height: 1.125rem;
  display: block;
  margin: auto;
}

.user-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.25;
  padding-right: 0.5rem;
  border-right: 1px solid var(--color-border);
  margin-right: 0.25rem;
}

.user-chip-name {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-text);
}

.user-chip-role {
  font-size: 0.6875rem;
  color: var(--color-muted);
  text-transform: capitalize;
}

.toolbar-link {
  display: inline-flex;
  align-items: center;
  padding: 0.4rem 0.65rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-muted);
  border-radius: var(--radius-sm);
  text-decoration: none;
  white-space: nowrap;
}

.toolbar-link:hover {
  color: var(--color-primary);
  background: var(--color-primary-soft);
  text-decoration: none;
}

.btn-logout {
  width: auto;
  margin: 0;
  padding: 0.45rem 0.85rem;
  font-size: 0.8125rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-muted);
  border: 1px solid var(--color-border);
  cursor: pointer;
}

.btn-logout:hover {
  color: var(--color-text);
  border-color: var(--color-muted);
  background: var(--color-border-subtle);
}

/* ── Brand (auth + sidebar) ── */

.brand-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  flex-shrink: 0;
}

.brand-link:hover {
  opacity: 0.92;
  text-decoration: none;
}

.brand-logo {
  display: block;
  width: auto;
  max-width: min(200px, 70vw);
  height: 36px;
  object-fit: contain;
  background: transparent;
}

.brand-logo-sm {
  height: 32px;
  max-width: min(160px, 45vw);
}

/* ── Cards ── */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.375rem;
  box-shadow: var(--shadow-sm);
  margin-bottom: 1rem;
}

.card h2 {
  margin: 0 0 1rem;
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
}

.card h3 {
  margin: 1.25rem 0 0.75rem;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--color-text);
}

.card-banner {
  border-left: 3px solid var(--color-primary);
  margin-bottom: 1.25rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

/* ── Forms ── */

input, select, textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  margin-top: 0.35rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
  font-size: 0.9375rem;
  transition: border-color 0.12s, box-shadow 0.12s;
}

button {
  width: 100%;
  padding: 0.6rem 0.75rem;
  margin-top: 0.35rem;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 0.9375rem;
  transition: border-color 0.12s, box-shadow 0.12s, background 0.12s, color 0.12s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--focus-ring);
}

button, .btn {
  background: var(--color-primary);
  color: var(--color-on-primary);
  border: none;
  cursor: pointer;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}

button:focus-visible, .btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

button[aria-busy='true'] {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}

button[aria-busy='true']::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 1.125rem;
  height: 1.125rem;
  border: 2px solid color-mix(in srgb, var(--color-on-primary) 35%, transparent);
  border-top-color: var(--color-on-primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  button[aria-busy='true']::after,
  .global-progress.active::after {
    animation: none;
  }
}

button.secondary {
  background: transparent;
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

button.secondary:hover {
  background: var(--color-border-subtle);
  border-color: var(--color-muted);
}

button.secondary[aria-busy='true']::after {
  border-color: color-mix(in srgb, var(--color-text) 35%, transparent);
  border-top-color: var(--color-text);
}

button:disabled { opacity: 0.55; cursor: not-allowed; }

label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--color-muted);
  display: block;
  margin-top: 0.75rem;
}

/* ── Lists ── */

.list-item {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 0.875rem;
  margin-top: 0.5rem;
  cursor: pointer;
  background: var(--color-bg);
  transition: border-color 0.12s, box-shadow 0.12s;
}

.list-item:hover {
  border-color: color-mix(in srgb, var(--color-primary) 40%, var(--color-border));
  box-shadow: var(--shadow-sm);
}

.list-item strong {
  font-size: 0.9375rem;
  font-weight: 600;
}

.badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
  font-size: 0.6875rem;
  font-weight: 600;
  background: var(--color-primary-soft);
  color: var(--color-primary-text);
  border: 1px solid color-mix(in srgb, var(--color-primary) 30%, var(--color-border));
  vertical-align: middle;
}

.hint {
  font-size: 0.8125rem;
  color: var(--color-muted);
  margin-top: 0.5rem;
  line-height: 1.5;
}

.empty-state {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--color-muted);
  font-size: 0.875rem;
}

.messages-pane {
  white-space: pre-wrap;
  font-size: 0.8125rem;
  font-family: ui-monospace, 'Cascadia Code', monospace;
  max-height: 480px;
  overflow: auto;
  padding: 0.75rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-sm);
  line-height: 1.6;
}

/* ── Auth pages ── */

.auth-wrap {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-card {
  width: min(400px, 100%);
  box-shadow: var(--shadow-lg);
}

.auth-brand { margin-bottom: 1.25rem; }

/* ── Toasts & overlay ── */

.toast-stack {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(360px, calc(100vw - 2rem));
}

.toast {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  box-shadow: var(--shadow-lg);
  font-size: 0.875rem;
  animation: slideIn 0.2s ease;
}

.toast.error { border-color: #fca5a5; }
.toast.success { border-color: #86efac; }

@keyframes slideIn {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  display: grid;
  place-items: center;
  z-index: 9998;
  pointer-events: none;
}

.global-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 9999;
  pointer-events: none;
  background: color-mix(in srgb, var(--color-primary) 25%, transparent);
  overflow: hidden;
}

.global-progress::after {
  content: '';
  display: block;
  height: 100%;
  width: 35%;
  background: var(--color-primary);
  transform: translateX(-100%);
}

.global-progress.active::after {
  animation: global-progress-slide 1s ease-in-out infinite;
}

@keyframes global-progress-slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(320%); }
}

.panel-busy {
  opacity: 0.75;
}

.row-actions button:disabled {
  opacity: 0.6;
  cursor: wait;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#wa-qr {
  max-width: 240px;
  display: block;
  margin: 0.75rem auto;
  background: #fff;
  padding: 0.5rem;
  border-radius: var(--radius-sm);
}

.impersonation-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--color-primary) 10%, var(--color-surface));
  border: 1px solid color-mix(in srgb, var(--color-primary) 35%, var(--color-border));
  font-size: 0.875rem;
}

.impersonation-banner.hidden {
  display: none !important;
}

.hidden {
  display: none !important;
}

.impersonation-banner .btn-logout {
  width: auto;
  margin: 0;
  white-space: nowrap;
}


.row-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.625rem;
  flex-wrap: wrap;
}

.row-actions button {
  width: auto;
  margin: 0;
  font-size: 0.8125rem;
  padding: 0.4rem 0.75rem;
}

/* ── Platform admin ── */

.page-intro {
  margin-bottom: 1.25rem;
}

.panel-title {
  margin: 0 0 0.35rem;
  font-size: 1.25rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  color: var(--color-text);
  text-transform: none;
}

.section-title {
  margin: 0 0 0.75rem;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--color-text);
}

.platform-split {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.9fr);
  gap: 1rem;
  align-items: start;
}

.platform-plans-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(300px, 0.85fr);
  gap: 1rem;
  align-items: start;
}

.platform-plans-side {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.platform-plans-layout .card,
.platform-plans-side .card {
  margin-bottom: 0;
}

.platform-tenants-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(300px, 0.85fr);
  gap: 1rem;
  align-items: start;
}

.platform-tenants-side {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.platform-tenants-layout .card,
.platform-tenants-side .card {
  margin-bottom: 0;
}

.input-readonly {
  opacity: 0.75;
  cursor: not-allowed;
  background: var(--color-border-subtle);
}

.badge-trial { background: color-mix(in srgb, #6366f1 14%, var(--color-bg)); color: #3730a3; border-color: color-mix(in srgb, #6366f1 35%, var(--color-border)); }
.badge-active { background: var(--color-primary-soft); color: var(--color-primary-text); border-color: color-mix(in srgb, var(--color-primary) 35%, var(--color-border)); }
.badge-grace { background: color-mix(in srgb, #d97706 14%, var(--color-bg)); color: var(--color-danger-text); border-color: color-mix(in srgb, #d97706 35%, var(--color-border)); }
.badge-blocked { background: color-mix(in srgb, #dc2626 14%, var(--color-bg)); color: #991b1b; border-color: color-mix(in srgb, #dc2626 35%, var(--color-border)); }
.badge-suspended { background: color-mix(in srgb, var(--color-muted) 18%, var(--color-bg)); color: var(--color-muted); border-color: var(--color-border); }

[data-theme='dark'] .badge-trial { color: #c7d2fe; }
[data-theme='dark'] .badge-grace { color: var(--color-danger-text); }
[data-theme='dark'] .badge-blocked { color: #fca5a5; }
[data-theme='dark'] .badge-suspended { color: #cbd5e1; }

.badge-warn {
  background: color-mix(in srgb, #d97706 14%, var(--color-bg));
  color: var(--color-danger-text);
  border: 1px solid color-mix(in srgb, #d97706 35%, var(--color-border));
}

[data-theme='dark'] .badge-warn {
  color: var(--color-danger-text);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  color: var(--color-text);
}

.platform-split .card {
  margin-bottom: 0;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.stat-card-accent {
  border-color: color-mix(in srgb, var(--color-primary) 45%, var(--color-border));
  background: var(--color-primary-soft);
}

.stat-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
}

.stat-value {
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-text);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 1rem;
}

.form-field label {
  margin-top: 0;
}

.form-field-span2 {
  grid-column: 1 / -1;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
}

.settings-block {
  padding: 1rem;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius);
  background: var(--color-bg);
}

.list-item-static {
  cursor: default;
}

.list-item-static:hover {
  box-shadow: none;
}

.list-item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn-block {
  width: 100%;
}

.empty-state {
  margin: 0.5rem 0 0;
  padding: 1.25rem;
  text-align: center;
  color: var(--color-muted);
  font-size: 0.9375rem;
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
}

/* ── Responsive ── */

@media (max-width: 1023px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    width: var(--sidebar-width);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .user-chip {
    display: none;
  }

  .app-content {
    padding: 1rem;
  }
}

@media (max-width: 900px) {
  .grid-2 { grid-template-columns: 1fr; }
  .platform-split { grid-template-columns: 1fr; }
  .platform-plans-layout { grid-template-columns: 1fr; }
  .platform-tenants-layout { grid-template-columns: 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: 1fr; }
  .app-header {
    padding: 0 1rem;
  }

  .header-end .toolbar-link span {
    display: none;
  }

  .toolbar-link {
    padding: 0.4rem;
  }
}
