:root {
  --paper: #faf9f6;
  --paper-alt: #f0ede4;
  --card: #ffffff;
  --ink: #1e2a38;
  --ink-muted: #5b6b7a;
  --ink-faint: #9099a4;
  --line: #dad5c8;
  --line-strong: #c3bdac;
  --accent: #9c6222;
  --accent-dark: #7a4a19;
  --accent-soft: #f1e3d0;
  --success: #3f7a5d;
  --success-soft: #e1eee7;
  --danger: #a63d40;
  --danger-soft: #f5e4e4;
  --info: #33607a;
  --info-soft: #e2edf1;
  --font-serif: "Source Serif 4", Georgia, serif;
  --font-sans: "IBM Plex Sans", system-ui, -apple-system, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", monospace;
  --radius-sm: 4px;
  --radius-md: 7px;
  --shadow-card:
    0 1px 2px rgba(30, 42, 56, 0.07), 0 1px 0 rgba(30, 42, 56, 0.04);
}
* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  padding: 0;
}
.hidden {
  display: none !important;
}
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1,
h2 {
  font-family: var(--font-serif);
  color: var(--ink);
  margin: 0;
}
h3 {
  font-family: var(--font-sans);
  font-size: 13.5px;
  font-weight: 600;
  margin: 0 0 8px 0;
  color: var(--ink);
}
button {
  font-family: var(--font-sans);
}
input,
textarea,
select {
  font-family: var(--font-sans);
  font-size: 13.5px;
}
.mono,
.mono-input,
input.mono-input {
  font-family: var(--font-mono);
}
a {
  color: var(--accent-dark);
}

