:root {
  --crimson: #a8123e;
  --crimson-dark: #7d0e2e;
  --ink: #1c1c22;
  --muted: #6b6b76;
  --border: #e2e0e4;
  --bg: #f5f4f7;
  --card: #ffffff;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex; flex-direction: row;
  background: #fafafa;
}
.login-visual {
  position: relative;
  flex: 1 1 50%;
  min-height: 100vh;
  background-image: url('https://images.unsplash.com/photo-1758873268745-dd2cf0d677b5?fm=jpg&q=70&w=1600&auto=format&fit=crop');
  background-size: cover;
  background-position: center;
}
.login-visual-overlay {
  position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(20,18,22,0.45) 0%, rgba(20,18,22,0.7) 100%);
  display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-end;
  padding: 48px;
}
.login-visual .login-mark {
  width: 40px; height: 40px; margin: 0 0 20px;
  border-radius: 10px;
  background: var(--crimson);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px;
  letter-spacing: -0.02em;
}
.login-hero-title {
  color: #fff; font-size: 28px; font-weight: 700; letter-spacing: -0.01em;
  max-width: 400px; margin: 0; line-height: 1.3;
  text-align: left;
}
.login-panel {
  flex: 1 1 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 28px;
  padding: 40px 24px;
}
.login-card {
  background: #fff; border-radius: 14px; padding: 40px 40px;
  width: 100%; max-width: 360px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 2px rgba(20,18,22,0.04), 0 12px 32px rgba(20,18,22,0.06);
  text-align: center;
}
.login-card h2 {
  font-size: 19px; font-weight: 700; letter-spacing: -0.01em;
  margin: 0 0 6px; color: var(--ink);
}
.login-sub { color: var(--muted); font-size: 13.5px; margin: 0 0 28px; }
.login-card label {
  text-align: left; font-size: 12px; font-weight: 500;
  color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
  margin-bottom: 16px;
}
.login-card label input {
  margin-top: 6px; padding: 11px 13px; font-size: 14px;
  border: 1px solid var(--border); border-radius: 8px;
  transition: border-color .12s, box-shadow .12s;
}
.login-card label input:focus {
  outline: none; border-color: var(--crimson);
  box-shadow: 0 0 0 3px rgba(168,18,62,0.1);
}
.login-error {
  background: #fbeaea; color: #b3261e; border: 1px solid #f2c9c7;
  border-radius: 8px; padding: 9px 11px; font-size: 12.5px; margin-bottom: 14px;
  text-align: left;
}
.btn-block { width: 100%; padding: 12px; font-size: 14px; font-weight: 600; border-radius: 8px; }
.login-footer {
  font-size: 12px; color: var(--muted); letter-spacing: .02em;
}
@media (max-width: 780px) {
  .login-screen { flex-direction: column; }
  .login-visual { display: none; }
  .login-panel { min-height: 100vh; }
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px;
  padding: 10px 24px;
  background: var(--ink);
  color: #fff;
  position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
.topbar-brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--crimson);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
}
.brand-title { font-weight: 600; font-size: 15px; }
.brand-sub { font-size: 11px; color: #b7b7c2; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.whoami {
  font-size: 12.5px; color: #cfcfd8;
  background: transparent; border: 1px solid transparent; border-radius: 7px;
  padding: 6px 10px; cursor: pointer; font-family: inherit;
}
.whoami:hover { background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.15); }
.whoami .role-badge { margin-left: 6px; }
.modal-box-small { max-width: 380px; }

