/* Customer Portal template — brand colors come from --brand / --accent (set per install on the admin Branding page). */
:root {
  --brand: #1f4fd8; --accent: #12b5a5;
  --bg: #f5f7fb; --surface: #fff; --text: #0f172a; --muted: #64748b; --line: #e5e9f2;
  --ok: #0f9d58; --warn: #d97706; --bad: #dc2626; --info: #2563eb;
  --radius: 14px; --shadow: 0 1px 2px rgba(15,23,42,.05), 0 6px 24px rgba(15,23,42,.06);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --brand-soft: color-mix(in srgb, var(--brand) 10%, white);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.55 var(--font); color: var(--text); background: var(--bg); min-height: 100vh; display: flex; flex-direction: column; }
a { color: var(--brand); text-decoration: none; } a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; line-height: 1.2; margin: 0 0 .25rem; letter-spacing: -.01em; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: #f1f4f9; padding: .1rem .35rem; border-radius: 6px; word-break: break-all; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .strong { font-weight: 600; } .text-warn { color: var(--warn); }
.icon { width: 18px; height: 18px; flex: none; vertical-align: -3px; }
.container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 1.5rem 1.25rem; flex: 1; }

/* Brand */
.brand-link { color: inherit; display: inline-flex; text-decoration: none !important; }
.brand { display: inline-flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.05rem; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; color: #fff; font-weight: 800;
  background: linear-gradient(135deg, var(--brand), var(--accent)); }
.brand-logo { max-height: 38px; max-width: 200px; display: block; }

