:root {
  color-scheme: light;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", Inter, "Segoe UI", sans-serif;
  --bg: #f5f5f7;
  --bg-soft: #ffffff;
  --card: rgba(255, 255, 255, .86);
  --card-solid: #ffffff;
  --field: rgba(255, 255, 255, .92);
  --text: #1d1d1f;
  --muted: #6e6e73;
  --subtle: #8e8e93;
  --border: #e5e5ea;
  --border-soft: rgba(60, 60, 67, .12);
  --shadow: 0 18px 45px rgba(0, 0, 0, .075);
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, .055);
  --blue: #007aff;
  --red: #ff3b30;
  --orange: #ff9500;
  --indigo: #5856d6;
  --purple: #af52de;
  --cyan: #5ac8fa;
  --green: #34c759;
  --gold: #c8942f;
  --slate: #3a3a3c;
  --gray: #8e8e93;
  --radius-card: 24px;
  --radius-field: 16px;
  --radius-pill: 999px;
  --tap: 44px;
  --ease: cubic-bezier(.2, .8, .2, 1);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1d1d1f;
  --bg-soft: #151517;
  --card: rgba(40, 40, 44, .78);
  --card-solid: #28282c;
  --field: rgba(58, 58, 62, .72);
  --text: #f5f5f7;
  --muted: #a1a1aa;
  --subtle: #7d7d86;
  --border: rgba(255, 255, 255, .10);
  --border-soft: rgba(255, 255, 255, .08);
  --shadow: 0 20px 50px rgba(0, 0, 0, .26);
  --shadow-soft: 0 10px 24px rgba(0, 0, 0, .18);
  --slate: #d1d1d6;
}

html[data-theme="system"] { color-scheme: light dark; }

@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    --bg: #1d1d1f;
    --bg-soft: #151517;
    --card: rgba(40, 40, 44, .78);
    --card-solid: #28282c;
    --field: rgba(58, 58, 62, .72);
    --text: #f5f5f7;
    --muted: #a1a1aa;
    --subtle: #7d7d86;
    --border: rgba(255, 255, 255, .10);
    --border-soft: rgba(255, 255, 255, .08);
    --shadow: 0 20px 50px rgba(0, 0, 0, .26);
    --shadow-soft: 0 10px 24px rgba(0, 0, 0, .18);
    --slate: #d1d1d6;
  }
}

* { box-sizing: border-box; }
html { min-height: 100%; -webkit-text-size-adjust: 100%; }
body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  overflow-x: hidden;
  text-rendering: optimizeLegibility;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }
svg { width: 20px; height: 20px; flex: 0 0 auto; }
::selection { background: rgba(0, 122, 255, .16); }
:where(a, button, input, textarea, select):focus-visible { outline: 3px solid rgba(0, 122, 255, .24); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  padding: 14px max(20px, env(safe-area-inset-left)) 10px max(20px, env(safe-area-inset-right));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(22px) saturate(150%);
  -webkit-backdrop-filter: blur(22px) saturate(150%);
  border-bottom: 1px solid var(--border-soft);
}
.header-inner { width: min(1080px, 100%); margin: 0 auto; display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.brand { display: inline-flex; align-items: center; gap: 10px; min-height: var(--tap); font-weight: 720; letter-spacing: 0; }
.brand-mark { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 14px; background: var(--text); color: var(--bg); font-size: 12px; font-weight: 760; }
.brand-title { white-space: nowrap; }
.header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; }
.header-select, .theme-toggle { display: block; }

