* { box-sizing: border-box; }
:root {
  --blue: #2563EB; --blue-dark: #1D4ED8; --orange: #F97316;
  --bg: #F1F5F9; --card: #fff; --border: #E7ECF3;
  --txt: #0F172A; --txt-soft: #64748B; --txt-xsoft: #94A3B8;
}
body {
  margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--txt); height: 100vh; overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
.view { height: 100vh; display: flex; flex-direction: column; }

/* ============================================================
   WIDE-SCREEN SHELL (≥1300px) — the whole app floats as a single
   boxed, elevated card instead of a sidebar pinned to the far left
   edge with the content awkwardly boxed off-center beside it.
   ============================================================ */
@media (min-width: 1300px) {
  body {
    display: flex; align-items: center; justify-content: center; padding: 28px;
    background: radial-gradient(1400px 900px at 15% -10%, #DBEAFE 0%, transparent 55%),
                radial-gradient(1200px 800px at 100% 110%, #FFEDD5 0%, transparent 50%),
                var(--bg);
  }
  #app-view {
    width: 100%; max-width: 1360px; height: min(940px, calc(100vh - 56px));
    border-radius: 22px; overflow: hidden;
    box-shadow: 0 30px 80px -24px rgba(15, 23, 42, .35), 0 0 0 1px rgba(15, 23, 42, .05);
  }
}

/* ---------- Login ---------- */
#login-view {
  align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 20% -10%, #1E3A8A 0%, #0B1220 55%, #05070C 100%);
}
.login-card {
  background: #fff; border-radius: 20px; padding: 36px 30px; width: 340px; max-width: 90vw;
  text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.45);
}
.login-mark {
  width: 52px; height: 52px; border-radius: 15px; margin: 0 auto 16px;
  background: linear-gradient(135deg, var(--blue), var(--blue-dark));
  color: #fff; font-weight: 800; font-size: 24px; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 24px rgba(37,99,235,.35);
}
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card .sub { color: var(--txt-soft); font-size: 13px; margin: 0 0 22px; }
.login-card input {
  width: 100%; padding: 12px 14px; margin-bottom: 10px; border: 1.5px solid var(--border);
  border-radius: 11px; font-size: 14px; transition: border-color .15s;
}
.login-card input:focus { outline: none; border-color: var(--blue); }
.login-card button {
  width: 100%; padding: 12px; margin-top: 4px;
  background: linear-gradient(135deg, var(--blue), var(--blue-dark)); color: #fff; border: none;
  border-radius: 11px; font-weight: 700; cursor: pointer; font-size: 14px;
  box-shadow: 0 8px 20px rgba(37,99,235,.3);
}
.error { color: #EF4444; font-size: 12.5px; min-height: 16px; margin: 10px 0 0; }

/* ---------- Topbar ---------- */
.topbar {
  background: linear-gradient(115deg, var(--blue-dark) 0%, var(--blue) 55%, #3B82F6 100%);
  color: #fff; padding: 13px 18px; display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0; box-shadow: 0 2px 12px rgba(15,23,42,.12); position: relative; z-index: 10;
}
.topbar::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(240px 100px at 88% -30%, rgba(249,115,22,.35), transparent 70%);
}
.brand { font-weight: 800; font-size: 15.5px; display: flex; align-items: center; gap: 9px; letter-spacing: -.01em; position: relative; }
.brand-mark {
  width: 27px; height: 27px; border-radius: 8px;
  background: linear-gradient(135deg, #fff, #FED7AA);
  color: var(--blue-dark);
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 800;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.topbar-actions { display: flex; gap: 8px; position: relative; }
.icon-btn { background: rgba(255,255,255,.16); border: none; color: #fff; width: 34px; height: 34px; border-radius: 50%; cursor: pointer; font-size: 15px; transition: background .15s; }
.icon-btn:active { background: rgba(255,255,255,.28); }

/* ---------- App body: sidebar (desktop) / top tabs (mobile) + content ---------- */
.app-body { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.content-area { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.main-tabs { display: flex; background: #fff; border-bottom: 1px solid var(--border); flex-shrink: 0; overflow-x: auto; }
.main-tab {
  flex: 1; min-width: 78px; display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 10px 4px 9px; background: none; border: none; cursor: pointer; font-size: 11px; font-weight: 600;
  color: var(--txt-soft); border-bottom: 3px solid transparent; transition: color .15s;
}
.main-tab .mt-icon { font-size: 19px; line-height: 1; }
.main-tab.active { color: var(--blue); border-bottom-color: var(--orange); }

.tab-panel { flex: 1; overflow-y: auto; display: flex; flex-direction: column; min-height: 0; }
.panel-title-row { padding: 16px 16px 4px; flex-shrink: 0; }
.panel-title { margin: 0; font-size: 17px; letter-spacing: -.01em; }
.tab-panel > .chat-region { flex: 1; overflow-y: auto; min-height: 0; }
.mgmt-scroll { flex: 1; overflow-y: auto; padding: 12px 16px 24px; }
.mgmt-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }

.site-tabs { display: flex; gap: 6px; padding: 4px 16px 12px; overflow-x: auto; flex-shrink: 0; }
.tab { flex-shrink: 0; padding: 7px 14px; border-radius: 100px; border: 1px solid var(--border); background: #fff; font-size: 12.5px; cursor: pointer; white-space: nowrap; color: var(--txt-soft); font-weight: 600; }
.tab.active { background: var(--blue); color: #fff; border-color: var(--blue); }

.chat-region { overflow-y: auto; }
.conv-list { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 8px; }
.conv-item { background: #fff; border-radius: 14px; padding: 13px 15px 13px 13px; cursor: pointer; border: 1px solid var(--border); border-left: 3px solid transparent; position: relative; transition: box-shadow .15s, border-color .15s, transform .15s; }
.conv-item:hover { box-shadow: 0 6px 20px rgba(15,23,42,.08); transform: translateY(-1px); }
.conv-item.unread { border-left-color: var(--orange); box-shadow: 0 2px 10px rgba(249,115,22,.1); }
.conv-item .row1 { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.conv-item .site-badge { font-size: 10.5px; font-weight: 700; color: var(--blue); text-transform: uppercase; letter-spacing: .03em; }
.conv-item .time { font-size: 11px; color: var(--txt-xsoft); }
.conv-item .preview { font-size: 13px; color: #475569; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .status-pill { display: inline-block; font-size: 10px; padding: 2px 8px; border-radius: 100px; margin-top: 6px; font-weight: 700; }
.status-pill.ai { background: #EFF6FF; color: var(--blue); }
.status-pill.live { background: #FEF3E2; color: var(--orange); }
.unread-dot { position: absolute; top: 13px; right: 15px; width: 8px; height: 8px; border-radius: 50%; background: #EF4444; }
.empty { text-align: center; color: var(--txt-xsoft); padding: 40px 20px; font-size: 14px; }

/* ---------- Conversation / lead detail overlays ---------- */
#conv-view, #lead-view, #blocked-view {
  position: fixed; inset: 0; z-index: 600; background: var(--bg);
  display: flex; flex-direction: column; overflow: hidden;
}
#blocked-view .lead-detail-body { overflow-y: auto; }
.conv-header { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: #fff; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.conv-header-info { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.conv-header-info strong { font-size: 13.5px; }
.conv-header-info span { font-size: 11px; color: var(--txt-xsoft); }
.small-btn { background: var(--bg); border: 1px solid var(--border); border-radius: 9px; padding: 7px 11px; font-size: 11.5px; cursor: pointer; font-weight: 700; color: var(--blue); flex-shrink: 0; }
.small-btn.danger { color: #EF4444; }
#back-btn, #lead-back-btn {
  background: var(--bg); border: 1px solid var(--border); color: var(--txt);
  width: 38px; height: 38px; border-radius: 50%; cursor: pointer;
  font-size: 18px; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
#back-btn:active, #lead-back-btn:active { background: #E2E8F0; }

.conv-messages { flex: 1; overflow-y: auto; padding: 16px; }
.msg { max-width: 78%; margin-bottom: 10px; padding: 10px 14px; border-radius: 15px; font-size: 13.5px; line-height: 1.5; white-space: pre-wrap; word-wrap: break-word; }
.msg.visitor { background: #fff; border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.ai { margin-left: auto; background: #EFF6FF; color: #1E3A8A; border-bottom-right-radius: 4px; }
.msg.admin { margin-left: auto; background: var(--blue); color: #fff; border-bottom-right-radius: 4px; }
.msg-tag { font-size: 10px; text-transform: uppercase; opacity: .6; display: block; margin-bottom: 3px; font-weight: 700; }

.reply-row { display: flex; gap: 8px; padding: 12px; background: #fff; border-top: 1px solid var(--border); flex-shrink: 0; }
.reply-row textarea { flex: 1; border: 1.5px solid var(--border); border-radius: 11px; padding: 10px 13px; font-size: 13.5px; resize: none; max-height: 90px; font-family: inherit; }
.reply-row textarea:focus { outline: none; border-color: var(--blue); }
.reply-row button { background: var(--blue); color: #fff; border: none; border-radius: 11px; padding: 0 20px; font-weight: 700; cursor: pointer; }
#suggest-btn { background: linear-gradient(135deg, var(--orange), #FB923C); color: #fff; border: none; border-radius: 11px; padding: 0 14px; font-size: 16px; cursor: pointer; flex-shrink: 0; }
#suggest-btn:disabled { opacity: .6; cursor: default; }

@media (max-width: 480px) {
  .conv-item .preview { max-width: 220px; }
}

/* ---------- Management panels (Tools / Launchpad) ---------- */
.mgmt-section { background: #fff; border-radius: 16px; padding: 18px; border: 1px solid var(--border); box-shadow: 0 2px 14px rgba(15,23,42,.04); }
.mgmt-h { font-size: 14.5px; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }
.mgmt-h-icon { font-size: 15px; }
.mgmt-loading, .mgmt-error { color: var(--txt-xsoft); font-size: 13px; text-align: center; padding: 24px 0; }
.mgmt-error { color: #EF4444; }

.stat-row { display: flex; gap: 10px; margin-bottom: 18px; }
.stat-card { flex: 1; background: linear-gradient(160deg,#F8FAFC,#F1F5F9); border-radius: 12px; padding: 14px 10px; text-align: center; border: 1px solid var(--border); }
.stat-num { font-size: 22px; font-weight: 800; color: var(--blue); letter-spacing: -.02em; }
.stat-label { font-size: 10.5px; color: var(--txt-soft); margin-top: 3px; font-weight: 600; }

.bar-chart { display: flex; align-items: flex-end; gap: 5px; height: 96px; padding: 0 2px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.bar-fill { width: 68%; background: linear-gradient(180deg, var(--blue), var(--blue-dark)); border-radius: 4px 4px 0 0; min-height: 3px; }
.bar-label { font-size: 8.5px; color: var(--txt-xsoft); margin-top: 4px; transform: rotate(-40deg); white-space: nowrap; }

.rank-list { display: flex; flex-direction: column; gap: 8px; }
.rank-row { display: flex; align-items: center; gap: 9px; font-size: 12.5px; }
.rank-name { width: 118px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #334155; font-weight: 500; }
.rank-bar-bg { flex: 1; height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.rank-bar-fill { height: 100%; background: linear-gradient(90deg, var(--blue), #60A5FA); border-radius: 4px; }
.rank-count { width: 28px; text-align: right; color: var(--txt-soft); font-weight: 700; flex-shrink: 0; }

.mgmt-divider { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

.mgmt-table { display: flex; flex-direction: column; }
.mgmt-table-row { display: flex; align-items: center; justify-content: space-between; padding: 11px 0; border-bottom: 1px solid #F1F5F9; gap: 10px; }
.mgmt-table-row:last-child { border-bottom: none; }
.mtr-name { font-size: 13px; color: #334155; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mtr-toggles { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.mtr-toggle-label { font-size: 9.5px; color: var(--txt-xsoft); font-weight: 700; text-transform: uppercase; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.pill-pro { font-size: 9px; font-weight: 800; background: #FEF3E2; color: var(--orange); padding: 1px 6px; border-radius: 100px; margin-left: 5px; }

.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; inset: 0; background: #CBD5E1; border-radius: 100px; transition: .2s; }
.slider::before { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.switch input:checked + .slider { background: var(--blue); }
.switch input:checked + .slider::before { transform: translateX(16px); }
.switch.orange input:checked + .slider { background: var(--orange); }

/* User rows: two-line layout with credit management */
.user-row { padding: 12px 0; border-bottom: 1px solid #F1F5F9; }
.user-row:last-child { border-bottom: none; }
.user-row-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 7px; }
.user-email { font-size: 13px; font-weight: 600; color: #334155; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-meta-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 11px; color: var(--txt-soft); }
.user-meta-row b { color: var(--txt); }
.user-id-chip { font-family: ui-monospace, monospace; font-size: 10px; background: var(--bg); padding: 2px 6px; border-radius: 6px; color: var(--txt-xsoft); }
.credit-input { width: 52px; border: 1px solid var(--border); border-radius: 8px; padding: 3px 6px; font-size: 12px; text-align: center; }
.credit-save-btn { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 4px 9px; font-size: 11px; font-weight: 700; cursor: pointer; color: var(--blue); }

.lead-list { display: flex; flex-direction: column; gap: 9px; }
.lead-item { background: var(--bg); border-radius: 12px; padding: 12px 14px; cursor: pointer; border: 1px solid var(--border); transition: box-shadow .15s; }
.lead-item:hover { box-shadow: 0 4px 14px rgba(15,23,42,.06); }
.lead-item .preview { font-size: 13px; margin: 3px 0; }
.lead-sub { font-size: 11.5px; color: var(--txt-xsoft); }
.lead-detail-body { padding: 16px; overflow-y: auto; flex: 1; }
.lead-detail-card { background: #fff; border-radius: 14px; padding: 18px; border: 1px solid var(--border); }
.lead-detail-card h4 { margin: 0 0 12px; font-size: 16px; }
.lead-detail-card p { font-size: 13px; margin: 7px 0; color: #334155; }

/* ============================================================
   DESKTOP LAYOUT (≥900px) — sidebar navigation, 2-col mgmt grid
   ============================================================ */
@media (min-width: 900px) {
  .app-body { flex-direction: row; }
  .main-tabs {
    flex-direction: column; width: 220px; flex-shrink: 0; border-right: 1px solid var(--border);
    border-bottom: none; padding: 14px 10px; gap: 3px; overflow-x: visible;
  }
  .main-tab {
    flex-direction: row; justify-content: flex-start; align-items: center; gap: 11px;
    padding: 11px 14px; border-radius: 11px; border-bottom: none; font-size: 13.5px;
    text-align: left;
  }
  .main-tab .mt-icon { font-size: 17px; }
  .main-tab.active { background: #EFF6FF; border-bottom-color: transparent; }
  .content-area { max-width: 980px; margin: 0 auto; width: 100%; }
  .panel-title-row { padding: 24px 24px 8px; }
  .panel-title { font-size: 20px; }
  .site-tabs { padding: 4px 24px 14px; }
  .mgmt-scroll { padding: 12px 24px 32px; }
  .mgmt-grid { grid-template-columns: 1fr 1fr; }
  .mgmt-grid .span-2 { grid-column: 1 / -1; }
  .conv-list { padding: 4px 24px 16px; }
}
