/* Cal FleetOps - self-contained styles (no external fonts or CDNs). */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #dde3ea;
  --text: #0f172a;
  --muted: #5b6676;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --primary-soft: #e8efff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --success: #067647;
  --success-soft: #e7f6ee;
  --warn: #b54708;
  --warn-soft: #fff4e5;
  --topbar: #0b1f3a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 .25rem; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
h2 { font-size: 1.1rem; margin: 0 0 .9rem; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1.2rem 0 .4rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.prewrap { white-space: pre-wrap; }
.warn-text { color: var(--warn); font-weight: 600; }
.inline { display: inline; margin: 0; }

/* ---- top bar ---- */
.topbar { background: var(--topbar); color: #fff; position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10; }
.topbar-inner { max-width: 1280px; margin: 0 auto; padding: .6rem 1rem; display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.brand { color: #fff; display: flex; align-items: center; gap: .55rem; font-size: 1.05rem; }
.brand:hover { text-decoration: none; }
.brand-mark { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; font-weight: 800; }
.brand-mark.lg { width: 46px; height: 46px; font-size: 1.4rem; border-radius: 12px; }
.nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; scrollbar-width: none; }
.nav a { color: #c7d2e3; padding: .45rem .75rem; border-radius: 8px; white-space: nowrap; font-weight: 500; }
.nav a:hover { color: #fff; background: rgba(255, 255, 255, .08); text-decoration: none; }
.nav a[aria-current="page"] { color: #fff; background: rgba(255, 255, 255, .14); }
.label-short { display: none; }
.who { display: flex; align-items: center; gap: .5rem; }
.who-name { color: #fff; font-weight: 600; }
.who .btn-ghost { color: #c7d2e3; border-color: rgba(255, 255, 255, .25); }
.who .btn-ghost:hover { color: #fff; background: rgba(255, 255, 255, .08); }

/* ---- layout ---- */
.page { max-width: 1280px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.crumb { margin: 0 0 .25rem; font-size: .9rem; color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; margin-bottom: 1.25rem; }
.card.narrow { max-width: 560px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; }
.two-col { display: grid; grid-template-columns: 1fr 1.4fr; gap: 1.25rem; }
.two-col > .card { margin-bottom: 0; }
.two-col + .card { margin-top: 1.25rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .9rem 1rem; }
.stack { display: flex; flex-direction: column; gap: .9rem; }
.empty { color: var(--muted); padding: 1rem 0; margin: 0; }

/* ---- forms ---- */
.field { display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.field > span { font-size: .85rem; font-weight: 600; color: #334155; }
.field em { color: var(--danger); font-style: normal; }
input, select, textarea {
  font: inherit; color: var(--text); background: #fff; border: 1px solid #c5cfdb; border-radius: 8px;
  padding: .55rem .7rem; min-height: 42px; width: 100%; transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(29, 78, 216, .18); }
input[type="radio"] { width: auto; min-height: 0; }
input[type="file"] { padding: .45rem; }
fieldset { border: 0; padding: 0; margin: 0 0 1rem; }
legend { font-size: .85rem; font-weight: 600; color: #334155; margin-bottom: .4rem; }
.type-picker { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: .75rem; }
.type-picker legend { grid-column: 1 / -1; }
.type-option { display: flex; gap: .7rem; align-items: flex-start; border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem .9rem; cursor: pointer; background: var(--surface-2); }
.type-option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 0 0 1px var(--primary) inset; }
.type-option input { margin-top: .25rem; }
.type-option small { display: block; color: var(--muted); font-weight: 400; margin-top: .15rem; }
.commercial-only { display: none; }
.is-commercial .commercial-only { display: inline; }
.filters { display: flex; gap: .9rem; align-items: flex-end; flex-wrap: wrap; }
.filters .field { min-width: 170px; }
.filters .grow { flex: 1; min-width: 220px; }
.form-actions { display: flex; justify-content: flex-end; gap: .6rem; position: sticky; bottom: 0; padding: .8rem 0 calc(.8rem + env(safe-area-inset-bottom, 0px)); background: linear-gradient(to top, var(--bg) 70%, transparent); }
.add-card summary { cursor: pointer; color: var(--primary); list-style: none; }
.add-card summary::-webkit-details-marker { display: none; }
.add-card[open] summary { margin-bottom: 1rem; }

/* ---- buttons ---- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; font: inherit; font-weight: 600; border-radius: 8px; padding: .55rem 1rem; min-height: 42px; border: 1px solid transparent; cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn:hover { text-decoration: none; }
.btn:focus-visible { outline: 3px solid rgba(29, 78, 216, .35); outline-offset: 2px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: #fff; color: var(--text); border-color: #c5cfdb; }
.btn-secondary:hover { background: var(--surface-2); border-color: #9aa8b8; }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { min-height: 34px; padding: .3rem .7rem; font-size: .88rem; }
.btn-block { width: 100%; }

/* ---- alerts & badges ---- */
.alert { border-radius: 8px; padding: .75rem 1rem; margin-bottom: 1rem; border: 1px solid transparent; }
.alert-error { background: var(--danger-soft); color: var(--danger); border-color: #f5c2bc; }
.alert-success { background: var(--success-soft); color: var(--success); border-color: #b7e4cb; }
.alert-info { background: var(--primary-soft); color: #1e3a8a; border-color: #c7d7fe; }
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: .2rem .55rem; border-radius: 999px; letter-spacing: .01em; white-space: nowrap; vertical-align: middle; }
.badge-draft { background: #eef1f5; color: #475467; }
.badge-submitted { background: #e8efff; color: #1d4ed8; }
.badge-approved { background: #e0f2fe; color: #075985; }
.badge-received { background: #f3e8ff; color: #6b21a8; }
.badge-invoiced { background: #fff4e5; color: #b54708; }
.badge-closed { background: var(--success-soft); color: var(--success); }
.badge-cancelled { background: var(--danger-soft); color: var(--danger); }
.badge-type-parts { background: #eef1f5; color: #344054; }
.badge-type-commercial { background: #fef3c7; color: #92400e; }
.badge-required { background: var(--warn-soft); color: var(--warn); font-size: .7rem; }

/* ---- KPIs ---- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.25rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; display: flex; flex-direction: column; gap: .3rem; color: var(--text); }
.kpi:hover { border-color: var(--primary); text-decoration: none; }
.kpi-label { color: var(--muted); font-size: .85rem; font-weight: 600; }
.kpi-value { font-size: 1.6rem; font-weight: 750; font-variant-numeric: tabular-nums; }
.kpi-value.warn { color: var(--warn); }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -.25rem; }
.table { width: 100%; border-collapse: collapse; font-size: .93rem; }
.table th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; padding: .6rem .6rem; border-bottom: 1px solid var(--border); white-space: nowrap; background: var(--surface-2); }
.table td { padding: .65rem .6rem; border-bottom: 1px solid #edf1f5; vertical-align: top; }
.table tbody tr:hover { background: #fafbfd; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .sub { font-size: .8rem; color: var(--muted); }
.table td:first-child a { white-space: nowrap; }
.table tr.inactive td { opacity: .55; }
.lines-table td { padding: .4rem .3rem; vertical-align: middle; }
.lines-table input, .lines-table select { min-height: 38px; padding: .4rem .5rem; }
.lines-table th:nth-child(1) { width: 14%; }
.lines-table .col-qty { width: 9%; }
.lines-table .col-price { width: 13%; }
.lines-table .col-tax { width: 9%; }
.lines-table .line-total { width: 12%; font-weight: 600; }
.lines-table .col-remove { width: 44px; text-align: right; }
#add-line { margin-top: .75rem; }
.totals-row { display: flex; justify-content: space-between; align-items: flex-end; gap: 1.5rem; flex-wrap: wrap; margin-top: 1.25rem; border-top: 1px solid var(--border); padding-top: 1rem; }
.tax-grid { grid-template-columns: repeat(2, minmax(120px, 160px)); }
.totals { display: grid; grid-template-columns: auto auto; gap: .35rem 2rem; margin: 0; font-variant-numeric: tabular-nums; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; font-weight: 600; }
.totals .grand { font-size: 1.15rem; color: var(--text); font-weight: 800; border-top: 1px solid var(--border); padding-top: .4rem; }

/* ---- PO document ---- */
.po-doc-head { display: flex; justify-content: space-between; gap: 1.5rem; flex-wrap: wrap; border-bottom: 1px solid var(--border); padding-bottom: 1rem; margin-bottom: .5rem; }
.po-doc-title { font-size: 1.2rem; font-weight: 750; }
.kv { display: grid; grid-template-columns: auto auto; gap: .2rem 1.2rem; margin: 0; }
.kv dt { color: var(--muted); font-size: .9rem; }
.kv dd { margin: 0; font-weight: 500; }
.doc-totals { margin: 1rem 0 0 auto; width: max-content; }
.invoice-list { list-style: none; padding: 0; margin: 0 0 1rem; display: flex; flex-direction: column; gap: .4rem; }
.invoice-list li { display: flex; flex-wrap: wrap; gap: .25rem .75rem; }
.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .45rem; font-size: .9rem; }

/* ---- login ---- */
.auth-page .page { min-height: 100vh; display: grid; place-items: center; padding-top: 2rem; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 10px 30px rgba(15, 23, 42, .08); padding: 2rem; }
.auth-brand { display: flex; align-items: center; gap: .9rem; margin-bottom: 1.5rem; }
.auth-brand h1 { margin: 0; font-size: 1.35rem; }
.auth-brand p { margin: 0; }

/* ---- responsive ---- */
@media (max-width: 960px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
  .two-col > .card { margin-bottom: 0; }
}
@media (max-width: 720px) {
  .topbar-inner { gap: .5rem .75rem; }
  .nav { order: 3; flex-basis: 100%; }
  .nav { display: grid; grid-template-columns: repeat(4, auto); justify-content: space-between; gap: 0; }
  .nav a { padding: .4rem .45rem; font-size: .88rem; text-align: center; }
  .label-long { display: none; }
  .label-short { display: inline; }
  .card-head h2 { font-size: 1rem; }
  .who { margin-left: auto; }
  .page { padding: 1rem 1rem 3rem; }
  h1 { font-size: 1.35rem; }
  .card { padding: 1rem; }
  .page-head .actions { width: 100%; }
  .page-head .actions .btn { flex: 1; }
  .kpi-value { font-size: 1.3rem; }
  /* PO line editor becomes stacked cards on phones. */
  .lines-table thead { display: none; }
  .lines-table, .lines-table tbody, .lines-table tr, .lines-table td { display: block; width: 100% !important; }
  .lines-table tr { border: 1px solid var(--border); border-radius: 8px; padding: .5rem .6rem; margin-bottom: .6rem; display: grid; grid-template-columns: 1fr 1fr; gap: .4rem .6rem; position: relative; }
  .lines-table td { border: 0; padding: 0; }
  .lines-table td[data-label]::before { content: attr(data-label); display: block; font-size: .75rem; font-weight: 600; color: var(--muted); margin-bottom: .15rem; }
  .lines-table .col-desc { grid-column: 1 / -1; order: -1; }
  .lines-table .line-total { text-align: left; align-self: end; }
  .lines-table .col-remove { position: absolute; top: .3rem; right: .3rem; width: auto !important; }
  .lines-table .col-desc { padding-right: 2.6rem; }
  .table-wrap:has(.lines-table) { overflow: visible; margin: 0; }
  .totals-row { flex-direction: column; align-items: stretch; }
  .tax-grid { grid-template-columns: 1fr 1fr; }
  .form-actions .btn { flex: 1; }
}

/* ---- print: the PO document only ---- */
@media print {
  body { background: #fff; font-size: 12px; }
  .topbar, .no-print, .form-actions { display: none !important; }
  .page { padding: 0; max-width: none; }
  .card { box-shadow: none; border: 0; padding: 0; }
  .table th { background: none; }
  a { color: inherit; }
}
