:root {
  --ink: #1B1B3A;
  --ink-soft: #4A4A68;
  --cream: #FBF7EF;
  --paper: #FFFFFF;
  --line: #E7E0D2;
  --muted: #837C6D;
  --orange: #D9642C;
  --orange-dark: #B94F1E;
  --gold: #C9A227;
  --teal: #3E7C74;
  --off: #E4DFD3;
  --danger: #B23A3A;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(27,27,58,0.06), 0 1px 2px rgba(27,27,58,0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--cream);
  color: var(--ink);
  font-family: 'Inter', system-ui, sans-serif;
  min-height: 100vh;
}

.wing-band {
  height: 6px;
  width: 100%;
  background: linear-gradient(90deg, var(--ink) 0%, var(--orange) 45%, var(--gold) 100%);
}

.config-warning {
  background: #FCE9DC;
  color: #7A3814;
  border-bottom: 1px solid #EFC3A0;
  padding: 12px 32px;
  font-size: 0.88rem;
  line-height: 1.5;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  padding: 20px 32px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.brand-name {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 1.25rem;
  color: var(--ink);
}

.brand-sub {
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 14px;
}

.sheet-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--muted);
}

.sheet-picker select {
  font-family: 'Inter', sans-serif;
  font-size: 0.9rem;
  color: var(--ink);
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
}

.btn {
  font-family: 'Inter', sans-serif;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 8px;
  padding: 10px 18px;
  border: none;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--orange);
  color: #fff;
}
.btn-primary:hover { background: var(--orange-dark); }

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--line);
}
.btn-ghost:hover { background: var(--cream); }

.content {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 32px 60px;
}

.stats-strip {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.stat {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 20px;
  min-width: 140px;
  box-shadow: var(--shadow);
}

.stat-value {
  display: block;
  font-family: 'Fraunces', serif;
  font-size: 1.6rem;
  font-weight: 600;
}

.stat-label {
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stat-contacted .stat-value { color: var(--teal); }
.stat-scheduled .stat-value { color: var(--orange); }
.stat-closed .stat-value { color: var(--gold); }
.stat-dead .stat-value { color: var(--danger); }

.table-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.table-scroll {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 780px;
}

thead th {
  text-align: left;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 600;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--cream);
  position: sticky;
  top: 0;
}

.col-toggle { text-align: center; }
.col-date { min-width: 150px; }
.col-actions { width: 44px; }

.meeting-date-input {
  font-family: 'Inter', sans-serif;
  font-size: 0.85rem;
  color: var(--ink);
  background: var(--cream);
  border: 1px solid var(--orange);
  border-radius: 6px;
  padding: 5px 8px;
  width: 100%;
  cursor: pointer;
}

.meeting-date-input:focus {
  outline: none;
  border-color: var(--orange-dark);
  box-shadow: 0 0 0 2px rgba(217,100,44,0.15);
}

tbody tr {
  border-bottom: 1px solid var(--line);
}
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #FCFAF4; }

td {
  padding: 12px 16px;
  font-size: 0.92rem;
  vertical-align: middle;
}

td input[type="text"], td input[type="tel"] {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  font-family: 'Inter', sans-serif;
  font-size: 0.92rem;
  color: var(--ink);
  padding: 6px 8px;
  border-radius: 6px;
}
td input[type="text"]:hover, td input[type="tel"]:hover {
  border-color: var(--line);
}
td input[type="text"]:focus, td input[type="tel"]:focus {
  outline: none;
  border-color: var(--orange);
  background: var(--cream);
}

td select.type-select {
  font-family: 'Inter', sans-serif;
  font-size: 0.85rem;
  color: var(--ink);
  background: var(--cream);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  width: 100%;
}

.carried-badge {
  font-size: 0.68rem;
  color: var(--orange-dark);
  background: rgba(217,100,44,0.12);
  padding: 2px 6px;
  border-radius: 5px;
  margin-left: 6px;
  white-space: nowrap;
}

/* Toggle switch */
.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 44px;
  height: 24px;
  cursor: pointer;
}
.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle .track {
  position: absolute;
  inset: 0;
  background: var(--off);
  border-radius: 999px;
  transition: background 0.18s ease;
}
.toggle .thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: transform 0.18s ease;
}
.toggle input:checked + .track { background: var(--contacted-on, var(--teal)); }
.toggle input:checked ~ .thumb { transform: translateX(20px); }

.toggle.contacted input:checked + .track { background: var(--teal); }
.toggle.scheduled input:checked + .track { background: var(--orange); }
.toggle.closed input:checked + .track { background: var(--gold); }
.toggle.dead input:checked + .track { background: var(--danger); }

