:root {
  --purple: #4A365B; --purple-2: #5a2d70; --gold: #FDB813;
  --bg: #f6f5f8; --card: #fff; --ink: #1f1a24; --muted: #6b6474; --line: #e6e2ea;
  --red: #c0392b; --red-bg: #fdecea; --orange: #b85c00; --orange-bg: #fff1e0;
  --yellow-bg: #fff8d6; --green: #1e7e45; --green-bg: #e6f5ec; --blue: #1f5fa8; --blue-bg: #e8f0fb;
  --grey-bg: #efedf2; --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px; color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--purple-2); }
button, input, select, textarea { font: inherit; }
.boot { padding: 40px; text-align: center; color: var(--muted); }

/* header */
header.top { background: var(--purple); color: #fff; border-bottom: 3px solid var(--gold); position: sticky; top: 0; z-index: 20; }
.top-inner { display: flex; align-items: center; gap: 18px; padding: 10px 20px; max-width: 1400px; margin: 0 auto; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; white-space: nowrap; text-decoration: none; color: #fff; }
.brand img { height: 40px; display: block; margin: -4px 0; }
.brand span { color: var(--gold); border-left: 1px solid rgba(255,255,255,.3); padding-left: 10px; }
nav.tabs { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
nav.tabs a { color: #e9e0ef; text-decoration: none; padding: 7px 11px; border-radius: 7px; }
nav.tabs a.on, nav.tabs a:hover { background: rgba(255,255,255,.14); color: #fff; }
.syncbox { display: flex; align-items: center; gap: 8px; font-size: 12px; color: #e9e0ef; }
.syncbox .dot { width: 8px; height: 8px; border-radius: 50%; background: #8fd19e; display: inline-block; }
.syncbox .dot.bad { background: #ff8a80; }

main { max-width: 1400px; margin: 0 auto; padding: 20px; }
h1 { font-size: 22px; margin: 4px 0 16px; }
h2 { font-size: 16px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

/* cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.card .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.card .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }

/* tables */
.tablewrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #faf9fb; position: sticky; top: 0; white-space: nowrap; cursor: default; }
th.sort { cursor: pointer; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #fbf8fd; }
tr:last-child td { border-bottom: 0; }
.desc { max-width: 380px; color: var(--muted); font-size: 12px; }

/* pills */
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap; background: var(--grey-bg); color: var(--ink); }
.s1 { background: var(--blue-bg); color: var(--blue); }
.s2 { background: var(--yellow-bg); color: #7a5b00; }
.s3 { background: var(--orange-bg); color: var(--orange); }
.s4 { background: var(--red-bg); color: var(--red); }
.s5 { background: var(--grey-bg); color: var(--muted); }
.st-promised, .fl-promised { background: #e0f4f3; color: #0f6b66; }
.st-says_paid, .fl-says_paid { background: var(--green-bg); color: var(--green); }
.st-disputed, .fl-disputed { background: var(--orange-bg); color: var(--orange); }
.st-chasing { background: var(--blue-bg); color: var(--blue); }
.st-on_hold { background: var(--red-bg); color: var(--red); }
.st-write_off_review, .st-written_off, .fl-write_off { background: var(--grey-bg); color: var(--muted); }
.good { color: var(--green); } .bad { color: var(--red); } .warn { color: var(--orange); }

/* forms */
input[type=text], input[type=password], input[type=date], input[type=number], input[type=search], select, textarea {
  border: 1px solid #cfc8d6; border-radius: 7px; padding: 7px 9px; background: #fff; min-width: 0; }
textarea { width: 100%; min-height: 80px; resize: vertical; }
label.f { display: block; margin-bottom: 10px; }
label.f > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
label.f input, label.f select { width: 100%; }
.btn { border: 1px solid #cfc8d6; background: #fff; border-radius: 7px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--purple-2); }
.btn.primary { background: var(--purple); color: #fff; border-color: var(--purple); }
.btn.primary:hover { background: var(--purple-2); }
.btn.gold { background: var(--gold); border-color: var(--gold); color: #2b1a00; font-weight: 600; }
.btn.danger { color: var(--red); }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }

/* login */
.login { max-width: 360px; margin: 10vh auto; background: #fff; border-radius: 12px; border: 1px solid var(--line); overflow: hidden; }
.login .head { background: var(--purple); color: #fff; padding: 14px 22px 16px; border-bottom: 3px solid var(--gold); }
.login .head img { width: 100%; max-width: 260px; display: block; margin: 0 auto 6px; }
.login .head b { font-size: 18px; } .login .head div { color: #e9e0ef; font-size: 12px; margin-top: 2px; }
.login form { padding: 22px; }
.err { color: var(--red); font-size: 13px; margin: 6px 0; min-height: 1em; }

/* client page */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.kv div b { display: block; font-size: 16px; }
.kv div span { font-size: 12px; color: var(--muted); }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.timeline li:last-child { border-bottom: 0; }
.timeline .meta { font-size: 12px; color: var(--muted); }

/* modal + toast */
.modal-back { position: fixed; inset: 0; background: rgba(20,10,30,.45); display: flex; align-items: flex-start; justify-content: center; z-index: 50; padding: 6vh 12px; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal h2 { margin-bottom: 14px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
pre.email { white-space: pre-wrap; background: #faf9fb; border: 1px solid var(--line); border-radius: 8px; padding: 12px; font-family: inherit; max-height: 50vh; overflow: auto; }
.notice { background: var(--yellow-bg); border: 1px solid #f0dc8a; border-radius: 8px; padding: 10px 12px; margin-bottom: 14px; }
.qbo-notes { background: var(--blue-bg); border: 1px solid #c9dbf3; border-radius: 8px; padding: 10px 12px; margin: 12px 0 0; font-size: 13px; }
.qbo-notes b { display: block; font-size: 12px; color: var(--blue); margin-bottom: 4px; }
.qnote { font-size: 12px; color: var(--blue); margin-top: 3px; white-space: pre-wrap; }

/* dashboard */
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
@media (max-width: 1100px) { .grid3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 760px) { .grid3 { grid-template-columns: 1fr; } }
.cards { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .cards { grid-template-columns: repeat(2, 1fr); } }
.chartrow { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.donut-wrap { position: relative; width: 180px; height: 180px; flex: none; }
.donut-wrap svg path { cursor: default; transition: opacity .15s; }
.donut-wrap svg path:hover { opacity: .85; }
.donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; text-align: center; }
.donut-center b { font-size: 18px; } .donut-center span { font-size: 11px; color: var(--muted); }
.legend { list-style: none; margin: 0; padding: 0; flex: 1; min-width: 150px; font-size: 12px; }
.legend li { display: grid; grid-template-columns: 12px 1fr auto auto; gap: 6px; align-items: center; padding: 3px 0; }
.legend i, .sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.sw { margin: 0 4px -1px 0; }
.legend b { font-weight: 600; font-variant-numeric: tabular-nums; } .legend em { font-style: normal; color: var(--muted); width: 32px; text-align: right; }
.hbars { display: flex; flex-direction: column; gap: 6px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; gap: 8px; align-items: center; font-size: 12px; }
.hbar.click { cursor: pointer; } .hbar.click:hover .hl { text-decoration: underline; }
.hbar .hl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .ht { background: #f1eff4; border-radius: 4px; height: 14px; overflow: hidden; }
.hbar .hf { display: block; height: 100%; border-radius: 0 4px 4px 0; }
.hbar .hv { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 64px; text-align: right; }
.agestack { display: flex; height: 12px; border-radius: 4px; overflow: hidden; gap: 2px; background: #fff; }
.agestack span { display: block; height: 100%; }
svg.vbars { width: 100%; height: 150px; display: block; }
svg.vbars text { font-size: 10px; fill: var(--muted); }
#viz-tip { position: fixed; z-index: 80; background: #1f1a24; color: #fff; font-size: 12px; padding: 6px 9px; border-radius: 6px; pointer-events: none; opacity: 0; transition: opacity .1s; max-width: 260px; line-height: 1.4; }

/* ---------- phones ---------- */
@media (max-width: 760px) {
  body { font-size: 15px; -webkit-text-size-adjust: 100%; }
  .m-hide { display: none; }
  header.top { padding-top: env(safe-area-inset-top); }
  .top-inner { padding: 8px 12px; gap: 8px; }
  .brand img { height: 32px; }
  .syncbox { margin-left: auto; }
  nav.tabs { order: 3; flex: 1 1 100%; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; margin: 0 -12px; padding: 0 12px 2px; }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs a { white-space: nowrap; padding: 8px 12px; }
  main { padding: 12px 12px calc(24px + env(safe-area-inset-bottom)); }
  h1 { font-size: 20px; }
  .panel { padding: 12px; }
  .cards { gap: 8px; }
  .card { padding: 10px 12px; } .card .value { font-size: 19px; }
  .grid2, .grid3 { grid-template-columns: 1fr; gap: 12px; }
  .filters { gap: 6px; }
  .filters > * { flex: 1 1 calc(50% - 6px); min-width: 0; }
  .filters input[type=search] { flex-basis: 100%; }
  .filters .spacer { display: none; }
  input[type=text], input[type=password], input[type=date], input[type=search], select, textarea { font-size: 16px; padding: 9px 10px; } /* 16px stops iPhone zooming */
  .btn { padding: 10px 14px; } .btn.small { padding: 7px 10px; font-size: 13px; }
  .actions .btn { flex: 1 1 calc(50% - 8px); }
  .row > label.small { flex: 1 1 100%; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
  .kv { grid-template-columns: 1fr 1fr; }
  .chartrow { flex-direction: column; align-items: stretch; } .donut-wrap { margin: 0 auto; }
  .hbar { grid-template-columns: minmax(80px, 40%) 1fr auto; }
  .modal-back { padding: 0; align-items: stretch; }
  .modal { max-width: none; min-height: 100%; border-radius: 0; padding: 16px 14px calc(16px + env(safe-area-inset-bottom)); }
  .modal .foot { position: sticky; bottom: 0; background: #fff; padding-top: 10px; flex-wrap: wrap; }
  .modal .foot .btn { flex: 1 1 auto; }
  .login { margin: 6vh 12px; }

  /* tables become cards */
  .tablewrap { background: none; border: 0; overflow: visible; }
  table.cardify, table.cardify tbody, table.cardify tr, table.cardify td { display: block; width: 100%; }
  table.cardify thead { display: none; }
  table.cardify tr { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin-bottom: 8px; }
  .panel table.cardify tr { border-color: var(--line); }
  table.cardify td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; text-align: right; }
  table.cardify td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; flex: none; padding-top: 2px; }
  table.cardify td[data-label=""]::before, table.cardify td:not([data-label])::before { content: none; }
  table.cardify td:first-child { display: block; text-align: left; font-size: 15px; padding-bottom: 6px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
  table.cardify td:first-child::before { content: none; }
  table.cardify td.desc { max-width: none; text-align: left; display: block; }
  table.cardify td.desc::before { display: block; margin-bottom: 2px; }
  table.cardify td .agestack { width: 55%; margin-top: 4px; }
  table.cardify td:empty { display: none; }
  tr.click:hover td { background: none; }
  tr.click:active { background: #f3eff7; }
}
@media (max-width: 760px) {
  table.cardify td:first-child:has(> input[type=checkbox]) { display: flex; justify-content: flex-end; border-bottom: 0; margin: 0 0 -28px; padding: 0; position: relative; z-index: 1; }
  table.cardify td:first-child:has(> input[type=checkbox]) input { width: 22px; height: 22px; }
  table.cardify td:first-child:has(> input[type=checkbox]) + td { display: block; text-align: left; font-size: 15px; border-bottom: 1px solid var(--line); padding-bottom: 6px; margin-bottom: 4px; }
  table.cardify td:first-child:has(> input[type=checkbox]) + td::before { content: none; }
}
@media (max-width: 760px) {
  table.cardify td { display: grid; grid-template-columns: auto 1fr; column-gap: 12px; }
  table.cardify td::before { grid-column: 1; grid-row: 1 / span 6; }
  table.cardify td > * { grid-column: 2; justify-self: end; text-align: right; }
  table.cardify td:first-child, table.cardify td.desc { display: block; }
  table.cardify td:first-child > *, table.cardify td.desc > * { text-align: left; }
}
@media (max-width: 760px) {
  table.cardify td { row-gap: 0; gap: 0 12px; }
  .cards { grid-template-columns: 1fr 1fr; }
}
.qnote { text-align: left !important; justify-self: start !important; }
@media (max-width: 760px) { table.cardify td.desc .qnote { display: block; } }

/* WhatsApp + phone card payments */
.btn.wa { background: #25D366; border-color: #1ebe5a; color: #fff; }
.btn.wa:hover { background: #1ebe5a; }
.nowrap { white-space: nowrap; }
.blink-card input, .blink-card select { max-width: 100%; }
.blink-card #cardfields { margin: 6px 0 4px; min-height: 120px; }
@media (max-width: 760px) { .nowrap { white-space: normal; } .nowrap .btn { margin: 2px 0; } }
.pickrow { display: flex; justify-content: space-between; gap: 10px; padding: 9px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.pickrow:hover { background: #f6f2fa; }
.pickrow:last-child { border-bottom: 0; }
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--purple); text-decoration: underline; }
th.sortable .arr { font-size: 11px; }
