:root {
  color-scheme: light;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-strong: #fdfefe;
  --surface-muted: #eef1f6;
  --border: #dde3ec;
  --border-strong: #cad3df;
  --text: #121826;
  --text-muted: #667085;
  --text-soft: #8a94a6;
  --brand: #0f766e;
  --brand-strong: #115e59;
  --brand-soft: #d9f4ef;
  --accent: #b45309;
  --danger: #c2410c;
  --success: #15803d;
  --shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
  --radius: 8px;
  --sidebar-width: 280px;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101418;
  --surface: #171c22;
  --surface-strong: #1d242c;
  --surface-muted: #232b35;
  --border: #2d3744;
  --border-strong: #3d4856;
  --text: #f4f7fb;
  --text-muted: #a8b3c2;
  --text-soft: #798698;
  --brand: #2dd4bf;
  --brand-strong: #5eead4;
  --brand-soft: rgba(45, 212, 191, 0.14);
  --accent: #f59e0b;
  --danger: #fb923c;
  --success: #4ade80;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  font-size: 16px;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

a {
  color: inherit;
}

button,
input,
textarea {
  font: inherit;
}

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
  transition: grid-template-columns 180ms ease;
}

[data-sidebar-state="closed"] .app-layout {
  grid-template-columns: 76px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 22px 16px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 40;
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 22px;
  text-decoration: none;
}

[data-sidebar-state="closed"] .brand {
  justify-content: center;
  padding-inline: 0;
}

[data-sidebar-state="closed"] .brand span:not(.brand-mark),
[data-sidebar-state="closed"] .sidebar-link span,
[data-sidebar-state="closed"] .sidebar-link small {
  display: none;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  background: var(--brand);
  color: #ffffff;
  font-weight: 800;
  box-shadow: 0 10px 22px rgba(15, 118, 110, 0.26);
}

.brand-mark img {
  width: 30px;
  height: 30px;
  display: block;
}

.brand-mark.large {
  width: 52px;
  height: 52px;
  font-size: 1.05rem;
}

.brand-mark.large img {
  width: 38px;
  height: 38px;
}

.brand strong,
.auth-brand strong {
  display: block;
  font-size: 0.98rem;
}

.brand small,
.auth-brand span {
  color: var(--text-muted);
  font-size: 0.78rem;
}

.sidebar-nav {
  display: grid;
  gap: 6px;
}

.sidebar-link {
  display: grid;
  grid-template-columns: 22px 1fr auto;
  align-items: center;
  min-height: 42px;
  padding: 0 12px;
  border-radius: var(--radius);
  color: var(--text-muted);
  text-decoration: none;
}

[data-sidebar-state="closed"] .sidebar-link {
  grid-template-columns: 1fr;
  justify-items: center;
  padding-inline: 0;
}

.sidebar-link.active {
  background: var(--brand-soft);
  color: var(--brand-strong);
  font-weight: 700;
}

.sidebar-link.disabled {
  cursor: not-allowed;
  opacity: 0.72;
}

.sidebar-link small {
  font-size: 0.68rem;
  color: var(--text-soft);
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(16px);
}

.topbar-title span,
.page-heading p,
.panel-header span,
.metric-card span,
.status-card span {
  display: block;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

.topbar-title strong {
  display: block;
  margin-top: 2px;
  font-size: 1.05rem;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-button,
.ghost-button,
.user-menu,
.primary-action {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.icon-button {
  width: 42px;
  height: 42px;
  display: inline-grid;
  place-items: center;
}

.sidebar-toggle {
  display: inline-grid;
}

.user-menu {
  min-height: 46px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 4px 5px;
}

.user-avatar {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  background: var(--surface-muted);
  color: var(--brand-strong);
  font-weight: 800;
}

.user-meta {
  display: grid;
  text-align: left;
}

.user-meta strong {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.user-meta small {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-muted);
  font-size: 0.76rem;
}

.content-shell {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: 28px;
  flex: 1;
}

.page-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 22px;
}

.page-heading h1 {
  margin: 4px 0 0;
  font-size: clamp(1.7rem, 2vw, 2.35rem);
  font-weight: 800;
}

.page-kicker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--success);
  font-weight: 700;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.metric-card,
.panel,
.status-card,
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.metric-card {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 112px;
  padding: 18px;
}

.metric-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  background: var(--surface-muted);
  color: var(--brand-strong);
  font-size: 1.2rem;
}

.metric-card strong {
  display: block;
  margin-top: 5px;
  font-size: 1.8rem;
  line-height: 1;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(320px, 0.7fr);
  gap: 16px;
  margin-top: 16px;
}

.panel {
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px;
  border-bottom: 1px solid var(--border);
}

.panel-header.compact {
  align-items: flex-start;
}

.panel-header h2 {
  margin: 3px 0 0;
  font-size: 1.08rem;
}

.ghost-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 12px;
  color: var(--text-muted);
}

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