.topnav { display: flex; gap: 4px; }
.navtab {
  background: transparent; border: none; color: #cfcfd8;
  padding: 8px 14px; border-radius: 7px; font-size: 13px; cursor: pointer;
}
.navtab:hover { background: rgba(255,255,255,0.08); color: #fff; }
.navtab.active { background: var(--crimson); color: #fff; }

.btn {
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--crimson); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--crimson-dark); }
.btn-ghost { background: transparent; border-color: rgba(255,255,255,0.25); color: inherit; }
.btn-ghost:hover:not(:disabled) { background: rgba(255,255,255,0.08); }
.builder .btn-ghost, .page-inner .btn-ghost, .settings-panel .btn-ghost, .card .btn-ghost, .modal-box .btn-ghost {
  border-color: var(--border); color: var(--ink);
}
.builder .btn-ghost:hover:not(:disabled), .page-inner .btn-ghost:hover:not(:disabled),
.settings-panel .btn-ghost:hover:not(:disabled), .card .btn-ghost:hover:not(:disabled),
.modal-box .btn-ghost:hover:not(:disabled) { background: #f0eef1; }
.btn-small { padding: 5px 10px; font-size: 12px; }

/* ---------- Views / Layout ---------- */
.view { padding: 0; }
.page-inner { max-width: 1100px; margin: 0 auto; padding: 24px; }
.page-title { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--crimson); margin: 0 0 12px; }

.layout { display: grid; grid-template-columns: minmax(380px, 460px) 1fr; gap: 20px; padding: 20px; align-items: start; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

.builder { display: flex; flex-direction: column; gap: 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  margin-bottom: 16px;
}
.card h2 { font-size: 14px; margin: 0 0 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--crimson); }
.card-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 4px; }
.card-head h2 { margin-bottom: 0; }
.step-hint { font-size: 11px; color: var(--muted); }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
label input, label select, label textarea {
  display: block; width: 100%; margin-top: 4px;
  padding: 8px 10px; font-size: 13px;
  border: 1px solid var(--border); border-radius: 7px;
  font-family: inherit;
  color: var(--ink);
  background: #fff;
}
label textarea { resize: vertical; }
label input:disabled { background: #f4f3f5; color: var(--muted); cursor: not-allowed; }
.required-mark { color: var(--crimson); }
label input.field-error, label select.field-error, label textarea.field-error {
  border-color: #b3261e; background: #fdf1f1;
}

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 0 14px; }

.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.checkbox-row input { width: auto; margin: 0; }

.subhead { display: flex; align-items: center; justify-content: space-between; margin: 10px 0 8px; }
.subhead h3 { font-size: 12px; margin: 0; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }

