/* IM AI Inbox — Luxury Warm Editorial, RTL */

:root {
  --bg: #faf7f2;
  --bg-elev: #ffffff;
  --bg-soft: #f3ede2;
  --ink: #1c1917;
  --ink-soft: #57534e;
  --ink-muted: #a8a29e;
  --line: #e7e1d4;
  --line-strong: #d6cfbe;
  --accent: #0f766e;       /* teal */
  --accent-soft: #ccfbf1;
  --accent-ink: #064e4a;
  --warn: #b45309;
  --danger: #b91c1c;
  --gold: #a67c00;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(28,25,23,0.04), 0 1px 1px rgba(28,25,23,0.02);
  --shadow-md: 0 6px 24px rgba(28,25,23,0.08);
  --font: "Heebo", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- Gate ---------- */
.gate {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #faf7f2 0%, #f3ede2 100%);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 100;
}
.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 32px 28px;
  box-shadow: var(--shadow-md);
  text-align: center;
}
.gate-brand {
  font-size: 13px;
  letter-spacing: 0.16em;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  margin-bottom: 12px;
}
.gate-card h1 { font-size: 22px; margin: 0 0 8px; }
.gate-card p  { color: var(--ink-soft); margin: 0 0 20px; font-size: 14px; }
.gate-card input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: inherit;
  font-size: 14px;
  margin-bottom: 12px;
  text-align: right;
  direction: ltr;
}
.gate-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-top: 10px;
}

/* ---------- Buttons ---------- */
.btn-primary {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px 20px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
  width: 100%;
}
.btn-primary:hover { background: #0d655e; }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }

.icon-btn {
  background: transparent;
  border: 1px solid var(--line);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--ink-soft);
  font-size: 16px;
  transition: all 0.15s ease;
}
.icon-btn:hover { background: var(--bg-soft); color: var(--ink); }

/* ---------- App layout ---------- */
.app {
  display: grid;
  grid-template-columns: 360px 1fr;
  height: 100vh;
  width: 100%;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--bg-elev);
  border-inline-start: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-header {
  padding: 18px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  background: var(--ink);
  color: var(--bg);
  font-weight: 700;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 6px;
  letter-spacing: 0.05em;
}
.brand-name {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.02em;
}

.filters {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.filters input[type="search"] {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  font-family: inherit;
  font-size: 14px;
  text-align: right;
}
.filter-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  align-items: center;
}
.filter-row select {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
}
.unread-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-soft);
  white-space: nowrap;
  cursor: pointer;
}

/* ---------- Conversation list ---------- */
.conv-list {
  flex: 1;
  overflow-y: auto;
}
.empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--ink-muted);
  font-size: 14px;
}
.conv-item {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: background 0.12s ease;
}
.conv-item:hover { background: var(--bg-soft); }
.conv-item.active { background: var(--accent-soft); }
.conv-item .row1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.conv-item .name {
  font-weight: 600;
  font-size: 14px;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 70%;
}
.conv-item .time { font-size: 11px; color: var(--ink-muted); }
.conv-item .preview {
  font-size: 13px;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-item .meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}
.status-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
  background: var(--bg-soft);
  color: var(--ink-soft);
  border: 1px solid var(--line);
}
.status-badge.new            { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }
.status-badge.ai_handling    { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.status-badge.waiting_customer { background: #f3e8ff; color: #6b21a8; border-color: #e9d5ff; }
.status-badge.ready_to_deliver { background: #ccfbf1; color: #064e4a; border-color: #99f6e4; }
.status-badge.done           { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.status-badge.stuck          { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.unread-badge {
  background: var(--accent);
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

/* ---------- Thread ---------- */
.thread {
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.thread-empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--ink-muted);
  font-size: 15px;
  text-align: center;
}
.thread-empty-art {
  font-size: 48px;
  margin-bottom: 12px;
  opacity: 0.4;
}

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

.thread-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.thread-name { font-size: 17px; font-weight: 600; }
.thread-phone { font-size: 13px; color: var(--ink-muted); direction: ltr; text-align: right; }
.thread-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}
.thread-controls select {
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: inherit;
  font-size: 13px;
}
.thread-job {
  font-size: 12px;
  color: var(--ink-muted);
  font-family: "SF Mono", "Menlo", monospace;
  direction: ltr;
}

.thread-body {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 280px;
  min-height: 0;
  overflow: hidden;
}

.messages {
  overflow-y: auto;
  padding: 24px 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.msg {
  max-width: 72%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  box-shadow: var(--shadow-sm);
}
.msg .meta-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 11px;
  margin-top: 6px;
  opacity: 0.7;
}
.msg .sender-tag {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
  opacity: 0.7;
}
.msg.in {
  align-self: flex-start;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-top-right-radius: 4px;
}
.msg.out {
  align-self: flex-end;
  background: var(--accent);
  color: white;
  border-top-left-radius: 4px;
}
.msg.out.system {
  background: #fef3c7;
  color: #78350f;
}
.msg.out.ai {
  background: #e0f2fe;
  color: #0c4a6e;
}
.msg.out.manual {
  background: var(--accent);
  color: white;
}

/* Ops panel */
.ops-panel {
  border-inline-start: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 20px 18px;
  overflow-y: auto;
}
.ops-panel h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-muted);
  margin: 0 0 16px;
  font-weight: 600;
}
.ops-section { margin-bottom: 18px; }
.ops-label {
  font-size: 11px;
  color: var(--ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}
.ops-value {
  font-size: 13px;
  color: var(--ink);
  word-break: break-all;
}
.ops-events {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ops-events li {
  font-size: 12px;
  padding: 8px 10px;
  background: var(--bg-soft);
  border-radius: 8px;
  border-inline-start: 3px solid var(--accent);
}
.ops-events li .event-label { font-weight: 600; color: var(--ink); }
.ops-events li .event-time  { color: var(--ink-muted); font-size: 11px; margin-top: 2px; }

/* Reply bar */
.reply {
  padding: 14px 20px;
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
  display: flex;
  gap: 12px;
  align-items: flex-end;
}
.reply textarea {
  flex: 1;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
  min-height: 44px;
  max-height: 160px;
  background: var(--bg);
  text-align: right;
}
.reply .btn-primary { width: auto; padding: 11px 22px; }
.reply-status {
  padding: 0 20px 10px;
  font-size: 12px;
  min-height: 18px;
  background: var(--bg-elev);
  color: var(--ink-muted);
}
.reply-status.error { color: var(--danger); }
.reply-status.ok    { color: var(--accent); }

/* Scrollbars */
.conv-list::-webkit-scrollbar,
.messages::-webkit-scrollbar,
.ops-panel::-webkit-scrollbar { width: 8px; }
.conv-list::-webkit-scrollbar-thumb,
.messages::-webkit-scrollbar-thumb,
.ops-panel::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }

/* Mobile */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .thread { display: none; }
  .app.viewing-thread .sidebar { display: none; }
  .app.viewing-thread .thread  { display: flex; }
  .thread-body { grid-template-columns: 1fr; }
  .ops-panel { display: none; }
  .msg { max-width: 85%; }
}
