*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --bg: #f8fafc;
  --card: #fff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --green: #059669;
  --red: #dc2626;
  --amber: #d97706;
  --sidebar-w: 240px;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.hidden { display: none !important; }
.text-right { text-align: right; }
.text-muted { color: var(--muted); font-size: 0.875rem; }
.mt { margin-top: 1rem; }
.empty { text-align: center; color: var(--muted); padding: 2rem; }

/* Loading */
.loading-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(255,255,255,0.7);
  display: flex; align-items: center; justify-content: center;
}
.spinner {
  width: 40px; height: 40px;
  border: 3px solid #e2e8f0;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-inline { text-align: center; padding: 3rem; color: var(--muted); }

/* Login */
.login-page {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #eef2ff, #fff, #eff6ff);
  padding: 1.5rem;
}
.login-card {
  width: 100%; max-width: 400px;
  background: var(--card);
  border-radius: 1rem;
  border: 1px solid var(--border);
  box-shadow: 0 20px 40px rgba(0,0,0,0.08);
  overflow: hidden;
}
.login-header { text-align: center; padding: 2rem 2rem 1rem; }
.logo-icon { font-size: 2.5rem; margin-bottom: 0.5rem; }
.login-header h1 { font-size: 1.5rem; }
.login-header p { color: var(--muted); font-size: 0.875rem; margin-top: 0.25rem; }
.login-form { padding: 1.5rem 2rem 2rem; display: flex; flex-direction: column; gap: 0.75rem; }
.login-hint { text-align: center; font-size: 0.75rem; color: var(--muted); margin-top: 0.5rem; }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w);
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 100;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
}
.logo-icon-sm { font-size: 1.5rem; }
.sidebar-brand strong { display: block; font-size: 0.9rem; }
.sidebar-brand small { color: var(--muted); font-size: 0.7rem; }
.sidebar-nav { flex: 1; padding: 0.75rem; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 0.6rem;
  width: 100%; padding: 0.65rem 0.75rem;
  border: none; background: none; border-radius: 0.5rem;
  font-size: 0.875rem; color: var(--muted);
  cursor: pointer; text-align: left; margin-bottom: 2px;
  transition: background 0.15s;
}
.nav-item:hover { background: #f1f5f9; color: var(--text); }
.nav-item.active { background: #eef2ff; color: var(--primary); font-weight: 600; }
.sidebar-footer { padding: 1rem; border-top: 1px solid var(--border); }
.user-info { font-size: 0.75rem; color: var(--muted); margin-bottom: 0.5rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { flex: 1; margin-left: var(--sidebar-w); padding: 1.5rem 2rem; min-width: 0; }
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.5rem; }
.page-header p { color: var(--muted); font-size: 0.875rem; }

/* Cards & Stats */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  margin-bottom: 1rem;
  overflow: hidden;
}
.card-header { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); }
.card-header h2 { font-size: 0.95rem; }
.card-body { padding: 1.25rem; }
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card { padding: 1.25rem; border-radius: 0.75rem; border: 1px solid var(--border); background: var(--card); }
.stat-label { font-size: 0.75rem; color: var(--muted); margin-bottom: 0.25rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-green .stat-value { color: var(--green); }
.stat-red .stat-value { color: var(--red); }
.stat-amber .stat-value { color: var(--amber); }
.stat-blue .stat-value { color: var(--primary); }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th, td { padding: 0.65rem 0.75rem; text-align: left; border-bottom: 1px solid #f1f5f9; }
th { font-size: 0.75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
tr:hover td { background: #f8fafc; }
.badge { display: inline-block; padding: 0.15rem 0.5rem; background: #f1f5f9; border-radius: 999px; font-size: 0.75rem; }
.badge-gray { background: #f1f5f9; color: #64748b; }
.badge-blue { background: #dbeafe; color: #1d4ed8; }
.badge-green { background: #d1fae5; color: #047857; }
.badge-red { background: #fee2e2; color: #b91c1c; }

/* Forms */
label { display: block; font-size: 0.8rem; font-weight: 500; margin-bottom: 0.3rem; color: var(--text); }
input, select, textarea {
  width: 100%; padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: 0.5rem;
  font-size: 0.875rem; font-family: inherit;
  transition: border-color 0.15s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.15);
}
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 1rem;
}
.span-2 { grid-column: span 2; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.55rem 1rem; border-radius: 0.5rem;
  font-size: 0.875rem; font-weight: 500; border: none;
  cursor: pointer; transition: background 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #f8fafc; }
.btn-danger { background: #fef2f2; color: var(--red); border: 1px solid #fecaca; }
.btn-danger:hover { background: #fee2e2; }
.btn-warn { background: var(--amber); color: #fff; }
.btn-ghost { background: none; color: var(--muted); padding: 0.4rem; }
.btn-ghost:hover { color: var(--red); }
.btn-sm { font-size: 0.8rem; padding: 0.4rem 0.75rem; }
.btn-xs { font-size: 0.75rem; padding: 0.25rem 0.45rem; min-height: auto; }
.btn-block { width: 100%; }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 1rem 0; }

/* Alerts */
.alert { padding: 0.75rem 1rem; border-radius: 0.5rem; font-size: 0.875rem; margin-bottom: 1rem; }
.alert-error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; white-space: pre-line; }
.alert-warn { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }
.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; font-size: 0.8rem; }

/* Toast */
.toast {
  position: fixed; bottom: 1.5rem; right: 1.5rem; z-index: 10000;
  padding: 0.75rem 1.25rem; border-radius: 0.5rem;
  background: #1e293b; color: #fff; font-size: 0.875rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  animation: fadeIn 0.2s ease;
}
.toast-error { background: #b91c1c; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } }

/* Nav sections */
.nav-section { margin-bottom: 0.75rem; }
.nav-section-label { padding: 0 0.75rem; margin-bottom: 0.25rem; font-size: 0.65rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: #94a3b8; }

/* Layout extras */
.page-header-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.page-header-row .page-header { margin-bottom: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.mb { margin-bottom: 1rem; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.font-mono { font-family: ui-monospace, monospace; }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.clickable { cursor: pointer; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.filter-bar { margin-bottom: 1rem; }
.filter-bar select { max-width: 240px; }
.type-picker { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.type-chip { padding: 0.35rem 0.75rem; border-radius: 999px; font-size: 0.75rem; border: 1px solid var(--border); background: #fff; cursor: pointer; }
.type-chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.line-row { display: grid; grid-template-columns: 1fr 2fr 70px 60px 90px 70px auto; gap: 0.4rem; align-items: center; margin-bottom: 0.5rem; }
.jline-row { display: grid; grid-template-columns: 2fr 90px 90px 1fr auto; gap: 0.4rem; align-items: center; margin-bottom: 0.5rem; }
.totals-box { text-align: right; font-size: 0.9rem; }
.totals-box .total-line { font-size: 1.1rem; margin-top: 0.5rem; padding-top: 0.5rem; border-top: 1px solid var(--border); }

.doc-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.print-doc {
  background: #fff; padding: 2.5rem 2.75rem; border: 1px solid var(--border); border-radius: 0.75rem;
  max-width: 210mm; margin: 0 auto; box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  font-size: 0.9rem; line-height: 1.5; color: #1e293b;
}
.print-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 2rem; border-bottom: 2px solid #1e293b; padding-bottom: 1.25rem; margin-bottom: 1.25rem; }
.company-name { font-size: 1.2rem; font-weight: 700; margin-bottom: 0.35rem; line-height: 1.35; }
.print-header p { margin: 0.15rem 0; font-size: 0.82rem; color: #475569; }
.print-title { text-align: right; flex-shrink: 0; }
.print-title h2 { font-size: 1.45rem; color: var(--primary); margin: 0 0 0.35rem; font-weight: 700; }
.doc-num { font-size: 1.05rem; font-weight: 600; margin: 0.2rem 0; }
.print-title p { margin: 0.15rem 0; font-size: 0.82rem; color: #64748b; }
.print-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-bottom: 1.25rem; font-size: 0.85rem; }
.print-meta strong { display: block; margin-bottom: 0.35rem; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; color: #64748b; }
.print-meta p { margin: 0.15rem 0; }
.print-table { width: 100%; border-collapse: collapse; margin-bottom: 1rem; font-size: 0.82rem; table-layout: fixed; }
.print-table th, .print-table td { border: 1px solid #cbd5e1; padding: 0.45rem 0.55rem; vertical-align: top; }
.print-table th { background: #f1f5f9; font-weight: 600; font-size: 0.78rem; }
.print-table th:nth-child(1), .print-table td:nth-child(1) { width: 2.5rem; text-align: center; }
.print-table th:nth-child(3), .print-table td:nth-child(3) { width: 3.5rem; text-align: center; }
.print-table th:nth-child(4), .print-table td:nth-child(4) { width: 3.5rem; text-align: center; }
.print-table th:nth-child(5), .print-table td:nth-child(5),
.print-table th:nth-child(6), .print-table td:nth-child(6),
.print-table th:nth-child(7), .print-table td:nth-child(7) { width: 5.5rem; }
.print-table .col-desc { white-space: pre-wrap; word-break: break-word; }
.print-totals { text-align: right; font-size: 0.88rem; margin-top: 0.5rem; padding-top: 0.75rem; border-top: 1px solid #e2e8f0; }
.print-totals > div { margin: 0.2rem 0; }
.print-totals .total-line { font-size: 1.1rem; font-weight: 700; margin-top: 0.5rem; padding-top: 0.5rem; border-top: 2px solid #1e293b; }
.print-notes { margin-top: 1.25rem; padding-top: 1rem; border-top: 1px dashed #cbd5e1; font-size: 0.82rem; color: #475569; }
.print-notes strong { display: block; margin-bottom: 0.4rem; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; color: #64748b; }
.print-notes-body { white-space: pre-wrap; word-break: break-word; line-height: 1.6; }
.print-header-left { display: flex; gap: 1rem; align-items: flex-start; }
.print-logo { max-height: 64px; max-width: 120px; object-fit: contain; flex-shrink: 0; }
.amount-words { font-size: 0.8rem; color: #64748b; font-style: italic; margin-top: 0.25rem; }
.print-bank { margin-top: 1rem; padding: 0.6rem 0.75rem; background: #f8fafc; border-radius: 0.4rem; font-size: 0.82rem; }
.print-signatures { display: grid; grid-template-columns: 1fr 1fr; gap: 2.5rem; margin-top: 2rem; padding-top: 1rem; }
.sig-block { text-align: center; font-size: 0.82rem; }
.sig-image-wrap { height: 72px; display: flex; align-items: flex-end; justify-content: center; margin-bottom: 0.35rem; }
.sig-image { max-height: 68px; max-width: 180px; object-fit: contain; }
.sig-line { width: 75%; border-bottom: 1px solid #94a3b8; height: 1px; margin: 0 auto 0.5rem; }
.sig-label { font-weight: 600; color: #475569; margin-bottom: 0.2rem; }
.sig-name { margin: 0.15rem 0; }
.sig-title { font-size: 0.75rem; color: #64748b; }
.sig-date { font-size: 0.75rem; color: #94a3b8; margin-top: 0.25rem; }
.print-terms { margin-top: 1rem; font-size: 0.75rem; color: #64748b; border-top: 1px solid #f1f5f9; padding-top: 0.75rem; }

/* Share & sign */
.modal-hint { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.75rem; }
.share-link-row { display: flex; gap: 0.5rem; margin-bottom: 0.75rem; }
.share-url-input { flex: 1; font-size: 0.8rem; font-family: ui-monospace, monospace; }
.text-muted { color: var(--muted); }

.sign-page { max-width: 900px; margin: 0 auto; }
.main-sign { margin: 0 !important; padding: 1.5rem !important; }
.sign-page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1.25rem; gap: 1rem; }
.sign-page-header h1 { font-size: 1.25rem; margin-bottom: 0.25rem; }
.sign-panel { margin-top: 1.5rem; padding: 1.25rem; background: #fff; border: 1px solid var(--border); border-radius: 0.75rem; }
.sign-panel h2 { font-size: 1rem; margin-bottom: 0.5rem; }
.sig-pad-wrap { margin: 1rem 0; }
.sig-canvas { width: 100%; height: 160px; border: 2px dashed #cbd5e1; border-radius: 0.5rem; background: #fff; touch-action: none; cursor: crosshair; display: block; }

.settings-upload .upload-row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; margin-top: 0.35rem; }
.upload-preview { width: 80px; height: 48px; object-fit: contain; border: 1px solid var(--border); border-radius: 0.4rem; background: #f8fafc; }
.upload-preview.empty { display: flex; align-items: center; justify-content: center; font-size: 0.65rem; color: var(--muted); padding: 0.25rem; text-align: center; }
.upload-preview.sig-preview { width: 120px; height: 56px; }
.card-footer { padding: 0.75rem 1.25rem; border-top: 1px solid var(--border); display: flex; gap: 1.5rem; font-size: 0.875rem; }

.journal-entry { margin-bottom: 1.5rem; padding-bottom: 1rem; border-bottom: 1px solid #f1f5f9; }
.journal-head { display: flex; flex-wrap: wrap; gap: 1rem; margin-bottom: 0.5rem; font-size: 0.875rem; }
.journal-lines { width: 100%; font-size: 0.8rem; }
.total-row td { border-top: 2px solid #1e293b; font-weight: 600; }
.report-section { font-size: 0.95rem; margin: 1rem 0 0.5rem; color: var(--muted); }
.report-net { margin-top: 1.5rem; padding: 1rem; background: #f8fafc; border-radius: 0.5rem; font-size: 1.1rem; text-align: right; }
.overdue-list { margin-top: 0.5rem; font-size: 0.8rem; color: var(--red); list-style: none; }

/* Toolbar & search */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.search-bar { flex: 1; min-width: 200px; }
.search-input { width: 100%; padding: 0.55rem 0.75rem; border: 1px solid var(--border); border-radius: 0.5rem; font-size: 0.875rem; }
.filter-bar-multi { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }
.filter-bar-multi .search-bar { flex: 1; min-width: 180px; }
.filter-bar-multi select { max-width: 200px; }
.checkbox-label { font-size: 0.8rem; color: var(--muted); display: flex; align-items: center; gap: 0.4rem; cursor: pointer; }
.actions-cell { white-space: nowrap; width: 80px; text-align: right; }
.row-inactive { opacity: 0.55; background: #fafafa; }
.row-inactive td { text-decoration: line-through; text-decoration-color: #cbd5e1; }
.convert-wrap { display: inline-flex; gap: 0.35rem; align-items: center; }
.convert-select { padding: 0.35rem 0.5rem; border: 1px solid var(--border); border-radius: 0.4rem; font-size: 0.75rem; }

/* Modal */
.modal-overlay { position: fixed; inset: 0; z-index: 10001; background: rgba(15,23,42,0.45); display: flex; align-items: center; justify-content: center; padding: 1rem; }
.modal { background: #fff; border-radius: 0.75rem; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; box-shadow: 0 24px 48px rgba(0,0,0,0.18); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 1rem; }
.modal-close { background: none; border: none; font-size: 1.1rem; cursor: pointer; color: var(--muted); padding: 0.25rem; }
.modal-body { padding: 1.25rem; }
.modal-footer { display: flex; justify-content: flex-end; gap: 0.5rem; padding: 1rem 1.25rem; border-top: 1px solid var(--border); }
.modal-form { gap: 0.75rem; }

/* Chart */
.chart-bars { display: flex; align-items: flex-end; gap: 0.5rem; height: 140px; padding-top: 0.5rem; }
.chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; }
.chart-bar-wrap { flex: 1; display: flex; align-items: flex-end; gap: 2px; width: 100%; justify-content: center; }
.chart-bar { width: 40%; min-height: 2px; border-radius: 3px 3px 0 0; transition: height 0.3s; }
.chart-bar-rev { background: var(--green); }
.chart-bar-exp { background: var(--red); opacity: 0.75; }
.chart-label { font-size: 0.65rem; color: var(--muted); margin-top: 0.35rem; }
.chart-legend { font-size: 0.75rem; color: var(--muted); margin-top: 0.75rem; }
.legend-rev { color: var(--green); margin-right: 1rem; }
.legend-exp { color: var(--red); }

@media print {
  @page { size: A4; margin: 12mm 14mm; }
  body { background: #fff !important; }
  .sidebar, .no-print, .doc-actions, .loading-overlay, .sign-panel, .sign-page-header { display: none !important; }
  .main, .main-sign { margin: 0 !important; padding: 0 !important; }
  .print-doc { border: none; padding: 0; box-shadow: none; max-width: none; border-radius: 0; }
  .print-table th, .print-table td { padding: 0.35rem 0.45rem; }
  .sign-page { max-width: none; }
}

/* Mobile */
@media (max-width: 768px) {
  .sidebar { width: 100%; height: auto; position: relative; border-right: none; border-bottom: 1px solid var(--border); }
  .sidebar-nav { display: flex; flex-wrap: wrap; gap: 0.25rem; padding: 0.5rem; }
  .nav-item { width: auto; flex: 1 1 auto; font-size: 0.75rem; padding: 0.5rem; }
  .main { margin-left: 0; padding: 1rem; }
  .layout { flex-direction: column; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .grid-2 { grid-template-columns: 1fr; }
  .line-row { grid-template-columns: 1fr; }
  .jline-row { grid-template-columns: 1fr; }
  .print-meta { grid-template-columns: 1fr; }
}