/* Top bar (customer) */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar-inner { max-width: 1120px; margin: 0 auto; padding: .7rem 1.25rem; display: flex; align-items: center; gap: 1.5rem; }
.mainnav { display: flex; gap: .25rem; flex: 1; }
.mainnav a { display: inline-flex; align-items: center; gap: .45rem; padding: .5rem .8rem; border-radius: 10px; color: var(--muted); font-weight: 500; position: relative; }
.mainnav a:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.mainnav a.active { background: var(--brand-soft); color: var(--brand); }
.logout-form { display: flex; align-items: center; gap: .6rem; margin: 0; }
.who { color: var(--muted); font-size: .9rem; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { background: var(--bad); color: #fff; font-size: 11px; line-height: 18px; min-width: 18px; padding: 0 5px; border-radius: 9px; text-align: center; font-weight: 700; }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 1.25rem; }
.card-title { margin: 0 0 .9rem; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; }
.card-head .card-title { margin-bottom: .9rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin: .5rem 0 1.25rem; }
.page-head p { margin: 0; }
.head-actions, .btn-row { display: flex; gap: .5rem; flex-wrap: wrap; }
.crumbs { margin: 0 0 .25rem; font-size: .9rem; }
.grid-hero { display: grid; grid-template-columns: 1.1fr 1fr; gap: 1.25rem; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; align-items: start; }
.invoice-layout { display: grid; grid-template-columns: 1fr 320px; gap: 1.25rem; align-items: start; }
.messages-layout { display: grid; grid-template-columns: 1fr 360px; gap: 1.25rem; align-items: start; }
.narrow-card { max-width: 520px; }

/* Balance */
.balance-card { background: linear-gradient(150deg, var(--brand), color-mix(in srgb, var(--brand) 55%, var(--accent))); color: #fff; border: 0; position: relative; overflow: hidden; }
.balance-card::after { content: ""; position: absolute; right: -60px; top: -60px; width: 220px; height: 220px; border-radius: 50%; background: rgba(255,255,255,.08); }
.balance-card .muted, .balance-card .eyebrow { color: rgba(255,255,255,.8); }
.eyebrow { text-transform: uppercase; letter-spacing: .08em; font-size: .72rem; font-weight: 700; color: var(--muted); }
.big-amount { font-size: clamp(2.2rem, 6vw, 3rem); font-weight: 800; letter-spacing: -.02em; line-height: 1.1; margin: .35rem 0 .6rem; }
.balance-actions { display: flex; gap: .6rem; flex-wrap: wrap; margin: 1rem 0 .5rem; align-items: flex-start; position: relative; z-index: 1; }
.balance-card .btn-primary { background: #fff; color: var(--brand); }
.balance-card .btn-secondary { background: rgba(255,255,255,.14); color: #fff; border-color: rgba(255,255,255,.35); }
.balance-card .pay-placeholder small { color: rgba(255,255,255,.8); }
.as-of { font-size: .82rem; margin: .5rem 0 0; }
.pill { display: inline-block; padding: .2rem .7rem; border-radius: 999px; font-size: .82rem; font-weight: 600; margin: 0; }
.pill-warn { background: #fff4e5; color: #9a4b00; } .pill-ok { background: rgba(255,255,255,.18); color: #fff; } .pill-info { background: var(--brand-soft); color: var(--brand); }

.aging { list-style: none; margin: 0; padding: 0; display: grid; gap: .8rem; }
.aging li { display: grid; grid-template-columns: 90px 1fr auto; gap: .75rem; align-items: center; }
.aging-label { color: var(--muted); font-size: .9rem; }
.aging-bar { height: 10px; background: #eef1f7; border-radius: 6px; overflow: hidden; }
.aging-fill { display: block; height: 100%; background: var(--accent); border-radius: 6px; min-width: 2px; }
.aging-fill.late { background: var(--warn); }
.aging-amt { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; min-width: 90px; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -1.25rem; padding: 0 1.25rem; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; padding: .6rem .5rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: .8rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafbfe; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { color: var(--muted); padding: 1rem 0; margin: 0; }

.badge { display: inline-block; padding: .15rem .6rem; border-radius: 999px; font-size: .75rem; font-weight: 600; white-space: nowrap; }
.badge-open { background: #e8f0fe; color: #1d4ed8; } .badge-partial { background: #fff4e5; color: #9a4b00; }
.badge-paid { background: #e6f6ee; color: #08743f; } .badge-void { background: #eef0f3; color: #556070; text-decoration: line-through; }
.badge-overdue { background: #fdecec; color: #b42318; }
.badge-void-plain { background: #eef0f3; color: #556070; }
.row-inactive td { opacity: .6; }
h1 .badge { font-size: .8rem; }

/* Invoice totals */
.totals { display: grid; grid-template-columns: 1fr auto; gap: .55rem 1rem; margin: 0; }
.totals dt { color: var(--muted); } .totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.totals .due { font-size: 1.2rem; font-weight: 800; color: var(--text); border-top: 1px solid var(--line); padding-top: .7rem; }
.pay-box { margin-top: 1.1rem; }
.pay-box .btn { width: 100%; }
.memo { margin-top: 1rem; padding-top: 1rem; border-top: 1px dashed var(--line); } .memo p { margin: .3rem 0 0; }
.pay-placeholder { display: inline-flex; flex-direction: column; gap: .25rem; }
.pay-placeholder small { font-size: .78rem; }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .45rem; padding: .62rem 1.05rem; border-radius: 10px; border: 1px solid transparent;
  font: 600 .93rem/1.2 var(--font); cursor: pointer; text-decoration: none !important; transition: filter .15s, background .15s; white-space: nowrap; }
.btn-primary { background: var(--brand); color: #fff; } .btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--line); } .btn-secondary:hover { background: var(--bg); }
.btn-ghost { background: transparent; color: var(--muted); } .btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: #fff; color: var(--bad); border-color: #f3c2c2; } .btn-danger:hover { background: #fdecec; }
.btn[disabled] { opacity: .55; cursor: not-allowed; filter: none; }
.btn-sm { padding: .4rem .7rem; font-size: .85rem; } .btn-block { width: 100%; }
.form { display: grid; gap: .95rem; }
.form label { display: grid; gap: .35rem; font-weight: 600; font-size: .88rem; }
.form label.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
input[type=text], input[type=email], input[type=password], input[type=search], input:not([type]), select, textarea {
  width: 100%; padding: .65rem .8rem; border: 1px solid #d5dbe7; border-radius: 10px; font: 400 15px/1.4 var(--font); background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 30%, transparent); outline-offset: 1px; border-color: var(--brand); }
textarea { resize: vertical; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .95rem 1.25rem; }
.color-row { display: flex; align-items: center; gap: .6rem; } input[type=color] { width: 48px; height: 38px; border: 1px solid #d5dbe7; border-radius: 8px; padding: 2px; background: #fff; }
.logo-field { border: 1px dashed var(--line); border-radius: 12px; padding: 1rem; display: grid; gap: .75rem; }
.logo-field legend { font-weight: 700; padding: 0 .4rem; }
.logo-preview { max-height: 64px; max-width: 260px; background: #f8fafc; padding: .5rem; border-radius: 8px; border: 1px solid var(--line); }
.inline-form { display: flex; gap: .75rem; align-items: flex-end; flex-wrap: wrap; margin-top: 1rem; }
.inline-form label { display: grid; gap: .3rem; font-size: .88rem; font-weight: 600; }
.search { display: flex; gap: .5rem; } .search input { min-width: 260px; }
.segmented { display: inline-flex; background: #eceff5; border-radius: 10px; padding: 3px; }
.segmented a { padding: .4rem .9rem; border-radius: 8px; color: var(--muted); font-weight: 600; font-size: .9rem; }
.segmented a.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); text-decoration: none; }

/* Alerts */
.alert { padding: .8rem 1rem; border-radius: 10px; margin-bottom: 1rem; border: 1px solid; font-size: .93rem; }
.alert-success { background: #e9f8f0; border-color: #b7e6cb; color: #0a5c33; }
.alert-error { background: #fdeeee; border-color: #f4c7c7; color: #8f1d1d; }
.alert-info { background: #eef4ff; border-color: #cfdcfb; color: #1e3a8a; }

/* Key/values */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .55rem 1.25rem; margin: 0 0 .75rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; overflow-wrap: anywhere; }
.secret-card { border: 1px solid #f4d48a; background: #fffbeb; }
.secret-card code { background: #fff; border: 1px solid #f4d48a; display: inline-block; padding: .3rem .5rem; cursor: copy; }
code.copied { outline: 2px solid var(--ok); }

/* Messages */
.thread { display: flex; flex-direction: column; gap: .8rem; max-height: 70vh; overflow-y: auto; }
.bubble { max-width: 82%; padding: .75rem .95rem; border-radius: 14px; background: #f1f4f9; }
.bubble.mine { align-self: flex-end; background: var(--brand-soft); border-bottom-right-radius: 4px; }
.bubble.theirs { align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble header { font-size: .8rem; color: var(--muted); margin-bottom: .25rem; }
.bubble header strong { color: var(--text); }
.bubble-subject { font-weight: 700; margin-bottom: .2rem; }
.bubble-body { white-space: normal; overflow-wrap: anywhere; }
.bubble-ref { display: inline-block; margin-top: .4rem; font-size: .8rem; font-weight: 600; }
.msg-preview, .thread-list { list-style: none; margin: 0; padding: 0; }
.msg-preview li { display: flex; gap: .6rem; align-items: baseline; padding: .65rem 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.msg-preview li:last-child { border-bottom: 0; }
.msg-preview li.unread strong::before, .thread-list li.unread strong::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--brand); margin-right: .4rem; vertical-align: 1px; }
.msg-preview time { margin-left: auto; font-size: .82rem; }
.who-tag { font-size: .72rem; background: #eef1f7; padding: .1rem .45rem; border-radius: 6px; color: var(--muted); font-weight: 600; }
.thread-list a { display: flex; gap: .6rem; padding: .8rem .25rem; border-bottom: 1px solid var(--line); color: var(--text); align-items: center; }
.thread-list a:hover { background: #fafbfe; text-decoration: none; }
.thread-list .right { margin-left: auto; display: flex; gap: .5rem; align-items: center; }

/* Stats (admin) */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); display: grid; gap: .15rem; }
.stat span { color: var(--muted); font-size: .85rem; } .stat b { font-size: 1.6rem; letter-spacing: -.01em; } .stat small { color: var(--muted); }

/* Admin shell */
.admin-shell { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; flex: 1; }
.sidebar { background: #0f172a; color: #cbd5e1; padding: 1.25rem 1rem; display: flex; flex-direction: column; gap: .5rem; position: sticky; top: 0; height: 100vh; }
.sidebar .brand { color: #fff; } .sidebar-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .1em; color: #64748b; margin: .75rem .5rem .25rem; }
.sidebar nav { display: grid; gap: .15rem; }
.sidebar nav a { display: flex; align-items: center; gap: .6rem; padding: .55rem .7rem; border-radius: 9px; color: #cbd5e1; font-weight: 500; }
.sidebar nav a:hover { background: rgba(255,255,255,.06); text-decoration: none; }
.sidebar nav a.active { background: rgba(255,255,255,.1); color: #fff; }
.sidebar nav a .dot { margin-left: auto; }
.sidebar-foot { margin-top: auto; display: grid; gap: .4rem; }
.sidebar-foot .who { color: #94a3b8; } .sidebar-foot .btn-ghost { color: #cbd5e1; justify-content: flex-start; }
.admin-main { padding: 1.5rem 2rem; max-width: 1200px; width: 100%; }
.mono-sm td { font-size: .85rem; }

/* Auth pages */
body.auth { background: radial-gradient(1200px 500px at 50% -10%, var(--brand-soft), var(--bg)); }
.auth-wrap { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2.5rem 1.25rem; gap: 1rem; }
.auth-brand { text-align: center; } .auth-brand .brand { font-size: 1.25rem; } .tagline { color: var(--muted); margin: .4rem 0 0; }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); padding: 1.75rem; }
.auth-card h1 { font-size: 1.35rem; }
.auth-card > .muted { margin-top: 0; }
.auth-links { display: flex; flex-direction: column; gap: .5rem; align-items: center; margin-top: 1.25rem; font-size: .92rem; }
.auth .alert { width: 100%; max-width: 420px; }

.footer { border-top: 1px solid var(--line); background: var(--surface); font-size: .85rem; color: var(--muted); }
.admin .footer { display: none; }
.footer-inner { display: flex; gap: 1.25rem; flex-wrap: wrap; padding-top: 1rem; padding-bottom: 1rem; flex: none; }
.auth .footer { background: transparent; border: 0; } .auth .footer-inner { justify-content: center; }

/* Responsive */
@media (max-width: 960px) {
  .grid-hero, .two-col, .invoice-layout, .messages-layout { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .admin-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: .75rem 1rem; }
  .sidebar-label { display: none; }
  .sidebar nav { display: flex; overflow-x: auto; width: 100%; order: 3; gap: .25rem; }
  .sidebar nav a { white-space: nowrap; }
  .sidebar-foot { margin: 0 0 0 auto; display: flex; align-items: center; }
  .sidebar-foot .who { display: none; }
  .admin-main { padding: 1.25rem; }
}
@media (max-width: 720px) {
  body.app { padding-bottom: 72px; }
  .topbar-inner { gap: .75rem; }
  .mainnav { position: fixed; bottom: 0; left: 0; right: 0; background: var(--surface); border-top: 1px solid var(--line); justify-content: space-around;
    padding: .35rem .25rem calc(.35rem + env(safe-area-inset-bottom)); z-index: 30; }
  .mainnav a { flex-direction: column; gap: .15rem; font-size: .72rem; padding: .35rem .6rem; }
  .mainnav a .icon { width: 22px; height: 22px; }
  .mainnav a .dot { position: absolute; top: 0; right: 6px; }
  .logout-form { margin-left: auto; } .who, .hide-sm { display: none; }
  .container { padding: 1rem; }
  h1 { font-size: 1.35rem; }
  .form-grid { grid-template-columns: 1fr; }
  .search input { min-width: 0; }
  /* Stack table rows into cards */
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { border-bottom: 1px solid var(--line); padding: .6rem 0; }
  .table td { border: 0; padding: .2rem 0; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  .table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: .82rem; text-align: left; }
  .table td.num { text-align: right; }
  .lines-table td:first-child { display: block; text-align: left; font-weight: 600; } .lines-table td:first-child::before { content: none; }
  .bubble { max-width: 94%; }
  .aging li { grid-template-columns: 80px 1fr auto; } .aging-amt { min-width: 0; }
  .stats { grid-template-columns: 1fr 1fr; gap: .75rem; } .stat b { font-size: 1.25rem; }
}
