:root {
  --paper: #f6f3ee;
  --card: #fffdf9;
  --ink: #1c1917;
  --ink-2: #44403c;
  --muted: #78716c;
  --hair: #e7e2da;
  --hair-2: #d6d0c6;
  --gold: #a1783f;
  --gold-soft: #f3ead9;
  --good: #2f6b4f;
  --good-soft: #e3efe7;
  --warn: #a4432f;
  --warn-soft: #f7e4de;
  --blue: #2f4f7a;
  --blue-soft: #e4eaf3;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --r: 14px;
  --shadow: 0 1px 2px rgba(28, 25, 23, .04), 0 8px 24px -12px rgba(28, 25, 23, .12);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--paper); color: var(--ink); font: 14px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
a { color: inherit; }
h1, h2, h3 { font-family: var(--serif); font-weight: 500; letter-spacing: -.015em; margin: 0; }
h1 { font-size: 34px; font-weight: 400; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
h3 { font-size: 19px; margin-bottom: 16px; }
h4 { font: 600 11px var(--sans); letter-spacing: .14em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; }
code { font-size: 12px; background: var(--gold-soft); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; line-height: 1.5; }
.mono, .num { font-variant-numeric: tabular-nums; }
.mono { font-feature-settings: 'tnum'; letter-spacing: .02em; }
.mt { margin-top: 20px; }
.ic { width: 16px; height: 16px; flex: none; }

/* ---------- Brand & lock ---------- */
.brand { font-family: var(--serif); font-style: italic; font-size: 26px; font-weight: 400; letter-spacing: -.02em; }
.brand span { color: var(--gold); font-style: normal; }
.brand.big { font-size: 44px; margin-bottom: 18px; }
.lock { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(1200px 600px at 50% -10%, #efe6d6 0%, transparent 60%), var(--paper); }
.lock-card { width: 100%; max-width: 420px; text-align: center; background: var(--card); border: 1px solid var(--hair); border-radius: 22px; padding: 44px 36px; box-shadow: var(--shadow); }
.lock-card h2 { font-size: 26px; margin-bottom: 8px; }
.lock-card p { margin: 0 0 22px; }
.lock-card .field { text-align: left; }
.lock-card .tiny { margin: 18px 0 0; }

/* ---------- Layout ---------- */
#app { min-height: 100vh; }
body:not(.locked) #app { display: grid; grid-template-columns: 232px 1fr; }
.side { position: sticky; top: 0; height: 100vh; padding: 28px 18px; border-right: 1px solid var(--hair); display: flex; flex-direction: column; gap: 32px; background: var(--paper); }
.side .brand { padding: 0 10px; }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px; text-decoration: none; color: var(--ink-2); font-weight: 500; }
.side nav a:hover { background: #efebe4; }
.side nav a.active { background: var(--ink); color: #fff; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; padding: 0 6px; }
.e2e { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--good); }
main { padding: 36px clamp(16px, 4vw, 48px) 80px; min-width: 0; max-width: 1400px; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 28px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.back { display: inline-block; text-decoration: none; color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.back:hover { color: var(--ink); }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; font: 600 13px var(--sans); padding: 10px 16px; border-radius: 999px;
  border: 1px solid var(--hair-2); background: var(--card); color: var(--ink); cursor: pointer; text-decoration: none; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s; }
.btn:hover { border-color: var(--ink-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #33302d; }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--warn); border-color: var(--warn); color: #fff; }
.btn.small { padding: 7px 12px; font-size: 12px; }
.btn.wide { width: 100%; padding: 13px; font-size: 14px; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.loading { position: relative; color: transparent !important; }
.btn.loading::after { content: ''; position: absolute; width: 14px; height: 14px; border: 2px solid currentColor; border-color: var(--muted) transparent var(--muted) transparent; border-radius: 50%; animation: spin .7s linear infinite; }
.btn.primary.loading::after { border-color: #fff transparent #fff transparent; }
@keyframes spin { to { transform: rotate(360deg); } }
.danger-text { color: var(--warn) !important; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; width: 28px; height: 28px; border-radius: 8px; }
.icon-btn:hover { background: var(--warn-soft); color: var(--warn); }

/* ---------- Cards / stats ---------- */
.card { background: var(--card); border: 1px solid var(--hair); border-radius: var(--r); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 24px; }
.stats.one { grid-template-columns: 1fr 1fr; }
.stat { background: var(--card); border: 1px solid var(--hair); border-radius: var(--r); padding: 18px 20px; box-shadow: var(--shadow); }
.stat .k { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--gold); font-weight: 600; margin-bottom: 6px; }
.stat.warn .k { color: var(--warn); }
.stat.good .k { color: var(--good); }
.stat .v { font-family: var(--serif); font-size: 24px; line-height: 1.25; font-variant-numeric: tabular-nums; }
.stat .v .muted { font-family: var(--sans); font-size: 18px; }

/* ---------- Toolbar / tables ---------- */
.toolbar { display: flex; gap: 12px; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; background: #ede8e0; padding: 4px; border-radius: 999px; overflow-x: auto; max-width: 100%; }
.tabs a { padding: 6px 14px; border-radius: 999px; text-decoration: none; font-size: 13px; font-weight: 500; color: var(--ink-2); text-transform: capitalize; white-space: nowrap; }
.tabs a.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.search { border: 1px solid var(--hair-2); background: var(--card); border-radius: 999px; padding: 9px 16px; font: inherit; min-width: 240px; flex: 0 1 300px; outline: none; }
.search:focus { border-color: var(--ink-2); }
.list { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); font-weight: 600; padding: 14px 18px; border-bottom: 1px solid var(--hair); white-space: nowrap; }
.tbl td { padding: 14px 18px; border-bottom: 1px solid var(--hair); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tbody tr { cursor: pointer; transition: background .1s; }
.tbl tbody tr:hover { background: #faf6ef; }
.tbl .r { text-align: right; }
.empty { padding: 56px 20px; text-align: center; color: var(--muted); }
.empty a { color: var(--ink); font-weight: 600; }

.badge { display: inline-block; font: 600 10.5px var(--sans); letter-spacing: .1em; text-transform: uppercase; padding: 3px 9px; border-radius: 999px; vertical-align: middle; }
.badge.draft { background: #eeeae3; color: var(--ink-2); }
.badge.sent { background: var(--blue-soft); color: var(--blue); }
.badge.paid { background: var(--good-soft); color: var(--good); }
.badge.overdue { background: var(--warn-soft); color: var(--warn); }
.badge.void { background: #eeeae3; color: var(--muted); text-decoration: line-through; }

/* ---------- Forms ---------- */
.form { padding: 24px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.span2 { grid-column: span 2; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 0; }
.field > span { font-size: 12px; font-weight: 500; color: var(--ink-2); }
input, select, textarea { font: inherit; color: inherit; }
.field input, .field select, .field textarea, .item input, .item textarea {
  width: 100%; border: 1px solid var(--hair-2); background: #fff; border-radius: 10px; padding: 10px 12px; outline: none; transition: border-color .15s, box-shadow .15s; }
.field textarea, .item textarea { resize: vertical; line-height: 1.45; }
.field input:focus, .field select:focus, .field textarea:focus, .item input:focus, .item textarea:focus { border-color: var(--ink-2); box-shadow: 0 0 0 3px rgba(161, 120, 63, .15); }
.lock-card .field { margin-bottom: 16px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--ink); width: 16px; height: 16px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.notice { padding: 10px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 14px; background: #eeeae3; }
.notice.ok { background: var(--good-soft); color: var(--good); }
.notice.err { background: var(--warn-soft); color: var(--warn); }

/* ---------- Invoice editor ---------- */
.editor { display: grid; grid-template-columns: minmax(360px, 520px) 1fr; gap: 24px; align-items: start; }
.items-head { margin: 26px 0 10px; }
.items-head h3 { margin: 0; }
.item { display: grid; grid-template-columns: 64px minmax(80px, 1fr) minmax(max-content, 1fr) 28px; gap: 8px 10px; align-items: end; padding: 12px; margin-bottom: 10px; border: 1px solid var(--hair); border-radius: 12px; background: #fff; }
.item textarea { grid-column: 1 / -1; min-height: 42px; overflow: hidden; resize: none; }
.item .mini { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.item .mini > span { font-size: 10.5px; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.item .line { padding: 10px 0; text-align: right; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .mini:has(.line) > span { text-align: right; }
.item .icon-btn { margin-bottom: 6px; }
.totals { margin-top: 22px; border-top: 1px solid var(--hair); padding-top: 14px; }
.totals div { display: flex; justify-content: space-between; padding: 4px 0; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.totals .grand { margin-top: 6px; padding-top: 10px; border-top: 1.5px solid var(--ink); color: var(--ink); font-family: var(--serif); font-size: 22px; }
.danger-zone { display: flex; gap: 8px; justify-content: space-between; margin-top: 28px; padding-top: 18px; border-top: 1px dashed var(--hair-2); }
.history { margin-top: 22px; font-size: 13px; display: flex; flex-direction: column; gap: 4px; }

.preview-wrap { position: sticky; top: 24px; }
.preview-label { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); font-weight: 600; margin-bottom: 10px; }
.preview { position: relative; overflow: hidden; border-radius: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.05), 0 18px 50px -18px rgba(28,25,23,.28); background: #fffdf9; }
.preview iframe { border: 0; display: block; transform-origin: 0 0; background: #fffdf9; }
.preview.mini { margin-top: 18px; max-width: 520px; }

/* ---------- Settings ---------- */
.settings { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 460px), 1fr)); gap: 18px; align-items: start; }
.two-col { display: grid; grid-template-columns: minmax(320px, 520px) 1fr; gap: 24px; align-items: start; }
.swatches { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.sw { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--card); background: var(--c); box-shadow: 0 0 0 1px var(--hair-2); cursor: pointer; padding: 0; }
.sw.on { box-shadow: 0 0 0 2px var(--ink); }
.swatches input[type=color] { width: 34px; height: 34px; border: 1px solid var(--hair-2); border-radius: 50%; padding: 2px; background: none; cursor: pointer; }
.logo-row { display: flex; gap: 8px; align-items: center; }
.logo-prev { width: 64px; height: 40px; border: 1px dashed var(--hair-2); border-radius: 8px; display: grid; place-items: center; overflow: hidden; background: #fff; }
.logo-prev img { max-width: 100%; max-height: 100%; }
.numprev { background: var(--gold-soft); padding: 10px 14px; border-radius: 10px; font-size: 13px; }
.numprev b { font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.pk-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.pk { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--hair); border-radius: 12px; background: #fff; }
.pk .ic { vertical-align: -3px; color: var(--gold); }

/* ---------- Modal / toast ---------- */
.modal { border: 0; padding: 0; border-radius: 20px; background: var(--card); box-shadow: 0 30px 80px -20px rgba(0,0,0,.35); width: min(440px, calc(100vw - 32px)); color: var(--ink); }
.modal:has(.wide) { width: min(620px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(28, 25, 23, .35); backdrop-filter: blur(3px); }
.modal-body { padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.modal-body h3 { margin: 0; font-size: 24px; }
.modal-body p { margin: 0; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; align-items: center; pointer-events: none; width: calc(100% - 32px); }
.toast { background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 999px; font-size: 13px; font-weight: 500; box-shadow: 0 10px 30px -10px rgba(0,0,0,.4); transition: opacity .4s, transform .4s; max-width: 560px; text-align: center; }
.toast.err { background: var(--warn); }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .editor, .two-col { grid-template-columns: 1fr; }
  .preview-wrap { position: static; }
}
@media (max-width: 760px) {
  body:not(.locked) #app { grid-template-columns: 1fr; }
  .side { position: sticky; top: 0; z-index: 10; height: auto; flex-direction: row; align-items: center; padding: 10px 16px; gap: 12px; border-right: 0; border-bottom: 1px solid var(--hair); }
  .side .brand { font-size: 22px; padding: 0; }
  .side nav { flex-direction: row; gap: 0; }
  .side nav a { padding: 8px; font-size: 0; }
  .side nav a .ic { width: 20px; height: 20px; }
  .side-foot { margin-left: auto; flex-direction: row; align-items: center; padding: 0; }
  .e2e { display: none; }
  main { padding: 24px 16px 64px; }
  h1 { font-size: 28px; }
  .stats { grid-template-columns: 1fr; }
  .stats.one { grid-template-columns: 1fr 1fr; }
  .grid2 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .search { min-width: 0; flex: 1 1 100%; }
  .item { grid-template-columns: 60px minmax(70px, 1fr) minmax(max-content, 1fr) 28px; }
  .form { padding: 18px; }
  .lock-card { padding: 36px 22px; }
}

/* sign-in extras */
.switch { margin: 22px 0 0 !important; font-size: 13px; color: var(--muted); }
.switch a, .lock-card .tiny a { color: var(--ink); font-weight: 600; }
.code-input { text-align: center; font: 600 24px ui-monospace, Menlo, monospace !important; letter-spacing: .4em; }
.lock-card form { margin: 0 0 4px; }
.lock-card form .btn { margin-top: 4px; }
.who { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 760px) { .who { display: none; } }
