:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --ink: #1b2430;
  --muted: #667085;
  --line: #dde2ea;
  --brand: #1f3a5f;
  --brand-2: #2d5a8e;
  --accent: #e8f0fa;
  --ok: #1e7b4f;
  --warn: #b54708;
  --bad: #b42318;
  --radius: 10px;
  --tap: 44px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 12px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
a { color: var(--brand-2); }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; min-height: var(--tap); padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: #fff;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: -1px; border-color: var(--brand-2); }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--ink); font-size: 15px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.msg { min-height: 20px; color: var(--bad); margin: 8px 0 0; }

.btn {
  min-height: var(--tap); padding: 8px 16px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--accent); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 13px; }
.btn.big { width: 100%; font-size: 16px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { min-width: var(--tap); min-height: var(--tap); border: 0; background: none; cursor: pointer; font-size: 18px; color: var(--muted); border-radius: 8px; }
.icon-btn:hover { background: #fde8e8; color: var(--bad); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.center-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; text-align: center; }
.login-card label { text-align: left; }
.login-logo { width: 64px; height: 64px; margin: 0 auto; border-radius: 14px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 0 16px; min-height: 56px; background: var(--brand); color: #fff; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 17px; }
#nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
#nav a { color: #dce6f3; text-decoration: none; padding: 0 14px; line-height: 40px; border-radius: 8px; }
#nav a:hover { background: rgba(255,255,255,.1); }
#nav a.active { background: rgba(255,255,255,.18); color: #fff; font-weight: 600; }
#nav a.nav-new { background: #fff; color: var(--brand); font-weight: 600; }
.top-right { display: flex; gap: 8px; align-items: center; }
.pill { border: 0; border-radius: 999px; padding: 6px 12px; font-size: 13px; background: rgba(255,255,255,.15); color: #fff; cursor: pointer; }
.pill.ok { background: rgba(30,123,79,.85); }
.pill.warn { background: rgba(181,71,8,.9); }
.pill.who { cursor: default; }

main { padding: 16px; max-width: 1280px; margin: 0 auto; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .grow { flex: 1; min-width: 200px; }
.toolbar h2 { margin: 0; margin-right: auto; }

/* Lists */
.list { display: grid; gap: 10px; }
.row-card { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.row-card .title { font-weight: 600; font-size: 16px; }
.row-card .meta { color: var(--muted); font-size: 13px; }
.row-card .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.row-card .total { font-weight: 700; font-size: 16px; }
.expiry { font-size: 12px; color: var(--warn); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

table.grid { width: 100%; border-collapse: collapse; background: #fff; }
table.grid th, table.grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.grid th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #f8f9fb; }
table.grid td.num, table.grid th.num { text-align: right; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 6px; border-radius: 4px; background: var(--accent); color: var(--brand); margin-right: 6px; vertical-align: 1px; }

/* Estimate editor */
.editor { display: grid; gap: 14px; }
.ed-head { display: grid; grid-template-columns: 1fr auto; gap: 14px; align-items: start; }
.ed-no { font-size: 13px; color: var(--muted); }
.ed-no b { color: var(--ink); font-size: 18px; }
.ed-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.dirty-dot { color: var(--warn); font-size: 13px; }

.picker { position: relative; }
.suggest {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 30; margin: 4px 0 0; padding: 4px; list-style: none;
  background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(16,24,40,.14);
  max-height: 340px; overflow-y: auto;
}
.suggest li { padding: 9px 10px; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.suggest li.hl, .suggest li:hover { background: var(--accent); }
.suggest li .sub { color: var(--muted); font-size: 12px; white-space: nowrap; }
.suggest li.add-new { color: var(--brand-2); font-weight: 600; }

.cust-box { display: flex; gap: 12px; align-items: center; justify-content: space-between; }
.cust-box .name { font-weight: 700; font-size: 16px; }

.lines .ln-pack { min-width: 104px; }
.lines input.ln-pcs { width: 80px; text-align: right; }
.lines input.ln-special { width: 110px; text-align: right; }
.lines td.ln-item { min-width: 200px; }
.lines .unavail { color: var(--bad); font-size: 12px; }
.add-item { margin-top: 10px; }

.bottom { display: grid; grid-template-columns: 1fr minmax(320px, 420px); gap: 16px; align-items: start; }
.totals { display: grid; gap: 8px; }
.totals .t-row { display: grid; grid-template-columns: 1fr 140px; gap: 8px; align-items: center; }
.totals .t-row > span:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.totals .t-row input { text-align: right; }
.totals .charge { grid-template-columns: 1fr 120px 44px; }
.totals .disc { grid-template-columns: 1fr 76px 110px; }
.totals .grand { font-size: 20px; font-weight: 700; border-top: 2px solid var(--ink); padding-top: 8px; }
.totals .gst { text-align: right; font-weight: 600; color: var(--brand); }
.terms-box { white-space: pre-line; color: var(--muted); font-size: 13px; }

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.section { margin-bottom: 18px; }
.logo-prev { max-height: 70px; max-width: 220px; display: block; margin: 6px 0; border: 1px solid var(--line); border-radius: 6px; padding: 4px; background: #fff; }

dialog { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(16,24,40,.45); }
#dlgForm { padding: 20px; display: grid; gap: 12px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; flex-wrap: wrap; }
.dev-codes { display: flex; gap: 6px; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100; background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: calc(100vw - 32px);
}
.toast.bad { background: var(--bad); }

/* Tablet portrait / small laptops */
@media (max-width: 900px) {
  .bottom { grid-template-columns: 1fr; }
  .ed-head { grid-template-columns: 1fr; }
  .ed-actions { justify-content: flex-start; }
}
/* Narrow screens: estimate lines become cards */
@media (max-width: 720px) {
  .topbar { padding: 8px 12px; }
  #nav a { padding: 0 10px; }
  main { padding: 12px; }
  .lines thead { display: none; }
  .lines tr { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; padding: 10px; border-bottom: 1px solid var(--line); }
  .lines td { border: 0; padding: 0; }
  .lines td.ln-item { grid-column: 1 / -1; font-weight: 600; }
  .lines td[data-l]::before { content: attr(data-l); display: block; font-size: 11px; color: var(--muted); }
  .lines td.ln-x { position: absolute; right: 8px; }
  .lines tr { position: relative; }
  .row-card { grid-template-columns: 1fr; }
  .row-card .actions { justify-content: flex-start; }
}
@media print { .topbar { display: none; } }
.btn.danger-bg { background: var(--bad); border-color: var(--bad); color: #fff; }
.totals .t-row input.ch-label { text-align: left; }
.editor > *, .bottom > * { min-width: 0; }
@media (max-width: 1000px) {
  .lines th, .lines td { padding: 6px 5px; }
  .lines th { font-size: 11px; letter-spacing: 0; }
  .lines td.ln-item { min-width: 130px; }
  .lines .ln-pack { min-width: 0; width: 92px; padding: 6px; }
  .lines input.ln-pcs { width: 60px; }
  .lines input.ln-special { width: 84px; }
  .lines .icon-btn { min-width: 34px; }
}

/* customer card, quantity line, terms box */
.cust-card { background: #fbf1d3; border-color: #e6cf8f; }
.cust-card h3 { margin: 4px 0 10px; }
.cust-card .ed-no { margin-bottom: 8px; }
.items-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.qty-total { font-size: 15px; color: var(--muted); margin-bottom: 12px; }
.qty-total b { color: var(--ink); }
.qty-line { margin: 0 0 14px; font-size: 15px; }
.terms-box { background: #fffbdc; border: 1px solid #efe1a0; border-radius: 8px; padding: 10px 12px; color: var(--ink); }