/* ---------- topbar & tabs ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 18px 24px 0 24px;
  max-width: 1180px;
  margin: 0 auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  overflow: hidden;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 13px;
}
.brand-mark img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
}
.brand-name {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 19px;
  color: var(--ink);
}

.global-search {
  position: relative;
  flex: 1;
  max-width: 360px;
  min-width: 180px;
}
.global-search input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--ink);
}
.global-search .suggestions {
  width: 100%;
}
.gs-group-label {
  padding: 7px 12px 3px 12px;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--ink-faint);
  text-transform: uppercase;
}

.tabs {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  max-width: 1180px;
  margin: 14px auto 0 auto;
  padding: 0 24px;
  border-bottom: 1px solid var(--line-strong);
}
.tab {
  appearance: none;
  border: 1px solid transparent;
  border-bottom: none;
  background: transparent;
  color: var(--ink-muted);
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 500;
  border-radius: 7px 7px 0 0;
  cursor: pointer;
  position: relative;
  top: 1px;
}
.tab:hover {
  color: var(--ink);
  background: var(--paper-alt);
}
.tab.active {
  background: var(--card);
  color: var(--ink);
  font-weight: 600;
  border-color: var(--line-strong);
  border-bottom: 1px solid var(--card);
}

.content {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px;
}
.panel {
  display: none;
}
.panel.active {
  display: block;
  animation: fade 0.15s ease;
}
@keyframes fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.doc-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0 8px 8px 8px;
  box-shadow: var(--shadow-card);
  padding: 28px 30px 30px 30px;
}
.doc-card + .doc-card {
  margin-top: 20px;
}
.doc-title {
  font-size: 25px;
  font-weight: 600;
}
.doc-sub {
  color: var(--ink-muted);
  margin: 5px 0 0 0;
  font-size: 13.5px;
}

.doc-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  flex-wrap: wrap;
  padding-bottom: 18px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--line);
}
.doc-meta {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}
.doc-meta label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--ink-muted);
  font-weight: 500;
}
.doc-meta input,
.doc-meta select {
  padding: 7px 9px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  width: 150px;
}

.doc-section {
  margin-bottom: 24px;
}
.doc-section h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 10px;
}
.optional {
  font-weight: 400;
  color: var(--ink-faint);
  font-size: 11.5px;
}

label {
  display: block;
  font-size: 12.5px;
  color: var(--ink-muted);
  font-weight: 500;
  margin-bottom: 4px;
}
input[type="text"],
input[type="number"],
input[type="email"],
input[type="date"],
textarea,
select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  outline: none;
}
input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea {
  resize: vertical;
  font-size: 13.5px;
  line-height: 1.5;
}
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}
.checkbox-label input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 16px;
  margin-bottom: 14px;
}
.form-grid .span-2 {
  grid-column: 1 / -1;
}
.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 6px;
  flex-wrap: wrap;
}

.btn {
  appearance: none;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.12s ease,
    border-color 0.12s ease;
}
.btn.primary {
  background: var(--accent);
  color: #fff;
}
.btn.primary:hover {
  background: var(--accent-dark);
}
.btn.secondary {
  background: var(--paper-alt);
  color: var(--ink);
  border-color: var(--line-strong);
}
.btn.secondary:hover {
  background: var(--line);
}
.btn.ghost {
  background: transparent;
  color: var(--ink-muted);
  border-color: transparent;
}
.btn.ghost:hover {
  color: var(--ink);
  background: var(--paper-alt);
}
.btn.danger-outline {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger-soft);
}
.btn.danger-outline:hover {
  background: var(--danger-soft);
}
.btn.small {
  padding: 5px 10px;
  font-size: 12px;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.search-row {
  position: relative;
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.search-row input[type="text"] {
  flex: 1;
  min-width: 200px;
}

.suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--card);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 20px rgba(30, 42, 56, 0.12);
  max-height: 320px;
  overflow-y: auto;
  z-index: 60;
}
.suggestions.hidden {
  display: none;
}
.sugg-item {
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.sugg-item:last-child {
  border-bottom: none;
}
.sugg-item:hover {
  background: var(--paper-alt);
}
.sugg-name {
  font-weight: 600;
  font-size: 13.5px;
}
.sugg-meta {
  color: var(--ink-muted);
  font-size: 12px;
  margin-top: 2px;
}
.sugg-empty {
  padding: 10px 12px;
  color: var(--ink-muted);
  font-size: 13px;
}
.sugg-add {
  padding: 10px 12px;
  color: var(--accent-dark);
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
}
.sugg-add:hover {
  background: var(--accent-soft);
}

.selected-card {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: var(--paper-alt);
}
.selected-card.hidden {
  display: none;
}
.selected-card .sc-name {
  font-weight: 600;
  font-size: 14.5px;
}
.selected-card .sc-line {
  color: var(--ink-muted);
  font-size: 12.5px;
  margin-top: 2px;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
}
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}
thead th {
  background: var(--paper-alt);
  color: var(--ink-muted);
  font-size: 11.5px;
  font-weight: 600;
  text-align: left;
  padding: 9px 8px;
  border-bottom: 1px solid var(--line-strong);
  white-space: nowrap;
}
tbody td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
tbody tr:last-child td {
  border-bottom: none;
}
.line-items-table input {
  padding: 6px 7px;
  font-size: 13px;
}
.line-items-table td.idx-cell {
  color: var(--ink-faint);
  font-size: 12.5px;
  text-align: center;
  width: 26px;
}
.line-items-table input.narrow {
  width: 64px;
}
.line-items-table input.name-input {
  min-width: 150px;
}
.amount-cell,
.taxamt-cell,
.total-cell {
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: right;
  white-space: nowrap;
}
.total-cell {
  font-weight: 600;
}
.row-remove {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 15px;
  padding: 2px 6px;
}
.row-remove:hover {
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
}

.empty-msg {
  padding: 16px;
  text-align: center;
  color: var(--ink-muted);
  font-size: 13px;
}

.totals-block {
  margin-top: 14px;
  margin-left: auto;
  max-width: 300px;
}
.totals-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 2px;
  font-size: 13.5px;
  color: var(--ink-muted);
}
.totals-row.grand {
  border-top: 1px solid var(--line-strong);
  margin-top: 4px;
  padding-top: 9px;
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
}
.totals-row .mono {
  font-family: var(--font-mono);
}
.totals-row .conv {
  font-size: 11.5px;
  color: var(--ink-faint);
  font-weight: 400;
  display: block;
  text-align: right;
}

.doc-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 26px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}

.entity-form {
  padding-bottom: 18px;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--line);
}
.list-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.list-toolbar input {
  max-width: 280px;
}
.muted-count {
  color: var(--ink-faint);
  font-size: 12.5px;
  white-space: nowrap;
}
.toolbar-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.entity-list {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.entity-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.entity-row:last-child {
  border-bottom: none;
}
.entity-row:nth-child(even) {
  background: #fcfbf9;
}
.entity-main .e-name {
  font-weight: 600;
  font-size: 14px;
}
.entity-main .e-meta {
  color: var(--ink-muted);
  font-size: 12.5px;
  margin-top: 2px;
}
.entity-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.entity-actions button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  color: var(--ink-muted);
}
.entity-actions button:hover {
  background: var(--paper-alt);
  color: var(--ink);
}
.entity-actions button.danger:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.hint {
  color: var(--ink-faint);
  font-size: 12px;
  margin: -4px 0 10px 0;
}
.terms-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}
.term-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.term-row input {
  flex: 1;
}
.term-row button {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 15px;
}

.history-table td {
  font-size: 13.5px;
}
.history-table .ht-total {
  font-family: var(--font-mono);
  text-align: right;
}
.history-table input[type="checkbox"] {
  width: auto;
}

/* status badges */
.status-badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.status-Draft {
  background: var(--paper-alt);
  color: var(--ink-muted);
}
.status-Pending-Approval {
  background: var(--accent-soft);
  color: var(--accent-dark);
}
.status-Approved {
  background: var(--success-soft);
  color: var(--success);
}
.status-Rejected {
  background: var(--danger-soft);
  color: var(--danger);
}
.status-Sent {
  background: var(--info-soft);
  color: var(--info);
}
.status-Cancelled {
  background: var(--line);
  color: var(--ink-faint);
}
.conf-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: var(--danger-soft);
  color: var(--danger);
}

