:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #e3e6eb;
  --primary: #1f6feb;
  --primary-text: #ffffff;
  --ok: #1a7f37;
  --warn: #9a6700;
  --bad: #cf222e;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d24;
    --text: #e6e9ed;
    --muted: #98a2b3;
    --border: #2a323c;
    --primary: #4c8dff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
a { color: var(--primary); text-decoration: none; }
.muted { color: var(--muted); }
.error { color: var(--bad); }
.notice {
  margin: 0; padding: .5rem .75rem; border-radius: 8px; font-size: .9rem;
  color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, transparent);
}

.shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); }
.nav { display: flex; gap: 1rem; flex: 1; }
.nav a { color: var(--muted); }
.nav a.active { color: var(--text); font-weight: 600; }
.user { display: flex; align-items: center; gap: .75rem; }
.user-email { color: var(--muted); font-size: .9rem; }
.content { padding: 1.5rem 1rem; max-width: 1100px; width: 100%; margin: 0 auto; }

.centered { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.splash { min-height: 100vh; display: grid; place-items: center; color: var(--muted); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem;
}
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); margin-top: 1rem; }

.login { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: .75rem; }
.login form { display: flex; flex-direction: column; gap: .75rem; }
label { display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; }
input {
  font: inherit; padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.validation-message { color: var(--bad); font-size: .85rem; }

.btn {
  font: inherit; cursor: pointer; padding: .55rem 1rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-ghost { border-color: transparent; background: transparent; }

.divider { display: flex; align-items: center; gap: .5rem; color: var(--muted); font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

.badge { display: inline-block; padding: .1rem .6rem; border-radius: 999px; font-size: .85rem; font-weight: 600; }
.badge.ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.badge.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent); }
.badge.bad { color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, transparent); }

.plain { list-style: none; padding: 0; margin: 0; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0 0 1rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

#blazor-error-ui {
  display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000;
  padding: .75rem 1rem; background: #fff3cd; color: #664d03; box-shadow: 0 -1px 4px rgba(0,0,0,.2);
}
#blazor-error-ui .dismiss { cursor: pointer; float: right; }

@media (max-width: 600px) {
  .user-email { display: none; }
  .topbar { gap: .75rem; }
}

/* ---- Phase 1: pages, tables, forms ---- */
h3 { font-size: .95rem; margin: 1rem 0 .5rem; }
.small { font-size: .85rem; }
code { font-size: .85em; padding: .05rem .3rem; border-radius: 4px; background: var(--bg); border: 1px solid var(--border); }

.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-header h1 { margin: 0; }
.toolbar { display: flex; gap: .5rem; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.row-between h2 { margin: 0; }

.user-meta { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.2; }
.user-company { font-weight: 600; font-size: .9rem; }

.alert { padding: .65rem .85rem; border-radius: 8px; margin: .75rem 0; font-size: .92rem; }
.alert-error { color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, transparent); }
.alert-ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 10%, transparent); }

.search { display: flex; gap: .5rem; margin-bottom: 1rem; max-width: 520px; }
.search input { flex: 1; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .02em; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: color-mix(in srgb, var(--primary) 4%, transparent); }
.table.compact th, .table.compact td { padding: .4rem .5rem; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }
.table tr.inactive td { color: var(--muted); }
.table select { font: inherit; padding: .3rem .4rem; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.tag { display: inline-block; margin: 0 .25rem .15rem 0; padding: 0 .45rem; border-radius: 999px; font-size: .78rem; border: 1px solid var(--border); color: var(--muted); }

.pager { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-top: .75rem; }

.form-section { margin-bottom: 1rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem 1rem; }
.form-grid .span-2 { grid-column: span 2; }
select, textarea {
  font: inherit; padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); width: 100%;
}
textarea { resize: vertical; }
input { width: 100%; }
input[type="checkbox"], input[type="radio"] { width: auto; }
label.check { flex-direction: row; align-items: center; gap: .5rem; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.form-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin: 1rem 0 2rem; }
.push-right { margin-left: auto; }
.confirm { display: inline-flex; gap: .25rem; }

.contact-row {
  display: grid; grid-template-columns: 1.2fr 1fr 1.2fr 1fr auto auto; gap: .5rem; align-items: end;
  padding: .75rem 0; border-bottom: 1px dashed var(--border);
}
.contact-row:last-child { border-bottom: 0; }
.contact-row .validation-message { grid-column: 1 / -1; }

