/* ContractorOS 360 - mobile-first UI. Brand colors are overridden at runtime
   from Company Settings (--brand, --accent). */
:root {
  --brand: #0f2a4a;
  --brand-ink: #ffffff;
  --accent: #f08a24;
  --accent-ink: #ffffff;
  --ok: #16a34a;
  --warn: #ea8a0c;
  --bad: #dc2626;
  --info: #2563eb;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --line: #e2e8f0;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #94a3b8;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .06);
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --nav-h: 64px;
  color-scheme: light;
}
:root[data-font="system"] { --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; }
:root[data-font="serif"] { --font: Georgia, 'Times New Roman', serif; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220; --surface: #111a2c; --surface-2: #0f1727; --line: #22304a;
    --ink: #e5e9f0; --ink-2: #a9b4c6; --ink-3: #6b7a93; color-scheme: dark;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220; --surface: #111a2c; --surface-2: #0f1727; --line: #22304a;
  --ink: #e5e9f0; --ink-2: #a9b4c6; --ink-3: #6b7a93; color-scheme: dark;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
a { color: var(--info); text-decoration: none; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 17px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 650; }
.muted { color: var(--ink-2); }
.tiny { font-size: 12px; color: var(--ink-3); }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--brand); color: var(--brand-ink);
}
.topbar .brand-logo { width: 32px; height: 32px; border-radius: 8px; object-fit: contain; background: #fff; }
.topbar .brand-mark { width: 32px; height: 32px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; font-weight: 800; color: var(--accent-ink); }
.topbar .t { flex: 1; min-width: 0; }
.topbar .t b { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .t span { display: block; font-size: 12px; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .icon-btn { color: var(--brand-ink); }

.layout { display: block; }
.sidebar { display: none; }
main { padding: 16px; padding-bottom: calc(var(--nav-h) + 32px + env(safe-area-inset-bottom)); max-width: 1100px; margin: 0 auto; }

.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.bottom-nav a, .bottom-nav button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--ink-3); font-size: 11px; font-weight: 600; background: none; border: 0; font-family: inherit;
}
.bottom-nav a.on { color: var(--brand); }
:root[data-theme="dark"] .bottom-nav a.on { color: var(--accent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .bottom-nav a.on { color: var(--accent); } }
.bottom-nav svg { width: 24px; height: 24px; }
.fab {
  width: 54px; height: 54px; margin-top: -22px; border-radius: 50%; background: var(--accent) !important;
  color: var(--accent-ink) !important; box-shadow: 0 6px 18px rgba(240, 138, 36, .45); justify-self: center; align-self: start;
}
.fab svg { width: 28px; height: 28px; }

@media (min-width: 900px) {
  .layout { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }
  .sidebar {
    display: flex; flex-direction: column; gap: 4px; padding: 16px 12px; background: var(--brand); color: var(--brand-ink);
    position: sticky; top: 0; height: 100vh; overflow-y: auto;
  }
  .sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 18px; }
  .sidebar .brand img, .sidebar .brand .brand-mark { width: 38px; height: 38px; border-radius: 9px; background: #fff; object-fit: contain; }
  .sidebar .brand .brand-mark { background: var(--accent); display: grid; place-items: center; font-weight: 800; color: var(--accent-ink); }
  .sidebar .brand b { font-size: 15px; }
  .sidebar a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; color: inherit; opacity: .8; font-weight: 550; }
  .sidebar a:hover { background: rgba(255,255,255,.08); opacity: 1; }
  .sidebar a.on { background: rgba(255,255,255,.14); opacity: 1; }
  .sidebar svg { width: 20px; height: 20px; }
  .sidebar .sep { height: 1px; background: rgba(255,255,255,.15); margin: 10px 4px; }
  .sidebar .new-btn { margin: 0 0 14px; }
  .topbar { display: none; }
  .bottom-nav { display: none; }
  main { padding: 28px 32px 48px; }
}

/* ---------- building blocks ---------- */
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { flex: 1; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card, .stack > * + * { margin-top: 14px; }
.card h2 { margin-bottom: 12px; }
.grid { display: grid; gap: 12px; }
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 700px) { .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.kpi .l { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.kpi .v { font-size: 20px; font-weight: 750; margin-top: 4px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi .s { font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.kpi.ok .v { color: var(--ok); } .kpi.warn .v { color: var(--warn); }
.two { display: grid; gap: 14px; }
@media (min-width: 900px) { .two { grid-template-columns: 1fr 1fr; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px;
  border-radius: 11px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: 600 15px var(--font); cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.brand { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn svg { width: 18px; height: 18px; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 10px; background: none; border: 0; color: var(--ink-2); cursor: pointer; }
.icon-btn svg { width: 22px; height: 22px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* forms */
.form { display: grid; gap: 14px; }
.form .row { display: grid; gap: 14px; }
@media (min-width: 640px) { .form .row { grid-template-columns: 1fr 1fr; } .form .row.three { grid-template-columns: 1fr 1fr 1fr; } }
label.f { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); min-width: 0; }
label.f small { font-weight: 400; color: var(--ink-3); }
input, select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--ink); font: 16px var(--font); /* 16px avoids iOS zoom */
}
textarea { min-height: 96px; resize: vertical; }
input[type="color"] { padding: 4px; height: 46px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px; border-color: var(--accent); }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--ink); min-height: 44px; }
.check input { width: 22px; height: 22px; min-height: 0; }
.field-error { outline: 2px solid var(--bad) !important; }
.help { font-size: 13px; color: var(--ink-2); background: var(--surface-2); border: 1px dashed var(--line); border-radius: 10px; padding: 10px 12px; }

/* lists */
.list { display: grid; gap: 0; }
.list > a, .list > .li {
  display: flex; align-items: center; gap: 12px; padding: 12px 4px; border-bottom: 1px solid var(--line); color: var(--ink); min-width: 0;
}
.list > a:last-child, .list > .li:last-child { border-bottom: 0; }
.list .main { flex: 1; min-width: 0; }
.list .main b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.list .main span { display: block; font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .side { text-align: right; flex-shrink: 0; }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; flex-shrink: 0;
  background: color-mix(in srgb, var(--brand) 14%, var(--surface)); color: var(--brand); }
:root[data-theme="dark"] .avatar { color: var(--ink); }
.empty { text-align: center; padding: 32px 12px; color: var(--ink-2); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 650; white-space: nowrap;
  background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); }
.badge.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); border-color: transparent; }
.badge.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); border-color: transparent; }
.badge.bad { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); border-color: transparent; }
.badge.info { background: color-mix(in srgb, var(--info) 14%, transparent); color: var(--info); border-color: transparent; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 14px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex-shrink: 0; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--ink-2); font: 600 13px var(--font); cursor: pointer; }
.chip.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.searchbar { position: relative; margin-bottom: 12px; }
.searchbar input { padding-left: 40px; }
.searchbar svg { position: absolute; left: 12px; top: 13px; width: 20px; height: 20px; color: var(--ink-3); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kv .strong { font-weight: 750; font-size: 17px; color: var(--ink); }
.kv hr { grid-column: 1 / -1; border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }

/* estimate items */
.item-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: var(--surface-2); display: grid; gap: 10px; }
.item-card .head { display: flex; gap: 8px; align-items: center; }
.item-card .head select { flex: 1; }
.item-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (min-width: 700px) { .item-grid { grid-template-columns: repeat(4, 1fr); } }
.item-total { display: flex; justify-content: space-between; font-weight: 650; font-size: 14px; }
.totals-panel { position: sticky; bottom: calc(var(--nav-h) + 8px + env(safe-area-inset-bottom)); z-index: 5; }
@media (min-width: 900px) { .totals-panel { bottom: 12px; } }
.editor-bar { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.editor-bar .t { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

.timeline { display: grid; gap: 10px; font-size: 13px; }
.timeline div { border-left: 3px solid var(--line); padding-left: 10px; }

/* chart */
.chart svg { width: 100%; height: auto; display: block; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* sheet / modal */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(15, 23, 42, .5); display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto; background: var(--surface); border-radius: 20px 20px 0 0; padding: 18px 16px; padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
@media (min-width: 700px) { .overlay { align-items: center; } .sheet { border-radius: 18px; } }
.sheet h2 { margin-bottom: 12px; }
.sheet .grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: -6px auto 12px; }
.qa { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.qa button { min-height: 72px; flex-direction: column; gap: 4px; white-space: normal; text-align: center; font-size: 13px; }
.qa small { font-weight: 500; color: var(--ink-3); font-size: 11px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600; font-size: 14px; max-width: calc(100% - 32px); box-shadow: var(--shadow); }
.toast.bad { background: var(--bad); color: #fff; }
.banner { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); color: var(--ink); border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); padding: 10px 14px; border-radius: 12px; font-size: 13px; margin-bottom: 14px; }
.banner.info { background: color-mix(in srgb, var(--info) 10%, var(--surface)); border-color: color-mix(in srgb, var(--info) 30%, transparent); }

/* auth / setup */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(160deg, var(--brand), color-mix(in srgb, var(--brand) 70%, #000)); }
.auth .card { width: 100%; max-width: 440px; }
.auth.wide .card { max-width: 640px; }
.auth .logo-row { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.steps { display: flex; gap: 4px; margin: 6px 0 18px; }
.steps i { flex: 1; height: 5px; border-radius: 3px; background: var(--line); }
.steps i.on { background: var(--accent); }
.logo-preview { width: 96px; height: 96px; border-radius: 14px; border: 1px dashed var(--line); display: grid; place-items: center; overflow: hidden; background: #fff; }
.logo-preview img { max-width: 100%; max-height: 100%; }

/* ---------- printable document ---------- */
.doc-wrap { background: #e5e7eb; margin: -16px; padding: 16px; min-height: 100vh; }
.doc-tools { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.doc {
  background: #fff; color: #111827; max-width: 816px; margin: 0 auto; padding: 40px 44px; border-radius: 6px; box-shadow: 0 4px 24px rgba(0,0,0,.12);
  font: 13px/1.45 var(--font); --doc-brand: var(--brand); --doc-accent: var(--accent);
}
.doc.a4 { max-width: 794px; }
.doc header { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; border-bottom: 3px solid var(--doc-brand); padding-bottom: 18px; }
.doc header img { max-width: 160px; max-height: 70px; object-fit: contain; }
.doc .co b { font-size: 16px; color: var(--doc-brand); }
.doc .title { text-align: right; }
.doc .title h1 { color: var(--doc-brand); font-size: 26px; letter-spacing: .06em; }
.doc .meta { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin: 20px 0; }
.doc .meta h4 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #6b7280; }
.doc table { width: 100%; border-collapse: collapse; margin: 8px 0 14px; }
.doc th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: #fff; background: var(--doc-brand); padding: 8px; }
.doc td { padding: 8px; border-bottom: 1px solid #e5e7eb; vertical-align: top; }
.doc td.r, .doc th.r { text-align: right; white-space: nowrap; }
.doc .cat td { background: #f3f4f6; font-weight: 650; font-size: 12px; }
.doc .sum { margin-left: auto; width: 300px; max-width: 100%; }
.doc .sum div { display: flex; justify-content: space-between; padding: 4px 0; }
.doc .sum .grand { border-top: 2px solid var(--doc-brand); margin-top: 6px; padding-top: 8px; font-size: 17px; font-weight: 800; color: var(--doc-brand); }
.doc section { margin-top: 18px; }
.doc section h4 { margin: 0 0 4px; color: var(--doc-brand); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
.doc .pre { white-space: pre-wrap; }
.doc .sign { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-top: 40px; }
.doc .sign div { border-top: 1px solid #9ca3af; padding-top: 6px; font-size: 12px; color: #6b7280; }
.doc footer { margin-top: 28px; padding-top: 10px; border-top: 1px solid #e5e7eb; font-size: 11px; color: #6b7280; text-align: center; }
.doc .show-sm { display: none; }
@media (max-width: 640px) {
  .doc .show-sm { display: block; }
  .doc { padding: 20px 16px; }
  .doc header { flex-direction: column; } .doc .title { text-align: left; }
  .doc .meta { grid-template-columns: 1fr; }
  .doc .hide-sm { display: none; }
}
@media print {
  body { background: #fff; }
  .topbar, .sidebar, .bottom-nav, .doc-tools, .banner, .toast { display: none !important; }
  .layout { display: block; }
  main { padding: 0 !important; max-width: none; }
  .doc-wrap { background: #fff; margin: 0; padding: 0; }
  .doc { box-shadow: none; border-radius: 0; max-width: none; padding: 0; }
  .doc .hide-sm { display: table-cell; }
  .doc .show-sm { display: none; }
  .doc tr { break-inside: avoid; }
}
