/* Andy's Tiling — glass theme. Dark is the default; [data-theme=light] switches to light. */
:root {
  color-scheme: dark;
  --accent: #38b6d9; --accent-2: #7c6cf2; --link: #7fd3ee;
  --ink: #eef3f7; --muted: #a9b8c6; --line: rgba(255,255,255,.11); --line-hi: rgba(255,255,255,.22);
  --bg: #0a0f14;
  --glass: rgba(255,255,255,.07); --glass-hi: rgba(255,255,255,.12); --glass-solid: rgba(24,32,42,.78); --edge: rgba(255,255,255,.14);
  --field: rgba(0,0,0,.25); --hover: rgba(255,255,255,.05); --overlay: rgba(3,6,9,.6);
  --on-bg: #eef3f7; --on-fg: #0a0f14; --shadow: 0 8px 32px rgba(0,0,0,.35);
  --blob-1: rgba(56,182,217,.45); --blob-2: rgba(124,108,242,.38); --blob-3: rgba(229,165,75,.20);
  --ok: #6ee7a0; --ok-bg: rgba(110,231,160,.12); --bad: #ff7676; --bad-bg: rgba(255,118,118,.12);
  --warn: #ffc56b; --warn-bg: rgba(255,197,107,.12); --info: #7fc4ff; --info-bg: rgba(127,196,255,.12);
  --draft-bg: rgba(255,255,255,.08); --expense: #e5a54b;
  --radius: 16px; --radius-sm: 10px; --blur: blur(18px) saturate(140%);
}
:root[data-theme=light] {
  color-scheme: light;
  --accent: #1d87ab; --accent-2: #6a5ae0; --link: #16708f;
  --ink: #17212b; --muted: #5f6e7c; --line: rgba(20,40,60,.10); --line-hi: rgba(20,40,60,.22);
  --bg: #eef3f7;
  --glass: rgba(255,255,255,.55); --glass-hi: rgba(255,255,255,.8); --glass-solid: rgba(255,255,255,.85); --edge: rgba(255,255,255,.9);
  --field: rgba(255,255,255,.7); --hover: rgba(20,40,60,.04); --overlay: rgba(20,30,40,.35);
  --on-bg: #17212b; --on-fg: #fff; --shadow: 0 8px 30px rgba(30,50,70,.12);
  --blob-1: rgba(56,182,217,.28); --blob-2: rgba(124,108,242,.20); --blob-3: rgba(229,165,75,.18);
  --ok: #1f8a4c; --ok-bg: rgba(31,138,76,.12); --bad: #c62828; --bad-bg: rgba(198,40,40,.10);
  --warn: #a15c00; --warn-bg: rgba(161,92,0,.10); --info: #1f5fa8; --info-bg: rgba(31,95,168,.10);
  --draft-bg: rgba(20,40,60,.07);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: var(--bg); min-height: 100vh; -webkit-font-smoothing: antialiased;
}
/* soft colour blobs behind the glass */
body::before {
  content: ""; position: fixed; inset: -20vmax; z-index: -1; pointer-events: none;
  background:
    radial-gradient(34vmax 34vmax at 12% 8%, var(--blob-1), transparent 62%),
    radial-gradient(32vmax 32vmax at 92% 28%, var(--blob-2), transparent 62%),
    radial-gradient(30vmax 30vmax at 30% 75%, var(--blob-2), transparent 65%),
    radial-gradient(34vmax 34vmax at 80% 105%, var(--blob-3), transparent 62%);
  filter: blur(20px);
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; font-weight: 700; letter-spacing: -.01em; margin: 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 15px; font-weight: 650; margin: 0 0 12px; letter-spacing: .01em; }
small, .muted { color: var(--muted); }
.pre { white-space: pre-line; }
.hidden { display: none !important; }

/* glass surface */
.card, .stat, .side, .mobile-nav, .modal, .toast {
  background: var(--glass); border: 1px solid var(--line);
  -webkit-backdrop-filter: var(--blur); backdrop-filter: var(--blur);
  box-shadow: var(--shadow), inset 0 1px 0 var(--edge);
}

