:root {
  --harris-blue: #205e8f;
  --harris-blue-dark: #184e78;
  --harris-blue-deep: #12456b;
  --harris-gray: #8c9199;
  --ink: #1d2733;
  --ink-soft: #55606b;
  --line: #d7dde3;
  --bg: #f4f6f8;
  --card: #ffffff;
  --good: #1c7c3f;
  --warn: #b5590a;
  --bad: #b3261e;
  --internal-bg: #fff8e8;
  --internal-border: #e8c76b;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
}

body { font-size: 14px; line-height: 1.45; }

img.logo { height: 42px; display: block; }

/* ---------- Sign-in Gate / Division Picker ---------- */
#gate, #divisionGate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #0f3654, var(--harris-blue) 70%);
}
#gate .card, #divisionGate .card {
  background: var(--card);
  padding: 40px 36px;
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
  width: 340px;
  text-align: center;
}
#gate img.logo, #divisionGate img.logo { margin: 0 auto 22px; height: 46px; }
#gate button {
  width: 100%;
  padding: 10px 12px;
  font-size: 15px;
}
#divisionGate .card { width: 420px; }
.division-cards { display: flex; gap: 12px; }
.division-card {
  flex: 1;
  padding: 22px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
  white-space: normal;
}
.division-card .dtitle { font-size: 14px; font-weight: 800; }
.division-card .dsub { font-size: 11px; color: var(--ink-soft); font-weight: 400; }
.google-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 14px;
  background: #fff;
  color: #3c4043;
  border: 1px solid #dadce0 !important;
  font-weight: 600;
}
.google-btn:hover { background: #f7f8f8; border-color: #c6c6c6 !important; color: #3c4043 !important; }
.google-icon { flex-shrink: 0; }
#gate .err { color: var(--bad); font-size: 12.5px; min-height: 18px; margin-top: 10px; }
#gate p.hint { color: var(--ink-soft); font-size: 12.5px; margin-top: 18px; }

/* ---------- App Shell ---------- */
#app { display: none; }
header.topbar {
  background: #fff;
  border-bottom: 1px solid var(--line);
  padding: 12px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 20;
}
header.topbar .left { display: flex; align-items: center; gap: 16px; }
header.topbar h1 {
  font-size: 15px;
  margin: 0;
  color: var(--ink-soft);
  font-weight: 600;
  border-left: 1px solid var(--line);
  padding-left: 16px;
}
nav.tabs { display: flex; gap: 6px; }
nav.tabs button {
  background: none;
  border: none;
  padding: 8px 16px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
  border-radius: 8px;
}
nav.tabs button.active { background: var(--harris-blue); color: #fff; }
header.topbar .repbox { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--ink-soft); }
header.topbar .repbox input { border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; font-size: 12.5px; width: 150px; }
header.topbar .repbox input[readonly] { background: var(--bg); color: var(--ink); cursor: default; }

main { max-width: 1680px; margin: 0 auto; padding: 20px 22px 60px; }

.view { display: none; }
.view.active { display: block; }

/* ---------- Buttons ---------- */
button {
  font-family: inherit;
  cursor: pointer;
}
.btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 600;
}
.btn:hover { border-color: var(--harris-blue); color: var(--harris-blue); }
.btn.primary { background: var(--harris-blue); color: #fff; border-color: var(--harris-blue); }
.btn.primary:hover { background: var(--harris-blue-deep); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-soft); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Layout: builder + preview ----------
   minmax(0, Nfr) — not minmax(Xpx, Yfr) — is what prevents the classic
   CSS grid "blowout": with a plain fr track, a grid item's content can
   never shrink below its min-content width, so a wide table forces the
   whole track wider and spills into the sibling column. minmax(0, ...)
   allows the track to shrink below that, and .table-scroll below
   provides horizontal scrolling as a fallback if it's ever still tight. */
.builder-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}
@media (max-width: 1300px) {
  .builder-grid { grid-template-columns: 1fr; }
  #previewWrap { position: static; max-width: 760px; margin: 0 auto; }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
}
.card h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--ink-soft);
  margin: 0 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field label { font-size: 11.5px; font-weight: 600; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .03em; }