.toggle-readonly {
  cursor: not-allowed;
}
.toggle-readonly .track {
  opacity: 0.6;
}

.delete-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 4px 8px;
  border-radius: 6px;
}
.delete-btn:hover {
  color: var(--danger);
  background: rgba(178,58,58,0.08);
}

.lead-dead-row {
  opacity: 0.55;
}
.lead-dead-row:hover {
  opacity: 0.85;
}

.empty-row {
  text-align: center;
  padding: 36px 24px;
  color: var(--muted);
  font-size: 0.9rem;
}

.footnote {
  margin-top: 18px;
  font-size: 0.82rem;
  color: var(--muted);
  max-width: 720px;
  line-height: 1.5;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(27,27,58,0.45);
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
  display: none;
}

.modal-backdrop:not([hidden]) {
  display: flex;
}

.modal {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 12px 40px rgba(27,27,58,0.25);
}

.modal h2 {
  font-family: 'Fraunces', serif;
  margin: 0 0 18px;
  font-size: 1.3rem;
}

.modal label {
  display: block;
  font-size: 0.85rem;
  color: var(--ink-soft);
  margin-bottom: 14px;
}

.optional-tag {
  color: var(--muted);
  font-weight: 400;
}

.modal input, .modal select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: 'Inter', sans-serif;
  font-size: 0.92rem;
  background: var(--cream);
}
.modal input:focus, .modal select:focus {
  outline: none;
  border-color: var(--orange);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 0.88rem;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  z-index: 60;
}

@media (max-width: 640px) {
  .topbar { padding: 16px 18px; }
  .content { padding: 20px 16px 40px; }
}

/* ── Auth loading (shown briefly before we know login state — never the form itself) ── */
.auth-loading {
  position: fixed;
  inset: 0;
  background: var(--cream);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99;
}
.auth-loading[hidden] { display: none; }
.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--orange);
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Password gate ── */
.login-backdrop {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, var(--ink) 0%, #2a2a52 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.login-backdrop[hidden] { display: none; }

.login-card {
  text-align: center;
}
.login-mark {
  font-size: 2.4rem;
  margin-bottom: 4px;
}
.login-card h2 {
  font-family: 'Fraunces', serif;
  margin: 0 0 6px;
  font-size: 1.4rem;
}
.login-sub {
  color: var(--muted);
  font-size: 0.88rem;
  margin: 0 0 20px;
}
.login-card label {
  text-align: left;
}
.login-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: -6px 0 14px;
}

/* ── Project button in leads table ── */
.col-actions-wide {
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}
.project-btn {
  background: rgba(62,124,116,0.12);
  color: var(--teal);
  border: 1px solid rgba(62,124,116,0.3);
  font-family: 'Inter', sans-serif;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
}
.project-btn:hover {
  background: rgba(62,124,116,0.2);
}

/* ── Projects list view ── */
.project-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.project-card {
  display: block;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  text-decoration: none;
  color: inherit;
  transition: transform 0.1s ease, box-shadow 0.15s ease;
}
.project-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(27,27,58,0.12);
}

.project-card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}
.project-card-top h3 {
  font-family: 'Fraunces', serif;
  font-size: 1.08rem;
  margin: 0;
  color: var(--ink);
}

.project-card-meta {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 14px;
}

.project-card-money {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}
.project-card-money > div {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.project-card-money strong {
  font-size: 0.95rem;
  color: var(--ink);
}

.balance-due { color: var(--orange-dark) !important; }

.assignee-tag {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(27,27,58,0.08);
  color: var(--ink-soft);
  font-size: 0.78rem;
}

.assigned-readout {
  display: inline-block;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cream);
  color: var(--ink);
}

.status-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.status-badge.status-active {
  background: rgba(217,100,44,0.14);
  color: var(--orange-dark);
}
.status-badge.status-done {
  background: rgba(201,162,39,0.18);
  color: #8a6d15;
}

/* ── Project detail view ── */
.back-link {
  display: inline-block;
  margin-bottom: 16px;
  font-size: 0.88rem;
  color: var(--ink-soft);
  text-decoration: none;
}
.back-link:hover { color: var(--orange-dark); }

.project-detail-card {
  padding: 28px 32px;
}

.project-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 22px;
}
.project-detail-header h1 {
  font-family: 'Fraunces', serif;
  font-size: 1.5rem;
  margin: 0;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 18px;
  margin-bottom: 18px;
}

