/* Blackstone - mobile-first layout.
   Phone: app bar on top, bottom tab bar, full-width cards and sheets.
   Desktop (>= 900px): black brand sidebar, content column up to 1120px, dialogs centred. */
:root {
  --brand: #090A0B;          /* Blackstone black: sidebar, primary buttons */
  --gold: #B8892D;           /* Blackstone gold: marks, focus, active tab */
  --gold-ink: #85601A;       /* gold dark enough for text on light surfaces */
  --bg: #F3F3F1;
  --surface: #FFFFFF;
  --surface-2: #F8F7F4;
  --ink: #16171A;
  --muted: #5D5F66;
  --line: #E2E1DC;
  --good: #1B7F3B;
  --warn: #8A5A00;
  --bad: #B00020;
  --good-bg: #E7F4EB;
  --warn-bg: #FBF1DC;
  --bad-bg: #FBE7EA;
  --on-brand: #FFFFFF;
  --shadow: 0 1px 2px rgba(9, 10, 11, .06), 0 4px 16px rgba(9, 10, 11, .05);
  --font: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Consolas, monospace;
  --r: 10px;
  --tabbar: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #050506; --gold: #C99A3C; --gold-ink: #D9AE58;
    --bg: #0E0F11; --surface: #18191C; --surface-2: #1F2024; --ink: #ECEBE7; --muted: #A3A4AA; --line: #2C2D32;
    --good: #5CC582; --warn: #E2B04F; --bad: #FF7A8A; --good-bg: #13291B; --warn-bg: #2E2410; --bad-bg: #331419;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --brand: #050506; --gold: #C99A3C; --gold-ink: #D9AE58;
  --bg: #0E0F11; --surface: #18191C; --surface-2: #1F2024; --ink: #ECEBE7; --muted: #A3A4AA; --line: #2C2D32;
  --good: #5CC582; --warn: #E2B04F; --bad: #FF7A8A; --good-bg: #13291B; --warn-bg: #2E2410; --bad-bg: #331419;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }  /* components set display; hidden must still win */
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 30; background: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; text-wrap: balance; }
h1 { font-size: 1.45rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; }
a { color: var(--gold-ink); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.label { font-size: .72rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---- Shell ---------------------------------------------------------------- */
#app { min-height: 100%; }
.shell { min-height: 100%; display: flex; flex-direction: column; }
.sidebar { display: none; }
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; background: var(--brand); color: var(--on-brand);
  display: flex; align-items: center; gap: 10px; min-height: 56px;
  padding-block: 8px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.topbar .mark { height: 30px; width: auto; flex: none; display: block; }
.topbar .title { font-weight: 700; font-size: 1.05rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.company-switch {
  max-width: 52vw; background: rgba(255,255,255,.08); color: var(--on-brand); border: 1px solid rgba(201,154,60,.55);
  border-radius: 8px; padding: 6px 10px; font-size: .85rem;
}
.company-switch option { color: #16171A; }
.sync-pill { font-size: .72rem; font-weight: 700; padding: 3px 8px; border-radius: 99px; background: var(--warn-bg); color: var(--warn); white-space: nowrap; }
.main {
  flex: 1; width: 100%; max-width: 1120px; margin: 0 auto;
  padding-block: 18px calc(var(--tabbar) + 92px + env(safe-area-inset-bottom, 0px)); /* room to scroll past the floating button */
  padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex; flex-direction: column; gap: 18px; min-width: 0;
}
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px); padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px);
}
.tabbar button {
  flex: 1; min-height: var(--tabbar); border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; font-size: .7rem; font-weight: 600; color: var(--muted); cursor: pointer;
}
.tabbar button svg { width: 22px; height: 22px; }
.tabbar button[aria-current="page"] { color: var(--ink); }
.tabbar button[aria-current="page"] svg { color: var(--gold); }

@media (min-width: 900px) {
  .shell { flex-direction: row; }
  .sidebar {
    display: flex; flex-direction: column; width: 248px; flex: none; background: var(--brand); color: var(--on-brand);
    position: sticky; top: 0; height: 100vh; padding: 22px 14px; gap: 4px; overflow-y: auto;
  }
  .sidebar .logo { width: 196px; margin: 0 6px 18px; }
  .sidebar .nav-head { font-size: .68rem; letter-spacing: .12em; text-transform: uppercase; color: var(--gold); margin: 14px 10px 4px; font-weight: 700; }
  .sidebar button {
    text-align: left; border: 0; background: none; color: #E9E8E4; padding: 9px 12px; border-radius: 8px; cursor: pointer; font-size: .92rem;
    display: flex; gap: 10px; align-items: center;
  }
  .sidebar button svg { width: 18px; height: 18px; opacity: .8; }
  .sidebar button:hover { background: rgba(255,255,255,.06); }
  .sidebar button[aria-current="page"] { background: var(--gold); color: #0B0B0C; font-weight: 700; }
  .sidebar .who { margin-top: auto; padding: 12px; font-size: .82rem; color: #BDBCB7; border-top: 1px solid rgba(255,255,255,.1); }
  .content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  .topbar { background: var(--surface); color: var(--ink); border-bottom: 1px solid var(--line); top: 0; }
  .topbar .mark { display: none; }
  .company-switch { background: var(--surface-2); color: var(--ink); border-color: var(--line); max-width: 360px; }
  .tabbar { display: none; }
  .main { padding-block: 28px 48px; }
  body::before { display: none; }
}

/* ---- Building blocks -------------------------------------------------------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); min-width: 0; }
.card-pad { padding: 16px; }
.stack { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 700px) { .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.kpi { padding: 14px; border-top: 3px solid var(--gold); }
.kpi .value { font-size: clamp(.98rem, 4.2vw, 1.25rem); font-weight: 700; margin-top: 4px; overflow-wrap: anywhere; }
.grid-2 { display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) { .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 42px; padding: 0 16px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--gold); }
.btn.primary { background: var(--brand); color: var(--on-brand); border-color: var(--brand); }
.btn.gold { background: var(--gold); color: #0B0B0C; border-color: var(--gold); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .85rem; }
.btn[disabled] { opacity: .55; cursor: progress; }
:root[data-theme="dark"] .btn.primary { background: var(--gold); color: #0B0B0C; border-color: var(--gold); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn.primary { background: var(--gold); color: #0B0B0C; border-color: var(--gold); } }
.fab {
  position: fixed; right: max(18px, env(safe-area-inset-right, 0px)); bottom: calc(var(--tabbar) + 18px + env(safe-area-inset-bottom, 0px)); z-index: 15;
  height: 54px; padding: 0 20px; border-radius: 99px; border: 0; background: var(--gold); color: #0B0B0C; font-weight: 700; box-shadow: 0 6px 20px rgba(0,0,0,.25); cursor: pointer;
}
@media (min-width: 900px) { .fab { display: none; } }
.desktop-only { display: none; }
@media (min-width: 900px) { .desktop-only { display: inline-flex; } }

.segmented { display: flex; gap: 4px; overflow-x: auto; padding: 3px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; scrollbar-width: none; }
.segmented button { flex: none; border: 0; background: none; padding: 8px 14px; border-radius: 7px; font-weight: 600; color: var(--muted); cursor: pointer; font-size: .88rem; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.list { display: flex; flex-direction: column; }
.list-row {
  display: flex; align-items: center; gap: 12px; padding: 13px 16px; border-bottom: 1px solid var(--line); min-width: 0;
  background: none; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left; cursor: default;
}
button.list-row { cursor: pointer; }
button.list-row:hover { background: var(--surface-2); }
.list-row:last-child { border-bottom: 0; }
.list-row .main-text { flex: 1; min-width: 0; }
.list-row .main-text strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .sub { color: var(--muted); font-size: .84rem; overflow: hidden; text-overflow: ellipsis; }
.list-row .end { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.chip { display: inline-block; font-size: .7rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 3px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.chip.good { background: var(--good-bg); color: var(--good); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.check-row { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.check-row:last-child { border-bottom: 0; }
.check-row .detail { grid-column: 2; color: var(--muted); font-size: .85rem; }
.banner { padding: 12px 14px; border-radius: 9px; font-weight: 600; }
.banner.good { background: var(--good-bg); color: var(--good); }
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.issues { display: flex; flex-direction: column; gap: 8px; }
.issue { padding: 10px 12px; border-radius: 8px; font-size: .9rem; border-left: 4px solid; }
.issue.ERROR { background: var(--bad-bg); border-color: var(--bad); }
.issue.WARNING { background: var(--warn-bg); border-color: var(--warn); }
.issue b { display: block; font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; margin-bottom: 2px; }

/* ---- Forms ------------------------------------------------------------------ */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: .85rem; font-weight: 600; }
.field .hint { font-size: .78rem; color: var(--muted); }
.field input, .field select, .field textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); font-size: 16px;
}
.field textarea { min-height: 88px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--gold); outline: none; box-shadow: 0 0 0 3px rgba(184,137,45,.18); }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .92rem; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--gold); flex: none; }
.two { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 560px) { .two { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.line-item { border: 1px solid var(--line); border-radius: 9px; padding: 12px; display: flex; flex-direction: column; gap: 10px; background: var(--surface-2); }
.totals { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; font-size: .95rem; }
.totals .grand { font-weight: 700; font-size: 1.1rem; border-top: 1px solid var(--line); padding-top: 6px; }
.error-text { color: var(--bad); font-size: .9rem; }

/* ---- Sheets (bottom sheet on phones, dialog on desktop) ---------------------- */
.scrim { position: fixed; inset: 0; z-index: 40; background: rgba(9,10,11,.5); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-height: calc(100% - 24px - env(safe-area-inset-top, 0px)); overflow: auto; background: var(--surface); border-radius: 16px 16px 0 0;
  padding-block: 18px calc(18px + env(safe-area-inset-bottom, 0px)); padding-left: max(18px, env(safe-area-inset-left, 0px)); padding-right: max(18px, env(safe-area-inset-right, 0px));
  display: flex; flex-direction: column; gap: 14px; animation: rise .18s ease-out;
}
.sheet.wide { max-width: none; }
.sheet-head { display: flex; align-items: center; gap: 10px; }
.sheet-head h2 { flex: 1; }
.icon-btn { border: 0; background: var(--surface-2); width: 38px; height: 38px; border-radius: 99px; cursor: pointer; font-size: 1.2rem; line-height: 1; }
@keyframes rise { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 900px) {
  .scrim { align-items: center; }
  .sheet { max-width: 620px; border-radius: 14px; padding: 24px; max-height: 88vh; }
  .sheet.wide { max-width: 980px; }
}
.pdf-pages { display: flex; flex-direction: column; gap: 12px; align-items: center; background: var(--surface-2); border-radius: 10px; padding: 12px; min-height: 200px; }
.pdf-pages canvas { width: 100%; max-width: 860px; height: auto; background: #fff; box-shadow: 0 1px 6px rgba(0,0,0,.18); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 60; bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom, 0px));
  background: var(--brand); color: var(--on-brand); padding: 12px 18px; border-radius: 10px; max-width: calc(100% - 32px); box-shadow: 0 8px 28px rgba(0,0,0,.3);
  font-weight: 600; font-size: .92rem;
}
@media (min-width: 900px) { .toast { bottom: 28px; } }

/* ---- Statements ------------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
table.statement { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.statement th { text-align: right; font-size: .78rem; color: var(--muted); padding: 8px 10px; border-bottom: 2px solid var(--gold); white-space: nowrap; }
table.statement th:first-child { text-align: left; }
table.statement td { padding: 6px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.statement td.v { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.statement tr.heading td { font-weight: 700; padding-top: 14px; border-bottom: 0; }
table.statement tr.subtotal td { font-weight: 700; }
table.statement tr.subtotal td.v { border-top: 1px solid var(--ink); }
table.statement tr.total td { font-weight: 700; }
table.statement tr.total td.v { border-top: 1px solid var(--ink); border-bottom: 3px double var(--ink); }

/* ---- Sign-in ------------------------------------------------------------------ */
.auth {
  min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; background: var(--brand);
  padding-block: 32px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.auth .card { width: 100%; max-width: 420px; padding: 26px; display: flex; flex-direction: column; gap: 18px; }
.auth .logo { width: 220px; max-width: 100%; align-self: center; }
.auth .logo-dark { display: none; }
:root[data-theme="dark"] .auth .logo-light { display: none; }
:root[data-theme="dark"] .auth .logo-dark { display: block; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .auth .logo-light { display: none; } :root:not([data-theme="light"]) .auth .logo-dark { display: block; } }
.preview-note { background: var(--warn-bg); color: var(--ink); border-radius: 9px; padding: 12px; font-size: .86rem; }
.preview-note code { font-family: var(--mono); font-weight: 700; }
.code-box { font-family: var(--mono); font-size: 1.3rem; font-weight: 700; letter-spacing: .06em; text-align: center; padding: 14px; border: 2px dashed var(--gold); border-radius: 10px; user-select: all; }
.loading { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 48px 16px; color: var(--muted); flex-direction: column; text-align: center; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--gold); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.boot { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; background: var(--brand); color: #DAD9D4; padding: 24px; text-align: center; }
.boot img { width: 230px; max-width: 70%; }
.boot .bar { width: 220px; height: 4px; border-radius: 4px; background: rgba(255,255,255,.12); overflow: hidden; }
.boot .bar i { display: block; height: 100%; width: 30%; background: var(--gold); animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(340%); } }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
details.card summary { cursor: pointer; }
details.card[open] summary { margin-bottom: 8px; }
.qr { align-self: center; width: 200px; max-width: 70vw; padding: 0; background: #fff; border-radius: 8px; line-height: 0; }
.qr svg { width: 100%; height: auto; }
.code-box.small { font-size: .95rem; word-break: break-all; }
.logo-box { width: 180px; height: 72px; display: grid; place-items: center; border: 1px dashed var(--line); border-radius: 8px; background: #fff; overflow: hidden; flex: none; }
.logo-box img { max-width: 170px; max-height: 64px; object-fit: contain; }
input[type="color"] { height: 44px; padding: 4px; }
.banner.info { background: var(--surface-2); color: var(--ink); font-weight: 400; border: 1px solid var(--line); }
.banner.info a { color: inherit; font-weight: 600; }