select, input, textarea {
  width: 100%;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--radius-field);
  background: var(--field);
  color: var(--text);
  padding: 10px 14px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18);
  transition: border-color 200ms var(--ease), background 200ms var(--ease), box-shadow 200ms var(--ease);
}
select {
  appearance: none;
  padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 20px, calc(100% - 13px) 20px;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
input::placeholder, textarea::placeholder { color: var(--subtle); }
textarea { min-height: 78px; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: rgba(0, 122, 255, .55); box-shadow: 0 0 0 4px rgba(0, 122, 255, .11); }
.header-actions select { min-height: 38px; border-radius: var(--radius-pill); padding: 8px 32px 8px 14px; font-size: 14px; background-position: calc(100% - 17px) 17px, calc(100% - 12px) 17px; }
.icon-button { width: 40px; height: 40px; display: inline-grid; place-items: center; border: 1px solid var(--border); border-radius: var(--radius-pill); background: var(--field); color: var(--muted); transition: transform 180ms var(--ease), color 180ms var(--ease), background 180ms var(--ease); }
.icon-button:hover { color: var(--text); transform: translateY(-1px); }
.logout-form { margin: 0; }

.segmented-tabs { width: min(680px, 100%); margin: 12px auto 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; padding: 4px; border: 1px solid var(--border); border-radius: var(--radius-pill); background: color-mix(in srgb, var(--field) 70%, transparent); }
.tab { min-height: 42px; border: 0; border-radius: var(--radius-pill); background: transparent; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 14px; transition: background 220ms var(--ease), color 220ms var(--ease), box-shadow 220ms var(--ease); }
.tab svg { width: 18px; height: 18px; }
.tab.is-active { background: var(--card-solid); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.06); }

.app-shell { width: min(1080px, calc(100% - 40px)); margin: 0 auto; padding: 34px 0 calc(96px + env(safe-area-inset-bottom)); }
.view { display: none; }
.view.is-active { display: block; animation: viewIn 220ms var(--ease); }
@keyframes viewIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.section-head { display: flex; align-items: end; justify-content: space-between; gap: 24px; margin-bottom: 22px; }
.section-head h1 { margin: 0; font-size: clamp(34px, 5vw, 54px); line-height: 1.02; letter-spacing: 0; }
.section-head p { margin: 10px 0 0; color: var(--muted); max-width: 580px; }
.primary-action, .secondary-action, .danger-action {
  min-height: var(--tap);
  border-radius: var(--radius-pill);
  border: 1px solid transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 18px;
  font-weight: 680;
  white-space: nowrap;
  transition: transform 180ms var(--ease), background 180ms var(--ease), border-color 180ms var(--ease), opacity 180ms var(--ease);
}
.primary-action { background: var(--blue); color: white; box-shadow: 0 10px 24px rgba(0, 122, 255, .22); }
.primary-action:hover, .secondary-action:hover { transform: translateY(-1px); }
.primary-action:active, .secondary-action:active, .danger-action:active { transform: translateY(0) scale(.985); }
.secondary-action { background: var(--field); color: var(--text); border-color: var(--border); }
.secondary-action.compact { min-height: 42px; padding: 0 14px; }
.danger-action { background: rgba(255, 59, 48, .11); color: var(--red); border-color: rgba(255, 59, 48, .18); }

