/* Terry Cockpit — Dark Theme (Beauty/Rosé variant, per design spec) */

/* ── Tokens ─────────────────────────────────────────────── */
:root {
  --bg-base:   #1a1618;
  --bg-panel:  #241f22;
  --bg-raised: #2c2629;
  --bg-hover:  #332c30;
  --bg-active: #3b3338;
  --overlay:   rgba(12, 8, 10, 0.6);

  --border:        #3a3236;
  --border-strong: #483e43;

  --text:        #ede8ea;
  --text-sec:    #b0a4aa;
  --text-muted:  #80737a;
  --text-invert: #171012;

  --rose:             #c4707f;
  --rose-hover:       #d98a98;
  --rose-deep:        #a55763;
  --rose-tint:        rgba(196, 112, 127, 0.12);
  --rose-tint-strong: rgba(196, 112, 127, 0.22);
  --rose-line:        rgba(217, 138, 152, 0.40);

  --sage:      #6fb8a8;
  --sage-tint: rgba(111, 184, 168, 0.12);
  --sage-line: rgba(111, 184, 168, 0.40);

  --bubble-in:       #2c2629;
  --bubble-out:      #5c333c;
  --bubble-out-line: #74434d;

  --ok:        #58c98b;
  --ok-tint:   rgba(88, 201, 139, 0.12);
  --ok-line:   rgba(88, 201, 139, 0.38);
  --warn:      #d9a649;
  --warn-tint: rgba(217, 166, 73, 0.12);
  --warn-line: rgba(217, 166, 73, 0.38);
  --err:       #e5685c;
  --err-tint:  rgba(229, 104, 92, 0.12);
  --err-line:  rgba(229, 104, 92, 0.38);
  --info:      #6da8e8;

  --wa-green: #4fb86a;

  --font-ui:   'IBM Plex Sans', -apple-system, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', monospace;

  --sh-1: 0 1px 2px rgba(0,0,0,.40);
  --sh-2: 0 4px 12px rgba(0,0,0,.45);
  --sh-3: 0 16px 40px rgba(0,0,0,.55);
  --ai-glow: 0 0 0 1px var(--rose-line), 0 0 20px rgba(196,112,127,.25);

  --t-fast: 120ms ease-out;
  --t-med:  200ms cubic-bezier(.2,.8,.3,1);
  --t-slow: 320ms cubic-bezier(.2,.8,.3,1);
}

/* ── Reset / Base ───────────────────────────────────────── */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.5;
  background: var(--bg-base);
  color: var(--text);
}
button { font: inherit; color: inherit; cursor: pointer; background: none; border: none; }
input, textarea { font: inherit; color: var(--text); }
h1, h2, p { margin: 0; }
[hidden] { display: none !important; }
.mono { font-family: var(--font-mono); }
.flex-spacer { flex: 1; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border: 2px solid transparent;
  border-radius: 999px;
  background-clip: content-box;
}
::-webkit-scrollbar-track { background: transparent; }

:focus-visible { outline: 2px solid var(--rose-hover); outline-offset: 1px; }

/* ── Buttons ────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 14px;
  border-radius: 6px; font-size: 13px; font-weight: 500;
  transition: background var(--t-fast), border-color var(--t-fast);
  white-space: nowrap;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-sm { height: 26px; padding: 0 10px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; }
.btn-primary { background: var(--rose); color: var(--text-invert); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--rose-hover); }
.btn-primary:active:not(:disabled) { background: var(--rose-deep); }
.btn-secondary { background: var(--bg-raised); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--bg-hover); }
.btn-ghost { color: var(--text-sec); }
.btn-ghost:hover:not(:disabled) { background: var(--bg-raised); color: var(--text); }
.btn-danger-ghost { color: var(--err); }
.btn-danger-ghost:hover:not(:disabled) { background: var(--err-tint); }
.btn-ai { background: var(--rose-tint); border: 1px solid var(--rose-line); color: var(--rose-hover); }
.btn-ai:hover:not(:disabled) { background: var(--rose-tint-strong); }

.icon-btn {
  width: 32px; height: 32px; border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text-sec); transition: background var(--t-fast);
}
.icon-btn:hover { background: var(--bg-raised); color: var(--text); }
.icon-btn-sm { width: 24px; height: 24px; }

/* ── Inputs ─────────────────────────────────────────────── */
.input, .search-input, .composer, .field input {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13px;
  width: 100%;
  transition: border-color var(--t-fast);
}
.input:focus, .search-input:focus, .field input:focus { border-color: var(--rose); outline: none; }
.input::placeholder, .search-input::placeholder, .composer::placeholder { color: var(--text-muted); }
textarea.input { resize: vertical; min-height: 60px; }

