* { box-sizing: border-box; }

:root {
  --paper: #F4F5F2;
  --surface: #FFFFFF;
  --surface-raised: #FBFBFA;
  --ink: #14161B;
  --ink-muted: #5B6472;
  --ink-faint: #8A93A3;
  --ink-soft: #EBEBE7;
  --line: #DCDEDA;
  --line-strong: #B7BAB4;
  --masthead-bg: #14161B;
  --masthead-fg: #F4F5F2;
  --accent: #1a4fd6;
  --shadow: 0 1px 2px rgba(20,22,27,0.05), 0 8px 24px rgba(20,22,27,0.07);
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #101114;
    --surface: #1A1C21;
    --surface-raised: #202227;
    --ink: #EDEDEA;
    --ink-muted: #9EA3AC;
    --ink-faint: #6B7078;
    --ink-soft: #2A2C31;
    --line: #303338;
    --line-strong: #45484E;
    --masthead-bg: #000000;
    --masthead-fg: #F4F5F2;
    --accent: #6C93F4;
    --shadow: 0 1px 2px rgba(0,0,0,0.35), 0 10px 26px rgba(0,0,0,0.4);
  }
}

/* kept for legacy references elsewhere in this file */
:root {
  --black: var(--ink);
  --white: var(--surface);
  --gray-soft: var(--ink-muted);
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 22px 28px;
  background: var(--masthead-bg);
  color: var(--masthead-fg);
}

.topbar h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 650;
  letter-spacing: 0.6px;
  color: var(--masthead-fg);
}

.topbar-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

#search {
  padding: 9px 14px;
  border: 1px solid rgba(244,245,242,0.3);
  border-radius: 8px;
  background: rgba(244,245,242,0.08);
  color: var(--masthead-fg);
  min-width: 220px;
  font-size: 13.5px;
}
#search::placeholder { color: rgba(244,245,242,0.5); }

.btn {
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.btn:hover { border-color: var(--ink); }

.topbar .btn {
  border-color: rgba(244,245,242,0.35);
  background: transparent;
  color: var(--masthead-fg);
}
.topbar .btn:hover { background: rgba(244,245,242,0.14); }

.btn-primary {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.btn-primary:hover {
  opacity: 0.85;
}

.topbar .btn-primary {
  background: var(--masthead-fg);
  color: var(--masthead-bg);
  border-color: var(--masthead-fg);
}
.topbar .btn-primary:hover { opacity: 0.85; }

.btn-outline.danger {
  border-color: #B23A2C;
  color: #B23A2C;
}
.btn-outline.danger:hover { background: #B23A2C; color: #fff; }

.btn-icon {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--ink-muted);
  padding: 4px 8px;
  border-radius: 6px;
}
.btn-icon:hover { background: var(--ink-soft); color: var(--ink); }

main { max-width: 1280px; margin: 0 auto; padding: 22px 28px 60px; }

.status-msg {
  font-size: 12.5px;
  color: var(--ink-faint);
  margin-bottom: 12px;
  min-height: 16px;
}

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 1200px;
  font-size: 13px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--ink);
  color: var(--paper);
  text-align: right;
  padding: 12px 12px;
  font-weight: 650;
  font-size: 11.5px;
  letter-spacing: 0.3px;
  white-space: nowrap;
  cursor: grab;
  user-select: none;
}
thead tr th:first-child { border-top-right-radius: 12px; }
thead tr th:last-child { border-top-left-radius: 12px; }

thead th.dragging { opacity: 0.4; }
thead th.drag-over { box-shadow: inset 0 0 0 2px var(--paper); background: var(--ink-muted); }

tbody td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  max-width: 220px;
}

tbody tr:hover { background: var(--surface-raised); }
tbody tr:last-child td { border-bottom: none; }

.cell-notes {
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--ink-muted);
}

.pill {
  display: inline-block;
  border: 1px solid currentColor;
  border-radius: 100px;
  padding: 3px 11px;
  font-size: 11.5px;
  font-weight: 650;
  white-space: nowrap;
  color: var(--ink-muted);
}

.row-actions {
  display: flex;
  gap: 6px;
  white-space: nowrap;
}

.row-actions button {
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
}

.row-actions button:hover { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20,22,27, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  backdrop-filter: blur(2px);
}

.overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 480px;
  padding: 22px 24px 24px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding-bottom: 12px;
  margin-bottom: 16px;
}