table.mh-table, table.tp-table { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-bottom: 6px; }
table.mh-table th, table.tp-table th {
  text-align: left; font-weight: 500; color: var(--muted); font-size: 11px;
  padding: 4px 6px; border-bottom: 1px solid var(--border);
}
table.mh-table td, table.tp-table td { padding: 4px 6px; vertical-align: middle; }
table.mh-table input, table.mh-table select,
table.tp-table input, table.tp-table select {
  width: 100%; padding: 6px 7px; font-size: 12.5px;
  border: 1px solid var(--border); border-radius: 6px;
}
.muted-cell { color: var(--muted); white-space: nowrap; }
.muted-cell.margin-negative { color: #b3261e; font-weight: 600; }

.table-scroll { overflow-x: auto; }
.table-scroll table.tp-table { min-width: 640px; }

.icon-btn {
  border: none; background: transparent; color: var(--muted);
  font-size: 16px; cursor: pointer; line-height: 1; padding: 4px 6px;
  border-radius: 5px;
}
.icon-btn:hover { background: #f0eef1; color: var(--crimson); }

.unit-qty-row { margin-top: 14px; align-items: end; }
.lock-label span { display: block; margin-bottom: 4px; }
.cost-display-row { display: flex; align-items: center; gap: 8px; }
.cost-display-row input { flex: 1; }
.lock-check { display: flex; align-items: center; gap: 4px; white-space: nowrap; font-size: 11px; margin: 0; }
.lock-check input { width: auto; margin: 0; }

.subtotal-preview {
  margin-top: 12px; padding: 10px 12px;
  background: #fbf1f4; border: 1px solid #f0d6df; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--crimson-dark);
}

.builder-actions { display: flex; gap: 10px; margin-top: 16px; }
.builder-actions.wrap { flex-wrap: wrap; }

.hint-text { font-size: 11.5px; color: var(--muted); margin: 8px 0 0; }
.hint-text code { background: #f0eef1; padding: 1px 5px; border-radius: 4px; }

.doc-actions-card p.hint-text { margin-top: 10px; }

.status-banner {
  padding: 10px 14px; border-radius: 8px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--border);
}
.status-banner.draft { background: #f0eef1; color: var(--muted); }
.status-banner.submitted { background: #fff6e0; color: #8a6100; border-color: #f0e0ad; }
.status-banner.approved { background: #e7f6ec; color: #1c7a3b; border-color: #bfe6cc; }
.status-banner.rejected { background: #fbeaea; color: #b3261e; border-color: #f2c9c7; }

/* ---------- Preview / Document ---------- */
.preview { min-width: 0; }
.doc {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 44px;
  max-width: 900px;
  margin: 0 auto;
  font-size: 13px;
}
.doc-header { display: flex; align-items: baseline; justify-content: space-between; border-bottom: 2px solid var(--ink); padding-bottom: 10px; margin-bottom: 16px; }
.doc-company { font-weight: 700; font-size: 15px; }
.doc-title { font-weight: 700; font-size: 20px; letter-spacing: .05em; }

table.doc-meta { border-collapse: collapse; margin-bottom: 22px; font-size: 12.5px; }
table.doc-meta td { padding: 2px 6px; vertical-align: top; }
.meta-label { color: var(--muted); white-space: nowrap; padding-left: 0 !important; min-width: 110px; }

table.doc-table { width: 100%; border-collapse: collapse; margin-bottom: 18px; font-size: 12px; }
table.doc-table th {
  background: var(--ink); color: #fff; text-align: left; font-weight: 600;
  padding: 8px 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .02em;
}
table.doc-table td { padding: 8px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.col-deliverable { width: 16%; font-weight: 600; }
.col-desc { width: 38%; }
.col-qty { width: 6%; text-align: center; }
.col-unit { width: 10%; }
.col-cost { width: 14%; white-space: nowrap; }
.col-subtotal { width: 14%; white-space: nowrap; font-weight: 600; }
.pre-wrap { white-space: pre-wrap; }

.total-row td { border-top: 2px solid var(--ink); border-bottom: none; font-weight: 700; }
.total-label { text-align: right; padding-right: 12px !important; }
.total-value { white-space: nowrap; font-weight: 600; }

table.doc-summary { width: 100%; border-collapse: collapse; margin: 4px 0 26px; font-size: 13px; }
table.doc-summary td { padding: 6px 8px; }
table.doc-summary .total-label { text-align: right; width: 80%; color: var(--muted); }
table.doc-summary .grand-row td { border-top: 2px solid var(--ink); font-weight: 700; font-size: 14.5px; color: var(--crimson-dark); padding-top: 10px; }

.doc-terms h4 { font-size: 12px; margin: 16px 0 6px; }
.doc-terms ol { margin: 0 0 4px; padding-left: 18px; font-size: 11px; color: var(--muted); line-height: 1.5; }

.doc-signature-row { display: flex; gap: 40px; margin-top: 40px; }
.doc-signature { flex: 1; font-size: 12px; line-height: 2.1; margin-top: 0; }
.sig-box { height: 60px; }
.sig-image { display: block; height: 60px; width: auto; margin: 4px 0; }

.row-actions { white-space: nowrap; }
.breakdown-toggle { font-size: 11px; width: auto; padding: 4px 8px; }
.breakdown-detail td { background: #fafafa; padding: 14px 16px; }
table.breakdown-table { width: 100%; border-collapse: collapse; font-size: 11.5px; margin-bottom: 8px; }
table.breakdown-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 4px 8px; border-bottom: 1px solid var(--border); }
table.breakdown-table td { padding: 4px 8px; }
.breakdown-total { font-size: 12px; color: var(--muted); font-weight: 600; }

/* ---------- List tables (My Quotations / Approvals / Settings) ---------- */
table.list-table { width: 100%; border-collapse: collapse; font-size: 13px; background: #fff; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
table.list-table th {
  text-align: left; background: #f8f7f9; color: var(--muted); font-weight: 600;
  padding: 10px 12px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .02em;
  border-bottom: 1px solid var(--border);
}
table.list-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.list-table tr:last-child td { border-bottom: none; }
table.list-table input, table.list-table select { padding: 5px 7px; font-size: 12.5px; border: 1px solid var(--border); border-radius: 6px; }

.empty-note { color: var(--muted); font-size: 13px; padding: 18px 4px; }

.status-pill {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
}
.status-pill.draft { background: #eee; color: #666; }
.status-pill.submitted { background: #fff1cc; color: #8a6100; }
.status-pill.approved { background: #d9f2e1; color: #1c7a3b; }
.status-pill.rejected { background: #f9d9d7; color: #b3261e; }

.role-badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 10.5px; font-weight: 700; text-transform: uppercase; }
.role-badge.admin { background: var(--crimson); color: #fff; }
.role-badge.user { background: #e2e0e4; color: var(--ink); }

/* ---------- Settings ---------- */
.settings-tabs { display: flex; gap: 6px; margin-bottom: 18px; }
.settingstab {
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 16px; font-size: 13px; cursor: pointer; color: var(--muted); font-weight: 500;
}
.settingstab.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.settings-panel textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.bulk-result { margin-top: 8px; font-size: 12px; color: var(--muted); }
.bulk-result .err { color: #b3261e; }

/* ---------- Margin popup ---------- */
.margin-popup {
  position: fixed; top: 24px; right: 24px; z-index: 60;
  min-width: 220px; max-width: 300px;
  background: #fff; border-radius: 12px; padding: 16px 18px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.22);
  border: 1px solid var(--border); border-left: 5px solid var(--muted);
  animation: margin-popup-in .15s ease-out;
}
@keyframes margin-popup-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }
.margin-popup.ok { border-left-color: #1c7a3b; }
.margin-popup.ok .margin-popup-value, .margin-popup.ok .margin-popup-pct { color: #1c7a3b; }
.margin-popup.low { border-left-color: #b3261e; background: #fdf1f1; }
.margin-popup.low .margin-popup-value, .margin-popup.low .margin-popup-pct { color: #b3261e; }
.margin-popup-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 700; margin-bottom: 6px; padding-right: 18px; }
.margin-popup-value { font-size: 17px; font-weight: 700; line-height: 1.3; }
.margin-popup-pct { font-size: 22px; font-weight: 800; line-height: 1.3; }
.margin-popup-sub { font-size: 11px; color: var(--muted); margin-top: 6px; }
.margin-popup-close {
  position: absolute; top: 10px; right: 10px; border: none; background: transparent;
  font-size: 16px; line-height: 1; cursor: pointer; color: var(--muted); padding: 2px 6px; border-radius: 5px;
}
.margin-popup-close:hover { background: #f0eef1; color: var(--ink); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(20,18,22,0.55);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50;
}
.modal-box {
  background: #fff; border-radius: 14px; padding: 24px 26px; max-width: 760px; width: 100%;
}
.modal-box h2 { font-size: 15px; margin: 0 0 14px; color: var(--crimson); }
.modal-doc-preview { border: 1px solid var(--border); border-radius: 10px; padding: 20px; margin-bottom: 16px; max-height: 50vh; overflow-y: auto; font-size: 12.5px; }
.modal-doc-preview table { width: 100%; border-collapse: collapse; margin-bottom: 10px; font-size: 12px; }
.modal-doc-preview th { text-align: left; background: #f8f7f9; padding: 6px 8px; font-size: 10.5px; text-transform: uppercase; color: var(--muted); }
.modal-doc-preview td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
.review-item-card { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; background: #fff; }
.review-item-head { font-size: 12.5px; margin-bottom: 8px; }
.review-item-card table.mh-table th { font-size: 10px; }
.review-item-card table.mh-table td { padding: 3px 5px; }

/* ---------- Print ---------- */
@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  .layout { display: block; padding: 0; }
  .doc { border: none; padding: 0; max-width: 100%; }
  .preview { width: 100%; }
}
