:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f1f4f9;
  --border: #d8dee9;
  --border-strong: #b4c0d3;
  --text: #121a28;
  --muted: #4a5568;
  --primary: #1b4fd6;
  --primary-hover: #153fb0;
  --green: #0e7a52;
  --green-soft: #ecfdf5;
  --amber: #b45309;
  --brand-accent: #1b4fd6;
  --radius: 10px;
  --radius-sm: 6px;
  --font-display: "Schibsted Grotesk", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-serif: "Source Serif 4", Georgia, serif;
  --font-body: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, monospace;
  font-synthesis: none;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: var(--bg);
  background-image:
    linear-gradient(to right, rgba(18, 26, 40, 0.025) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(18, 26, 40, 0.025) 1px, transparent 1px);
  background-size: 40px 40px;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }

h1, h2, h3 {
  font-family: var(--font-display);
  letter-spacing: -0.02em;
  color: var(--text);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  gap: 12px;
  flex-wrap: wrap;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--text);
  text-decoration: none;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 18px;
  letter-spacing: -0.02em;
}
.brand svg { width: 26px; height: 26px; flex-shrink: 0; }

.pill {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
}
.pill-free { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.pill-pro { background: var(--green-soft); color: var(--green); border: 1px solid #86efac; }
.pill-local {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 5px;
}

.header-nav { display: flex; gap: 6px; flex-wrap: wrap; }
.nav-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  padding: 8px 14px;
  min-height: 38px;
  border-radius: var(--radius-sm);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13.5px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.app-main { max-width: 1280px; margin: 0 auto; padding: 26px 24px 56px; }
.view { display: none; }
.view.active { display: block; }

.dash-top { display: flex; justify-content: space-between; align-items: flex-end; flex-wrap: wrap; gap: 14px; margin-bottom: 20px; }
.dash-top h1 { font-size: 26px; font-weight: 800; line-height: 1.2; }
.dash-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13.5px; }
.text-green { color: var(--green); }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 20px; }
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.kpi-lbl { display: block; font-size: 12.5px; font-weight: 500; color: var(--muted); margin-bottom: 6px; }
.kpi strong {
  font-family: var(--font-mono);
  font-size: 24px;
  font-weight: 600;
  font-variant-numeric: tabular-nums slashed-zero;
  color: var(--text);
}
.kpi strong.text-green { color: var(--green); }

.card, .form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}
.form-card h3 { font-size: 16px; font-weight: 700; margin-bottom: 12px; }