.btn-sm { padding: .3rem .65rem; font-size: .85rem; }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger-outline { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); }
a.btn { display: inline-block; text-decoration: none; }
.btn-primary:hover { filter: brightness(1.05); }

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin: .5rem 0 1rem; overflow-x: auto; }
.tab { font: inherit; padding: .55rem .9rem; border: 0; background: none; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); font-weight: 600; border-bottom-color: var(--primary); }

.badge.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent); }
.badge:not(.ok):not(.warn):not(.bad) { color: var(--muted); background: color-mix(in srgb, var(--muted) 12%, transparent); }

.card.empty { text-align: center; padding: 2rem 1rem; }
.tile { color: var(--text); text-decoration: none; transition: border-color .15s; }
.tile:hover { border-color: var(--primary); }
.tile h2 { color: var(--primary); }
.import-panel { margin-bottom: 1rem; }
.onboarding { max-width: 460px; }

@media (max-width: 700px) {
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
  .contact-row { grid-template-columns: 1fr 1fr; }
  .hide-sm { display: none; }
  .topbar { flex-wrap: wrap; }
  .nav { order: 3; width: 100%; overflow-x: auto; }
  .user-meta { display: none; }
}

/* ---- Phase 2: quotes ---- */
.badge.info { color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, transparent); }
h1 .badge { vertical-align: middle; margin-left: .4rem; font-size: .8rem; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-line; }

.lookup { position: relative; }
.lookup-menu {
  position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 2px); margin: 0; padding: .25rem 0; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12);
  max-height: 280px; overflow-y: auto;
}
.lookup-menu li { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem .7rem; cursor: pointer; }
.lookup-menu li:hover, .lookup-menu li.active { background: color-mix(in srgb, var(--primary) 10%, transparent); }

.lines td { vertical-align: top; }
.lines .desc { min-width: 240px; width: 40%; }
.lines textarea { min-height: 2.4rem; }
.lines input, .lines select, .totals-card input, .totals-card select { padding: .35rem .45rem; }
.lines input.qty { width: 5rem; text-align: right; }
.lines input.unit { width: 4.5rem; }
.lines input.money, .totals-card input.money { width: 7rem; text-align: right; }
.lines input.disc { width: 4.5rem; text-align: right; }
select.disc-type { width: auto; }
.lines select { min-width: 5.5rem; }
.line-add { display: grid; grid-template-columns: 1fr auto; gap: .5rem; margin-top: .75rem; }

.builder-bottom { display: grid; grid-template-columns: 1.4fr 1fr; gap: 1rem; align-items: start; }
.totals-card label { margin-bottom: .75rem; }
.totals { display: grid; grid-template-columns: 1fr auto; gap: .35rem 1rem; margin: 0; font-variant-numeric: tabular-nums; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; }
.totals .grand { color: var(--text); font-weight: 700; font-size: 1.1rem; border-top: 1px solid var(--border); padding-top: .4rem; }

.facts { display: flex; flex-wrap: wrap; gap: .75rem 2rem; margin-bottom: .5rem; }
.facts > div { display: flex; flex-direction: column; }
.facts .muted { font-size: .8rem; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: .5rem 0; border-bottom: 1px dashed var(--border); }
.timeline li:last-child { border-bottom: 0; }
.send-panel { border-color: var(--primary); }

@media (max-width: 700px) {
  .builder-bottom { grid-template-columns: 1fr; }
  .line-add { grid-template-columns: 1fr; }
}

/* ---- Phase 3: invoices ---- */
.search.filters { max-width: 720px; }
.search.filters select { width: auto; }
span.req { color: var(--req); }
.choice-list { display: flex; flex-direction: column; gap: .5rem; margin: .5rem 0 1rem; }
.choice { display: flex; flex-direction: row; align-items: flex-start; gap: .65rem; padding: .7rem .85rem; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; }
.choice > span { display: flex; flex-direction: column; gap: .1rem; }
.choice.active { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 6%, transparent); }
.choice.disabled { opacity: .55; cursor: default; }
.choice input { margin-top: .2rem; }
.choice-detail { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; padding: 0 0 .25rem 2rem; }
.choice-detail .money, .choice-detail .qty { width: 7rem; text-align: right; }
.totals-box a { color: inherit; text-decoration: underline dotted; }