.toolbar { display: grid; grid-template-columns: minmax(260px, 1fr) auto; gap: 12px; align-items: center; margin-bottom: 14px; }
.search-field { position: relative; display: block; }
.search-field svg { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 18px; height: 18px; pointer-events: none; }
.search-field input { padding-left: 44px; border-radius: var(--radius-pill); }
.filter-bar { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.filter-bar select, .filter-bar input { width: auto; min-width: 150px; border-radius: var(--radius-pill); }
.sync-state { min-height: 20px; margin: 4px 4px 10px; color: var(--muted); font-size: 13px; }
.sync-state[data-tone="ok"] { color: var(--green); }
.sync-state[data-tone="error"] { color: var(--red); }

.profile-surface { border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--card); box-shadow: var(--shadow); overflow: hidden; backdrop-filter: blur(18px) saturate(140%); -webkit-backdrop-filter: blur(18px) saturate(140%); }
.profile-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.profile-table th { height: 44px; padding: 0 14px; text-align: left; color: var(--muted); font-size: 12px; font-weight: 680; border-bottom: 1px solid var(--border); }
.profile-table td { padding: 12px 14px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
.profile-table tr:last-child td { border-bottom: 0; }
.profile-table col:nth-child(1) { width: 18%; }
.profile-table col:nth-child(2) { width: 14%; }
.profile-table col:nth-child(3) { width: 14%; }
.profile-table col:nth-child(4) { width: 14%; }
.profile-table col:nth-child(5) { width: 13%; }
.profile-table col:nth-child(6) { width: 21%; }
.profile-table col:nth-child(7) { width: 56px; }
.profile-table input, .profile-table select, .profile-table textarea { min-height: 38px; border-radius: 14px; font-size: 14px; background-color: color-mix(in srgb, var(--field) 85%, transparent); }
.profile-table textarea { min-height: 42px; max-height: 94px; }
.ig-link { display: inline-flex; align-items: center; min-height: 38px; max-width: 100%; color: var(--blue); font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted-text { color: var(--subtle); }
.badge { min-height: 32px; display: inline-flex; align-items: center; gap: 6px; border-radius: var(--radius-pill); padding: 5px 10px; font-size: 13px; font-weight: 680; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 999px; background: currentColor; }
.stage-sent { color: var(--blue); background: rgba(0, 122, 255, .11); }
.stage-delivered { color: var(--red); background: rgba(255, 59, 48, .11); }
.stage-read { color: var(--orange); background: rgba(255, 149, 0, .12); }
.stage-dialogue { color: var(--indigo); background: rgba(88, 86, 214, .12); }
.stage-value { color: var(--purple); background: rgba(175, 82, 222, .12); }
.stage-call { color: #1684aa; background: rgba(90, 200, 250, .16); }
.stage-success { color: var(--green); background: rgba(52, 199, 89, .13); }
.qualification-high { color: var(--gold); background: rgba(201, 148, 47, .14); }
.qualification-medium { color: var(--slate); background: rgba(99, 99, 102, .13); }
.qualification-low { color: var(--gray); background: rgba(142, 142, 147, .14); }
.row-status { color: var(--muted); font-size: 12px; margin-top: 6px; min-height: 16px; }
.row-status[data-tone="ok"] { color: var(--green); }
.row-status[data-tone="error"] { color: var(--red); }
.delete-icon { width: 38px; height: 38px; border-radius: 999px; border: 1px solid transparent; background: transparent; color: var(--muted); display: grid; place-items: center; }
.delete-icon:hover { background: rgba(255, 59, 48, .1); color: var(--red); }

.mobile-list { display: none; }
.profile-card { padding: 16px; border-bottom: 1px solid var(--border-soft); }
.profile-card:last-child { border-bottom: 0; }
.card-top, .card-meta, .card-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-top { margin-bottom: 10px; }
.card-name { margin: 8px 0 10px; }
.card-name input { font-weight: 680; font-size: 17px; }
.card-meta { flex-wrap: wrap; justify-content: flex-start; margin-bottom: 10px; }
.card-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.card-actions { justify-content: flex-end; }

.empty-state { min-height: 260px; display: grid; place-items: center; text-align: center; padding: 28px; }
.empty-state-inner { max-width: 380px; display: grid; gap: 10px; justify-items: center; }
.empty-state h2 { margin: 0; font-size: 24px; }
.empty-state p { margin: 0 0 8px; color: var(--muted); }
.list-summary { display: flex; justify-content: flex-end; gap: 14px; color: var(--muted); font-size: 14px; margin-top: 12px; }

.analytics-grid { display: grid; gap: 16px; }
.kpi-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; }
.kpi-card, .computed-card, .funnel-panel, .insight-card, .settings-panel { border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--card); box-shadow: var(--shadow-soft); backdrop-filter: blur(16px) saturate(140%); -webkit-backdrop-filter: blur(16px) saturate(140%); }
.kpi-card { padding: 16px; min-height: 122px; display: grid; align-content: space-between; }
.kpi-icon { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 12px; color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.kpi-card strong { font-size: 32px; line-height: 1; margin-top: 18px; }
.kpi-card span { display: block; color: var(--muted); font-size: 13px; margin-top: 6px; }
.computed-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; }
.computed-card { padding: 16px; }
.computed-card strong { display: block; font-size: 26px; }
.computed-card span { color: var(--muted); font-size: 13px; }
.funnel-panel { padding: 20px; }
.funnel-panel h2, .settings-panel h2 { margin: 0 0 16px; font-size: 20px; }
.funnel-list { display: grid; gap: 12px; }
.funnel-row { display: grid; grid-template-columns: 120px minmax(0, 1fr) 92px 92px; gap: 14px; align-items: center; }
.funnel-bar-track { height: 18px; border-radius: 999px; background: color-mix(in srgb, var(--field) 78%, transparent); overflow: hidden; }
.funnel-bar { height: 100%; width: 0; border-radius: 999px; background: var(--accent); transition: width 520ms var(--ease); }
.funnel-row strong { font-size: 14px; }
.funnel-row small { color: var(--muted); }
.insight-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.insight-card { padding: 16px; }
.insight-card span { color: var(--muted); font-size: 13px; }
.insight-card strong { display: block; margin-top: 8px; font-size: 20px; }

.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.settings-panel { padding: 20px; }
.settings-panel.wide { grid-column: 1 / -1; }
.settings-panel p { margin: 0 0 14px; color: var(--muted); }
.profile-line, .setting-row { min-height: 52px; display: grid; grid-template-columns: minmax(0, 1fr) minmax(160px, 220px); gap: 14px; align-items: center; border-top: 1px solid var(--border-soft); padding-top: 12px; margin-top: 12px; }
.profile-line span, .setting-row span { color: var(--muted); }
.stack-form, .data-actions, .import-form { display: grid; gap: 10px; }
.stack-form label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; }
.data-actions { grid-template-columns: auto minmax(260px, 1fr); align-items: start; }
.import-form { grid-template-columns: minmax(220px, 1fr) auto; }
.form-note, .import-summary { min-height: 20px; color: var(--muted); font-size: 14px; }
.form-note[data-tone="ok"], .import-summary[data-tone="ok"] { color: var(--green); }
.form-note[data-tone="error"], .import-summary[data-tone="error"] { color: var(--red); }