/* ── Avatars / Badges ───────────────────────────────────── */
.avatar {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 999px;
  background: var(--bg-active);
  color: var(--text-sec);
  font-weight: 600;
  flex: none;
  user-select: none;
}
.avatar-sm { width: 32px; height: 32px; font-size: 12px; }
.avatar-md { width: 38px; height: 38px; font-size: 13px; }
.avatar-lg { width: 48px; height: 48px; font-size: 16px; }
.avatar-wrap { position: relative; flex: none; }
.channel-badge {
  position: absolute; right: -2px; bottom: -2px;
  width: 16px; height: 16px; border-radius: 999px;
  background: var(--wa-green);
  border: 2px solid var(--bg-panel);
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; color: #fff;
}
.badge {
  display: inline-flex; align-items: center;
  height: 18px; padding: 0 7px;
  border-radius: 4px;
  font-size: 11px; font-weight: 600;
  white-space: nowrap;
}
.badge-crm { background: var(--ok-tint); color: var(--ok); border: 1px solid var(--ok-line); }
.badge-unknown { background: var(--bg-raised); color: var(--text-muted); border: 1px solid var(--border); }
.badge-auto { background: var(--sage-tint); color: var(--sage); border: 1px solid var(--sage-line); }

/* ── Login ──────────────────────────────────────────────── */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 360px; max-width: 100%;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--sh-3);
  padding: 32px;
  display: flex; flex-direction: column; gap: 16px;
  text-align: center;
}
.login-logo { font-size: 40px; line-height: 1; }
.login-title { font-size: 22px; font-weight: 700; }
.login-sub { font-size: 12px; color: var(--text-muted); margin-top: -10px; }
.field { display: flex; flex-direction: column; gap: 4px; text-align: left; }
.field span { font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; color: var(--text-muted); }
.login-error {
  background: var(--err-tint); border: 1px solid var(--err-line);
  color: var(--err); border-radius: 6px; padding: 8px 10px; font-size: 12px;
  text-align: left;
}

/* ── App frame ──────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-rows: 52px 1fr;
  height: 100vh;
}

.topbar {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  min-width: 0;
}
.brand { font-weight: 700; font-size: 14px; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.brand-emoji { font-size: 20px; }
.topbar-spacer { flex: 1; }
.agent { display: flex; align-items: center; gap: 8px; min-width: 0; }
.agent-name { font-size: 12px; color: var(--text-sec); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agent .avatar { background: var(--rose-tint-strong); color: var(--rose-hover); }

.view-tabs { display: flex; gap: 4px; height: 100%; }
.view-tab {
  padding: 0 14px; height: 100%;
  font-size: 13px; font-weight: 500; color: var(--text-sec);
  border-bottom: 2px solid transparent;
  transition: color var(--t-fast);
}
.view-tab:hover { color: var(--text); }
.view-tab.is-active { color: var(--text); border-bottom-color: var(--rose); }

.main {
  display: grid;
  grid-template-columns: 348px minmax(0, 1fr) 320px;
  min-height: 0;
}
.main.side-collapsed { grid-template-columns: 348px minmax(0, 1fr) 0; }
.main.side-collapsed .col-side { display: none; }

/* ── Column 1: chat list ────────────────────────────────── */
.col-list {
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
}
.list-head { padding: 12px 12px 8px; border-bottom: 1px solid var(--border); }
.search-wrap { position: relative; }
.search-icon {
  position: absolute; left: 9px; top: 50%; transform: translateY(-50%);
  font-size: 12px; opacity: .55; pointer-events: none;
}
.search-input { padding-left: 30px; }
.list-meta { margin-top: 6px; font-size: 11px; color: var(--text-muted); }
.chat-list { flex: 1; overflow-y: auto; min-height: 0; }