.modal-header h2 { margin: 0; font-size: 17px; font-weight: 650; }

#leadForm label,
#taskForm label,
#automationForm label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 14px;
  color: var(--ink-muted);
}

#leadForm input,
#leadForm select,
#leadForm textarea,
#taskForm input,
#taskForm select,
#taskForm textarea,
#automationForm input,
#automationForm select,
#automationForm textarea {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 9px 11px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-family: inherit;
}
#leadForm input:focus,
#leadForm select:focus,
#leadForm textarea:focus,
#taskForm input:focus,
#taskForm select:focus,
#taskForm textarea:focus,
#automationForm input:focus,
#automationForm select:focus,
#automationForm textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

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

.modal-actions-right {
  display: flex;
  gap: 8px;
}

.hidden { display: none !important; }

.phone-link {
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  font-weight: 600;
}
.phone-link:hover { color: var(--accent); }

/* row name link */
.name-link {
  color: var(--accent);
  text-decoration: none;
  cursor: pointer;
  font-weight: 650;
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
  font-family: inherit;
}
.name-link:hover { text-decoration: underline; }

.cell-field {
  color: var(--accent);
  font-weight: 600;
}

/* detail view */
.back-link {
  display: inline-block;
  margin-bottom: 18px;
  color: var(--ink-muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
}
.back-link:hover { color: var(--ink); }

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.detail-header h2 { margin: 0; font-size: 26px; font-weight: 650; letter-spacing: 0.2px; }

.stepper {
  display: flex;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}

.stepper button {
  flex: 1 1 auto;
  min-width: 120px;
  border: none;
  border-inline-end: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-muted);
  padding: 11px 8px;
  font-size: 11.5px;
  font-weight: 650;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.stepper button:last-child { border-inline-end: none; }
.stepper button:hover { background: var(--surface-raised); color: var(--ink); }
.stepper button.active { background: var(--ink); color: var(--paper); font-weight: 700; }

.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 22px;
}

