/* Everything visual derives from the tokens in /theme/<tenant>.css, so a tenant's
   colors, font, corners and density reach every screen without per-tenant rules. */
:root {
  --c-muted: color-mix(in srgb, var(--c-text) 62%, var(--c-surface));
  --c-border: color-mix(in srgb, var(--c-text) 16%, var(--c-surface));
  --c-hover: color-mix(in srgb, var(--c-text) 6%, var(--c-surface));
  --c-tint: color-mix(in srgb, var(--c-primary) 12%, var(--c-surface));
  --c-ok: #1E8E5A;
  --c-warn: #B7791F;
  --c-danger: #C8352B;
  --sp-1: calc(4px * var(--density));
  --sp-2: calc(8px * var(--density));
  --sp-3: calc(12px * var(--density));
  --sp-4: calc(16px * var(--density));
  --sp-5: calc(24px * var(--density));
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--c-bg); color: var(--c-text); font: 14px/1.45 var(--font); }
h1, h2, h3 { margin: 0; line-height: 1.25; font-weight: 650; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 13px; color: var(--c-muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
a { color: var(--c-primary); }
.muted { color: var(--c-muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 13px; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.prewrap { white-space: pre-wrap; }

/* Shell */
.shell { display: flex; min-height: 100%; }
.shell[data-nav="TOPBAR"] { flex-direction: column; }
.nav { background: var(--c-nav); color: var(--c-on-nav); display: flex; flex-direction: column; flex: none; width: 220px; padding: var(--sp-4) var(--sp-3); gap: var(--sp-4); position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.shell[data-nav="TOPBAR"] .nav { flex-direction: row; flex-wrap: wrap; align-items: center; width: auto; height: auto; padding: var(--sp-2) var(--sp-4); z-index: 5; overflow: visible; }
.brand { display: flex; align-items: center; gap: var(--sp-2); font-weight: 650; font-size: 15px; min-width: 0; flex: none; max-width: 200px; }
.brand img { max-height: 32px; max-width: 120px; object-fit: contain; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-links { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.shell[data-nav="TOPBAR"] .nav-links { flex-direction: row; flex-wrap: wrap; }
.nav-links a { color: inherit; text-decoration: none; padding: var(--sp-2) var(--sp-3); border-radius: var(--radius); opacity: .78; white-space: nowrap; }
.nav-links a:hover { opacity: 1; background: color-mix(in srgb, var(--c-on-nav) 10%, transparent); }
.nav-links a.active { opacity: 1; background: color-mix(in srgb, var(--c-on-nav) 16%, transparent); font-weight: 600; }
.nav-foot { display: flex; flex-direction: column; gap: var(--sp-2); font-size: 12px; }
.shell[data-nav="TOPBAR"] .nav-foot { flex-direction: row; align-items: center; }
.shell[data-nav="TOPBAR"] .nav-foot select { width: auto; flex: none; max-width: 180px; }
.nav-foot select { background: color-mix(in srgb, var(--c-on-nav) 12%, transparent); color: inherit; border-color: transparent; }
.nav-foot select option { color: #111; }
.nav-foot button { background: none; border: 0; color: inherit; opacity: .78; padding: 0; text-align: left; cursor: pointer; font: inherit; text-decoration: underline; white-space: nowrap; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.content { padding: var(--sp-5); max-width: 1200px; width: 100%; }
.banner { background: var(--c-accent); color: var(--c-on-accent); padding: var(--sp-2) var(--sp-5); display: flex; gap: var(--sp-3); align-items: center; justify-content: space-between; font-weight: 500; }
.banner button { background: none; color: inherit; border: 1px solid currentColor; border-radius: var(--radius); padding: 2px 10px; cursor: pointer; font: inherit; white-space: nowrap; }

/* Blocks */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-4); flex-wrap: wrap; }
.page-head .actions, .actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; }
.card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: var(--sp-4); }
.card + .card { margin-top: var(--sp-4); }
.card > h2 { margin-bottom: var(--sp-3); }
.grid { display: grid; gap: var(--sp-4); }
.grid-2 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.grid-even { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid .card + .card { margin-top: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--sp-3); margin-bottom: var(--sp-4); }
.stat { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: var(--sp-3) var(--sp-4); }
.stat b { display: block; font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat span { color: var(--c-muted); font-size: 12px; }
.empty { padding: var(--sp-5); text-align: center; color: var(--c-muted); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: var(--sp-1) var(--sp-3); margin: 0; }
.kv dt { color: var(--c-muted); }
.kv dd { margin: 0; }

/* Tables */
.table-wrap { overflow-x: auto; background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); }
.card .table-wrap { border: 0; border-radius: 0; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: var(--sp-2) var(--sp-3); text-align: left; border-bottom: 1px solid var(--c-border); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--c-muted); white-space: nowrap; }
th.num { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
tr.link { cursor: pointer; }
tr.link:hover td { background: var(--c-hover); }
.pager { display: flex; justify-content: space-between; align-items: center; padding-top: var(--sp-3); color: var(--c-muted); font-size: 12px; }

/* Controls */
button.btn, a.btn { font: inherit; display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: calc(var(--sp-2) - 1px) var(--sp-3); border-radius: var(--radius); border: 1px solid var(--c-border); background: var(--c-surface); color: var(--c-text); cursor: pointer; font-weight: 500; text-decoration: none; white-space: nowrap; }
button.btn:hover, a.btn:hover { background: var(--c-hover); }
button.btn.primary { background: var(--c-primary); color: var(--c-on-primary); border-color: var(--c-primary); }
button.btn.primary:hover { background: color-mix(in srgb, var(--c-primary) 86%, #000); }
button.btn.danger { color: var(--c-danger); }
a.btn.primary-link { background: var(--c-primary); color: var(--c-on-primary); border-color: var(--c-primary); }
a.btn.primary-link:hover { background: color-mix(in srgb, var(--c-primary) 86%, #000); }
button.btn.small { padding: 2px var(--sp-2); font-size: 12px; }
button.btn:disabled { opacity: .55; cursor: default; }
:focus-visible { outline: 2px solid var(--c-accent); outline-offset: 1px; }
input, select, textarea { font: inherit; color: var(--c-text); background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: calc(var(--sp-2) - 1px) var(--sp-2); width: 100%; min-width: 0; }
input[type="color"] { padding: 2px; height: 34px; width: 44px; flex: none; cursor: pointer; }
textarea { resize: vertical; min-height: 72px; }
select.inline, input.inline { width: auto; }
.search { width: 260px; max-width: 100%; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: var(--sp-3); }
.field > label, .field > .label { font-size: 12px; font-weight: 600; color: var(--c-muted); }
.hint { font-size: 12px; color: var(--c-muted); }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 var(--sp-3); }
.checks { display: flex; flex-direction: column; gap: 4px; }
.checks label { display: flex; gap: 6px; align-items: center; font-weight: 400; color: var(--c-text); font-size: 14px; }
.checks input { width: auto; }
.form-error { color: var(--c-danger); margin-bottom: var(--sp-3); font-size: 13px; }

/* Pills */
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; letter-spacing: .02em; white-space: nowrap; background: var(--c-hover); color: var(--c-muted); }
.pill.primary { background: var(--c-tint); color: color-mix(in srgb, var(--c-primary) 75%, var(--c-text)); }
.pill.ok { background: color-mix(in srgb, var(--c-ok) 16%, var(--c-surface)); color: color-mix(in srgb, var(--c-ok) 80%, var(--c-text)); }
.pill.warn { background: color-mix(in srgb, var(--c-warn) 18%, var(--c-surface)); color: color-mix(in srgb, var(--c-warn) 80%, var(--c-text)); }
.pill.danger { background: color-mix(in srgb, var(--c-danger) 14%, var(--c-surface)); color: color-mix(in srgb, var(--c-danger) 80%, var(--c-text)); }

/* Job rules, sign-off and the printed sheet */
.notice { border-color: var(--c-warn); }
.checklist { margin: 0; padding-left: 18px; }
.checklist li + li { margin-top: 4px; }
.warn-text { color: var(--c-warn); }
.top-gap { margin-top: var(--sp-3); }
.terms { white-space: pre-wrap; font-size: 13px; background: var(--c-hover); border-radius: var(--radius); padding: var(--sp-2) var(--sp-3); max-height: 200px; overflow-y: auto; }
.signature-pad { display: block; width: 100%; aspect-ratio: 900 / 280; background: #fff; border: 1px solid var(--c-border); border-radius: var(--radius); touch-action: none; cursor: crosshair; margin-bottom: var(--sp-2); }
.signature-image { display: block; max-width: 280px; width: 100%; background: #fff; border: 1px solid var(--c-border); border-radius: var(--radius); margin-top: var(--sp-2); }
.sheet { border: 1px solid var(--c-border); }
.sheet th { width: 34%; white-space: normal; vertical-align: top; color: var(--c-text); text-transform: uppercase; font-size: 11px; letter-spacing: .03em; border-right: 1px solid var(--c-border); }
.sheet td { height: 30px; }
.signature-blank { height: 56px; }

/* Job board */
.board { display: grid; grid-template-columns: repeat(4, minmax(210px, 1fr)); gap: var(--sp-3); overflow-x: auto; align-items: start; }
.column { background: var(--c-hover); border-radius: var(--radius); padding: var(--sp-2); min-height: 120px; }
.column > h3 { padding: var(--sp-1) var(--sp-2) var(--sp-2); display: flex; justify-content: space-between; }
.job-card { display: block; width: 100%; text-align: left; font: inherit; color: inherit; background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: var(--sp-2) var(--sp-3); margin-bottom: var(--sp-2); cursor: pointer; }
.job-card:hover { border-color: var(--c-primary); }
.job-card b { display: block; }

/* Agenda */
.day-head { font-weight: 600; padding: var(--sp-2) 0; }

/* Dialog */
.overlay { position: fixed; inset: 0; background: rgb(0 0 0 / .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; overflow-y: auto; z-index: 20; }
.dialog { background: var(--c-surface); color: var(--c-text); border-radius: var(--radius); width: 100%; max-width: 520px; padding: var(--sp-5); box-shadow: 0 16px 48px rgb(0 0 0 / .3); }
.dialog h2 { margin-bottom: var(--sp-4); font-size: 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); margin-top: var(--sp-4); }
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 30; }
.toast { background: var(--c-text); color: var(--c-surface); border-radius: var(--radius); padding: 10px 14px; max-width: 360px; box-shadow: 0 6px 20px rgb(0 0 0 / .25); }
.toast.error { background: var(--c-danger); color: #fff; }

/* Login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: var(--c-bg); }
.login-box { width: 100%; max-width: 380px; background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: var(--sp-5); border-top: 4px solid var(--c-primary); }
.login-box img { max-height: 64px; max-width: 200px; object-fit: contain; display: block; margin-bottom: var(--sp-3); }
.login-box h1 { margin-bottom: 4px; }
.login-box form { margin-top: var(--sp-4); }
.login-box button { width: 100%; }

/* Branding editor */
.color-row { display: flex; gap: var(--sp-2); align-items: center; }
.presets { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.preset { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgb(0 0 0 / .2); display: inline-block; }
.logo-box { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.logo-box img { max-height: 48px; max-width: 160px; object-fit: contain; background: var(--c-nav); padding: 4px; border-radius: var(--radius); }
.preview { position: sticky; top: var(--sp-4); border: 1px solid var(--c-border); border-radius: var(--radius); overflow: hidden; background: var(--c-bg); color: var(--c-text); font-family: var(--font); }
.preview-shell { display: flex; min-height: 300px; }
.preview[data-nav="TOPBAR"] .preview-shell { flex-direction: column; min-height: 0; }
.preview-nav { background: var(--c-nav); color: var(--c-on-nav); padding: var(--sp-3); width: 110px; flex: none; font-size: 12px; display: flex; flex-direction: column; gap: var(--sp-2); }
.preview[data-nav="TOPBAR"] .preview-nav { width: auto; flex-direction: row; align-items: center; flex-wrap: wrap; }
.preview-nav strong { overflow: hidden; text-overflow: ellipsis; }
.preview-nav span { opacity: .75; }
.preview-body { padding: var(--sp-3); flex: 1; min-width: 0; }
.preview-card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: var(--sp-3); margin-top: var(--sp-2); }

/* Invoice document */
.invoice-doc { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: calc(var(--sp-5) * 1.5); max-width: 820px; }
.invoice-head { display: flex; justify-content: space-between; gap: var(--sp-4); border-bottom: 3px solid var(--c-primary); padding-bottom: var(--sp-4); margin-bottom: var(--sp-4); flex-wrap: wrap; }
.invoice-head img { max-height: 56px; max-width: 200px; object-fit: contain; display: block; margin-bottom: var(--sp-2); }
.invoice-parties { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-4); margin-bottom: var(--sp-4); }
.totals { margin-left: auto; width: min(320px, 100%); margin-top: var(--sp-3); }
.totals div { display: flex; justify-content: space-between; padding: 3px 0; font-variant-numeric: tabular-nums; }
.totals .grand { font-weight: 700; font-size: 16px; border-top: 2px solid var(--c-text); margin-top: 4px; padding-top: 6px; }
.invoice-footer { margin-top: var(--sp-5); padding-top: var(--sp-3); border-top: 1px solid var(--c-border); color: var(--c-muted); font-size: 12px; white-space: pre-wrap; }

@media (max-width: 1100px) {
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .job-side { order: -1; }
}

@media (max-width: 860px) {
  .shell { flex-direction: column; }
  .nav { flex-direction: row; align-items: center; width: auto; height: auto; position: static; padding: var(--sp-2) 16px; overflow-x: auto; }
  .nav-links { flex-direction: row; }
  .nav-foot { flex-direction: row; align-items: center; }
  .content { padding: 16px; }
  .banner { padding: var(--sp-2) 16px; }
  .grid-2, .grid-even, .invoice-parties { grid-template-columns: minmax(0, 1fr); }
  .invoice-doc { padding: 16px; }
  .preview { position: static; }
}

@media print {
  body { background: #fff; color: #000; }
  .nav, .banner, .page-head, #toasts, .no-print { display: none !important; }
  .content { padding: 0; max-width: none; }
  .invoice-doc { border: 0; padding: 0; max-width: none; background: #fff; color: #000; }
}
