:root {
  --bg: #eef2fb;
  --surface: #ffffff;
  --surface-2: #f5f7fc;
  --border: #e8ecf4;
  --text: #2f3552;
  --muted: #9aa3b8;
  --primary: #5c7cfa;
  --primary-dark: #4c6ef5;
  --primary-text: #ffffff;
  --danger: #f25f5c;
  --ok: #58c26b;
  --warn: #f5a524;
  --late-bg: #fff6f6;

  --side-bg: #3f4378;
  --side-bg-2: #4a4f8a;
  --side-text: #c9cdf2;
  --side-muted: #8d92c4;

  --radius: 10px;
  --shadow: 0 10px 30px rgba(60, 72, 140, .08);

  /* status pills: solid colour, white text */
  --s-not-started: #a3aabd;
  --s-in-progress: #3d9df3;
  --s-on-hold: #f5a524;
  --s-follow-up: #8b6cf6;
  --s-waiting-feedback: #22b8cf;
  --s-adjustment-needed: #f25f5c;
  --s-completed: #7ac943;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.role-team .admin-only { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Poppins", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
button, input, select { font: inherit; color: inherit; }

/* ---------- App shell ---------- */
.app { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  background: var(--side-bg); color: var(--side-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand {
  background: var(--primary); color: #fff; font-weight: 600; font-size: 18px; letter-spacing: -.01em;
  padding: 20px 24px; flex: none;
}
.side-search { padding: 18px 18px 6px; }
.side-search input {
  width: 100%; border: 0; border-radius: 999px; padding: 9px 16px 9px 38px;
  background: var(--side-bg-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%23c9cdf2' stroke-width='2' stroke-linecap='round' viewBox='0 0 24 24'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E") no-repeat 14px center;
  color: #fff; font-size: 13px;
}
.side-search input::placeholder { color: var(--side-muted); }
.side-search input:focus { outline: 2px solid var(--primary); outline-offset: 0; }

.sidebar-title {
  font-size: 11px; font-weight: 600; color: var(--side-muted); text-transform: uppercase;
  letter-spacing: .08em; margin: 18px 24px 8px;
}
#sidebar { display: flex; flex-direction: column; gap: 2px; padding: 0 12px; position: relative; }
.coach {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; color: var(--side-text);
  border: 0; background: transparent; border-radius: 8px; padding: 9px 12px; cursor: pointer;
  border-left: 3px solid transparent;
}
.coach:hover { background: var(--side-bg-2); color: #fff; }
.coach.active { background: var(--side-bg-2); color: #fff; font-weight: 600; border-left-color: var(--primary); }
.coach-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.coach.muted .coach-name { color: var(--side-muted); font-style: italic; }
.coach-count { color: var(--side-muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.coach-overdue {
  background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; font-weight: 600;
  padding: 0 7px; line-height: 18px; font-variant-numeric: tabular-nums;
}
.sidebar-divider { height: 1px; background: rgba(255, 255, 255, .08); margin: 6px 12px; }

.side-foot { margin-top: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; }
.role-badge { font-size: 12px; color: var(--side-muted); }
.side-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.side-link {
  border: 0; background: transparent; color: var(--side-text); padding: 6px 10px; border-radius: 6px;
  cursor: pointer; font-size: 13px;
}
.side-link:hover { background: var(--side-bg-2); color: #fff; }

.pill {
  display: inline-flex; align-items: center; gap: 8px; border-radius: 999px; padding: 6px 12px;
  font-size: 12.5px; border: 0; background: var(--side-bg-2); color: var(--side-text); width: fit-content;
}
.wa-pill { cursor: pointer; }
.wa-pill:hover { color: #fff; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--side-muted); }
.wa-pill.connected .dot { background: var(--ok); }
.wa-pill.pending .dot { background: var(--warn); }
.wa-pill.off .dot { background: var(--danger); }

/* ---------- Main area ---------- */
.main { min-width: 0; background: var(--surface); }
.main-head {
  display: flex; align-items: stretch; justify-content: space-between; gap: 12px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; background: var(--surface);
}
.tabs { display: flex; justify-content: center; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; padding: 0 16px; }
.tab {
  border: 0; background: transparent; cursor: pointer; white-space: nowrap;
  padding: 20px 14px 17px; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase;
  color: var(--muted); border-bottom: 3px solid transparent; display: inline-flex; align-items: center; gap: 8px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
.tab-count {
  font-size: 11px; letter-spacing: 0; background: var(--surface-2); color: var(--muted);
  border-radius: 999px; padding: 1px 8px; font-variant-numeric: tabular-nums;
}
.tab.active .tab-count { background: var(--primary); color: #fff; }
.tab-overdue .tab-count { background: #ffe3e3; color: var(--danger); }
.tab-overdue.active .tab-count { background: var(--danger); color: #fff; }

.add-btn {
  flex: none; width: 64px; border: 0; background: var(--primary); color: #fff; font-size: 26px; font-weight: 300;
  cursor: pointer; box-shadow: 0 8px 20px rgba(92, 124, 250, .35);
}
.add-btn:hover { background: var(--primary-dark); }

.main-body { padding: 24px 32px 48px; }
.view-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.view-title { font-size: 20px; font-weight: 600; }
.filters { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }

input, select {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 8px 12px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
input:disabled, select:disabled { opacity: .55; cursor: not-allowed; background: var(--surface-2); }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; font-size: 13px; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 8px 16px; cursor: pointer; white-space: nowrap; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer; color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px; font-size: 15px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.chevron { font-size: 22px; line-height: 1; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 14px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th {
  font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .08em;
  cursor: pointer; user-select: none; white-space: nowrap; padding-top: 10px; padding-bottom: 12px;
}
th:hover { color: var(--text); }
th.actions-col { cursor: default; width: 1%; }
th.sorted { color: var(--text); }
th.sorted::after { content: " ↑"; }
th.sorted.desc::after { content: " ↓"; }
tbody tr { transition: background .15s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.late { background: var(--late-bg); }
tbody tr.late td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
tbody tr.late:hover { background: #fff0f0; }
tr.done td { color: var(--muted); }
tr.done .proj-name, tr.done .task-line { text-decoration: line-through; }

.client-cell { min-width: 180px; }
.proj-name { font-weight: 500; color: var(--text); word-break: break-word; display: block; }
.task-line { color: var(--muted); font-size: 12.5px; margin-top: 2px; overflow-wrap: anywhere; }

.pic-cell { min-width: 140px; white-space: nowrap; }
.pic-wrap { display: inline-flex; align-items: center; gap: 8px; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex: none; display: inline-grid; place-items: center;
  color: #fff; font-size: 11px; font-weight: 600; box-shadow: 0 0 0 2px #fff;
}
.avatar-empty { background: #e3e7f1; color: var(--muted); }
.pic-name { font-size: 13px; }

.deadline, .created { white-space: nowrap; }
td.deadline, td.created { position: relative; font-size: 13px; }
.created { color: var(--muted); }
.cell-label { display: none; color: var(--muted); }
.rel { display: block; font-size: 12px; color: var(--muted); }
.rel.overdue { color: var(--danger); font-weight: 500; }
.rel.today { color: var(--warn); font-weight: 500; }
.rel.soon { color: var(--warn); }
.row-actions { white-space: nowrap; text-align: right; }

/* Status: solid uppercase pill */
.status-select {
  appearance: none; -webkit-appearance: none; border: 0; border-radius: 999px; color: #fff;
  padding: 4px 12px; font-size: 10.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase;
  cursor: pointer; field-sizing: content;
}
.status-select option { background: var(--surface); color: var(--text); text-transform: none; font-weight: 400; }
.st-not-started       { background: var(--s-not-started); }
.st-in-progress       { background: var(--s-in-progress); }
.st-on-hold           { background: var(--s-on-hold); }
.st-follow-up         { background: var(--s-follow-up); }
.st-waiting-feedback  { background: var(--s-waiting-feedback); }
.st-adjustment-needed { background: var(--s-adjustment-needed); }
.st-completed         { background: var(--s-completed); }

/* Payment: soft pill */
.payment-cell { white-space: nowrap; }
.payment-select {
  appearance: none; -webkit-appearance: none; border-radius: 999px; border: 1px solid transparent;
  padding: 3px 12px; font-size: 12px; font-weight: 500; cursor: pointer; field-sizing: content;
}
.payment-select option { background: var(--surface); color: var(--text); font-weight: 400; }
.pay-paid-in-full    { background: #ebf8e3; color: #4c9a26; }
.pay-partial-payment { background: #fff4e0; color: #c47f0a; }
.pay-none            { background: transparent; color: var(--muted); border-color: var(--border); border-style: dashed; }

/* Notes */
th.notes-col { cursor: default; }
.note-cell { min-width: 180px; max-width: 300px; }
.note-btn {
  display: block; width: 100%; border: 0; background: transparent; font: inherit; color: inherit; text-align: left;
  padding: 4px 8px; margin: -4px -8px; border-radius: 6px; cursor: text; font-size: 12.5px;
}
.note-btn:hover { background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); }
.note-text {
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
  white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text);
}
.note-empty { color: var(--muted); opacity: 0; transition: opacity .15s; }
tbody tr:hover .note-empty, .note-btn:focus .note-empty { opacity: 1; }
.note-when { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }
.inline-textarea {
  width: 100%; min-width: 200px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
  font: inherit; font-size: 12.5px; resize: vertical; background: var(--surface);
}
.inline-textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
dialog textarea {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; font: inherit; font-size: 14px;
  color: var(--text); resize: vertical;
}
dialog textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

/* Inline editing: looks like text until hovered */
.inline-edit, .deadline-btn {
  border: 0; background: transparent; font: inherit; color: inherit; text-align: left;
  padding: 3px 8px; margin: -3px -8px; border-radius: 6px;
}
.inline-edit { cursor: text; overflow-wrap: anywhere; }
.deadline-btn { cursor: pointer; }
.inline-edit:hover, .deadline-btn:hover, .pic-inline:hover {
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--border);
}
.inline-input { width: 100%; min-width: 140px; padding: 4px 8px; margin: -4px -8px; font-size: 13px; }
.pic-inline {
  appearance: none; -webkit-appearance: none; border: 0; background: transparent; color: inherit; font-size: 13px;
  padding: 4px 8px; margin: -4px -8px; border-radius: 6px; cursor: pointer; field-sizing: content;
}
.pic-inline.pic-none { color: var(--muted); }
.pic-inline option { background: var(--surface); color: var(--text); }
/* The real date input sits under the button so the picker opens in the right place. */
.deadline-input {
  position: absolute; left: 16px; bottom: 8px; width: 1px; height: 1px;
  opacity: 0; pointer-events: none; border: 0; padding: 0;
}

.empty { text-align: center; color: var(--muted); padding: 48px 16px; margin: 0; }

/* ---------- Dialogs ---------- */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(480px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 30px 60px rgba(40, 45, 90, .25);
}
dialog.wide { width: min(640px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(47, 53, 82, .45); }
dialog form, .dialog-body { padding: 24px; display: flex; flex-direction: column; gap: 14px; }
dialog h2 { font-size: 18px; font-weight: 600; }
dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 500; color: var(--muted); }
dialog label input, dialog label select { color: var(--text); font-size: 14px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.spacer { flex: 1; }
.form-error { color: var(--danger); margin: 0; font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; margin: 0; font-weight: 400; }
.hint.warn { color: var(--warn); }
.saved { color: var(--ok); font-size: 13px; }
.next-send { margin: 0; font-size: 13px; background: var(--surface-2); border-radius: 8px; padding: 8px 12px; }
.warn-text { color: var(--warn); }

.panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
.panel h3 { font-size: 14px; font-weight: 600; }
.settings-form { padding: 0 !important; }
.wa-state { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.wa-state .qr { width: 220px; height: 220px; border-radius: 8px; background: #fff; padding: 6px; border: 1px solid var(--border); }
.wa-state ol { margin: 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.wa-state ol li { margin-bottom: 4px; }
.wa-info { display: flex; flex-direction: column; gap: 10px; flex: 1; min-width: 200px; }
.pic-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.pic-list li {
  display: inline-flex; align-items: center; gap: 2px; padding: 3px 4px 3px 12px;
  border-radius: 999px; background: var(--surface-2); font-size: 13px;
}
.pic-list .icon-btn { width: 24px; height: 24px; font-size: 12px; }
.pic-list .empty-note { background: none; padding: 0; color: var(--muted); }
.pic-add { display: flex; gap: 8px; padding: 0 !important; flex-direction: row !important; }
.pic-add input { flex: 1; }
.preview {
  margin: 0; white-space: pre-wrap; font: 13px/1.5 inherit; font-family: inherit;
  background: var(--surface-2); border-radius: 8px; padding: 12px; max-height: 260px; overflow: auto;
}

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 20;
  background: var(--side-bg); color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 13px;
  box-shadow: 0 10px 30px rgba(40, 45, 90, .3);
}

/* ---------- Login ---------- */
.login-page {
  display: grid; place-items: center; min-height: 100vh; padding: 16px;
  background: linear-gradient(160deg, #7fd3f7 0%, #6a8cf7 100%);
}
.login-card {
  background: var(--surface); border-radius: 16px; padding: 32px;
  width: min(360px, 100%); display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 20px; font-weight: 600; }
.login-card label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 12px; font-weight: 500; }

/* ---------- Narrow screens: sidebar becomes a top bar ---------- */
@media (max-width: 1100px) {
  .app { display: block; }
  .sidebar { position: static; height: auto; overflow: visible; }
  .brand { padding: 14px 16px; font-size: 16px; }
  .side-search { padding: 12px 16px 4px; }
  .sidebar-title, .sidebar-divider { display: none; }
  #sidebar { flex-direction: row; overflow-x: auto; gap: 6px; padding: 8px 16px 12px; scrollbar-width: none; }
  .coach {
    width: auto; flex: none; border-radius: 999px; padding: 5px 12px; background: var(--side-bg-2); border-left: 0;
  }
  .coach.active { background: var(--primary); }
  .coach.active .coach-count { color: #fff; }
  .coach-name { overflow: visible; }
  .side-foot { flex-direction: row; align-items: center; flex-wrap: wrap; padding: 0 16px 12px; margin: 0; }
  .tabs { justify-content: flex-start; padding: 0 8px; }
  .tab { padding: 16px 10px 13px; }
  .add-btn { width: 52px; }
  .main-body { padding: 18px 16px 40px; }
}

@media (max-width: 680px) {
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tbody tr {
    border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; padding: 12px 14px;
    position: relative; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px;
  }
  tbody tr.late { box-shadow: inset 3px 0 0 var(--danger); }
  tbody tr.late td:first-child { box-shadow: none; }
  td { border: 0; padding: 2px 0; width: auto; min-width: 0 !important; }
  td.client-cell { width: 100%; padding-right: 36px; }
  td.pic-cell { width: 100%; }
  td.created { order: 10; width: 100%; font-size: 12px; }
  td.payment-cell { order: 5; }
  td.note-cell { order: 8; width: 100%; max-width: none; }
  .note-empty { opacity: 1; }
  .note-btn { margin: 0 -8px; }
  .cell-label { display: inline; }
  .rel { display: inline; margin-left: 6px; }
  .row-actions { position: absolute; top: 8px; right: 6px; width: auto; }
  .inline-edit, .pic-inline, .inline-input, .deadline-btn { margin: 0 -8px; }
  .row2 { grid-template-columns: 1fr; }
}