/* ---------- layout ---------- */
.shell { display: flex; min-height: 100vh; }
.side {
  width: 232px; flex-shrink: 0; position: sticky; top: 14px; height: calc(100vh - 28px);
  margin: 14px 0 14px 14px; border-radius: var(--radius); padding: 18px 12px; display: flex; flex-direction: column;
}
.side .brand { font-weight: 750; font-size: 16px; padding: 4px 10px 20px; display: flex; gap: 10px; align-items: center; }
.side .brand i { font-style: normal; width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-size: 15px; flex-shrink: 0; }
.side a { display: flex; gap: 12px; align-items: center; color: var(--muted); padding: 10px 12px; border-radius: 10px; margin-bottom: 2px; font-weight: 500; }
.side a:hover { color: var(--ink); text-decoration: none; }
.side a:not(.active):hover { background: var(--hover); }
.side a.active { background: var(--glass-hi); color: var(--ink); box-shadow: inset 3px 0 0 var(--accent); }
.side .spacer { flex: 1; }
.side .spacer + a { border-top: 1px solid var(--line); border-radius: 0 0 10px 10px; padding-top: 14px; }
.content { flex: 1; min-width: 0; padding: 28px 28px 40px; max-width: 1080px; margin: 0 auto; }
.topbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; min-height: 40px; }
.topbar h1 { margin-right: auto; }
.mobile-nav { display: none; }