.modal-root[hidden] { display: none; }
.modal-root { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .34); backdrop-filter: blur(8px); }
.modal-card { position: relative; width: min(560px, 100%); max-height: min(720px, calc(100vh - 40px)); overflow: auto; border: 1px solid var(--border); border-radius: 28px; background: var(--card-solid); box-shadow: 0 32px 90px rgba(0,0,0,.22); padding: 22px; animation: modalIn 220ms var(--ease); }
@keyframes modalIn { from { opacity: 0; transform: scale(.97) translateY(8px); } to { opacity: 1; transform: scale(1) translateY(0); } }
.modal-head { display: flex; align-items: start; justify-content: space-between; gap: 14px; margin-bottom: 18px; }
.modal-head h2 { margin: 0; font-size: 26px; }
.modal-head p { margin: 6px 0 0; color: var(--muted); }
.modal-form { display: grid; gap: 12px; }
.modal-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.modal-form label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.close-button { width: 38px; height: 38px; border-radius: 999px; border: 1px solid var(--border); background: var(--field); color: var(--muted); }

.mobile-tabs { position: fixed; left: 12px; right: 12px; bottom: max(10px, env(safe-area-inset-bottom)); z-index: 50; display: none; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 6px; border: 1px solid var(--border); border-radius: 24px; background: color-mix(in srgb, var(--card-solid) 88%, transparent); box-shadow: 0 18px 46px rgba(0,0,0,.16); backdrop-filter: blur(22px) saturate(150%); -webkit-backdrop-filter: blur(22px) saturate(150%); }
.mobile-tab { min-height: 58px; border: 0; border-radius: 18px; background: transparent; color: var(--muted); display: grid; place-items: center; gap: 2px; }
.mobile-tab svg { width: 20px; height: 20px; }
.mobile-tab small { font-size: 11px; line-height: 1.1; }
.mobile-tab.is-active { background: var(--text); color: var(--bg); }