.table-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.filter-pills { display: flex; gap: 6px; flex-wrap: wrap; }
.fp {
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.fp:hover { color: var(--text); border-color: var(--border-strong); }
.fp.active { background: var(--primary); color: #ffffff; border-color: var(--primary); }

input, select, textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  font-size: 14px;
  font-family: var(--font-body);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  background: var(--surface);
  box-shadow: 0 0 0 2px rgba(27, 79, 214, 0.16);
}
input[type="color"] { height: 40px; padding: 3px 6px; cursor: pointer; }
#dashSearch { width: 250px; }
label, .field-group { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
.field-lbl { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.file-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 4px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.btn-xs {
  min-height: 30px;
  padding: 0 11px;
  font-size: 12.5px;
  flex-shrink: 0;
}
.file-name {
  font-size: 13px;
  font-weight: 400;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}
.file-clear {
  background: none;
  border: none;
  color: #b91c1c;
  font-family: var(--font-body);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 6px;
  flex-shrink: 0;
}
.file-clear:hover { text-decoration: underline; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  font-family: var(--font-display);
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.btn-primary { background: var(--primary); color: #ffffff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { border-color: var(--text); background: var(--surface-2); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }
.btn-danger { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.btn-danger:hover { background: #fee2e2; border-color: #ef4444; }

.doc-list { display: flex; flex-direction: column; gap: 8px; }
.doc-row {
  display: grid;
  grid-template-columns: 125px 1.4fr 115px 96px auto;
  align-items: center;
  gap: 12px;
  padding: 13px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.doc-num { font-weight: 600; font-family: var(--font-mono); font-size: 13px; color: var(--text); }
.doc-client { font-weight: 600; color: var(--text); }
.doc-sub { font-size: 12.5px; color: var(--muted); }
.doc-amt { font-family: var(--font-mono); font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
.status-badge {
  display: inline-block;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
}
.st-Draft { background: #e2e8f0; color: #334155; }
.st-Sent { background: #dbeafe; color: #1d4ed8; }
.st-Signed { background: #d1fae5; color: #065f46; }
.st-Invoiced { background: #fef3c7; color: #92400e; }
.st-Paid { background: #dcfce7; color: #166534; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

.editor-bar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.bar-left, .bar-right { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#templatePicker { width: 230px; background: var(--surface); }

.editor-grid { display: grid; grid-template-columns: 1fr 1.08fr; gap: 20px; align-items: start; }
@media (max-width: 960px) {
  .editor-grid { grid-template-columns: 1fr; }
  .doc-row { grid-template-columns: 1fr; gap: 8px; }
  .doc-amt { text-align: left; }
  #dashSearch { width: 100%; }
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.row-between { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.row-gap { display: flex; gap: 8px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.mt-4 { margin-top: 4px; }
.mt-8 { margin-top: 8px; }
.mt-10 { margin-top: 10px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }

.items-editor { display: flex; flex-direction: column; gap: 8px; }
.item-row { display: grid; grid-template-columns: 1fr 68px 98px 34px; gap: 6px; align-items: center; }
.item-del {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: var(--radius-sm);
  height: 36px;
  cursor: pointer;
  font-weight: 700;
}
.item-del:hover { color: #b91c1c; border-color: #ef4444; background: #fef2f2; }

.toast {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--green-soft);
  border: 1px solid #86efac;
  color: var(--green);
  font-size: 13.5px;
  font-weight: 500;
  margin-bottom: 14px;
}
.toast.err { background: #fef2f2; border-color: #fecaca; color: #b91c1c; }

/* Printable White Paper Preview */
.paper {
  background: #ffffff;
  color: #121a28;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 34px 36px;
  box-shadow: 0 18px 42px -18px rgba(18, 26, 40, 0.14), 0 2px 6px rgba(18, 26, 40, 0.04);
  border-top: 5px solid var(--brand-accent);
}
.paper-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding-bottom: 20px; border-bottom: 2px solid #121a28; margin-bottom: 20px; }
.paper-biz { display: flex; gap: 12px; align-items: center; }
.paper-logo { max-width: 56px; max-height: 56px; object-fit: contain; border-radius: 6px; }
.paper-biz-name { font-family: var(--font-serif); font-size: 20px; font-weight: 600; color: #121a28; }
.paper-biz-meta { font-size: 12px; color: #4a5568; white-space: pre-line; margin-top: 2px; }
.paper-doc-meta { text-align: right; }
.paper-doc-type { font-family: var(--font-display); font-size: 22px; font-weight: 800; letter-spacing: 0.04em; color: var(--brand-accent); }
.paper-doc-num { font-family: var(--font-mono); font-size: 13px; color: #4a5568; }
.paper-status { display: inline-block; margin-top: 5px; font-family: var(--font-mono); font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #f1f4f9; color: #121a28; }

.paper-info-grid { display: flex; justify-content: space-between; gap: 18px; margin-bottom: 22px; flex-wrap: wrap; }
.paper-lbl { font-family: var(--font-mono); font-size: 10.5px; font-weight: 600; letter-spacing: 0.06em; color: #4a5568; margin-bottom: 3px; }
.paper-strong { font-weight: 700; font-size: 15px; color: #121a28; }
.paper-sub { font-size: 12.5px; color: #4a5568; }
.paper-dates { text-align: right; font-size: 12.5px; color: #334155; display: flex; flex-direction: column; gap: 3px; }

.paper-table { width: 100%; border-collapse: collapse; margin-bottom: 18px; font-size: 13.5px; }
.paper-table th { text-align: left; font-size: 11.5px; font-weight: 600; color: #4a5568; padding: 8px 6px; border-bottom: 1.5px solid #d8dee9; }
.paper-table td { padding: 10px 6px; border-bottom: 1px solid #edf0f5; color: #121a28; }
.paper-table .num { font-family: var(--font-mono); text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.paper-totals { margin-left: auto; max-width: 270px; display: flex; flex-direction: column; gap: 5px; font-family: var(--font-mono); font-size: 13.5px; font-variant-numeric: tabular-nums; }
.tot-row { display: flex; justify-content: space-between; color: #4a5568; }
.tot-grand { border-top: 2px solid #121a28; padding-top: 8px; margin-top: 5px; font-size: 16px; font-weight: 600; color: #121a28; }
.tot-dep { font-weight: 600; color: var(--brand-accent); font-size: 13px; }

.paper-notes { margin-top: 22px; padding-top: 14px; border-top: 1px solid #d8dee9; font-size: 12.5px; color: #4a5568; white-space: pre-line; }
.paper-sig { margin-top: 20px; padding: 14px 16px; border: 1px solid #86efac; background: #f0fdf4; border-radius: 8px; }
.sig-row { display: flex; gap: 14px; align-items: center; margin-top: 6px; }
.sig-img { max-height: 54px; max-width: 180px; background: #fff; border: 1px solid #bbf7d0; border-radius: 4px; padding: 2px; }
.sig-meta { font-size: 12px; color: #166534; }
.sig-hash { font-family: var(--font-mono); font-size: 10.5px; color: #15803d; word-break: break-all; }
.paper-watermark { margin-top: 24px; text-align: center; font-size: 11.5px; color: #64748b; border-top: 1px dashed #d8dee9; padding-top: 10px; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 18px; }
.pro-card { border: 2px solid var(--primary); }

.modal-backdrop { position: fixed; inset: 0; background: rgba(18, 26, 40, 0.55); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 490px; box-shadow: 0 24px 48px -12px rgba(18, 26, 40, 0.25); }
.modal-box h3 { margin-bottom: 6px; font-size: 18px; }
.modal-box textarea { margin-top: 10px; }

@media print {
  body { background: #fff !important; background-image: none !important; }
  .no-print { display: none !important; }
  .app-main { padding: 0 !important; max-width: none !important; }
  .editor-grid { display: block !important; }
  .paper { box-shadow: none !important; border: none !important; border-top: 5px solid var(--brand-accent) !important; border-radius: 0 !important; padding: 12px !important; }
}
