:root {
  --brand: #2563eb;
  --ink: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bg: #f8fafc;
  --card: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Inter", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px;
  background: #111827;
  color: #d1d5db;
  padding: 20px 14px;
  flex-shrink: 0;
}
.sidebar .brand { color: #fff; font-weight: 700; font-size: 18px; padding: 6px 10px 20px; }
.sidebar a {
  display: block;
  color: #d1d5db;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 2px;
}
.sidebar a.active, .sidebar a:hover { background: rgba(255,255,255,.08); color: #fff; }

.main { flex: 1; min-width: 0; }

.topbar {
  height: 64px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}
.topbar .search {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  width: 320px;
  font-size: 14px;
}

.content { padding: 24px; }

.page-title { font-size: 20px; font-weight: 700; margin: 0 0 18px; }

/* ---------- Cards / grid ---------- */
.cards-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stat-card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat-card .value { font-size: 22px; font-weight: 700; margin-top: 6px; }

/* ---------- Create Invoice layout ---------- */
.invoice-builder { display: grid; grid-template-columns: 480px 1fr; gap: 20px; align-items: start; }
@media (max-width: 1100px) { .invoice-builder { grid-template-columns: 1fr; } }

.form-panel { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.form-section { padding: 18px 20px; border-bottom: 1px solid var(--border); }
.form-section:last-child { border-bottom: none; }
.form-section h3 { margin: 0 0 12px; font-size: 14px; font-weight: 700; color: var(--ink); }

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.field-row.single { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13.5px;
  font-family: inherit;
  width: 100%;
}
.field textarea { resize: vertical; min-height: 60px; }
.field input[type="range"] { width: 100%; padding: 0; accent-color: var(--brand); }
.field label span { color: var(--ink); font-weight: 600; }

.radio-group, .toggle-group { display: flex; gap: 16px; align-items: center; }
.radio-group label, .toggle-group label { font-size: 13.5px; display: flex; align-items: center; gap: 6px; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--brand); color: #fff; }
.btn-secondary { background: #eef2ff; color: var(--brand); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-row { display: flex; gap: 8px; padding: 16px 20px; flex-wrap: wrap; }

.items-table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 8px; }
.items-table-scroll::-webkit-scrollbar { height: 8px; }
.items-table-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
.items-scroll-hint { font-size: 11px; color: var(--muted); margin: 4px 0 0; display: none; }
@media (max-width: 1100px) { .items-scroll-hint.show { display: block; } }

table.items-table { width: 100%; min-width: 640px; border-collapse: collapse; font-size: 12.5px; }
table.items-table th { text-align: left; font-size: 11px; text-transform: uppercase; color: var(--muted); padding: 6px 4px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.items-table td { padding: 4px; vertical-align: top; }
table.items-table input, table.items-table select { padding: 6px; font-size: 12.5px; }
table.items-table td:first-child input { min-width: 140px; }
.remove-item { color: #dc2626; background: none; border: none; cursor: pointer; font-size: 16px; }

/* ---------- Live preview shell ---------- */
.preview-panel { position: sticky; top: 20px; }
.preview-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.template-switch { display: flex; gap: 6px; }
.template-switch button { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: #fff; font-size: 12.5px; cursor: pointer; }
.template-switch button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.invoice-sheet-wrap {
  background: #e2e8f0;
  border-radius: var(--radius);
  padding: 24px;
  overflow: auto;
}

/* ---------- Invoice sheet (A4-like) — shared by preview + print ---------- */
.invoice-sheet {
  background: #fff;
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 16mm;
  box-sizing: border-box;
  box-shadow: 0 4px 18px rgba(0,0,0,.12);
  font-size: 12px;
  color: #1f2937;
}
/* NOTE: these were `display:flex` before. Flex containers don't fragment
   reliably across a print page-break (Chromium in particular can render a
   flex box fully, then duplicate a fragment of it at the top of the next
   page even with page-break-inside:avoid set). Table/table-cell layout
   fragments correctly, so we use that instead for anything print-critical,
   while keeping the same visual result. */
.inv-head { display: table; width: 100%; table-layout: fixed; margin-bottom: 18px; }
.inv-head .logo { max-height: 60px; max-width: 180px; object-fit: contain; }
.inv-head .company-block { display: table-cell; vertical-align: top; width: 55%; max-width: 55%; }
.inv-head .company-name { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
.inv-head .meta-block { display: table-cell; vertical-align: top; width: 45%; text-align: right; }
.inv-head .inv-title { font-size: 24px; font-weight: 800; letter-spacing: .04em; color: var(--brand-color, var(--brand)); margin-bottom: 6px; }
.inv-head .inv-number { font-weight: 700; }

.inv-parties { display: table; width: 100%; table-layout: fixed; margin: 16px 0; padding: 12px 0; border-top: 1px solid #e5e7eb; border-bottom: 1px solid #e5e7eb; }
.inv-parties .block { display: table-cell; vertical-align: top; padding-right: 20px; }
.inv-parties .block:last-child { padding-right: 0; }
.inv-parties h4 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--brand-color, var(--brand)); }

.inv-table { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: 11.5px; }
.inv-table th { background: var(--brand-color, var(--brand)); color: #fff; text-align: left; padding: 6px 8px; font-weight: 600; }
.inv-table td { padding: 6px 8px; border-bottom: 1px solid #eef0f3; }
.inv-table td.num, .inv-table th.num { text-align: right; }

.inv-totals { text-align: right; margin-top: 8px; }
.inv-totals table { display: inline-block; text-align: left; width: 260px; font-size: 12px; }
.inv-totals td { padding: 4px 6px; }
.inv-totals tr.grand td { font-weight: 800; font-size: 14px; border-top: 2px solid #1f2937; }

.inv-footer { display: table; width: 100%; table-layout: fixed; margin-top: 30px; }
.inv-notes { display: table-cell; vertical-align: top; width: 55%; max-width: 55%; font-size: 11px; color: #4b5563; }
.inv-signoff { display: table-cell; vertical-align: top; text-align: right; }
.inv-signoff img.seal { max-height: 70px; opacity: .9; }
.inv-signoff img.sig { max-height: 50px; display: block; margin-left: auto; }
.inv-signoff .line { border-top: 1px solid #9ca3af; width: 180px; margin: 6px 0 4px auto; }

.reverse-charge-tag { display: inline-block; background: #fef3c7; color: #92400e; padding: 2px 8px; border-radius: 6px; font-size: 10.5px; font-weight: 700; margin-left: 8px; }

/* Template variants */
.tpl-classic .inv-title { font-family: Georgia, 'Times New Roman', serif; }
.tpl-classic .inv-table th { background: #1f2937; }
.tpl-corporate .inv-head { border-bottom: 3px solid var(--brand-color, var(--brand)); padding-bottom: 12px; }

/* ---------- Print ---------- */
#printArea { display: none; }
@media print {
  .sidebar, .topbar, .btn-row, .toolbar, .alert, .page-title { display: none !important; }
  .app-shell, .main, .content { display: block !important; margin: 0 !important; padding: 0 !important; width: auto !important; min-height: 0 !important; height: auto !important; }
  #printArea { display: block !important; }
  .invoice-sheet-wrap { background: none !important; padding: 0 !important; overflow: visible !important; }
  .invoice-sheet {
    box-shadow: none;
    width: auto;
    min-height: 0;
    height: auto;
    margin: 0;
    padding: 12mm;
    box-sizing: border-box;
  }
  .inv-footer { page-break-inside: avoid; break-inside: avoid; margin-top: 18px; }
  .inv-totals, .inv-parties, .inv-head { page-break-inside: avoid; break-inside: avoid; }
  .inv-table tr { page-break-inside: avoid; break-inside: avoid; }
  @page { size: A4; margin: 0; }
}

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }

.toolbar { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 3px; }
.tabs .tab { padding: 6px 14px; border-radius: 6px; font-size: 13px; color: var(--muted); text-decoration: none; }
.tabs .tab.active { background: var(--brand); color: #fff; }
.search-form { display: flex; gap: 6px; }
.search-form input[type="text"] { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; }
.toolbar .btn-primary { margin-left: auto; }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }
.alert-success { background: #ecfdf5; color: #047857; border: 1px solid #a7f3d0; }
.alert-error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }

.actions-cell form { display: inline; }
.link-btn-danger { background: none; border: none; color: #dc2626; cursor: pointer; font-size: 12.5px; padding: 0; text-decoration: underline; }
.link-btn-danger:hover { color: #991b1b; }
.toast { position: fixed; bottom: 20px; right: 20px; background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); opacity: 0; transform: translateY(8px); transition: .2s; }
.toast.show { opacity: 1; transform: translateY(0); }
