:root {
  --navy: #1f2a3c; --navy-2: #2b3950; --accent: #8c6a4f; --accent-soft: #f5efe9;
  --bg: #f4f5f7; --card: #ffffff; --line: #e5e7eb; --text: #111827; --muted: #6b7280;
  --green: #15803d; --green-bg: #dcfce7; --red: #b91c1c; --red-bg: #fee2e2; --amber: #b45309; --amber-bg: #fef3c7; --blue: #1d4ed8; --blue-bg: #dbeafe; --grey-bg: #e5e7eb;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
h1 { font-size: 22px; margin: 0; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.mono { font-variant-numeric: tabular-nums; }

.app { display: flex; min-height: 100vh; }
.sidebar { width: 230px; background: var(--card); border-right: 1px solid var(--line); padding: 22px 16px; display: flex; flex-direction: column; gap: 6px; position: sticky; top: 0; height: 100vh; }
.brand img { width: 100%; max-width: 190px; display: block; margin: 4px auto 22px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: #374151; font-weight: 500; }
.sidebar nav a .ico { width: 18px; text-align: center; color: var(--muted); font-size: 14px; }
.sidebar nav a:hover { background: var(--bg); }
.sidebar nav a.active { background: var(--navy); color: #fff; }
.sidebar nav a.active .ico { color: #cbd5e1; }
.sidebar .new-invoice { margin-top: 14px; text-align: center; }
.sidebar-foot { margin-top: auto; font-size: 12px; color: var(--muted); line-height: 1.5; }
.sidebar-foot span { font-size: 11px; }
.main { flex: 1; padding: 28px 36px 60px; min-width: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.card + .card { margin-top: 18px; }
.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, 1fr); } .grid-3 { grid-template-columns: 1fr; } }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } .sidebar { display: none; } .main { padding: 18px; } }

.stat { padding: 18px 20px; }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.02em; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.stat.good .value { color: var(--green); }
.stat.bad .value { color: var(--red); }
.stat.warn .value { color: var(--amber); }
.stat-mini { display: flex; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--line); }
.stat-mini:first-child { border-top: 0; padding-top: 0; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 500; line-height: 1.2; white-space: nowrap; }
.btn:hover { background: #f9fafb; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-2); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent:hover { filter: brightness(1.08); }
.btn-danger { color: var(--red); border-color: #fecaca; }
.btn-danger:hover { background: var(--red-bg); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--bg); }

.pill { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600; text-transform: capitalize; }
.pill.draft { background: var(--grey-bg); color: #374151; }
.pill.sent { background: var(--blue-bg); color: var(--blue); }
.pill.partial { background: var(--amber-bg); color: var(--amber); }
.pill.overdue { background: var(--red-bg); color: var(--red); }
.pill.paid { background: var(--green-bg); color: var(--green); }
.pill.void { background: #f3f4f6; color: #9ca3af; text-decoration: line-through; }

table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 11px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.row-link { cursor: pointer; }
.tbl tr.row-link:hover td { background: #fafafa; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl .empty { text-align: center; color: var(--muted); padding: 32px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tabs button { background: none; border: 0; padding: 10px 14px; cursor: pointer; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.active { color: var(--navy); border-bottom-color: var(--navy); }
.tabs button .count { background: var(--grey-bg); border-radius: 999px; padding: 1px 7px; font-size: 11px; margin-left: 6px; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field label { font-size: 12.5px; font-weight: 600; color: #374151; }
.field .hint { font-size: 12px; color: var(--muted); }
input[type=text], input[type=email], input[type=number], input[type=date], input[type=password], input[type=color], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--navy); box-shadow: 0 0 0 3px rgba(31,42,60,.12); }
textarea { min-height: 80px; resize: vertical; line-height: 1.45; }
input[type=color] { padding: 2px; height: 38px; width: 60px; }
.row { display: flex; gap: 14px; }
.row > * { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.check input { width: 16px; height: 16px; }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 600; }
.switch input { appearance: none; width: 38px; height: 22px; border-radius: 999px; background: #d1d5db; position: relative; transition: .15s; cursor: pointer; margin: 0; }
.switch input:checked { background: var(--green); }
.switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .15s; }
.switch input:checked::after { left: 19px; }

.editor { display: grid; grid-template-columns: 1fr 330px; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .editor { grid-template-columns: 1fr; } }
.items-table td { padding: 6px 6px; border-bottom: 0; }
.items-table th { padding: 6px; }
.items-table input { padding: 7px 9px; }
.items-table .del { color: var(--muted); background: none; border: 0; cursor: pointer; font-size: 16px; }
.items-table .del:hover { color: var(--red); }
.totals { margin-top: 10px; }
.totals .line { display: flex; justify-content: space-between; padding: 7px 0; border-top: 1px solid var(--line); }
.totals .line.grand { font-weight: 700; font-size: 16px; border-top: 2px solid var(--navy); margin-top: 4px; }
.sticky { position: sticky; top: 24px; }

.viewer { display: grid; grid-template-columns: 1fr 340px; gap: 18px; align-items: start; }
@media (max-width: 1100px) { .viewer { grid-template-columns: 1fr; } }
.pdf-frame { width: 100%; height: calc(100vh - 170px); min-height: 700px; border: 1px solid var(--line); border-radius: var(--radius); background: #525659; }
.kv { display: flex; justify-content: space-between; padding: 6px 0; }
.kv b { font-variant-numeric: tabular-nums; }
.list-item { padding: 10px 0; border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.list-item:first-of-type { border-top: 0; }

.chart { width: 100%; height: 220px; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

.modal-back { position: fixed; inset: 0; background: rgba(17,24,39,.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: #fff; border-radius: 14px; width: 560px; max-width: 100%; max-height: 92vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { width: 720px; }
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; }
.modal-body { padding: 20px 22px; }
.modal-foot { padding: 14px 22px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.close-x { background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--muted); }

#toast-root { position: fixed; bottom: 22px; right: 22px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--navy); color: #fff; padding: 12px 16px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.2); font-weight: 500; max-width: 380px; animation: pop .18s ease-out; }
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.placeholders { display: flex; flex-wrap: wrap; gap: 6px; }
.placeholders code { background: var(--bg); border: 1px solid var(--line); padding: 2px 6px; border-radius: 6px; font-size: 12px; cursor: pointer; }
.placeholders code:hover { background: var(--accent-soft); }
.logo-preview { max-width: 240px; max-height: 70px; display: block; border: 1px dashed var(--line); padding: 10px; border-radius: 8px; background: #fff; margin-bottom: 10px; }
.status-banner { padding: 10px 14px; border-radius: 8px; font-weight: 500; margin-bottom: 14px; }
.status-banner.overdue { background: var(--red-bg); color: var(--red); }
.status-banner.paid { background: var(--green-bg); color: var(--green); }
.status-banner.draft { background: var(--grey-bg); color: #374151; }
.status-banner.void { background: #f3f4f6; color: #6b7280; }
.dropdown { position: relative; }
.dropdown-menu { position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.14); min-width: 190px; z-index: 20; padding: 6px; display: none; }
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 6px; cursor: pointer; }
.dropdown-menu button:hover { background: var(--bg); }
.dropdown-menu button.danger { color: var(--red); }
.bar { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
