:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-soft: #f8f9fc;
  --text: #1f2329;
  --muted: #646e7e;
  --line: #dde2eb;
  --accent: #2f7ef7;
  --accent-pressed: #2467ca;
  --danger: #dc3d4b;
  --success: #1f9d63;
  --shadow: 0 10px 30px rgba(18, 33, 62, 0.08);

  --status-new-bg: #e9f3ff;
  --status-new-text: #1f63bf;
  --status-progress-bg: #fff2dc;
  --status-progress-text: #9a5b02;
  --status-done-bg: #e8f8ef;
  --status-done-text: #167d4f;

  --toast-success-bg: #1d8f59;
  --toast-error-bg: #c53a46;
  --toast-info-bg: #2f7ef7;

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

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font: 15px/1.45 "Segoe UI", "Inter", "Roboto", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(70% 40% at 0% 0%, rgba(72, 145, 255, 0.16), transparent 60%),
    radial-gradient(60% 35% at 100% 30%, rgba(20, 178, 137, 0.14), transparent 65%);
  pointer-events: none;
  z-index: -1;
}

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

.app-shell {
  max-width: 860px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 12px calc(90px + var(--safe-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border: 1px solid color-mix(in srgb, var(--line) 86%, transparent);
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
}

.topbar-main {
  min-width: 0;
}

.screen-title {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
  font-weight: 700;
}

.tenant-info {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sync-indicator {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 92px;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 12px;
  font-weight: 600;
}

.sync-indicator.is-online {
  color: var(--success);
  background: color-mix(in srgb, var(--success) 12%, var(--surface));
  border-color: color-mix(in srgb, var(--success) 25%, transparent);
}

.sync-indicator.is-sync {
  color: var(--accent-pressed);
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  border-color: color-mix(in srgb, var(--accent) 30%, transparent);
}

.sync-indicator.is-error {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 13%, var(--surface));
  border-color: color-mix(in srgb, var(--danger) 25%, transparent);
}

.screen-wrap {
  margin-top: 12px;
  display: grid;
  gap: 12px;
}

.hint-screen {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 92%, var(--bg));
  box-shadow: var(--shadow);
  display: grid;
  gap: 10px;
}

.hint-title {
  margin: 0;
  font-size: 16px;
  line-height: 1.3;
  font-weight: 700;
}

.hint-note {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.screen {
  display: grid;
  gap: 12px;
}

.panel {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-flat {
  padding: 10px;
  box-shadow: none;
  background: var(--surface-soft);
}

.panel-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 10px;
}

.panel-head {
  display: grid;
  gap: 4px;
}

.panel-title {
  margin: 0;
  font-size: 16px;
  line-height: 1.3;
  font-weight: 700;
}

.panel-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.input-wrap {
  display: grid;
  gap: 6px;
}

.field-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.input,
.textarea,
.select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input:focus,
.textarea:focus,
.select:focus {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.textarea {
  resize: vertical;
  min-height: 92px;
}

.btn {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.06s ease, background-color 0.15s ease, border-color 0.15s ease, opacity 0.2s ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  color: #ffffff;
  background: var(--accent);
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent-pressed);
  border-color: var(--accent-pressed);
}

.btn-secondary {
  color: var(--text);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
  border-color: color-mix(in srgb, var(--accent) 24%, var(--line));
}

.btn-ghost {
  color: var(--text);
  background: transparent;
  border-color: var(--line);
}

.btn-sm {
  min-height: 36px;
  padding: 0 11px;
  border-radius: 10px;
  font-size: 13px;
}

.status-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.status-chip {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.status-chip.is-active {
  color: #ffffff;
  background: var(--accent);
  border-color: var(--accent);
}

.state-box {
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 92%, var(--bg));
}

.state-title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.state-note {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.state-actions {
  margin-top: 12px;
}

.lead-list {
  display: grid;
  gap: 10px;
}

.lead-card {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: grid;
  gap: 9px;
  cursor: pointer;
}

.lead-card:active {
  transform: translateY(1px);
}

.lead-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.lead-card-title {
  margin: 0;
  font-size: 16px;
  line-height: 1.25;
}

.lead-card-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

.lead-phone {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.lead-preview {
  margin: 0;
  color: var(--text);
  font-size: 14px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 12px;
  font-weight: 700;
}

.badge-new {
  color: var(--status-new-text);
  background: var(--status-new-bg);
  border-color: color-mix(in srgb, var(--status-new-text) 20%, transparent);
}

.badge-progress {
  color: var(--status-progress-text);
  background: var(--status-progress-bg);
  border-color: color-mix(in srgb, var(--status-progress-text) 20%, transparent);
}

.badge-done {
  color: var(--status-done-text);
  background: var(--status-done-bg);
  border-color: color-mix(in srgb, var(--status-done-text) 20%, transparent);
}

.badge-neutral {
  color: var(--muted);
  background: color-mix(in srgb, var(--muted) 12%, var(--surface));
  border-color: color-mix(in srgb, var(--muted) 30%, transparent);
}

.save-state {
  margin: 0;
  min-height: 20px;
  font-size: 13px;
  color: var(--muted);
}

.save-state.is-success {
  color: var(--success);
}

.save-state.is-error {
  color: var(--danger);
}

.settings-list {
  display: grid;
  gap: 10px;
}

.settings-item {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-soft);
  display: grid;
  gap: 8px;
}

.settings-item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.settings-item-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.settings-item-note {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.settings-row-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.switch {
  position: relative;
  width: 46px;
  height: 28px;
  flex-shrink: 0;
}

.switch input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.switch-track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: color-mix(in srgb, var(--muted) 24%, var(--surface));
  transition: background-color 0.15s ease;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
  transition: transform 0.15s ease;
}

.switch input:checked + .switch-track {
  background: var(--accent);
}

.switch input:checked + .switch-track::after {
  transform: translateX(18px);
}

.advanced-settings {
  overflow: hidden;
}

.advanced-summary {
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  list-style: none;
}

.advanced-summary::-webkit-details-marker {
  display: none;
}

.advanced-content {
  margin-top: 12px;
  display: grid;
  gap: 12px;
}

.advanced-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#leadFormEditor {
  min-height: 180px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(860px, 100%);
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: color-mix(in srgb, var(--surface) 93%, transparent);
  border-top: 1px solid color-mix(in srgb, var(--line) 88%, transparent);
  backdrop-filter: blur(12px);
  z-index: 35;
}

.nav-btn {
  min-height: 48px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.nav-btn.is-active {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 13%, var(--surface));
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: block;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 17, 26, 0.45);
}

.modal-sheet {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(860px, 100%);
  max-height: 88dvh;
  overflow: auto;
  border-radius: 18px 18px 0 0;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 0 -16px 40px rgba(16, 22, 37, 0.24);
}

.modal-head {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-title {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.modal-body {
  padding: 12px;
  display: grid;
  gap: 12px;
}

.modal-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 8px;
}

.modal-copy-btn {
  min-width: 172px;
}

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.payload-list {
  display: grid;
  gap: 8px;
}

.payload-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  background: var(--surface);
}

.payload-key {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.payload-value {
  margin: 3px 0 0;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.toast-container {
  position: fixed;
  left: 50%;
  bottom: calc(76px + var(--safe-bottom));
  transform: translateX(-50%);
  width: min(560px, calc(100% - 24px));
  display: grid;
  gap: 8px;
  z-index: 60;
  pointer-events: none;
}

.toast {
  border-radius: 12px;
  padding: 11px 12px;
  color: #ffffff;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: toast-in 0.2s ease;
}

.toast.is-success {
  background: var(--toast-success-bg);
}

.toast.is-error {
  background: var(--toast-error-bg);
}

.toast.is-info {
  background: var(--toast-info-bg);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 720px) {
  .panel-toolbar,
  .modal-grid {
    grid-template-columns: 1fr;
  }

  .modal-copy-btn {
    width: 100%;
  }

  .sync-indicator {
    min-width: 76px;
  }

  .topbar {
    align-items: flex-start;
  }
}

@media (max-width: 420px) {
  .app-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  .panel,
  .topbar,
  .modal-body,
  .modal-head {
    padding: 10px;
  }

  .screen-title {
    font-size: 17px;
  }
}
