/* ===== Base ===== */
:root {
  --brand: #0f2a4a;
  --bg: #f4f6f9;
  --card: #fff;
  --text: #1c2430;
  --muted: #6b7584;
  --line: #e4e8ee;
  --green: #12805c;
  --red: #c0392b;
  --amber: #b7791f;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px; color: var(--text); background: var(--bg); line-height: 1.45;
}
a { color: var(--brand); }
h1, h2 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.r { text-align: right; }
.c { text-align: center; }
.nowrap { white-space: nowrap; }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.pre { white-space: pre-line; }
.inline { display: inline; }

/* ===== Layout ===== */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; flex-shrink: 0; background: var(--brand); color: #fff;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px; color: #fff; text-decoration: none; font-weight: 700; font-size: 15px; border-bottom: 1px solid rgba(255,255,255,.1); }
.brand img { width: 38px; height: 38px; object-fit: contain; background: #fff; border-radius: 8px; padding: 3px; }
.sidebar nav { padding: 12px 10px; flex: 1; }
.sidebar nav a { display: block; padding: 10px 12px; border-radius: 8px; color: rgba(255,255,255,.78); text-decoration: none; font-weight: 500; margin-bottom: 2px; }
.sidebar nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.sidebar nav a.active { background: rgba(255,255,255,.16); color: #fff; }
.sidebar-foot { padding: 16px 18px; border-top: 1px solid rgba(255,255,255,.1); font-size: 13px; }
.sidebar-foot a { color: rgba(255,255,255,.7); }
.who { font-weight: 600; margin-bottom: 4px; }

.main { flex: 1; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 16px 28px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar h1 { font-size: 20px; flex: 1; }
.top-actions { display: flex; gap: 8px; }
.menu-btn { display: none; border: 0; background: none; font-size: 22px; cursor: pointer; }
.content { padding: 24px 28px 60px; max-width: 1200px; }

/* ===== Components ===== */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.card.narrow { max-width: 760px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head.wrap { flex-wrap: wrap; }
.card-head h2 { font-size: 16px; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.hint { background: #fff8e6; border: 1px solid #f3dfa8; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 8px; border: 1px solid transparent; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { filter: brightness(1.15); }
.btn-light { background: #fff; color: var(--text); border-color: var(--line); }
.btn-light:hover { background: #f1f3f6; }
.btn-danger-light { background: #fff; color: var(--red); border-color: #f2d3cf; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 12px 22px; font-size: 15px; }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: none; font-size: 20px; color: var(--muted); cursor: pointer; line-height: 1; }
.icon-btn:hover { color: var(--red); }

.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 18px; }
.alert-success { background: #e7f6ef; color: #0d5c42; border: 1px solid #bfe6d4; }
.alert-error { background: #fdecea; color: #8e2a1f; border: 1px solid #f5c6c0; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: #eef1f5; color: #4a5565; }
.b-paid, .b-accepted { background: #e2f5ec; color: var(--green); }
.b-partial, .b-sent { background: #e6effc; color: #2457a6; }
.b-unpaid, .b-draft { background: #f1f2f5; color: #4a5565; }
.b-overdue, .b-rejected, .b-cancelled { background: #fdecea; color: var(--red); }
.b-converted { background: #f3ecfb; color: #6b3fa0; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat-value { font-size: 22px; font-weight: 700; margin-top: 6px; }
.stat-sub { font-size: 12px; margin-top: 4px; color: var(--muted); }

/* Tables */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; padding: 10px; border-bottom: 1px solid var(--line); }
.table th.r { text-align: right; }
.table td { padding: 11px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tfoot td { border-bottom: 0; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: #f8fafc; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs a { padding: 7px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 500; }
.tabs a.active { background: #eef1f5; color: var(--text); font-weight: 600; }
.search input { width: 240px; }

/* Forms */
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; color: #3b4452; }
label.check { flex-direction: row; align-items: center; font-weight: 500; }
input, select, textarea {
  font: inherit; font-weight: 400; color: var(--text); padding: 9px 11px; border: 1px solid #d5dbe3; border-radius: 8px; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 30%, transparent); border-color: var(--brand); }
input[type=color] { padding: 2px; height: 40px; }
input[type=checkbox] { width: auto; }
textarea { resize: vertical; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.form-stack { display: flex; flex-direction: column; gap: 14px; }
.span2 { grid-column: span 2; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; }
.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 16px 0; }
.with-btn { display: flex; gap: 8px; }
.grid-2-1 { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; align-items: start; }
.logo-preview { border: 1px dashed #cfd6df; border-radius: 8px; height: 110px; display: flex; align-items: center; justify-content: center; background: #fafbfc; }
.logo-preview img { max-height: 90px; max-width: 90%; }

/* Item editor */
.items-table td { padding: 8px 6px; }
.items-table textarea { min-height: 42px; }
.items-table .amount { padding-top: 18px; font-weight: 600; }
.items-table .row-no { padding-top: 18px; color: var(--muted); }
.service-picker { width: auto; max-width: 340px; }
.totals-box { margin-left: auto; margin-top: 16px; width: 340px; display: flex; flex-direction: column; gap: 8px; }
.totals-box > div { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.totals-box input { width: 130px; }
.totals-box .grand { border-top: 2px solid var(--text); padding-top: 10px; font-size: 17px; }

/* Doc toolbar & dropdown */
.doc-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.doc-toolbar .left, .doc-toolbar .right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.dropdown { position: relative; }
.dropdown-menu { display: none; position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(15,30,50,.12); min-width: 210px; padding: 6px; z-index: 20; }
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 9px 12px; border-radius: 6px; font: inherit; cursor: pointer; }
.dropdown-menu button:hover { background: #f1f3f6; }
.dropdown-menu button.danger { color: var(--red); }

/* Auth */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, var(--brand), #1d4e7e); padding: 20px; }
.auth-card { background: #fff; width: 100%; max-width: 400px; padding: 34px; border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.auth-card h1 { font-size: 22px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.auth-logo { max-height: 64px; margin-bottom: 12px; }

/* ===== The printable document ===== */
.paper-wrap { overflow-x: auto; }
.paper {
  --ink: #1c2430;
  position: relative; display: flex; flex-direction: column; background: #fff; width: 210mm; min-height: 297mm; margin: 0 auto; padding: 0 16mm 8mm;
  box-shadow: 0 4px 24px rgba(15,30,50,.10); border: 1px solid var(--line); color: var(--ink); font-size: 12.5px;
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.p-band { height: 8px; background: var(--brand); margin: 0 -16mm; }
.p-head { display: flex; justify-content: space-between; align-items: center; padding: 8mm 0 5mm; border-bottom: 1px solid #e3e7ed; }
.p-company { display: flex; align-items: center; gap: 14px; }
.p-logo { max-height: 64px; max-width: 160px; object-fit: contain; }
.p-company-name { font-size: 20px; font-weight: 700; color: var(--brand); letter-spacing: -.01em; }
.p-tagline { color: #6b7584; font-size: 11.5px; margin-top: 2px; }
.p-title { text-align: right; }
.p-title h2 { font-size: 26px; letter-spacing: .12em; color: var(--brand); font-weight: 700; }
.p-number { color: #6b7584; font-weight: 600; margin-top: 2px; }

.p-parties { display: grid; grid-template-columns: 1fr 1fr 1.05fr; gap: 18px; padding: 5mm 0 4mm; line-height: 1.5; }
.p-label { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: .1em; color: #8a93a1; font-weight: 700; margin-bottom: 4px; }
.p-strong { font-weight: 700; font-size: 13.5px; }
.p-meta table { width: 100%; border-collapse: collapse; background: #f6f8fb; border-radius: 8px; overflow: hidden; }
.p-meta th, .p-meta td { padding: 6px 10px; text-align: left; font-size: 12px; }
.p-meta th { color: #6b7584; font-weight: 600; }
.p-meta td { text-align: right; font-weight: 600; }
.p-meta tr.p-due th, .p-meta tr.p-due td { background: var(--brand); color: #fff; font-size: 13px; }
.p-subject { margin-bottom: 5mm; padding: 8px 12px; border-left: 3px solid var(--brand); background: #f6f8fb; }

.p-items { width: 100%; border-collapse: collapse; }
.p-items th { background: var(--brand); color: #fff; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding: 9px 10px; text-align: left; }
.p-items th.r { text-align: right; }
.p-items th.c { text-align: center; }
.p-items td { padding: 9px 10px; border-bottom: 1px solid #e8ecf1; vertical-align: top; }
.p-items tbody tr:nth-child(even) td { background: #fafbfd; }
.p-item-title { font-weight: 600; }
.p-item-desc { color: #6b7584; font-size: 11.5px; margin-top: 2px; }

.p-summary { display: flex; justify-content: space-between; gap: 24px; margin-top: 6mm; }
.p-summary-left { flex: 1; }
.p-words { padding: 10px 12px; background: #f6f8fb; border-radius: 8px; font-weight: 600; margin-bottom: 12px; }
.p-totals { width: 78mm; border-collapse: collapse; align-self: flex-start; }
.p-totals th, .p-totals td { padding: 6px 10px; }
.p-totals th { text-align: left; font-weight: 500; color: #4a5565; }
.p-totals td { text-align: right; font-weight: 600; }
.p-totals .p-grand th, .p-totals .p-grand td { background: var(--brand); color: #fff; font-size: 15px; font-weight: 700; padding: 10px; }
.p-totals .p-balance th, .p-totals .p-balance td { border-top: 2px solid var(--ink); font-weight: 700; font-size: 14px; }
.p-block { margin-top: 4.5mm; }
.p-block .small { font-size: 11.5px; color: #4a5565; }

.p-sign { margin-bottom: 8mm; display: flex; justify-content: space-between; gap: 40px; margin-top: 12mm; font-size: 12px; }
.p-sign > div { width: 62mm; }
.p-sign-line { border-top: 1px solid #9aa3af; margin-bottom: 6px; }

.p-stamp { position: absolute; top: 95mm; right: 28mm; transform: rotate(-14deg); border: 4px solid var(--green); color: var(--green); font-size: 34px; font-weight: 800; letter-spacing: .15em; padding: 4px 18px; border-radius: 10px; opacity: .55; }
.p-stamp-red { border-color: var(--red); color: var(--red); }

.p-foot { margin-top: auto; padding-top: 8px; border-top: 1px solid #e3e7ed; text-align: center; font-size: 11px; color: #4a5565; }

/* ===== Print ===== */
@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .sidebar, .topbar, .no-print, .alert { display: none !important; }
  .content { padding: 0; max-width: none; }
  .paper { box-shadow: none; border: 0; margin: 0; width: 210mm; min-height: 296mm; }
  .paper-wrap { overflow: visible; }
  .p-items tr { page-break-inside: avoid; }
}

/* ===== Responsive ===== */
@media (max-width: 1000px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid-2-1 { grid-template-columns: 1fr; }
  .form-grid.cols-4 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 760px) {
  .sidebar { position: fixed; left: -240px; z-index: 30; transition: left .2s; }
  .sidebar.open { left: 0; }
  .menu-btn { display: block; }
  .topbar { padding: 12px 16px; }
  .top-actions .btn-light { display: none; }
  .content { padding: 16px; }
  .form-grid, .form-grid.cols-4 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .stats { grid-template-columns: 1fr; }
  .search input { width: 100%; }
  .totals-box { width: 100%; }
  .table { display: block; overflow-x: auto; }
}