/* ---- Phase 4: deliveries & jobs ---- */
.job-cards { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.job-card { color: var(--text); text-decoration: none; display: flex; flex-direction: column; gap: .3rem; }
.job-card:hover { border-color: var(--primary); }
.checks { display: flex; flex-wrap: wrap; gap: .35rem 1.25rem; margin-top: .35rem; }
.job-grid { margin: 1rem 0; }
.material-row { display: grid; grid-template-columns: 1fr 6rem 5rem auto; gap: .5rem; align-items: center; margin-bottom: .4rem; }
.photo-grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); margin-top: .5rem; }
.photo-grid figure { margin: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--bg); }
.photo-grid img { display: block; width: 100%; height: 120px; object-fit: cover; }
.photo-grid figcaption { display: flex; align-items: center; justify-content: space-between; gap: .25rem; padding: .25rem .4rem; font-size: .8rem; word-break: break-all; }
.signature-pad { position: relative; margin: .5rem 0; }
.signature-pad canvas { width: 100%; height: 180px; border: 1px dashed var(--border); border-radius: 8px; background: #fff; touch-action: none; display: block; }
.signature-line { position: absolute; left: 1rem; right: 1rem; bottom: 2.4rem; border-top: 1px solid #d0d5dd; padding-top: .15rem; pointer-events: none; }
.signature-image { max-width: 320px; max-height: 120px; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: .25rem; }

/* ---- Phase 5: payments & dashboard ---- */
.kpis { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); margin: .5rem 0 1.25rem; }
.kpi { display: flex; flex-direction: column; gap: .2rem; padding: .9rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); text-decoration: none; }
.kpi strong { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.kpi.bad strong { color: var(--bad); }
a.kpi:hover { border-color: var(--primary); }
.dash-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.dash-grid .card { min-width: 0; }
.inline-filter { flex-direction: row; align-items: center; gap: .35rem; }
.inline-filter input { width: auto; }
.deal-timeline { list-style: none; display: flex; flex-wrap: wrap; gap: 0; margin: 0 0 1rem; padding: .75rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.deal-timeline li { position: relative; flex: 1 1 120px; display: flex; flex-direction: column; gap: .1rem; padding: 0 .5rem 0 1.4rem; min-width: 110px; }
.deal-timeline .dot { position: absolute; left: 0; top: .3rem; width: .8rem; height: .8rem; border-radius: 50%; border: 2px solid var(--border); background: var(--surface); }
.deal-timeline li.done .dot { background: var(--ok); border-color: var(--ok); }
.deal-timeline .label { font-weight: 600; }
.deal-timeline li:not(.done) .label { color: var(--muted); }
@media (max-width: 700px) { .dash-grid { grid-template-columns: 1fr; } }

/* ---- Phase 6: templates ---- */
.template-types { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.template-row { display: flex; align-items: center; gap: .5rem; padding: .3rem 0; }
.swatch { width: 14px; height: 14px; border-radius: 4px; border: 1px solid var(--border); flex: none; }
.template-editor { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 1.25rem; align-items: start; }
.template-preview { position: sticky; top: 1rem; display: flex; flex-direction: column; gap: .5rem; }
.template-preview iframe { width: 100%; height: calc(100vh - 7rem); min-height: 520px; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.color-field { display: flex; gap: .4rem; }
.color-field input[type="color"] { width: 3rem; padding: .15rem; height: 2.4rem; }
.logo-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.logo-preview { max-height: 64px; max-width: 220px; background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: .25rem; }
@media (max-width: 1000px) { .template-editor { grid-template-columns: 1fr; } .template-preview { position: static; } }

/* ---- Sidebar app shell ---- */
:root { --sidebar: #1e2433; --sidebar-text: #c9cfdb; --sidebar-active: #2f6fed; --req: #d9363e; }
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  background: var(--sidebar); color: var(--sidebar-text); padding: .75rem .6rem; overflow-y: auto;
}
.sidebar-brand { display: flex; align-items: center; gap: .55rem; color: #fff; font-weight: 700; font-size: 1.05rem; padding: .35rem .5rem 1rem; text-decoration: none; }
.brand-mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--sidebar-active); color: #fff; font-size: .95rem; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar-nav a, .nav-soon {
  display: flex; align-items: center; gap: .6rem; padding: .5rem .6rem; border-radius: 7px;
  color: var(--sidebar-text); text-decoration: none; font-size: .92rem;
}
.sidebar-nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar-nav a.active { background: var(--sidebar-active); color: #fff; }
.nav-row { display: flex; align-items: center; }
.nav-row > a:first-child { flex: 1; }
.nav-row .nav-add { padding: .4rem; margin-left: 2px; }
.nav-soon { opacity: .45; cursor: default; }
.nav-soon small { margin-left: auto; font-size: .7rem; text-transform: uppercase; letter-spacing: .04em; }
.nav-section { margin: .9rem .6rem .25rem; font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; opacity: .55; }
.sidebar-foot { border-top: 1px solid rgba(255,255,255,.08); padding: .75rem .5rem 0; font-size: .82rem; }
.sidebar-company { color: #fff; font-weight: 600; }
.sidebar-user { opacity: .7; margin-bottom: .5rem; word-break: break-all; }
.btn-sidebar { background: transparent; color: var(--sidebar-text); border-color: rgba(255,255,255,.15); width: 100%; }
.main { min-width: 0; display: flex; flex-direction: column; }
.appbar { display: none; align-items: center; gap: .5rem; padding: .5rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
.appbar-company { font-weight: 600; }
.scrim { display: none; }
.app .content { max-width: 1200px; padding: 1.25rem 1.5rem 0; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 50; left: 0; top: 0; width: 240px; transform: translateX(-100%); transition: transform .2s; }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); }
  .appbar { display: flex; }
  .app .content { padding: 1rem 1rem 0; }
}

/* ---- Document form (label-left rows) ---- */
.doc-header h1 { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
.doc-form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem 1.5rem; margin-bottom: 1.25rem; }
.doc-form hr { border: 0; border-top: 1px solid var(--border); margin: .9rem 0; }
.frow { display: grid; grid-template-columns: 170px minmax(0, 340px) auto minmax(0, 200px); align-items: start; gap: .5rem 1rem; margin: .55rem 0; }
.frow > label { padding-top: .5rem; font-size: .9rem; }
.frow > label.req { color: var(--req); }
.frow .inline-label { padding-left: 1.5rem; }
.fcontrol { grid-column: 2; min-width: 0; }
.fcontrol.wide { grid-column: 2 / -1; max-width: 560px; }
.frow .inline-label + .fcontrol { grid-column: 4; }
.input-with-action { position: relative; }
.input-with-action input { padding-right: 2.2rem; background: var(--bg); }
.input-action { position: absolute; right: .55rem; top: 50%; transform: translateY(-50%); color: var(--primary); display: flex; }
.quick-add { display: grid; gap: .45rem; margin-top: .5rem; padding: .75rem; border: 1px dashed var(--border); border-radius: 8px; }
.form-actions.tight { margin: .25rem 0 0; }
.lookup-footer { color: var(--primary); font-weight: 600; border-top: 1px solid var(--border); }
.lookup-empty { cursor: default; }

/* ---- Item table ---- */
.item-table { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.item-table-head { padding: .7rem 1rem; font-weight: 600; background: color-mix(in srgb, var(--muted) 6%, var(--surface)); border-bottom: 1px solid var(--border); }
.item-table .table-wrap { border: 0; border-radius: 0; }
.lines .details { margin-top: .35rem; font-size: .88rem; }
.lines .amount { font-weight: 600; padding-top: .85rem; }
.lines input.unit { display: block; width: 5rem; margin: .3rem 0 0 auto; font-size: .82rem; text-align: right; }
.grip-col { width: 28px; padding-left: .25rem !important; padding-right: 0 !important; }
.icon-btn { display: inline-flex; padding: .2rem; border: 0; background: none; color: var(--muted); cursor: pointer; border-radius: 4px; }
.icon-btn:hover:not(:disabled) { color: var(--text); background: color-mix(in srgb, var(--muted) 12%, transparent); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.link-btn { border: 0; background: none; padding: .2rem 0; color: var(--primary); cursor: pointer; font: inherit; }
.row-actions { display: flex; gap: .5rem; margin: .75rem 0 1.25rem; flex-wrap: wrap; }
.btn-soft { display: inline-flex; align-items: center; gap: .35rem; color: var(--primary); background: color-mix(in srgb, var(--primary) 8%, var(--surface)); border-color: color-mix(in srgb, var(--primary) 20%, var(--border)); }
.bulk input[type="search"] { margin: .5rem 0; }

/* ---- Totals, notes, terms, attachments ---- */
.doc-bottom { display: grid; grid-template-columns: 1fr minmax(0, 440px); gap: 1.5rem; align-items: start; margin-bottom: 1.25rem; }
.totals-box { background: color-mix(in srgb, var(--muted) 6%, var(--surface)); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; }
.tline { display: grid; grid-template-columns: 1fr auto 6.5rem; gap: .5rem; align-items: center; padding: .35rem 0; font-variant-numeric: tabular-nums; }
.tline > span:last-child { text-align: right; }
.tline.strong { font-weight: 600; }
.tline.grand { font-weight: 700; font-size: 1.15rem; border-top: 1px solid var(--border); margin-top: .4rem; padding-top: .7rem; }
.tinput { display: inline-flex; gap: .25rem; }
.tinput input { width: 6.5rem; text-align: right; padding: .35rem .45rem; }
.tinput select { padding: .35rem; }
.adj-label { border-style: dashed; padding: .35rem .45rem; max-width: 9.5rem; }
.doc-terms { display: grid; grid-template-columns: 1fr minmax(0, 340px); gap: 1.5rem; padding: 1.25rem 0; border-top: 1px solid var(--border); }
.doc-terms > div { display: flex; flex-direction: column; gap: .4rem; }
.upload-btn { position: relative; overflow: hidden; width: fit-content; cursor: pointer; }
.upload-btn input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.upload-btn.disabled { opacity: .5; pointer-events: none; }
.attachments { list-style: none; padding: 0; margin: .25rem 0 0; }
.attachments li { display: flex; align-items: center; gap: .4rem; padding: .25rem 0; font-size: .9rem; }

.sticky-actions {
  position: sticky; bottom: 0; z-index: 10; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
  margin: 0 -1.5rem; padding: .75rem 1.5rem; background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(0,0,0,.04);
}

@media (max-width: 900px) {
  .frow { grid-template-columns: 1fr; gap: .25rem; }
  .frow > label { padding-top: 0; }
  .fcontrol, .fcontrol.wide, .frow .inline-label + .fcontrol { grid-column: 1; max-width: none; }
  .frow .inline-label { padding-left: 0; }
  .doc-bottom, .doc-terms { grid-template-columns: 1fr; }
  .sticky-actions { margin: 0 -1rem; padding: .75rem 1rem; }
}

/* ---- In-app PDF preview ---- */
.pdf-frame { position: relative; flex: 1; min-height: 0; display: flex; background: #525659; border-radius: 8px; overflow: hidden; }
.pdf-frame iframe { flex: 1; width: 100%; height: 100%; min-height: 480px; border: 0; background: #525659; }
.pdf-loading { position: absolute; inset: 0; display: grid; place-items: center; color: #fff; background: rgba(40,44,52,.55); font-size: .95rem; z-index: 1; }
.pdf-loading.error { background: var(--surface); color: var(--bad); padding: 1rem; text-align: center; }
.modal-backdrop { position: fixed; inset: 0; z-index: 90; background: rgba(15,20,25,.5); }
.modal {
  position: fixed; z-index: 91; inset: 2.5vh 2.5vw; display: flex; flex-direction: column;
  background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: 0 20px 50px rgba(0,0,0,.3); overflow: hidden;
}
.modal.pdf-modal { max-width: 1200px; margin: 0 auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .7rem 1rem; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.modal-head h2 { margin: 0; font-size: 1.05rem; }
.modal-body { flex: 1; min-height: 0; display: flex; padding: .75rem; gap: .75rem; }
.pdf-side { width: 240px; flex: none; overflow-y: auto; display: flex; flex-direction: column; gap: .4rem; padding-right: .25rem; }
.pdf-side h3 { margin: 0 0 .35rem; font-size: .95rem; }
.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: .4rem 1rem; }
@media (max-width: 700px) {
  .modal { inset: 0; border-radius: 0; }
  .modal-body.with-side { flex-direction: column; }
  .pdf-side { width: auto; max-height: 34vh; }
}

/* ---- Split list + preview (Zoho-style) ---- */
.app .content:has(.browser) { max-width: none; padding: 0; }
.browser { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: 100vh; }
.browser-list { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.browser-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .8rem .9rem .5rem; }
.view-select { border: 0; background: transparent; font-size: 1.1rem; font-weight: 600; color: var(--text); padding: .2rem 0; min-width: 0; flex: 1; cursor: pointer; }
.view-select.narrow { flex: none; font-size: .85rem; font-weight: 400; color: var(--muted); }
.browser-search { padding: 0 .9rem .6rem; border-bottom: 1px solid var(--border); }
.browser-search input { width: 100%; }
.browser-list .pad, .browser-pane .pad { padding: 1rem; }
.doc-cards { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.doc-cards li { border-bottom: 1px solid var(--border); }
.doc-cards a { display: flex; flex-direction: column; gap: .3rem; padding: .7rem .9rem; color: inherit; text-decoration: none; }
.doc-cards a .row-between { gap: .5rem; }
.doc-cards a strong:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-cards a:hover { background: color-mix(in srgb, var(--primary) 5%, var(--surface)); }
.doc-cards li.selected a { background: color-mix(in srgb, var(--primary) 11%, var(--surface)); box-shadow: inset 3px 0 0 var(--primary); }
.doc-cards li.inactive { opacity: .6; }
.browser-list .pager { padding: .5rem .9rem; border-top: 1px solid var(--border); }
.browser-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.browser-empty { display: grid; place-items: center; }
.doc-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: .75rem; padding: .8rem 1rem 1rem; }
.doc-pane-head { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.doc-pane-head h2 { margin: 0; flex: 1; display: flex; align-items: center; gap: .5rem; font-size: 1.25rem; font-weight: 500; }
.doc-pane-head .toolbar { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
.pane-back { display: none; }
.whats-next {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; padding: .7rem 1rem; border-radius: 8px;
  background: color-mix(in srgb, var(--primary) 7%, var(--surface)); border: 1px solid color-mix(in srgb, var(--primary) 18%, var(--border));
}
.whats-next > span:nth-child(2) { flex: 1; min-width: 12rem; }
.whats-next strong { font-size: .8rem; letter-spacing: .03em; margin-right: .3rem; }
.whats-next .sparkle { color: var(--primary); }
.doc-pane .pdf-frame { min-height: 480px; }

@media (max-width: 900px) {
  .browser { grid-template-columns: 1fr; height: calc(100vh - 49px); }
  .browser.has-selection .browser-list, .browser:not(.has-selection) .browser-pane { display: none; }
  .pane-back { display: inline-flex; }
  .pane-close { display: none; }
}

/* ---- Phone app: PDFs open in the viewer app instead of an iframe ---- */
.pdf-frame.no-inline { background: var(--surface); border: 1px solid var(--border); min-height: 220px; }
.pdf-native { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .6rem; padding: 1.5rem; text-align: center; color: var(--muted); }
.pdf-native strong { color: var(--text); word-break: break-all; }
.pdf-native .btn { min-width: 12rem; justify-content: center; }
.doc-pane .pdf-frame.no-inline { min-height: 220px; }

/* ---- Phone app shell (UrusNiaga.Mobile MobileLayout) ---- */
.m-app { min-height: 100vh; background: var(--bg); }
.m-top {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: .75rem;
  padding: calc(.6rem + env(safe-area-inset-top)) 1rem .6rem; background: var(--sidebar); color: #fff;
}
.m-brand { display: flex; align-items: center; gap: .5rem; color: #fff; font-weight: 700; text-decoration: none; }
.m-company { color: var(--sidebar-text); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-content { padding: 1rem 1rem calc(96px + env(safe-area-inset-bottom)); }
.m-content .page-header { flex-wrap: wrap; }
.m-content .browser { height: auto; min-height: calc(100vh - 170px); margin: -1rem -1rem 0; }
.m-content .browser-list { border-right: 0; }
.m-content .doc-cards { overflow: visible; }
.m-tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
  height: calc(64px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border); box-shadow: 0 -4px 16px rgba(0,0,0,.06);
}
.m-tabs > a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; height: 64px;
  color: var(--muted); text-decoration: none; font-size: .72rem; min-width: 0;
}
.m-tabs > a span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-tabs > a.active { color: var(--primary); font-weight: 600; }
.m-fab-slot { display: flex; justify-content: center; height: 64px; position: relative; }
.m-fab {
  position: absolute; top: -24px; width: 62px; height: 62px; border-radius: 50%; border: 5px solid var(--bg);
  display: grid; place-items: center; background: var(--primary); color: #fff; cursor: pointer;
  box-shadow: 0 6px 16px color-mix(in srgb, var(--primary) 45%, transparent); transition: transform .15s;
}
.m-fab.open { transform: rotate(90deg); }
.m-sheet-backdrop { position: fixed; inset: 0; z-index: 35; background: rgba(15,20,25,.45); }
.m-sheet {
  position: fixed; left: 0; right: 0; bottom: calc(64px + env(safe-area-inset-bottom)); z-index: 36; max-height: 70vh; overflow-y: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; padding: .5rem 1rem 1.25rem; box-shadow: 0 -10px 30px rgba(0,0,0,.18);
  animation: m-sheet-in .18s ease-out;
}
@keyframes m-sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.m-sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: .25rem auto .9rem; }
.m-quick { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin-bottom: 1rem; }
.m-quick .btn { justify-content: center; display: inline-flex; align-items: center; gap: .35rem; }
.m-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; }
.m-grid a {
  display: flex; flex-direction: column; align-items: center; gap: .4rem; padding: .8rem .25rem; border-radius: 12px;
  color: var(--text); text-decoration: none; font-size: .75rem; text-align: center; background: color-mix(in srgb, var(--muted) 7%, var(--surface));
}
.m-grid a svg { color: var(--primary); }
.m-profile { display: flex; align-items: center; gap: .9rem; }
.m-avatar { width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center; flex: none; background: var(--primary); color: #fff; font-size: 1.3rem; font-weight: 700; }
.m-section { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 1.25rem .25rem .5rem; }
.m-list { padding: 0; overflow: hidden; }
.m-list > a, .m-logout {
  display: flex; align-items: center; gap: .8rem; width: 100%; padding: .9rem 1rem; color: var(--text); text-decoration: none;
  border: 0; border-bottom: 1px solid var(--border); background: none; font: inherit; text-align: left; cursor: pointer;
}
.m-list > :last-child { border-bottom: 0; }
.m-list > a > span:nth-child(2), .m-logout span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.m-chev { color: var(--muted); font-size: 1.3rem; }
.m-logout { color: var(--bad); }
.m-version { text-align: center; margin-top: 1.5rem; }

/* ---- Organisation switcher ---- */
.org-switch { position: relative; margin-bottom: .2rem; }
.org-current {
  display: flex; align-items: center; justify-content: space-between; gap: .4rem; width: 100%; padding: .35rem .45rem; margin-left: -.45rem;
  border: 0; border-radius: 6px; background: none; color: #fff; font: inherit; font-weight: 600; text-align: left; cursor: pointer;
}
.org-current:hover { background: rgba(255,255,255,.08); }
.org-current span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.org-menu {
  position: absolute; left: -.45rem; right: -.45rem; bottom: calc(100% + 4px); z-index: 60; max-height: 60vh; overflow-y: auto;
  display: flex; flex-direction: column; padding: .3rem; border-radius: 10px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.org-menu > button, .org-menu > a {
  display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .5rem .6rem; border: 0; border-radius: 6px;
  background: none; color: var(--text); font: inherit; font-size: .88rem; text-align: left; text-decoration: none; cursor: pointer;
}
.org-menu > button:hover, .org-menu > a:hover { background: color-mix(in srgb, var(--primary) 8%, var(--surface)); }
.org-menu > button.current { font-weight: 600; box-shadow: inset 3px 0 0 var(--primary); }
.org-menu small { color: var(--muted); font-size: .75rem; }
.org-menu .org-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.org-menu > a { justify-content: flex-start; color: var(--primary); border-top: 1px solid var(--border); border-radius: 0 0 6px 6px; margin-top: .2rem; }
.org-list > button {
  display: flex; align-items: center; gap: .8rem; width: 100%; padding: .9rem 1rem; border: 0; border-bottom: 1px solid var(--border);
  background: none; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.org-list > button.current { font-weight: 600; color: var(--primary); cursor: default; }
.org-list > button:disabled { opacity: 1; }
a.m-company { text-decoration: none; }
.narrow-form { max-width: 560px; }
.narrow-form form { display: flex; flex-direction: column; gap: .75rem; }