.toast-region { position: fixed; right: 18px; top: 18px; z-index: 120; display: grid; gap: 8px; pointer-events: none; }
.toast { max-width: 340px; padding: 12px 14px; border-radius: 18px; background: var(--text); color: var(--bg); box-shadow: var(--shadow); pointer-events: auto; animation: toastIn 220ms var(--ease); }
.toast.error { background: #b42318; color: #fff; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }

.auth-body { display: grid; place-items: center; padding: 24px; }
.auth-shell { width: min(420px, 100%); }
.auth-card { display: grid; gap: 22px; padding: 26px; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--card); box-shadow: var(--shadow); }
.auth-brand { justify-content: center; }
.auth-copy { text-align: center; }
.auth-copy h1 { margin: 0; font-size: 34px; letter-spacing: 0; }
.auth-copy p { margin: 8px 0 0; color: var(--muted); }
.form-error { border: 1px solid rgba(255,59,48,.22); background: rgba(255,59,48,.1); color: var(--red); border-radius: 16px; padding: 10px 12px; }
.form-error p { margin: 0; }

@media (max-width: 1024px) {
  .app-shell, .header-inner { width: min(100% - 32px, 820px); }
  .toolbar { grid-template-columns: 1fr; }
  .filter-bar { justify-content: flex-start; }
  .kpi-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .computed-grid, .insight-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .app-header { position: static; padding: 12px max(14px, env(safe-area-inset-left)) 6px max(14px, env(safe-area-inset-right)); }
  .header-inner { width: 100%; align-items: start; }
  .brand-title { max-width: 132px; overflow: hidden; text-overflow: ellipsis; }
  .header-actions { gap: 6px; }
  .header-actions select { max-width: 112px; font-size: 13px; padding-left: 12px; }
  .desktop-tabs { display: none; }
  .mobile-tabs { display: grid; }
  .app-shell { width: min(100% - 24px, 520px); padding-top: 24px; padding-bottom: calc(110px + env(safe-area-inset-bottom)); }
  .section-head { align-items: stretch; flex-direction: column; gap: 16px; margin-bottom: 18px; }
  .section-head h1 { font-size: clamp(32px, 12vw, 46px); }
  .primary-action { width: 100%; }
  .filter-bar { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .filter-bar select, .filter-bar input, .filter-bar button { width: 100%; min-width: 0; }
  .profile-surface { border-radius: 22px; }
  .profile-table { display: none; }
  .mobile-list { display: grid; }
  .card-fields { grid-template-columns: 1fr; }
  .list-summary { justify-content: flex-start; flex-wrap: wrap; padding: 0 2px; }
  .kpi-grid, .computed-grid, .insight-grid, .settings-grid { grid-template-columns: 1fr; }
  .funnel-row { grid-template-columns: 92px minmax(0, 1fr) 50px; gap: 10px; }
  .funnel-row small:nth-of-type(2) { display: none; }
  .profile-line, .setting-row, .data-actions, .import-form { grid-template-columns: 1fr; }
  .modal-root { align-items: end; padding: 0; }
  .modal-card { width: 100%; max-height: calc(92vh - env(safe-area-inset-bottom)); border-radius: 28px 28px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom)); }
  .modal-grid { grid-template-columns: 1fr; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; }
  .toast-region { left: 12px; right: 12px; top: 12px; }
  .toast { max-width: none; }
}

@media (max-width: 430px) {
  .brand-title { display: none; }
  .header-inner { gap: 10px; }
  .header-actions select { max-width: 102px; }
}

@media (max-width: 360px) {
  .header-actions select { max-width: 92px; }
  .brand-mark { width: 34px; height: 34px; border-radius: 12px; }
  .filter-bar { grid-template-columns: 1fr; }
}