.field input, .field select, .field textarea {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 13.5px;
  font-family: inherit;
  width: 100%;
}
.field textarea { resize: vertical; min-height: 44px; }

/* Internal / sales-only section */
.card.internal {
  background: var(--internal-bg);
  border-color: var(--internal-border);
}
.card.internal h2 { color: #8a6415; }
.internal-flag {
  display: inline-block;
  background: var(--internal-border);
  color: #5b4408;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 20px;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.col-toggles {
  display: flex;
  gap: 18px;
  margin: -6px 0 12px;
  font-size: 12px;
  color: var(--ink-soft);
}
.col-toggles label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.col-toggles input[type=checkbox] { cursor: pointer; }

.email-status-note {
  font-size: 12px;
  color: var(--ink-soft);
  margin-right: auto;
}
.email-status-note.sent { color: var(--good); font-weight: 600; }

/* Line items table
   table-layout: fixed makes the browser honor the declared <th> widths
   instead of growing columns to fit input min-content — that's what was
   causing Ext. Price to push past the card edge. .table-scroll is a
   fallback: if the window is ever too narrow for all 8 columns, the
   table scrolls horizontally within its own card instead of spilling
   into the preview panel next to it. */
.table-scroll { overflow-x: auto; }
table.items { width: 100%; min-width: 640px; table-layout: fixed; border-collapse: collapse; font-size: 13px; }
table.items th {
  text-align: left;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--ink-soft);
  border-bottom: 2px solid var(--line);
  padding: 6px 6px;
  overflow-wrap: break-word;
}
table.items td { padding: 5px 6px; border-bottom: 1px solid var(--line); vertical-align: top; overflow: hidden; }
table.items td.cost-col, table.items th.cost-col { background: var(--internal-bg); }
table.items input {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 7px;
  font-size: 12.5px;
  width: 100%;
  min-width: 0;
}
table.items input.num { text-align: right; }
table.items td.rowtotal { text-align: right; font-weight: 600; white-space: nowrap; overflow: visible; }
table.items td.del { text-align: center; }

.addrow { margin-top: 10px; }

/* Summary strip */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-top: 6px;
}
.summary-tile {
  background: #fff;
  border: 1px solid var(--internal-border);
  border-radius: 8px;
  padding: 10px 12px;
}
.summary-tile .lbl { font-size: 10.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--ink-soft); }
.summary-tile .lbl span[title] { cursor: help; opacity: .7; text-transform: none; letter-spacing: normal; }
.summary-tile .val { font-size: 18px; font-weight: 700; margin-top: 2px; }
.summary-tile .val.good { color: var(--good); }
.summary-tile .val.bad { color: var(--bad); }

/* ---------- Quote Preview (matches Harris letterhead) ----------
   Capped narrower on screen (it's a preview, not the working area) so
   the builder column on the left gets the room it needs. The cap is
   screen-only — see @media print below, which removes it so the
   printed page uses full letter width. */
#previewWrap { position: sticky; top: 74px; max-width: 640px; margin-left: auto; }
.quote-sheet {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 30px;
  min-height: 700px;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
  font-size: 12.5px;
}
.qhead { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; }
.qhead .qtitle { text-align: right; }
.qhead .qtitle .lbl { font-size: 22px; font-weight: 800; color: var(--ink); letter-spacing: .04em; }
.qhead .qtitle .num { font-size: 12.5px; color: var(--ink-soft); margin-top: 2px; }

.qinfo { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 22px; font-size: 12.5px; }
.qinfo .block { flex: 1; }
.qinfo .block .h { font-weight: 700; color: var(--ink-soft); margin-bottom: 3px; }
.qinfo table.meta { font-size: 12.5px; }
.qinfo table.meta td { padding: 1px 0; }
.qinfo table.meta td.k { color: var(--ink-soft); padding-right: 10px; white-space: nowrap; }