/* bulk action bar */
.bulk-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--accent-soft);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  margin-bottom: 12px;
}
.bulk-bar.hidden {
  display: none;
}
.bulk-bar span {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent-dark);
}
.bulk-bar select {
  width: auto;
  padding: 6px 8px;
}

/* dashboard summary cards */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.summary-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  background: var(--paper-alt);
}
.summary-card .sc-label {
  font-size: 11.5px;
  color: var(--ink-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.summary-card .sc-value {
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 600;
  color: var(--ink);
  margin-top: 4px;
}

.charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 22px;
}
@media (max-width: 760px) {
  .charts-row {
    grid-template-columns: 1fr;
  }
}
.chart-box {
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px 16px;
}
.chart-box svg {
  width: 100%;
  height: auto;
  display: block;
}
.chart-bar-label {
  font-size: 10px;
  fill: var(--ink-muted);
  font-family: var(--font-sans);
}
.chart-value-label {
  font-size: 10px;
  fill: var(--ink);
  font-family: var(--font-mono);
}

/* activity / approval log */
.log-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-left: 2px solid var(--line-strong);
}
.log-list li {
  padding: 0 0 10px 14px;
  margin-left: -1px;
  position: relative;
  font-size: 12.5px;
  color: var(--ink-muted);
}
.log-list li::before {
  content: "";
  position: absolute;
  left: -5px;
  top: 4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}
.log-list li .log-time {
  color: var(--ink-faint);
  font-size: 11px;
  display: block;
}
.log-list li .log-action {
  color: var(--ink);
  font-weight: 600;
}
.log-empty {
  color: var(--ink-faint);
  font-size: 12.5px;
  padding: 4px 0;
}

/* templates */
.template-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.template-card .tc-name {
  font-weight: 600;
  font-size: 14px;
}
.template-card .tc-meta {
  color: var(--ink-muted);
  font-size: 12px;
  margin-top: 2px;
}

/* logo preview */
.logo-preview {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}
.logo-preview img {
  max-width: 140px;
  max-height: 70px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  padding: 6px;
}
.logo-placeholder {
  width: 140px;
  height: 70px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-faint);
  font-size: 11.5px;
  text-align: center;
}

.settings-section {
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--line);
}
.settings-section:last-of-type {
  border-bottom: none;
}

.draft-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--info-soft);
  border: 1px solid var(--info);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
  color: var(--info);
}
.draft-banner.hidden {
  display: none;
}

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(30, 42, 56, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 6vh 16px;
  overflow-y: auto;
  z-index: 100;
}
.modal-overlay.hidden {
  display: none;
}
.modal-box {
  background: var(--card);
  border-radius: var(--radius-md);
  max-width: 600px;
  width: 100%;
  padding: 24px 26px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
.modal-box.wide {
  max-width: 760px;
}
.modal-box h2 {
  font-size: 18px;
  margin-bottom: 14px;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  padding: 11px 18px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  z-index: 200;
  max-width: 90vw;
}
.toast.success {
  background: var(--success);
}
.toast.error {
  background: var(--danger);
}
.toast.hidden {
  display: none;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  font-family: var(--font-serif);
  color: var(--ink-muted);
  font-size: 15px;
}
.loading-overlay.hidden {
  display: none;
}

@media (max-width: 640px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
  .doc-card {
    padding: 20px 16px;
  }
  .doc-meta input,
  .doc-meta select {
    width: 100%;
  }
  .doc-meta {
    flex-direction: column;
    width: 100%;
  }
  .doc-meta label {
    width: 100%;
  }
  .list-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .list-toolbar input {
    max-width: none;
  }
  .global-search {
    max-width: none;
    width: 100%;
    order: 3;
  }
}