.quick-actions button {
  border: 1px solid var(--line-strong);
  border-radius: 100px;
  background: var(--surface);
  color: var(--ink);
  padding: 8px 16px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.quick-actions button:hover { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

@media (max-width: 800px) {
  .detail-grid { grid-template-columns: 1fr; }
}

.detail-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px 22px;
}

.detail-card h3 {
  margin: 0 0 16px;
  font-size: 13.5px;
  font-weight: 650;
  letter-spacing: 0.3px;
  color: var(--ink-muted);
  text-transform: uppercase;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.activity-log {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 520px;
  overflow-y: auto;
}

.activity-log li {
  border-bottom: 1px solid var(--line);
  padding: 12px 0;
}

.activity-log li:last-child { border-bottom: none; }

.activity-log .activity-meta {
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-faint);
  margin-bottom: 4px;
}

.activity-log .activity-text {
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.activity-empty {
  color: var(--ink-faint);
  font-size: 13px;
}

.modal.small { max-width: 380px; }

/* per-field inline edit rows */
.field-row {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.field-row:last-child { border-bottom: none; }

.field-label {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.2px;
  color: var(--ink-faint);
  margin-bottom: 5px;
  text-transform: uppercase;
}

.field-value-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-value {
  font-size: 15px;
  cursor: pointer;
  flex: 1;
  min-height: 20px;
  word-break: break-word;
  white-space: pre-wrap;
  border-radius: 6px;
  padding: 2px 4px;
  margin: -2px -4px;
  transition: background 0.12s ease;
}
.field-value:hover { background: var(--ink-soft); }

.field-value.accent { color: var(--accent); font-weight: 650; }

.pencil-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  padding: 3px 5px;
  line-height: 1;
  opacity: 0.55;
  border-radius: 6px;
}
.pencil-btn:hover { opacity: 1; background: var(--ink-soft); }

.field-input {
  flex: 1;
  padding: 7px 9px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-family: inherit;
}
.field-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.icon-btn {
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-size: 13px;
  padding: 4px 9px;
  line-height: 1;
}
.icon-btn:hover { background: var(--ink); color: var(--paper); border-color: var(--ink); }

/* shared top navigation */
.mainnav {
  display: flex;
  gap: 4px;
  padding: 10px 28px;
  background: var(--masthead-bg);
  border-bottom: 1px solid rgba(244,245,242,0.12);
  overflow-x: auto;
}
.mainnav a {
  color: rgba(244,245,242,0.65);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 8px;
  white-space: nowrap;
}
.mainnav a:hover { background: rgba(244,245,242,0.1); color: var(--masthead-fg); }
.mainnav a.active { background: var(--masthead-fg); color: var(--masthead-bg); }

/* view toggle (table / kanban) */
.view-toggle {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
}
.view-toggle button {
  border: 1px solid var(--line-strong);
  border-radius: 100px;
  background: var(--surface);
  color: var(--ink);
  padding: 6px 16px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.view-toggle button.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }

/* kanban board */
.kanban-board {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  padding-bottom: 10px;
}
.kanban-col {
  flex: 0 0 250px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 220px);
}
.kanban-col-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.kanban-col-body {
  padding: 10px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.kanban-col-body.drag-over { background: var(--surface-raised); }
.kanban-card {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 12px;
  background: var(--surface);
  cursor: grab;
  font-size: 13px;
}
.kanban-card:hover { border-color: var(--line-strong); }
.kanban-card .kc-name { font-weight: 650; color: var(--accent); margin-bottom: 3px; }
.kanban-card .kc-meta { font-size: 11.5px; color: var(--ink-faint); }
.kanban-card.dragging { opacity: 0.4; }

/* dashboard */
.widgets-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.widget-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.widget-card .w-label { font-size: 11.5px; color: var(--ink-faint); font-weight: 650; margin-bottom: 8px; }
.widget-card .w-value { font-size: 28px; font-weight: 700; }

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 18px 20px;
}
.chart-card h3 { margin: 0 0 14px; font-size: 13px; color: var(--ink-muted); font-weight: 650; }
.bar-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 12px; }
.bar-row .bl { flex: 0 0 110px; color: var(--ink-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .bt { flex: 1; background: var(--ink-soft); border-radius: 4px; height: 14px; overflow: hidden; }
.bar-row .bf { height: 100%; border-radius: 4px; background: var(--accent); }
.bar-row .bv { flex: 0 0 26px; text-align: left; color: var(--ink-faint); font-size: 11px; }

/* tasks + calendar */
.tasks-layout { display: grid; grid-template-columns: 1fr 1.2fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .tasks-layout { grid-template-columns: 1fr; } }
.task-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.task-row.done .task-title { text-decoration: line-through; color: var(--ink-faint); }
.task-row input[type="checkbox"] { margin-top: 3px; width: 16px; height: 16px; }
.task-title { font-size: 14px; font-weight: 600; }
.task-meta { font-size: 11.5px; color: var(--ink-faint); margin-top: 2px; }
.task-meta.overdue { color: #B23A2C; font-weight: 650; }
.task-del { border: none; background: none; color: var(--ink-faint); cursor: pointer; font-size: 16px; }
.task-del:hover { color: #B23A2C; }

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.cal-daycell {
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 74px;
  padding: 6px;
  font-size: 11px;
  background: var(--surface);
}
.cal-daycell.empty { background: transparent; border-color: transparent; }
.cal-daynum { font-weight: 650; color: var(--ink-faint); margin-bottom: 4px; }
.cal-task { background: var(--ink-soft); border-radius: 5px; padding: 2px 5px; margin-bottom: 3px; font-size: 10.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.cal-nav button { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 7px; padding: 5px 12px; cursor: pointer; }
.cal-weekday { text-align: center; font-size: 11px; font-weight: 650; color: var(--ink-faint); padding-bottom: 4px; }

/* automations */
.automation-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 16px 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.automation-card .a-desc { font-size: 13.5px; }
.automation-card .a-name { font-weight: 650; margin-bottom: 3px; }
.automation-actions { display: flex; gap: 8px; align-items: center; }
.toggle-switch {
  width: 38px; height: 22px;
  border-radius: 100px;
  border: 1px solid var(--line-strong);
  background: var(--ink-soft);
  position: relative;
  cursor: pointer;
}
.toggle-switch.on { background: var(--ink); }
.toggle-switch .knob {
  position: absolute; top: 2px; right: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--surface);
  transition: transform 0.15s ease;
}
.toggle-switch.on .knob { transform: translateX(-16px); }

@media (max-width: 640px) {
  main { padding: 14px; }
  .topbar { padding: 16px 18px; }
  #search { min-width: 0; flex: 1; }
}