.conv {
  display: grid;
  grid-template-columns: 38px 1fr;
  gap: 10px;
  padding: 10px 12px 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  position: relative;
  transition: background var(--t-fast);
  min-width: 0;
}
.conv:hover { background: var(--bg-hover); }
.conv.is-active { background: var(--bg-active); }
.conv.is-active::before {
  content: ''; position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px; background: var(--rose);
}
.conv-body { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.conv-top { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.conv-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.conv-name.is-linked { font-weight: 600; }
.conv-time { font-size: 11px; color: var(--text-muted); flex: none; }
.conv-preview {
  font-size: 12px; color: var(--text-sec);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-preview.is-empty { font-style: italic; color: var(--text-muted); }
.conv-meta { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.conv-number { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); }

/* ── Column 2: conversation ─────────────────────────────── */
.col-chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg-base); }
.chat-pane { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.chat-header {
  display: flex; align-items: center; gap: 12px;
  min-height: 56px;
  padding: 8px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}
.chat-head-text { min-width: 0; flex: 1; }
.chat-title { font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 12px; color: var(--text-muted); font-family: var(--font-mono); }

.chat-scroll {
  flex: 1; overflow-y: auto; min-height: 0;
  padding: 20px 24px;
  display: flex; flex-direction: column; gap: 8px;
}

.day-divider {
  display: flex; align-items: center; gap: 12px;
  font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase;
  color: var(--text-muted);
  margin: 8px 0 4px;
}
.day-divider::before, .day-divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.msg-row { display: flex; }
.msg-row.in { justify-content: flex-start; }
.msg-row.out { justify-content: flex-end; }
.msg-group { max-width: 62%; min-width: 0; }
.bubble {
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 14px;
  word-wrap: break-word; overflow-wrap: break-word; white-space: normal;
}
.msg-row.in .bubble { background: var(--bubble-in); border-bottom-left-radius: 4px; }
.msg-row.out .bubble {
  background: var(--bubble-out);
  border: 1px solid var(--bubble-out-line);
  border-bottom-right-radius: 4px;
}
.bubble-media-tag {
  display: block; font-size: 11px; color: var(--text-sec);
  margin-bottom: 2px;
}
.msg-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; padding: 0 4px; }
.msg-row.out .msg-meta { text-align: right; }
.msg-arriving { animation: msg-in var(--t-slow); }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* ── Composer ───────────────────────────────────────────── */
.composer-zone { padding: 12px 16px 16px; }
.ai-flag {
  display: flex; align-items: center; gap: 8px;
  font-size: 11px; font-weight: 700; letter-spacing: .5px; text-transform: uppercase;
  color: var(--rose-hover);
  padding: 4px 6px;
}
.ai-flag.is-edited { color: var(--warn); }
.composer-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.composer-card:focus-within { border-color: var(--rose); }
.composer-card.has-ai { border-color: var(--rose); box-shadow: var(--ai-glow); }
.composer {
  display: block;
  width: 100%;
  background: transparent; border: none; resize: none;
  min-height: 64px; max-height: 200px;
  padding: 10px 12px;
  font-size: 14px;
}
.composer:focus { outline: none; }
.composer-toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
}
.composer-hint { flex: 1; text-align: right; font-size: 11px; color: var(--text-muted); }

