:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #dcdfe4;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --danger-dark: #b91c1c;
  --success: #16a34a;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

.app-header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  height: 64px;
  width: auto;
  display: block;
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.account-name {
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
}

.tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tab-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
}

.tab-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

main {
  max-width: 1300px;
  margin: 0 auto;
  padding: 20px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

h2 { margin-top: 0; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  background: var(--panel);
  padding: 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.form-grid label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  gap: 4px;
}

.form-grid label.wide {
  grid-column: span 2;
}

input, select {
  font-size: 15px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  color: var(--text);
  background: white;
}

input:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

input[readonly] {
  background: #f0f1f3;
  color: var(--muted);
}

.form-actions {
  margin-top: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}

button {
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  border-radius: 6px;
  padding: 9px 16px;
  border: 1px solid transparent;
}

button.primary {
  background: var(--primary);
  color: white;
}
button.primary:hover { background: var(--primary-dark); }

button.secondary {
  background: white;
  border: 1px solid var(--border);
  color: var(--text);
}
button.secondary:hover { background: #eef0f3; }

button.small { padding: 5px 10px; font-size: 12px; }

button.danger-btn {
  background: var(--danger);
  color: white;
}
button.danger-btn:hover { background: var(--danger-dark); }

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.hidden { display: none !important; }

.status-msg {
  font-size: 13px;
  color: var(--muted);
}

.status-msg.success { color: var(--success); }
.status-msg.error { color: var(--danger); }

.invite-note {
  color: var(--muted);
  font-size: 13px;
  margin: -8px 0 12px;
}

h3 { margin-top: 28px; margin-bottom: 10px; }

.table-wrap {
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.table-wrap.no-scroll {
  overflow-x: hidden;
}

.fit-table {
  table-layout: fixed;
  width: 100%;
}

.fit-table th, .fit-table td {
  white-space: normal;
  word-break: break-word;
  padding: 6px 6px;
}

.fit-table td.actions-cell {
  white-space: normal;
}

.fit-table td.actions-cell button {
  margin-bottom: 4px;
}

.hint-text {
  color: var(--muted);
  font-size: 13px;
  margin-top: -6px;
}

.grid-table {
  table-layout: fixed;
  width: 100%;
}

.grid-table th, .grid-table td {
  overflow: hidden;
}

.grid-table th {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grid-table td {
  padding: 4px 4px;
}

.grid-table tbody#quickDraftBody tr {
  background: #f5f8ff;
}

.grid-table #quickDraftDivider td {
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: #eef2ff;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.grid-table input {
  width: 100%;
  min-width: 0;
  padding: 6px 5px;
  font-size: 12.5px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  text-overflow: ellipsis;
}

.grid-table input:hover {
  border-color: var(--border);
}

.grid-table input:focus {
  border-color: var(--primary);
  background: white;
  outline: none;
}

.grid-table input[readonly] {
  background: transparent;
  color: var(--muted);
}

.new-buyer-hint {
  display: inline-block;
  margin-top: 4px;
  padding: 3px 8px;
  font-size: 11px;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 12px;
  color: #92400e;
  cursor: pointer;
  white-space: nowrap;
}

.new-buyer-hint:hover {
  background: #fde68a;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.log-comments {
  white-space: normal;
  word-break: break-word;
  max-width: 240px;
  min-width: 160px;
}

.checkbox-cell {
  text-align: center;
}

.checkbox-cell input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.truncate-cell {
  max-width: 0;
}

.ellipsis-text {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  background: #fafbfc;
  font-weight: 600;
  color: var(--muted);
  cursor: default;
  position: sticky;
  top: 0;
}

th[data-sort] { cursor: pointer; }
th[data-sort]:hover { color: var(--text); }

tbody tr:hover { background: #f8f9fb; }

td.actions-cell { white-space: nowrap; }
td.actions-cell button { margin-right: 6px; }

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: end;
  margin-bottom: 14px;
  background: var(--panel);
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.filter-bar label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 3px;
}

.quick-range.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.stat-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  text-align: center;
}

.stat-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--primary);
}

.stat-label {
  font-size: 13px;
  color: var(--muted);
  margin-top: 4px;
}

.recap-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 28px;
}

.recap-block-header h3 { margin: 0; }

.stat-value.negative { color: var(--danger); }

.chart-wrap {
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  max-width: 50%;
}

.profit-chart-svg {
  width: 100%;
  height: auto;
  display: block;
}

.chart-bar {
  filter: drop-shadow(1px 3px 3px rgba(0, 0, 0, 0.3));
}

.chart-axis-label {
  font-size: 11px;
  fill: var(--muted);
}

.chart-bar-label {
  font-size: 10px;
  font-weight: 600;
  fill: var(--text);
}

.chart-legend-label {
  font-size: 12px;
  fill: var(--text);
}

.chart-zero-line {
  stroke: var(--border);
  stroke-width: 1;
}

.chart-empty {
  color: var(--muted);
  padding: 24px;
  text-align: center;
}

.inline-form {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  background: var(--panel);
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  margin-bottom: 12px;
}

