/* ============================================================
   Tokens
   ============================================================ */
:root {
  color-scheme: dark;

  --ease: cubic-bezier(0.2, 0, 0, 1);
  --dur-fast: 150ms;
  --dur: 200ms;
  --dur-slow: 250ms;

  --radius-sm: 8px;
  --radius-md: 10px;
  --radius-lg: 12px;

  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  /* Neutros (zinc) */
  --zinc-950: #0a0a0b;
  --zinc-900: #111113;
  --zinc-850: #17171a;
  --zinc-800: #1e1e21;
  --zinc-700: #2b2b2f;
  --zinc-600: #3f3f45;
  --zinc-500: #6b6b72;
  --zinc-400: #96969d;
  --zinc-300: #b8b8bf;
  --zinc-200: #d8d8dc;
  --zinc-100: #ececee;
  --zinc-50: #f7f7f8;

  /* Acento (verde sobrio) */
  --accent: #30a46c;
  --accent-hover: #37b878;
  --accent-active: #2b9560;
  --accent-fg: #ffffff;
  --accent-soft-bg: rgba(48, 164, 108, 0.14);
  --accent-soft-border: rgba(48, 164, 108, 0.32);

  /* Semántica de estado */
  --state-online: #30a46c;
  --state-qr: #e2a336;
  --state-reconnecting: #e8792e;
  --state-loggedout: #e5484d;
  --state-neutral: var(--zinc-500);

  /* Superficies */
  --bg: var(--zinc-950);
  --surface: var(--zinc-900);
  --surface-raised: var(--zinc-850);
  --border: var(--zinc-700);
  --border-subtle: var(--zinc-800);

  --text-primary: var(--zinc-100);
  --text-secondary: var(--zinc-400);
  --text-tertiary: var(--zinc-500);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.45);

  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #fafafa;
    --surface: #ffffff;
    --surface-raised: #ffffff;
    --border: #e4e4e7;
    --border-subtle: #ececef;
    --text-primary: #18181b;
    --text-secondary: #52525b;
    --text-tertiary: #8b8b93;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -0.006em;
}

h1, h2, h3 { margin: 0; letter-spacing: -0.02em; }

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

button {
  background: none;
  border: none;
  cursor: pointer;
}

::selection { background: var(--accent-soft-bg); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.tabular {
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   Toasts
   ============================================================ */
.toasts {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(360px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: 13px;
  color: var(--text-primary);
  animation: toast-in var(--dur-slow) var(--ease);
}

.toast[data-leaving="true"] {
  animation: toast-out var(--dur) var(--ease) forwards;
}

.toast__icon { font-size: 15px; line-height: 1; flex-shrink: 0; }
.toast--success .toast__icon::before { content: "✓"; color: var(--accent); }
.toast--error .toast__icon::before { content: "✕"; color: var(--state-loggedout); }
.toast--info .toast__icon::before { content: "•"; color: var(--text-tertiary); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(-6px) scale(0.98); }
}

/* ============================================================
   Config gate
   ============================================================ */
.config-gate {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.config-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 28px 24px 24px;
  box-shadow: var(--shadow-md);
  animation: card-in var(--dur-slow) var(--ease);
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.config-card__title {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 4px;
}

.config-card__hint {
  font-size: 13px;
  color: var(--text-tertiary);
  margin: 0 0 20px;
}

.field {
  display: block;
  margin-bottom: 16px;
}

.field__label {
  display: block;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.field input,
.field .textarea {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  font-size: 13.5px;
  color: var(--text-primary);
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}

.field input::placeholder,
.field .textarea::placeholder {
  color: var(--zinc-600);
}

.field input:hover,
.field .textarea:hover {
  border-color: var(--zinc-600);
}

.field input:focus-visible,
.field .textarea:focus-visible {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-soft-bg);
}

.field input[aria-invalid="true"] {
  border-color: var(--state-loggedout);
}

.field__error {
  display: block;
  min-height: 16px;
  font-size: 12px;
  color: var(--state-loggedout);
  margin-top: 5px;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  border: 1px solid transparent;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
    transform var(--dur-fast) var(--ease), opacity var(--dur) var(--ease);
  white-space: nowrap;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn--primary {
  background: var(--accent);
  color: var(--accent-fg);
}
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn--primary:active:not(:disabled) { background: var(--accent-active); }

.btn--ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
}
.btn--ghost:hover { border-color: var(--zinc-500); color: var(--text-primary); }

.btn--small { padding: 6px 12px; font-size: 12.5px; }
.btn--block { width: 100%; }

/* ============================================================
   Layout
   ============================================================ */
.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.topbar__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.topbar__title { letter-spacing: -0.02em; }

.tabs--top { margin-left: auto; }
.topbar .btn--ghost { flex-shrink: 0; }

.main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 20px;
  animation: panel-in var(--dur-slow) var(--ease);
}

@keyframes panel-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel__head { margin-bottom: 16px; }
.panel__title { font-size: 16px; font-weight: 600; }

/* Tabs */
.tabs { display: flex; gap: 4px; }

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-tertiary);
  transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}