@media (max-width: 760px) {
  .side { display: none; }
  .content { padding: 18px 14px 120px; scroll-padding-bottom: 120px; }
  h1 { font-size: 21px; }
  .topbar { margin-bottom: 14px; }
  .mobile-nav {
    display: flex; position: fixed; left: 14px; right: 14px; bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 20; border-radius: 22px; padding: 6px; background: var(--glass-solid);
  }
  .mobile-nav a { flex: 1 1 0; min-width: 0; text-align: center; color: var(--muted); font-size: 10.5px; font-weight: 600; padding: 7px 2px 6px; border-radius: 16px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .mobile-nav { gap: 2px; background: color-mix(in srgb, var(--bg) 88%, transparent); }
  .mobile-nav a:hover { text-decoration: none; }
  .mobile-nav a span { display: block; font-size: 19px; line-height: 1.3; }
  .mobile-nav a.active { color: var(--ink); background: var(--glass-hi); }
}

/* ---------- components ---------- */
.card { border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.card.flush { padding: 0; overflow-x: auto; }
.card.flush > h2 { padding: 18px 20px 4px; margin: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 0 14px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat { border-radius: var(--radius); padding: 16px 18px; color: inherit; display: block; transition: transform .15s, border-color .15s; }
a.stat:hover { text-decoration: none; transform: translateY(-2px); border-color: var(--line-hi); }
.stat small { font-size: 12.5px; }
.stat b { display: block; font-size: 24px; font-weight: 700; letter-spacing: -.01em; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.stat.bad b { color: var(--bad); }
.stat.bad { border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.stat.good:not(.zero) b { color: var(--ok); }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.cols > .card { margin-bottom: 0; }

/* document page */
.back { display: inline-block; color: var(--muted); font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.doc-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.doc-actions .main, .doc-actions .more { display: flex; flex-wrap: wrap; gap: 8px; }
.linkrow { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.linkrow code { flex: 1 1 200px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 9px 12px; border-radius: var(--radius-sm); background: var(--field); border: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.danger-row { justify-content: flex-end; padding-top: 4px; }
.mini td { padding: 10px 6px; }
.mini td:first-child { padding-left: 0; white-space: nowrap; }
.mini td:last-child { padding-right: 0; }
.mini td.num { width: 1%; }
td.nw, th.nw { white-space: nowrap; }
.wrap-any { overflow-wrap: anywhere; }

/* list tables become cards on phones (.rt) — slots: a=top-left, b=top-right, c=middle-left, d=middle-right, e/f=bottom */
.only-m { display: none; }
@media (max-width: 640px) {
  .rt thead { display: none; }
  .rt, .rt tbody { display: block; }
  .rt tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 12px; row-gap: 2px; padding: 13px 16px; border-bottom: 1px solid var(--line); }
  .rt tr:last-child { border-bottom: 0; }
  .rt td { padding: 0 !important; border: 0; text-align: left; min-width: 0; }
  .rt td.hide-m { display: none; }
  .rt .only-m { display: inline; }
  .rt td.only-m { display: block; }
  .rt .m-a { grid-area: 1 / 1; font-weight: 600; }
  .rt .m-b { grid-area: 1 / 2; text-align: right; white-space: nowrap; }
  .rt .m-c { grid-area: 2 / 1; }
  .rt .m-d { grid-area: 2 / 2; text-align: right; font-weight: 700; white-space: nowrap; }
  .rt .m-e { grid-area: 3 / 1 / 4 / 3; color: var(--muted); font-size: 13px; }
  .rt .m-f { grid-area: 4 / 1 / 5 / 3; color: var(--muted); font-size: 13px; }
  .rt td small { font-weight: 400; }
  .rt tr.click:hover td { background: none; }
  .rt tr.click:active { background: var(--hover); }
}
}
.totals-mini { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin: 12px 0; font-variant-numeric: tabular-nums; }
.totals-mini b { text-align: right; }
@media (max-width: 760px) {
  .doc-actions { display: grid; gap: 8px; }
  .doc-actions .main { display: grid; grid-template-columns: 1fr; gap: 8px; }
  .doc-actions .main:has(> :nth-child(2)) { grid-template-columns: 1fr 1fr; }
  .doc-actions .more { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .doc-actions button, .doc-actions .btn { justify-content: center; padding: 11px 10px; }
  .danger-row { display: grid; grid-template-columns: 1fr 1fr; }
  .danger-row button { justify-content: center; padding: 11px 10px; }
  .danger-row:has(> :only-child) { grid-template-columns: 1fr; }
}
@media (max-width: 900px) { .cols { grid-template-columns: 1fr; } }
@media (max-width: 420px) { .stats { grid-template-columns: 1fr 1fr; gap: 10px; } .stat { padding: 13px 14px; } .stat b { font-size: 19px; } }

button, .btn {
  font: inherit; font-weight: 550; padding: 9px 15px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--glass); color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap; transition: background .15s, border-color .15s, transform .1s;
}
button:hover, .btn:hover { border-color: var(--line-hi); background: var(--glass-hi); text-decoration: none; }
button:active, .btn:active { transform: scale(.98); }
button.primary, .btn.primary {
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 55%, var(--accent-2)));
  border-color: transparent; color: #fff; box-shadow: 0 4px 16px color-mix(in srgb, var(--accent) 35%, transparent);
}
button.primary:hover, .btn.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); }
button.small { padding: 5px 11px; font-size: 13px; border-radius: 10px; }
button:disabled { opacity: .5; cursor: default; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

label { display: block; font-size: 12.5px; font-weight: 550; color: var(--muted); margin: 12px 0 5px; }
input, select, textarea {
  font: inherit; width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--field); color: var(--ink); transition: border-color .15s, box-shadow .15s;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .7; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
select option { background: var(--bg); color: var(--ink); }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
input[type=color] { padding: 3px; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--hover); }
.card.flush table th:first-child, .card.flush table td:first-child { padding-left: 20px; }
.card.flush table th:last-child, .card.flush table td:last-child { padding-right: 20px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 650; text-transform: capitalize; white-space: nowrap; vertical-align: middle; }
.b-draft { background: var(--draft-bg); color: var(--muted); }
.b-sent { background: var(--info-bg); color: var(--info); }
.b-part-paid { background: var(--warn-bg); color: var(--warn); }
.b-paid, .b-accepted, .b-invoiced { background: var(--ok-bg); color: var(--ok); }
.b-overdue { background: var(--bad); color: #fff; }
.b-void, .b-declined, .b-expired { background: var(--bad-bg); color: var(--bad); }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs button { border-radius: 99px; }
.tabs button.on { background: var(--on-bg); color: var(--on-fg); border-color: var(--on-bg); }

.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.toast {
  position: fixed; left: 50%; top: calc(14px + env(safe-area-inset-top)); transform: translateX(-50%); background: var(--glass-solid); color: var(--ink);
  padding: 11px 20px; border-radius: 99px; z-index: 50; font-weight: 550; border-color: var(--line-hi);
}
.toast.err { background: var(--bad); color: #fff; border-color: transparent; }

/* sticky save bar on the editor */
.savebar { position: sticky; bottom: 12px; z-index: 15; display: flex; gap: 10px; padding: 10px; border-radius: 18px;
  background: var(--glass-solid); border: 1px solid var(--line-hi); -webkit-backdrop-filter: var(--blur); backdrop-filter: var(--blur); box-shadow: var(--shadow); }
.savebar button { flex: 1; justify-content: center; padding: 12px; }
@media (max-width: 760px) { .savebar { bottom: calc(88px + env(safe-area-inset-bottom)); } }
textarea { resize: vertical; }

/* line editor */
.lines th, .lines td { padding: 6px 4px; }
.lines input { padding: 8px 10px; }
.lines .d { min-width: 180px; }
.lines button.small { min-width: 34px; min-height: 34px; justify-content: center; }
#pick { flex: 1; min-width: 220px; max-width: none !important; }
.lines .q { width: 76px; text-align: right; }
.lines .u { width: 70px; }
.lines .p { width: 96px; text-align: right; }
.calc { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; background: var(--hover); border: 1px dashed var(--line-hi); border-radius: 12px; padding: 12px; margin-top: 12px; }
.calc input { width: 90px; }
@media (max-width: 640px) {
  .lines thead { display: none; }
  .lines, .lines tbody { display: block; }
  .lines tr { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; padding: 12px; margin-bottom: 10px;
    border: 1px solid var(--line); border-radius: 14px; background: var(--hover); }
  .lines td { padding: 0; border: 0; }
  .lines td:nth-child(1) { grid-column: 1 / -1; }
  .lines td:nth-child(5) { grid-column: 1 / 3; align-self: center; text-align: left; font-weight: 700; font-size: 16px; }
  .lines td:nth-child(6) { text-align: right; align-self: center; }
  .lines .d, .lines .q, .lines .u, .lines .p { width: 100%; min-width: 0; text-align: left; }
  .lines td[data-ph]::before { content: attr(data-ph); display: block; font-size: 11.5px; color: var(--muted); font-weight: 600; margin-bottom: 3px; }
  .lines tr td[colspan] { grid-column: 1 / -1; }
}

/* modal (bottom sheet on phones) */
.modal-bg { position: fixed; inset: 0; background: var(--overlay); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; z-index: 40; padding: 16px; }
.modal { background: var(--glass-solid); border-radius: 20px; padding: 22px; width: 100%; max-width: 480px; max-height: 90vh; overflow: auto; border-color: var(--line-hi); }
.modal h2 { font-size: 18px; margin-bottom: 4px; }
@media (max-width: 760px) {
  .modal-bg { align-items: flex-end; padding: 0; }
  .modal { max-width: none; border-radius: 22px 22px 0 0; padding-bottom: calc(22px + env(safe-area-inset-bottom)); }
}

/* chart */
.chart { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: 12px; }
.chart .m { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; min-width: 0; }
.chart .bars { display: flex; gap: 3px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.chart .bar { width: 42%; max-width: 14px; border-radius: 5px 5px 2px 2px; min-height: 2px; }
.chart .in { background: linear-gradient(180deg, var(--accent), color-mix(in srgb, var(--accent) 45%, transparent)); }
.chart .out { background: linear-gradient(180deg, var(--expense), color-mix(in srgb, var(--expense) 40%, transparent)); }
.chart small { font-size: 11px; color: var(--muted); }
.chart-wrap { position: relative; }
.chart-wrap .chart { border-bottom: 1px solid var(--line); }
.chart-wrap.is-empty .chart { opacity: .35; }
.chart-empty { position: absolute; inset: 0 0 24px; display: grid; place-items: center; color: var(--muted); font-size: 13.5px; }
.banner { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; font-size: 12.5px; color: var(--muted); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 5px; }

/* "More" menu */
.menu-link { display: flex; gap: 14px; align-items: center; margin-bottom: 10px; }
.menu-link .ico { font-size: 20px; width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; background: var(--glass-hi); flex-shrink: 0; }
.menu-link b { font-size: 15.5px; margin: 0; }
.menu-link small { display: block; }

/* auth */
.auth { max-width: 400px; margin: 14vh auto; padding: 0 16px; }
.auth .card { padding: 28px; border-radius: 22px; }

/* ---------- printable document (always paper-white) ---------- */
.doc {
  color-scheme: light; --ink: #1d2430; --muted: #6b7480; --line: #e3e7eb; --link: #1f6f8b; --ok: #2e7d32; --bad: #c62828;
  color: var(--ink); background: #fff; padding: 40px; border-radius: var(--radius); margin: 0 0 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,.35); font-variant-numeric: tabular-nums;
}
.doc .head { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 28px; flex-wrap: wrap; }
.doc .logo { max-height: 70px; max-width: 220px; margin-bottom: 8px; display: block; }
.doc .title { font-size: 30px; font-weight: 800; letter-spacing: .06em; color: var(--accent); margin: 0; }
.doc .meta { text-align: right; }
.doc .meta td { padding: 1px 0 1px 14px; border: 0; text-align: right; }
.doc .parties { display: flex; gap: 28px; flex-wrap: wrap; margin-bottom: 22px; }
.doc .parties > div { min-width: 180px; }
.doc .label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 3px; font-weight: 600; }
.doc table.items th { background: color-mix(in srgb, var(--accent) 10%, white); color: var(--ink); }
.doc table.items th, .doc table.items td { padding: 10px; }
.doc .totals { margin: 14px 0 0 auto; max-width: 300px; }
.doc .totals div { display: flex; justify-content: space-between; padding: 4px 10px; }
.doc .totals .grand { font-weight: 800; font-size: 18px; border-top: 2px solid var(--ink); margin-top: 6px; padding-top: 8px; }
.doc .totals .bal { font-weight: 700; color: var(--bad); }
.doc .foot { margin-top: 28px; display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.doc .stamp { display: inline-block; border: 3px solid var(--ok); color: var(--ok); font-weight: 800; padding: 4px 14px; border-radius: 6px; transform: rotate(-6deg); letter-spacing: .1em; margin-top: 8px; }
@media (max-width: 600px) {
  .doc { padding: 20px; border-radius: 14px; } .doc .meta { text-align: left; } .doc .meta td { text-align: left; padding: 1px 14px 1px 0; }
  .doc .foot { grid-template-columns: 1fr; }
  .doc table.items thead { display: none; }
  .doc table.items, .doc table.items tbody { display: block; }
  .doc table.items tr { display: grid; grid-template-columns: auto auto 1fr; gap: 2px 14px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .doc table.items td { padding: 0; border: 0; font-size: 13.5px; }
  .doc table.items td:first-child { grid-column: 1 / -1; font-weight: 600; font-size: 14px; margin-bottom: 2px; }
  .doc table.items td:nth-child(2), .doc table.items td:nth-child(3) { text-align: left; color: var(--muted); }
  .doc table.items td:nth-child(3)::before { content: "× "; }
  .doc table.items td:nth-child(4) { text-align: right; font-weight: 600; }
  .doc table.items tr td[colspan] { grid-column: 1 / -1; }
  .doc .totals { max-width: none; margin-top: 8px; }
  .doc .totals div { padding: 4px 0; }
  .doc .items-wrap { overflow-x: auto; }
}

@media print {
  :root { --bg: #fff; color-scheme: light; }
  body { background: #fff; color: #1d2430; }
  body::before { display: none; }
  .side, .mobile-nav, .topbar, .no-print, .toast { display: none !important; }
  .content { padding: 0; max-width: none; }
  .doc { box-shadow: none; padding: 0; border-radius: 0; }
  @page { margin: 16mm; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

@media (max-width: 380px) { .mobile-nav a { font-size: 9.5px; } }