.dropdown-wrap { position: relative; }
.dropdown {
  position: absolute; bottom: calc(100% + 6px); left: 0;
  min-width: 220px; max-width: 320px; max-height: 260px; overflow-y: auto;
  background: var(--bg-raised);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  box-shadow: var(--sh-2);
  z-index: 100;
  padding: 4px;
}
.dropdown-item {
  display: block; width: 100%; text-align: left;
  padding: 7px 10px; border-radius: 4px; font-size: 13px;
}
.dropdown-item:hover { background: var(--bg-hover); }
.dropdown-item small { display: block; color: var(--text-muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dropdown-empty { padding: 10px; font-size: 12px; color: var(--text-muted); }

/* ── Column 3: context panel ────────────────────────────── */
.col-side {
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  min-height: 0;
}
.side-block { padding: 16px; border-bottom: 1px solid var(--border); }
.side-caption {
  font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase;
  color: var(--text-muted);
  margin: 0 0 10px;
}
.client-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.client-head .avatar-lg { background: var(--rose-tint-strong); color: var(--rose-hover); }
.client-name { font-weight: 600; font-size: 14px; }
.client-id { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); }

.kv { display: grid; grid-template-columns: 96px 1fr; gap: 4px 10px; margin: 0; }
.kv dt { color: var(--text-muted); font-size: 12px; }
.kv dd { margin: 0; font-size: 13px; word-break: break-word; }

.doc-row {
  display: flex; align-items: baseline; gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.doc-row:last-child { border-bottom: none; }
.doc-row.is-voided { opacity: .5; text-decoration: line-through; }
.doc-date { color: var(--text-muted); flex: none; font-family: var(--font-mono); }
.doc-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-amount { font-family: var(--font-mono); flex: none; }

.appt-row { padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.appt-row:last-child { border-bottom: none; }
.appt-when { color: var(--sage); font-weight: 600; font-family: var(--font-mono); }
.appt-sub { color: var(--text-sec); }

.note-row {
  background: rgba(232, 201, 90, 0.08);
  border: 1px solid rgba(232, 201, 90, 0.30);
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: 12px;
}
.note-meta { color: var(--text-muted); font-size: 11px; margin-top: 4px; }

.saldo-value { font-family: var(--font-mono); font-size: 16px; font-weight: 600; }
.saldo-pos { color: var(--err); }
.saldo-zero { color: var(--ok); }

.side-empty-hint { font-size: 12px; color: var(--text-muted); }

.unlinked-card {
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  padding: 16px;
  text-align: center;
  margin-bottom: 12px;
}
.unlinked-card .empty-icon { font-size: 28px; opacity: .6; }
.result-list { margin-top: 8px; max-height: 280px; overflow-y: auto; }
.result-item {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  transition: background var(--t-fast);
}
.result-item:hover { background: var(--bg-hover); }
.result-item.is-selected { border-color: var(--rose-line); background: var(--rose-tint); }
.result-name { font-size: 13px; font-weight: 500; }
.result-sub { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); }
.link-actions { display: flex; gap: 8px; margin-top: 10px; }

/* ── Empty / loading states ─────────────────────────────── */
.empty-state {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px;
  padding: 32px;
  text-align: center;
}
.empty-state-side { min-height: 200px; }
.empty-icon { font-size: 40px; opacity: .5; }
.empty-title { font-size: 16px; font-weight: 600; }
.empty-sub { font-size: 13px; color: var(--text-muted); }

.spinner-line { padding: 16px; color: var(--text-muted); font-size: 12px; }
.skeleton {
  height: 14px; border-radius: 4px; margin: 10px 16px;
  background: linear-gradient(90deg, var(--bg-raised) 25%, var(--bg-hover) 37%, var(--bg-raised) 63%);
  background-size: 600px 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { from { background-position: -600px 0; } to { background-position: 600px 0; } }

/* ── Automations view ───────────────────────────────────── */
.auto-main { overflow-y: auto; min-height: 0; background: var(--bg-base); }
.auto-inner { max-width: 760px; margin: 0 auto; padding: 24px 20px 60px; display: flex; flex-direction: column; gap: 20px; }
.banner { border-radius: 10px; padding: 12px 16px; font-size: 13px; }
.banner-warn { background: var(--warn-tint); border: 1px solid var(--warn-line); color: var(--warn); }
.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
}
.panel-title { font-size: 16px; font-weight: 600; margin-bottom: 14px; }
.tpl-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.tpl-item {
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}
.tpl-item-text { flex: 1; min-width: 0; }
.tpl-item-title { font-weight: 600; font-size: 13px; }
.tpl-item-body { font-size: 12px; color: var(--text-sec); white-space: pre-wrap; word-break: break-word; }
.tpl-form { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.auto-list { display: flex; flex-direction: column; gap: 8px; }
.auto-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}
.auto-item-text { flex: 1; min-width: 0; }
.auto-item-name { font-weight: 600; font-size: 13px; }
.auto-item-sub { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); word-break: break-all; }

.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track {
  position: absolute; inset: 0;
  background: var(--bg-active);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background var(--t-fast);
  pointer-events: none;
}
.switch-track::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 999px;
  background: var(--text-sec);
  transition: transform var(--t-fast), background var(--t-fast);
}
.switch input:checked + .switch-track { background: var(--rose); border-color: var(--rose); }
.switch input:checked + .switch-track::after { transform: translateX(16px); background: #fff; }

/* ── Toasts ─────────────────────────────────────────────── */
.toasts {
  position: fixed; right: 16px; bottom: 16px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 1100;
}
.toast {
  background: var(--bg-raised);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  box-shadow: var(--sh-2);
  padding: 10px 14px;
  font-size: 13px; font-weight: 500;
  max-width: 340px;
  animation: toast-in var(--t-med);
}
.toast-err { border-color: var(--err-line); color: var(--err); }
.toast-ok { border-color: var(--ok-line); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 1100px) {
  .main { grid-template-columns: 300px minmax(0, 1fr) 0; }
  .main .col-side { display: none; }
  .main.side-open { grid-template-columns: 300px minmax(0, 1fr) 300px; }
  .main.side-open .col-side { display: block; }
  .composer-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
