:root {
  --ink: #14201f;
  --muted: #5c6b69;
  --line: rgba(20, 32, 31, 0.1);
  --surface: rgba(255, 252, 247, 0.88);
  --surface-solid: #fffdf9;
  --panel: #ffffff;
  --teal: #0f5c56;
  --teal-deep: #0a3d39;
  --teal-soft: #d8efeb;
  --amber: #c9851a;
  --amber-soft: #f7e7c8;
  --coral: #c45a45;
  --coral-soft: #f8e4df;
  --sage: #3d7a62;
  --sage-soft: #dceee5;
  --sky: #2f6f8f;
  --sky-soft: #d9eaf2;
  --shadow: 0 18px 50px rgba(15, 40, 36, 0.1);
  --radius: 18px;
  --font: "Sora", "Segoe UI", sans-serif;
  --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(15, 92, 86, 0.18), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(201, 133, 26, 0.16), transparent 50%),
    linear-gradient(165deg, #e8f2ef 0%, #f4efe6 42%, #eef3f1 100%);
  background-attachment: fixed;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.35;
  background-image:
    linear-gradient(rgba(15, 92, 86, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(15, 92, 86, 0.04) 1px, transparent 1px);
  background-size: 28px 28px;
  mask-image: linear-gradient(to bottom, black, transparent 90%);
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* Login */
.login-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
}
.login-atmosphere {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(120deg, rgba(10, 61, 57, 0.55), rgba(20, 32, 31, 0.25)),
    url("./logo.jpeg") center/cover;
  filter: saturate(0.7) blur(0px);
  opacity: 0.22;
}
.login-card {
  position: relative;
  width: min(420px, 100%);
  padding: 36px 32px;
  border-radius: 28px;
  background: rgba(255, 253, 249, 0.94);
  border: 1px solid rgba(255, 255, 255, 0.7);
  box-shadow: var(--shadow);
  animation: rise 0.55s ease both;
}
.login-logo {
  width: 56px; height: 56px; border-radius: 14px; object-fit: cover;
  box-shadow: 0 8px 20px rgba(15, 92, 86, 0.25);
}
.login-brand {
  margin: 14px 0 0;
  font-family: var(--display);
  font-size: 2rem;
  letter-spacing: -0.03em;
  color: var(--teal-deep);
}
.login-card h1 {
  margin: 4px 0 0;
  font-size: 1.15rem;
  font-weight: 600;
}
.login-sub { margin: 8px 0 22px; color: var(--muted); font-size: 0.92rem; }
.login-card label {
  display: grid; gap: 6px; margin-bottom: 14px; font-size: 0.82rem; color: var(--muted); font-weight: 500;
}
.login-card input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
}
.login-card button[type="submit"] {
  width: 100%; margin-top: 8px; padding: 13px 16px; border: 0; border-radius: 12px;
  background: linear-gradient(135deg, var(--teal), var(--teal-deep));
  color: #fff; font-weight: 600;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.login-card button[type="submit"]:hover { transform: translateY(-1px); box-shadow: 0 10px 24px rgba(15, 92, 86, 0.3); }
.login-error { color: var(--coral); min-height: 1.2em; font-size: 0.88rem; }
.login-hint { margin: 10px 0 0; color: var(--muted); font-size: 0.78rem; }

/* Shell */
.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
}
.rail {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 18px;
  padding: 22px 16px;
  background: linear-gradient(180deg, rgba(10, 61, 57, 0.96), rgba(15, 40, 36, 0.98));
  color: #e8f4f1;
  z-index: 5;
}
.rail-brand {
  display: flex; gap: 12px; align-items: center; padding: 4px 8px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.rail-brand img { width: 40px; height: 40px; border-radius: 10px; object-fit: cover; }
.rail-brand strong {
  display: block; font-family: var(--display); font-size: 1.25rem; letter-spacing: -0.02em;
}
.rail-brand span { font-size: 0.75rem; opacity: 0.7; }
.rail-nav { display: grid; gap: 4px; flex: 1; align-content: start; }
.nav-btn {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 12px; border: 0; border-radius: 12px;
  background: transparent; color: rgba(232, 244, 241, 0.72); text-align: left;
  transition: background 0.18s ease, color 0.18s ease, transform 0.18s ease;
}
.nav-btn i { font-size: 1.05rem; width: 1.2em; }
.nav-btn:hover { background: rgba(255,255,255,0.08); color: #fff; }
.nav-btn.active {
  background: linear-gradient(135deg, rgba(201, 133, 26, 0.95), rgba(180, 110, 20, 0.9));
  color: #fff; box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}
.rail-foot { display: grid; gap: 8px; }
.user-chip {
  font-size: 0.78rem; padding: 8px 10px; border-radius: 10px;
  background: rgba(255,255,255,0.08); color: rgba(255,255,255,0.85);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.workspace { padding: 22px 26px 40px; min-width: 0; }
.topbar {
  display: flex; flex-wrap: wrap; gap: 14px; justify-content: space-between; align-items: end;
  margin-bottom: 22px; animation: rise 0.4s ease both;
}
.eyebrow {
  margin: 0; text-transform: uppercase; letter-spacing: 0.12em;
  font-size: 0.7rem; color: var(--muted); font-weight: 600;
}
.topbar h2 {
  margin: 4px 0 0; font-family: var(--display); font-size: clamp(1.6rem, 2.4vw, 2.1rem);
  letter-spacing: -0.03em; color: var(--teal-deep);
}
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sync-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 999px; background: var(--sage-soft); color: var(--sage);
  font-size: 0.78rem; font-weight: 600;
}
.sync-pill.bad { background: var(--coral-soft); color: var(--coral); }
.sync-pill.busy { background: var(--amber-soft); color: var(--amber); }

.ghost-btn, .primary-btn, .soft-btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); font-weight: 600; font-size: 0.88rem;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.ghost-btn:hover, .primary-btn:hover, .soft-btn:hover { transform: translateY(-1px); }
.primary-btn {
  border: 0; background: linear-gradient(135deg, var(--teal), var(--teal-deep)); color: #fff;
  box-shadow: 0 8px 18px rgba(15, 92, 86, 0.22);
}
.soft-btn { background: var(--teal-soft); border-color: transparent; color: var(--teal-deep); }
.rail .ghost-btn { background: rgba(255,255,255,0.08); border-color: transparent; color: #fff; }

.view { display: none; animation: rise 0.35s ease both; }
.view.active { display: block; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.kpi {
  padding: 16px 16px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid rgba(255,255,255,0.65);
  box-shadow: 0 10px 28px rgba(15, 40, 36, 0.05);
  transition: transform 0.2s ease;
}
.kpi:hover { transform: translateY(-2px); }
.kpi span { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 500; }
.kpi strong {
  display: block; margin-top: 6px; font-size: 1.45rem; font-weight: 700; letter-spacing: -0.02em;
  color: var(--teal-deep);
}
.kpi em { display: block; margin-top: 4px; font-style: normal; font-size: 0.72rem; color: var(--muted); }

.panel-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 12px 30px rgba(15, 40, 36, 0.05);
  min-height: 280px;
}
.panel h3 {
  margin: 0 0 12px; font-size: 0.95rem; font-weight: 600; color: var(--ink);
}
.panel canvas { width: 100% !important; max-height: 240px; }

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
}
.filters label {
  display: grid; gap: 5px; font-size: 0.72rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
}
.filters input, .filters select {
  padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: #fff;
}
.filter-actions { display: flex; gap: 8px; align-items: end; }

.table-wrap {
  overflow: auto;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 12px 30px rgba(15, 40, 36, 0.05);
}
table.data {
  width: 100%; border-collapse: collapse; font-size: 0.86rem;
}
table.data th, table.data td {
  padding: 11px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap;
}
table.data th {
  position: sticky; top: 0; background: #f7faf8; font-size: 0.72rem;
  text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
}
table.data tbody tr { cursor: pointer; transition: background 0.15s ease; }
table.data tbody tr:hover { background: var(--teal-soft); }
table.data .money { font-variant-numeric: tabular-nums; font-weight: 600; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 9px; border-radius: 999px; font-size: 0.72rem; font-weight: 600;
  background: var(--teal-soft); color: var(--teal-deep);
}
.badge.at_risk, .badge.lost { background: var(--coral-soft); color: var(--coral); }
.badge.inactive { background: var(--amber-soft); color: #8a5a10; }
.badge.loyal, .badge.high_value { background: var(--sage-soft); color: var(--sage); }
.badge.new, .badge.first_time { background: var(--sky-soft); color: var(--sky); }

.segment-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}
.segment-card {
  padding: 18px; border-radius: var(--radius); background: var(--panel);
  border: 1px solid var(--line); cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.segment-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); }
.segment-card h4 { margin: 0 0 6px; font-size: 1rem; }
.segment-card .count {
  font-family: var(--display); font-size: 2rem; color: var(--teal-deep); letter-spacing: -0.03em;
}
.segment-card p { margin: 8px 0 0; color: var(--muted); font-size: 0.82rem; }

.profile-hero {
  display: grid; grid-template-columns: 1.2fr 1fr; gap: 14px; margin-bottom: 14px;
}
.profile-card, .stat-card {
  padding: 18px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line);
}
.profile-card h3 {
  margin: 0 0 4px; font-family: var(--display); font-size: 1.7rem; letter-spacing: -0.02em; color: var(--teal-deep);
}
.meta-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 14px;
}
.meta-grid div { padding: 10px 12px; border-radius: 12px; background: #f6f9f7; }
.meta-grid small { display: block; color: var(--muted); font-size: 0.72rem; }
.meta-grid strong { font-size: 0.9rem; }

.timeline {
  list-style: none; margin: 0; padding: 0; position: relative;
}
.timeline::before {
  content: ""; position: absolute; left: 10px; top: 4px; bottom: 4px; width: 2px;
  background: linear-gradient(var(--teal), transparent);
}
.timeline li {
  position: relative; padding: 0 0 16px 36px;
}
.timeline li::before {
  content: ""; position: absolute; left: 4px; top: 4px; width: 14px; height: 14px;
  border-radius: 50%; background: var(--teal); box-shadow: 0 0 0 4px var(--teal-soft);
}
.timeline .when { font-size: 0.75rem; color: var(--muted); }
.timeline .what { font-weight: 600; margin-top: 2px; }
.timeline .detail { font-size: 0.84rem; color: var(--muted); }

.pager {
  display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px;
}
.empty {
  padding: 40px 20px; text-align: center; color: var(--muted);
}
.toast {
  position: fixed; right: 18px; bottom: 18px; z-index: 50;
  padding: 12px 16px; border-radius: 12px; background: var(--teal-deep); color: #fff;
  box-shadow: var(--shadow); animation: rise 0.25s ease both;
}
.toast.err { background: var(--coral); }

.whatsapp-panel {
  padding: 28px; border-radius: 24px;
  background:
    linear-gradient(135deg, rgba(15, 92, 86, 0.08), rgba(201, 133, 26, 0.1)),
    var(--panel);
  border: 1px dashed rgba(15, 92, 86, 0.35);
}
.whatsapp-panel h3 { margin-top: 0; font-family: var(--display); font-size: 1.6rem; color: var(--teal-deep); }
.feature-list {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 8px; margin-top: 16px;
}
.feature-list span {
  padding: 10px 12px; border-radius: 12px; background: #fff; border: 1px solid var(--line);
  font-size: 0.84rem;
}

.wa-hero {
  display: grid; gap: 14px;
  padding: 20px 20px 8px;
  border-radius: 22px;
  margin-bottom: 14px;
  background:
    radial-gradient(600px 200px at 100% 0%, rgba(37, 211, 102, 0.16), transparent 55%),
    linear-gradient(135deg, rgba(15, 92, 86, 0.08), rgba(255,253,249,0.95));
  border: 1px solid var(--line);
}
.wa-hero h3 {
  margin: 0;
  font-family: var(--display);
  font-size: 1.55rem;
  color: var(--teal-deep);
  letter-spacing: -0.02em;
}
.wa-hero p { margin: 6px 0 0; color: var(--muted); max-width: 70ch; }
.wa-hero-kpis { margin: 0; }
.wa-tabs {
  display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px;
}
.wa-tab {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
  padding: 9px 14px;
  font-weight: 600;
  font-size: 0.84rem;
  color: var(--muted);
}
.wa-tab.active {
  background: linear-gradient(135deg, #128C7E, #075E54);
  color: #fff;
  border-color: transparent;
}
.wa-grid {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 14px;
}
.wa-grid-templates { grid-template-columns: 0.95fr 1.05fr; }
.wa-form-panel h3 { margin-top: 0; }
.wa-help { color: var(--muted); font-size: 0.84rem; margin: 0 0 10px; }
.wa-form { background: transparent; border: 0; padding: 0; box-shadow: none; }
.wa-form textarea {
  width: 100%; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: #fff;
  font: inherit; resize: vertical;
}
.wa-header-field[hidden], .wa-warning[hidden] { display: none !important; }
.wa-upload-action {
  display: flex; flex-direction: column; justify-content: end; align-items: flex-start; gap: 6px;
}
.wa-upload-action small { color: var(--muted); font-size: 0.7rem; }
.wa-check {
  display: flex !important; flex-direction: row !important; align-items: center; gap: 8px;
  text-transform: none !important; letter-spacing: 0 !important; font-size: 0.88rem !important;
  color: var(--ink) !important; font-weight: 600 !important;
}
.wa-steps { margin: 0; padding-left: 18px; color: var(--ink); line-height: 1.55; }
.wa-steps li { margin-bottom: 8px; }
.wa-result {
  margin-top: 12px; padding: 12px; border-radius: 12px;
  background: #0f2a27; color: #d7fff0; font-size: 0.78rem; overflow: auto; max-height: 220px;
}
.wa-tpl-list { display: grid; gap: 12px; }
.wa-tpl-card {
  padding: 14px; border-radius: 18px; background: var(--panel); border: 1px solid var(--line);
}
.wa-tpl-card-top {
  display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-bottom: 10px;
}
.wa-tpl-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 6px; }
.wa-phone {
  background: linear-gradient(180deg, #0b3d39, #062926);
  border-radius: 22px;
  padding: 14px 12px 16px;
  color: #e8fff6;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.08);
}
.wa-phone-top {
  font-size: 0.72rem; opacity: 0.7; margin-bottom: 10px; text-align: center; letter-spacing: 0.04em;
}
.wa-bubble {
  background: #dcf8c6;
  color: #143028;
  border-radius: 14px 14px 4px 14px;
  padding: 10px 12px;
  max-width: 92%;
  margin-left: auto;
  box-shadow: 0 8px 18px rgba(0,0,0,0.18);
}
.wa-bubble-image {
  display: block; width: calc(100% + 24px); max-height: 210px; object-fit: cover;
  margin: -10px -12px 9px; border-radius: 14px 14px 0 0;
}
.wa-bubble-media-note {
  margin: -2px 0 8px; padding: 9px; border-radius: 9px;
  background: rgba(15, 92, 86, 0.1); font-size: 0.74rem; font-weight: 600;
}
.wa-bubble-header { font-weight: 700; margin-bottom: 4px; font-size: 0.88rem; }
.wa-bubble-body { white-space: pre-wrap; font-size: 0.86rem; line-height: 1.4; }
.wa-bubble-footer { margin-top: 6px; font-size: 0.72rem; opacity: 0.7; }
.wa-bubble-btn {
  margin-top: 8px; text-align: center; padding: 7px; border-radius: 8px;
  background: rgba(15, 92, 86, 0.12); font-weight: 600; font-size: 0.8rem; color: var(--teal-deep);
}
.wa-phone-meta {
  display: flex; justify-content: space-between; gap: 8px; align-items: center;
  margin-top: 10px; font-size: 0.72rem;
}
.wa-meta-name { opacity: 0.75; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wa-sample {
  padding: 12px; border-radius: 14px; border: 1px solid var(--line); margin-bottom: 10px; background: #f7faf8;
}
.wa-warning {
  margin: 10px 0 0; padding: 10px 12px; border-radius: 10px;
  background: var(--amber-soft); color: #7a4d08; font-size: 0.84rem; font-weight: 600;
}
.wa-automation-toolbar, .wa-log-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px;
}
.wa-automation-toolbar h3, .wa-automation-card h3 { margin: 0 0 4px; }
.wa-automation-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px;
}
.wa-automation-card {
  padding: 18px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line);
}
.wa-automation-card p { color: var(--muted); font-size: 0.82rem; }
.wa-switch-state {
  display: inline-flex; padding: 5px 10px; border-radius: 999px; font-size: 0.72rem; font-weight: 700;
}
.wa-switch-state.on { background: #d9f8e3; color: #137438; }
.wa-switch-state.off { background: var(--coral-soft); color: var(--coral); }
.wa-automation-metrics {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
}
.wa-automation-metrics span {
  padding: 9px 10px; border-radius: 10px; background: var(--surface); color: var(--muted); font-size: 0.75rem;
}
.wa-automation-metrics strong { display: block; color: var(--ink); font-size: 1rem; }
.wa-log-toolbar { margin-bottom: 8px; }
.wa-log-toolbar .wa-help { margin: 0; }
.badge.marketing, .badge.welcome { background: var(--amber-soft); color: #8a5a10; }
.badge.reminder, .badge.reengagement { background: var(--coral-soft); color: var(--coral); }
.badge.order, .badge.utility { background: var(--sky-soft); color: var(--sky); }

@media (max-width: 980px) {
  .wa-grid, .wa-grid-templates { grid-template-columns: 1fr; }
}

.rules-editor textarea {
  width: 100%; min-height: 320px; padding: 14px; border-radius: 14px;
  border: 1px solid var(--line); font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem; background: #fff;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 1fr; }
  .rail {
    position: sticky; top: 0; height: auto; z-index: 20;
    flex-direction: row; flex-wrap: wrap; align-items: center;
    padding: 12px;
  }
  .rail-nav { display: flex; flex-wrap: wrap; flex: 1; }
  .rail-foot { flex-direction: row; align-items: center; }
  .nav-btn span { display: none; }
  .panel-grid, .profile-hero { grid-template-columns: 1fr; }
  .workspace { padding: 16px; }
}