.table-shell.no-horizontal-scroll {
  overflow-x: visible;
}

.app-table {
  width: 100%;
  border-collapse: collapse;
}

.app-table th,
.app-table td {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}

.instances-table {
  table-layout: fixed;
}

.instances-table th,
.instances-table td {
  white-space: normal;
  overflow-wrap: anywhere;
}

.instances-table th:last-child,
.instances-table td:last-child {
  width: 112px;
  text-align: right;
}

.instances-table small {
  overflow-wrap: anywhere;
}

.app-table th {
  color: var(--text);
  font-size: 0.78rem;
  text-transform: uppercase;
}

.empty-row td {
  height: 178px;
  text-align: center;
}

.empty-row i {
  display: block;
  margin-bottom: 8px;
  font-size: 1.5rem;
  color: var(--text-soft);
}

.status-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.status-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px;
  box-shadow: none;
}

.status-card strong {
  display: block;
  margin-top: 3px;
}

.status-card i {
  color: var(--brand-strong);
  font-size: 1.3rem;
}

.app-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 28px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  font-size: 0.86rem;
}

.auth-shell {
  min-height: 100vh;
  background:
    linear-gradient(135deg, color-mix(in srgb, var(--brand-soft) 54%, transparent), transparent 44%),
    var(--bg);
}

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: min(440px, 100%);
  padding: 30px;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.auth-heading {
  margin: 28px 0 22px;
}

.auth-heading h1 {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 800;
}

.auth-heading p {
  margin: 8px 0 0;
  color: var(--text-muted);
}

.app-alert {
  display: flex;
  gap: 10px;
  padding: 12px;
  margin-bottom: 16px;
  border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
  border-radius: var(--radius);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
}

.auth-form,
.form-field {
  display: grid;
  gap: 12px;
}

.form-field {
  gap: 7px;
}

.form-field label {
  color: var(--text);
  font-weight: 700;
}

.input-shell {
  display: grid;
  grid-template-columns: 20px 1fr;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-strong);
}

.input-shell:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-soft);
}

.input-shell input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.field-validation-error {
  color: var(--danger);
  font-size: 0.82rem;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
}

.primary-action {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 48px;
  margin-top: 4px;
  background: var(--brand);
  color: #ffffff;
  font-weight: 800;
}

.primary-action:hover {
  background: var(--brand-strong);
}

.inline-action {
  display: inline-flex;
  width: auto;
  min-height: 42px;
  margin: 0;
  padding: 0 14px;
  text-decoration: none;
}

.heading-actions,
.status-actions,
.table-actions,
.form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-actions .btn-group {
  flex-wrap: nowrap;
}

.table-actions form {
  display: inline-flex;
  margin: 0;
}

.filter-bar,
.app-form {
  display: grid;
  gap: 16px;
  padding: 18px;
}

.filter-bar {
  grid-template-columns: repeat(2, minmax(220px, 1fr)) auto auto;
  align-items: end;
  border-bottom: 1px solid var(--border);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.form-control {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-strong);
  color: var(--text);
}

.form-control:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-soft);
}

textarea.form-control {
  padding: 12px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: var(--radius);
  font-size: 0.78rem;
  font-weight: 800;
}

.status-pill.active,
.success-alert {
  color: var(--success);
  background: color-mix(in srgb, var(--success) 10%, var(--surface));
  border-color: color-mix(in srgb, var(--success) 32%, var(--border));
}

.status-pill.inactive {
  color: var(--text-muted);
  background: var(--surface-muted);
}

.integration-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.integration-stat {
  display: grid;
  gap: 4px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.integration-stat span,
.integration-card-header span,
.integration-meta span {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
}

.integration-stat strong {
  font-size: 1.35rem;
}

.integration-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.integration-card {
  display: grid;
  gap: 18px;
  padding: 18px;
}

.integration-card-header {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
}

.integration-card-header h2 {
  margin: 3px 0 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1.08rem;
}

.integration-provider-mark {
  display: inline-grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: var(--radius);
  background: var(--brand-soft);
  color: var(--brand-strong);
  font-size: 1.25rem;
}

.integration-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--border);
}

.integration-meta div {
  min-width: 0;
  display: grid;
  gap: 5px;
  padding: 12px;
  background: var(--surface-strong);
}

.integration-meta strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.integration-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.integration-actions form {
  margin: 0;
}

.icon-button.danger {
  color: var(--danger);
}

.integration-empty {
  grid-column: 1 / -1;
  display: grid;
  justify-items: center;
  gap: 12px;
  padding: 42px 18px;
  text-align: center;
}

.integration-empty i {
  font-size: 2rem;
  color: var(--brand-strong);
}

.integration-empty h2 {
  margin: 0;
  font-size: 1.15rem;
}

.integration-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.ghost-button.danger {
  color: var(--danger);
}

.ghost-button.success {
  color: var(--success);
}

.pager {
  display: flex;
  gap: 8px;
  padding: 16px 18px;
}