.tab:hover { color: var(--text-primary); background: var(--surface); }

.tab[aria-selected="true"] {
  color: var(--text-primary);
  background: var(--surface-raised);
}

.tabs--bottom {
  display: none;
}

/* ============================================================
   Phones view
   ============================================================ */
.phones-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.phone-card {
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 16px;
  transition: border-color var(--dur) var(--ease);
  animation: card-in var(--dur-slow) var(--ease);
}

.phone-card:hover { border-color: var(--border); }

.phone-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.phone-card__name {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.phone-card__time {
  font-size: 12px;
  color: var(--text-tertiary);
}

.phone-card__qr {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  background: #fff;
  border-radius: var(--radius-md);
  padding: 10px;
}

.phone-card__qr img {
  width: 100%;
  max-width: 200px;
  height: auto;
  display: block;
  border-radius: 4px;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px 3px 6px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 500;
  flex-shrink: 0;
}

.badge__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  position: relative;
}

.badge--online {
  color: var(--state-online);
  background: rgba(48, 164, 108, 0.14);
}
.badge--online .badge__dot {
  background: var(--state-online);
}
.badge--online .badge__dot::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  background: var(--state-online);
  opacity: 0.35;
  animation: pulse 1.8s var(--ease) infinite;
}

.badge--qr { color: var(--state-qr); background: rgba(226, 163, 54, 0.14); }
.badge--qr .badge__dot { background: var(--state-qr); }

.badge--reconnecting { color: var(--state-reconnecting); background: rgba(232, 121, 46, 0.14); }
.badge--reconnecting .badge__dot { background: var(--state-reconnecting); }

.badge--loggedout { color: var(--state-loggedout); background: rgba(229, 72, 77, 0.14); }
.badge--loggedout .badge__dot { background: var(--state-loggedout); }

.badge--neutral { color: var(--text-tertiary); background: var(--surface-raised); }
.badge--neutral .badge__dot { background: var(--text-tertiary); }

@keyframes pulse {
  0% { transform: scale(0.7); opacity: 0.5; }
  70% { transform: scale(1.8); opacity: 0; }
  100% { opacity: 0; }
}

/* ============================================================
   Inbox
   ============================================================ */
.inbox {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
}

.inbox__list {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  overflow-y: auto;
  background: var(--surface);
}

.chat-row {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-subtle);
  transition: background var(--dur-fast) var(--ease);
}

.chat-row:last-child { border-bottom: none; }
.chat-row:hover { background: var(--surface-raised); }
.chat-row[aria-current="true"] { background: var(--accent-soft-bg); }

.chat-row__top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.chat-row__phone { font-weight: 600; font-size: 13.5px; }
.chat-row__time { font-size: 11.5px; color: var(--text-tertiary); flex-shrink: 0; }