table.qitems { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 16px; }
table.qitems thead th {
  background: var(--harris-blue);
  color: #fff;
  text-align: left;
  padding: 7px 8px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .03em;
}
table.qitems thead th.num { text-align: right; }
table.qitems tbody td { padding: 5px 8px; border-bottom: 1px solid #eef1f4; }
table.qitems tbody td.num { text-align: right; white-space: nowrap; }
table.qitems tbody tr.sub td { font-style: italic; color: var(--ink-soft); padding-left: 20px; }

table.qtotals { margin-left: auto; width: 260px; font-size: 12.5px; border-collapse: collapse; }
table.qtotals td { padding: 4px 8px; }
table.qtotals td.k { text-align: left; color: var(--ink-soft); }
table.qtotals td.v { text-align: right; font-weight: 600; }
table.qtotals tr.total td { border-top: 2px solid var(--ink); font-size: 15px; font-weight: 800; padding-top: 8px; }

/* Film & Packaging preview: spec label/value rows (Shape:, Size:, etc.).
   table.qitems is reused as-is for the price-breaks preview table since
   it's structurally identical (header row, right-aligned numeric cells). */
table.qspecs { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-bottom: 18px; }
table.qspecs td { padding: 3px 0; vertical-align: top; }
table.qspecs td.k { font-weight: 700; color: var(--ink); padding-right: 12px; white-space: nowrap; width: 150px; }
table.qspecs td.v { color: var(--ink); }
.qcomments { font-size: 11.5px; color: var(--ink); white-space: pre-line; margin-bottom: 18px; }
.qcomments .h { font-weight: 700; color: var(--ink-soft); margin-bottom: 4px; }

.qterms { margin-top: 26px; font-size: 9.5px; line-height: 1.5; color: var(--ink-soft); border-top: 1px solid var(--line); padding-top: 10px; }
.qterms .h { font-weight: 700; margin-bottom: 4px; text-decoration: underline; }
.qterms p { margin: 6px 0 0; }
.qterms p:first-of-type { margin-top: 0; }
.qsign { display: flex; gap: 40px; margin-top: 30px; font-size: 11px; color: var(--ink-soft); }
.qsign .ln { flex: 1; border-top: 1px solid var(--ink); padding-top: 4px; }

/* ---------- History view ---------- */
.history-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.history-toolbar input[type=search] {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  min-width: 240px;
}
.history-toolbar select,
.history-toolbar input[type=date] {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 12.5px;
  color: var(--ink);
  background: #fff;
}
.history-toolbar .date-filter { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); }
.history-stats { display: flex; gap: 14px; margin-left: auto; font-size: 12.5px; color: var(--ink-soft); }
.history-stats b { color: var(--ink); }

table.history { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; font-size: 12.5px; }
table.history th {
  text-align: left;
  background: #eef2f6;
  padding: 9px 10px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
}
table.history td { padding: 9px 10px; border-top: 1px solid var(--line); white-space: nowrap; }
table.history tr:hover td { background: #f7f9fb; }
table.history td.num { text-align: right; }
table.history a.pdf { color: var(--harris-blue); font-weight: 600; text-decoration: none; }
table.history a.pdf:hover { text-decoration: underline; }
.gp-pos { color: var(--good); font-weight: 700; }
.gp-neg { color: var(--bad); font-weight: 700; }

.empty-state, .loading-state, .error-state {
  text-align: center;
  padding: 50px 20px;
  color: var(--ink-soft);
  background: #fff;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.error-state { color: var(--bad); }

.toast {
  position: fixed;
  bottom: 22px;
  right: 22px;
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  opacity: 0;
  transform: translateY(10px);
  transition: all .25s ease;
  pointer-events: none;
  z-index: 100;
  max-width: 320px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--bad); }
.toast.success { background: var(--good); }

/* ---------- Print ---------- */
@media print {
  header.topbar, .builder-grid > div:first-child, .history-toolbar, #previewActions, .toast { display: none !important; }
  main { padding: 0; max-width: none; }
  .builder-grid { display: block; }
  #previewWrap { position: static; max-width: none; margin: 0; }
  .quote-sheet { box-shadow: none; border: none; padding: 0; font-size: 13px; }
  body { background: #fff; }
}