.pager a {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text-muted);
}

.pager a.active {
  background: var(--brand);
  color: #ffffff;
}

.details-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(320px, 0.7fr);
  gap: 16px;
}

.settings-layout {
  display: grid;
  grid-template-columns: minmax(240px, 0.35fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.settings-whatsapp {
  display: grid;
  gap: 16px;
}

.settings-whatsapp-hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px;
}

.settings-whatsapp-hero span,
.integration-modal .modal-header span {
  display: block;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

.settings-whatsapp-hero h2,
.integration-modal .modal-header h2 {
  margin: 3px 0 0;
  font-size: 1.25rem;
}

.legacy-settings-panel {
  margin-top: 4px;
}

.integration-modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.integration-modal .modal-header,
.integration-modal .modal-footer {
  border-color: var(--border);
}

.integration-modal .modal-footer {
  gap: 8px;
}

.instance-edit-meta {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-strong);
}

.instance-edit-meta strong,
.instance-edit-meta small {
  overflow-wrap: anywhere;
}

.instance-modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

.detail-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  margin: 0;
  background: var(--border);
}

.detail-list div {
  padding: 16px;
  background: var(--surface);
}

.detail-list dt {
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

.detail-list dd {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
  color: var(--text);
}

.notes-block {
  padding: 18px;
  border-top: 1px solid var(--border);
}

.notes-block p {
  margin: 8px 0 0;
  color: var(--text-muted);
}

.future-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.future-block {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-muted);
  color: var(--text-muted);
  text-align: left;
}

.future-block.enabled {
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}

.future-block.enabled:hover {
  border-color: var(--brand);
  color: var(--brand-strong);
}

.key-reveal {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.key-reveal p {
  margin: 0;
  color: var(--text-muted);
}

.key-reveal code {
  display: block;
  padding: 14px;
  overflow-wrap: anywhere;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-muted);
  color: var(--text);
}

.qr-shell,
.qr-empty {
  display: grid;
  place-items: center;
  min-height: 320px;
  padding: 24px;
}

.qr-code {
  width: min(280px, 100%);
  aspect-ratio: 1;
  object-fit: contain;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
  padding: 14px;
}

.qr-empty {
  color: var(--text-muted);
  text-align: center;
}

.qr-empty i {
  font-size: 2rem;
  color: var(--text-soft);
}

.qr-refresh-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-strong);
  color: var(--text);
  font-weight: 800;
  transition: border-color 0.2s ease, background 0.2s ease, color 0.2s ease;
}

.qr-refresh-button:hover {
  border-color: var(--brand);
  background: var(--brand-soft);
  color: var(--brand-strong);
}

.message-preview-shell {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.whatsapp-preview {
  min-height: 180px;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.38) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255, 255, 255, 0.38) 25%, transparent 25%),
    var(--surface);
  background-size: 18px 18px;
}

.whatsapp-bubble {
  max-width: min(640px, 100%);
  min-width: 96px;
  min-height: 42px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: #dcf8c6;
  color: #102016;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.12);
}

@media (max-width: 1180px) {
  .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .integration-summary,
  .integration-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .dashboard-grid {
    grid-template-columns: 1fr;
  }

  .filter-bar,
  .details-grid,
  .settings-layout {
    grid-template-columns: 1fr;
  }

  .integration-summary,
  .integration-grid,
  .integration-form-grid,
  .integration-meta {
    grid-template-columns: 1fr;
  }
}

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

  [data-sidebar-state="closed"] .app-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    left: 0;
    transform: translateX(-100%);
    width: min(var(--sidebar-width), calc(100vw - 32px));
    transition: transform 180ms ease;
  }

  [data-sidebar-state="closed"] .brand {
    justify-content: flex-start;
    padding: 8px 8px 22px;
  }

  [data-sidebar-state="closed"] .brand span:not(.brand-mark),
  [data-sidebar-state="closed"] .sidebar-link span,
  [data-sidebar-state="closed"] .sidebar-link small {
    display: initial;
  }

  [data-sidebar-state="closed"] .brand span:not(.brand-mark) {
    display: inline;
  }

  [data-sidebar-state="closed"] .sidebar-link {
    grid-template-columns: 22px 1fr auto;
    justify-items: stretch;
    padding-inline: 12px;
  }

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

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    display: none;
    background: rgba(0, 0, 0, 0.35);
    z-index: 35;
  }

  .sidebar-backdrop.is-open {
    display: block;
  }

  .content-shell,
  .topbar {
    padding-inline: 18px;
  }

  .user-meta {
    display: none;
  }
}

@media (max-width: 620px) {
  .topbar-title span {
    display: none;
  }

  .metrics-grid {
    grid-template-columns: 1fr;
  }

  .page-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .app-footer {
    flex-direction: column;
  }

  .form-grid,
  .detail-list {
    grid-template-columns: 1fr;
  }

  .heading-actions {
    width: 100%;
    flex-wrap: wrap;
  }
}