.chat-row__preview {
  font-size: 12.5px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inbox__thread {
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.inbox__thread--empty {
  align-items: center;
  justify-content: center;
}

.thread__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.thread__back {
  display: none;
}

.thread__phone { font-weight: 600; font-size: 14px; }

.thread__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  max-width: 72%;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  animation: bubble-in var(--dur) var(--ease);
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.bubble--in {
  align-self: flex-start;
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-bottom-left-radius: 3px;
}

.bubble--out {
  align-self: flex-end;
  background: var(--accent-soft-bg);
  border: 1px solid var(--accent-soft-border);
  border-bottom-right-radius: 3px;
}

.bubble__time {
  display: block;
  margin-top: 4px;
  font-size: 10.5px;
  color: var(--text-tertiary);
}

/* Toggle bot/humano */
.mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--surface-raised);
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

.mode-toggle:active { transform: scale(0.98); }

.mode-toggle[data-mode="bot"] {
  color: var(--accent);
  border-color: var(--accent-soft-border);
  background: var(--accent-soft-bg);
}

.mode-toggle[data-mode="human"] {
  color: var(--state-qr);
  border-color: rgba(226, 163, 54, 0.32);
  background: rgba(226, 163, 54, 0.14);
}

/* ============================================================
   Settings
   ============================================================ */
.settings {
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 16px;
}

.settings-row__title { font-weight: 600; font-size: 13.5px; margin: 0 0 3px; }
.settings-row__hint { font-size: 12.5px; color: var(--text-tertiary); margin: 0; }

.settings-block {
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 16px;
}

.textarea {
  resize: vertical;
  min-height: 160px;
  font-family: var(--font-sans);
}

/* Switch */
.switch {
  --w: 40px;
  --h: 22px;
  width: var(--w);
  height: var(--h);
  border-radius: 999px;
  background: var(--zinc-700);
  position: relative;
  flex-shrink: 0;
  transition: background var(--dur) var(--ease);
}

.switch[aria-checked="true"] { background: var(--accent); }

.switch__thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(var(--h) - 4px);
  height: calc(var(--h) - 4px);
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform var(--dur) var(--ease);
}

.switch[aria-checked="true"] .switch__thumb {
  transform: translateX(calc(var(--w) - var(--h)));
}

.switch:active .switch__thumb { transform: scale(0.92); }
.switch[aria-checked="true"]:active .switch__thumb {
  transform: translateX(calc(var(--w) - var(--h))) scale(0.92);
}

/* ============================================================
   Skeletons
   ============================================================ */
.skeleton {
  border-radius: var(--radius-md);
  background: linear-gradient(90deg, var(--surface-raised) 25%, var(--zinc-700) 50%, var(--surface-raised) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-card {
  height: 96px;
  border-radius: var(--radius-lg);
}

.skeleton-row {
  height: 56px;
  border-radius: var(--radius-md);
  margin-bottom: 8px;
}

/* ============================================================
   Empty / error states
   ============================================================ */
.empty-state,
.error-state {
  text-align: center;
  padding: 48px 20px;
  margin: auto;
}

.empty-state__title,
.error-state__title {
  font-size: 14px;
  font-weight: 600;
  margin: 0 0 4px;
  color: var(--text-primary);
}

.empty-state__hint,
.error-state__hint {
  font-size: 13px;
  color: var(--text-tertiary);
  margin: 0 0 16px;
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
}

.error-state__title { color: var(--state-loggedout); }

/* ============================================================
   Responsive / mobile
   ============================================================ */
@media (max-width: 768px) {
  .tabs--top { display: none; }
  .topbar { gap: 12px; }
  .topbar__title { font-size: 13.5px; }

  .tabs--bottom {
    display: flex;
    position: sticky;
    bottom: 0;
    z-index: 20;
    background: var(--bg);
    border-top: 1px solid var(--border-subtle);
    padding: 6px 8px calc(6px + var(--safe-bottom));
  }

  .tabs--bottom .tab {
    flex: 1;
    flex-direction: column;
    gap: 2px;
    padding: 6px 4px;
    border-radius: var(--radius-md);
  }

  .tabs--bottom .tab__label { font-size: 10.5px; }
  .tabs--bottom .tab__icon { font-size: 17px; }

  .panel { padding: 14px; }

  .inbox {
    grid-template-columns: 1fr;
    position: relative;
    overflow: hidden;
  }

  .inbox__list {
    transition: transform var(--dur-slow) var(--ease), opacity var(--dur-slow) var(--ease);
  }

  .inbox__thread {
    position: absolute;
    inset: 0;
    transform: translateX(100%);
    opacity: 0;
    transition: transform var(--dur-slow) var(--ease), opacity var(--dur-slow) var(--ease);
    pointer-events: none;
  }

  .inbox[data-thread-open="true"] .inbox__list {
    transform: translateX(-6%);
    opacity: 0;
    pointer-events: none;
  }

  .inbox[data-thread-open="true"] .inbox__thread {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }

  .thread__back {
    display: inline-flex;
  }
}

/* ============================================================
   Reduced motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
    scroll-behavior: auto !important;
  }
  .badge--online .badge__dot::after { animation: none; opacity: 0; }
}