.detail-grid label, .full-width {
  display: block;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.detail-grid input, .detail-grid select, .full-width textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: 'Inter', sans-serif;
  font-size: 0.92rem;
  background: var(--cream);
  color: var(--ink);
}
.full-width textarea {
  resize: vertical;
  font-family: 'Inter', sans-serif;
}
.full-width {
  margin-bottom: 18px;
}

.detail-grid input:focus, .detail-grid select:focus, .full-width textarea:focus {
  outline: none;
  border-color: var(--orange);
}

.balance-box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: var(--cream);
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  gap: 4px;
}
.balance-box .stat-value {
  font-size: 1.2rem;
}

.save-hint {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 4px 0 0;
}

/* ── Phone cell + copy button (leads table, project card, project detail) ── */
.phone-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 190px;
}
.phone-cell input {
  flex: 1 1 auto;
  min-width: 130px;
}

.icon-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--cream);
  color: var(--ink-soft);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.icon-btn:hover {
  background: rgba(62,124,116,0.14);
  color: var(--teal);
  border-color: rgba(62,124,116,0.35);
}
.icon-btn:active { transform: translateY(1px); }

.danger-icon-btn:hover {
  background: rgba(178,58,58,0.12);
  color: var(--danger);
  border-color: rgba(178,58,58,0.3);
}

.btn-danger-ghost {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(178,58,58,0.35);
}
.btn-danger-ghost:hover {
  background: rgba(178,58,58,0.1);
}

.project-btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

/* ── Project card top row (status + delete) ── */
.project-card-top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.project-card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.card-copy-btn, .card-delete-btn, .card-invoice-btn {
  width: 26px;
  height: 26px;
}

/* ── Meetings page ── */
.meeting-overdue-row td:first-child::after {
  content: ' (overdue)';
  color: var(--danger);
  font-size: 0.72rem;
  font-weight: 600;
}

.project-detail-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ── Mobile responsiveness ── */
@media (max-width: 760px) {
  .topbar {
    padding: 14px 16px;
    gap: 12px;
  }
  .topbar-controls {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
  }
  .sheet-picker {
    width: 100%;
    justify-content: space-between;
  }
  .sheet-picker select { flex: 1; }
  #addLeadBtn, #addMeetingBtn { flex: 1 1 100%; }
  .btn { font-size: 0.85rem; padding: 10px 14px; }

  .content { padding: 18px 12px 40px; }

  .stats-strip { gap: 8px; }
  .stat { min-width: 0; flex: 1 1 calc(50% - 8px); padding: 12px 14px; }
  .stat-value { font-size: 1.25rem; }

  /* Leads / Meetings table becomes a stacked card list */
  .table-scroll { overflow-x: visible; }
  #leadsTable, #meetingsTable { min-width: 0; }
  #leadsTable thead, #meetingsTable thead { display: none; }
  #leadsTable, #leadsTable tbody, #leadsTable tr,
  #meetingsTable, #meetingsTable tbody, #meetingsTable tr {
    display: block;
    width: 100%;
  }
  #leadsTable tr, #meetingsTable tr {
    border-bottom: 1px solid var(--line);
    padding: 12px 4px;
  }
  #leadsTable tbody tr:last-child, #meetingsTable tbody tr:last-child { border-bottom: none; }
  #leadsTable td, #meetingsTable td {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    padding: 8px 4px;
    border-bottom: none;
    text-align: left;
  }
  #leadsTable td[data-label]::before, #meetingsTable td[data-label]::before {
    content: attr(data-label);
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    font-weight: 600;
    text-align: left;
  }
  #leadsTable td input, #leadsTable td select,
  #meetingsTable td input, #meetingsTable td select {
    text-align: left;
    width: 100%;
    max-width: 100%;
  }
  #leadsTable .phone-cell, #meetingsTable .phone-cell { width: 100%; max-width: 100%; }
  #leadsTable .meeting-date-input, #meetingsTable .meeting-date-input { max-width: 100%; }

  /* Toggles and the actions row read better as a plain left/right row */
  #leadsTable td.col-toggle, #meetingsTable td.col-toggle {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
  #leadsTable td.col-toggle[data-label]::before, #meetingsTable td.col-toggle[data-label]::before { text-align: left; }
  #leadsTable .col-actions-wide, #meetingsTable .col-actions-wide {
    flex-direction: row;
    justify-content: flex-end;
  }

  .project-cards { grid-template-columns: 1fr; }
  .project-detail-card { padding: 20px 18px; }
  .detail-grid { grid-template-columns: 1fr; }

  .modal { padding: 22px 18px; max-width: 100%; }
}