.inline-form input { flex: 1; min-width: 140px; }
.inline-form input#buyerState { flex: 0 1 70px; min-width: 60px; }
.inline-form input#buyerZip { flex: 0 1 90px; min-width: 80px; }

.search-box {
  width: 100%;
  margin-bottom: 12px;
}

.data-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.data-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.data-card h3 { margin-top: 0; }
.data-card p { color: var(--muted); font-size: 13px; }

.data-card.danger {
  border-color: #fca5a5;
  background: #fef2f2;
}

.data-card input[type="color"] {
  padding: 2px;
  height: 36px;
  width: 60px;
}

.data-card.wide {
  grid-column: 1 / -1;
}

.data-card h4 {
  margin: 20px 0 8px;
  font-size: 14px;
}

.data-card .hint {
  color: var(--muted);
  font-weight: 400;
  font-size: 12px;
}

.team-invite-form {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
}

.team-invite-result {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.team-invite-result label,
.team-invite-form label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  gap: 4px;
}

#teamInviteLink {
  min-width: 260px;
}

.invite-link-cell {
  width: 100%;
  font-size: 12px;
}

.tab-visibility-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin: 10px 0 16px;
}

.tab-visibility-list label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: normal;
}

@media (max-width: 800px) {
  .form-grid { grid-template-columns: repeat(2, 1fr); }
  .stat-cards { grid-template-columns: 1fr; }
  .data-actions { grid-template-columns: 1fr; }
  .chart-wrap { max-width: 100%; }
}

/* ---------- Invoice ---------- */

.biz-settings {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 14px;
}

.biz-settings summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
  font-size: 13px;
}

.biz-settings .filter-bar {
  margin-top: 10px;
  margin-bottom: 0;
  box-shadow: none;
  border: none;
  padding: 0;
}

.invoice-doc {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 32px;
  max-width: 800px;
}

.invoice-header {
  display: flex;
  align-items: center;
  gap: 16px;
  border-bottom: 2px solid var(--text);
  padding-bottom: 16px;
  margin-bottom: 20px;
}

.invoice-logo {
  height: 64px;
  width: auto;
  border-radius: 8px;
}

.invoice-business-info h3 {
  margin: 0 0 4px 0;
  font-size: 18px;
}

.invoice-business-info div {
  font-size: 13px;
  color: var(--muted);
}

.invoice-meta {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 20px;
  font-size: 14px;
  line-height: 1.6;
}

.invoice-items {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.invoice-items th, .invoice-items td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

.invoice-items th {
  background: #fafbfc;
  font-weight: 600;
  color: var(--muted);
}

.invoice-totals {
  margin-left: auto;
  max-width: 320px;
  font-size: 14px;
}

.invoice-totals div {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.invoice-total-due {
  font-weight: 700;
  font-size: 17px;
  border-bottom: none !important;
  border-top: 2px solid var(--text);
  margin-top: 4px;
  padding-top: 10px !important;
}

@media print {
  .app-header, .no-print { display: none !important; }
  .tab-panel { display: none !important; }
  #tab-invoice { display: block !important; }
  main { max-width: 100%; padding: 0; }
  .invoice-doc { border: none; padding: 0; max-width: 100%; }
  body { background: white; }
}

#printSelectionDoc { display: none; }

.print-doc-title { font-size: 18px; margin: 0 0 16px 0; }

@media print {
  body.printing-selection #tab-invoice { display: none !important; }
  body.printing-selection #printSelectionDoc { display: block !important; }
}

.recap-block-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ---------- Auth ---------- */

.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 20px;
}

.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.auth-logo {
  height: 140px;
  width: auto;
  border-radius: 10px;
  margin-bottom: 12px;
}

.auth-card h2 {
  margin: 0 0 20px 0;
}

.auth-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: left;
}

.auth-card label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  gap: 4px;
}

.auth-card button[type="submit"] {
  margin-top: 6px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 13px;
  margin-top: 14px;
  padding: 4px;
}

.app-footer {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

.app-footer a {
  color: var(--muted);
}

.app-footer span {
  margin: 0 8px;
}

.legal-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 32px 20px 60px;
}

.legal-page .brand {
  justify-content: center;
  margin-bottom: 24px;
}

.brand-text {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

.legal-page h1 {
  margin-bottom: 4px;
}

.legal-page .legal-meta {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 28px;
}

.legal-page h2 {
  margin-top: 32px;
  font-size: 18px;
}

.legal-page p, .legal-page li {
  line-height: 1.6;
}

.legal-page a {
  color: var(--primary);
}

.legal-page .back-link {
  display: inline-block;
  margin-bottom: 20px;
}

.auth-card .agree-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 400;
  text-align: left;
}

.auth-card .agree-row input[type="checkbox"] {
  margin-top: 3px;
  flex-shrink: 0;
}

.link-btn:hover {
  text-decoration: underline;
}

#authStatus {
  margin-top: 10px;
}

.global-loading {
  text-align: center;
  padding: 10px;
  background: #eef2ff;
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
}
