* { box-sizing: border-box; }
:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --text: #162033;
  --muted: #748096;
  --line: #e6eaf0;
  --primary: #2457e6;
  --primary-dark: #1947c8;
  --soft: #eef3ff;
  --danger: #d83b44;
  --shadow: 0 12px 35px rgba(27, 39, 65, 0.07);
}
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 250px 1fr; }
.sidebar {
  background: #101827;
  color: #fff;
  padding: 26px 18px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; gap: 12px; align-items: center; padding: 0 8px 30px; }
.brand-logo {
  width: 84px;
  height: 46px;
  max-width: 84px;
  max-height: 46px;
  flex: 0 0 84px;
  object-fit: contain;
  object-position: center;
  display: block;
}
.brand strong { display: block; font-size: 16px; }
.brand span { display: block; color: #9eabbd; font-size: 11px; margin-top: 4px; }
nav { display: grid; gap: 8px; }
.nav-link {
  width: 100%; border: 0; border-radius: 10px; padding: 13px 14px; color: #b8c1cf;
  background: transparent; text-align: left; display: flex; align-items: center; gap: 10px;
}
.nav-link:hover, .nav-link.active { background: rgba(255,255,255,.09); color: white; }
.nav-link span { font-size: 17px; }
.sidebar-note {
  margin-top: auto; padding: 16px; border-radius: 14px; background: rgba(255,255,255,.06);
}
.sidebar-note strong { font-size: 13px; }
.sidebar-note p { margin: 6px 0 0; color: #9eabbd; font-size: 11px; line-height: 1.55; }

.content { padding: 32px 34px 50px; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 28px; }
.eyebrow { margin: 0 0 6px; color: var(--primary); text-transform: uppercase; letter-spacing: .12em; font-size: 10px; font-weight: 800; }
.topbar h1 { margin: 0; font-size: 30px; letter-spacing: -.04em; }
.primary-btn, .secondary-btn, .text-btn {
  border: none; border-radius: 10px; font-weight: 700; transition: .2s ease;
}
.primary-btn { padding: 12px 17px; background: var(--primary); color: white; box-shadow: 0 7px 16px rgba(36,87,230,.18); }
.primary-btn:hover { background: var(--primary-dark); transform: translateY(-1px); }
.secondary-btn { padding: 12px 17px; background: #fff; border: 1px solid var(--line); color: var(--text); }
.secondary-btn:hover { background: #f7f9fc; }
.danger-btn { color: #c73743; border-color: #f1c7cb; background: #fff8f8; }
.danger-btn:hover { color: #a92934; border-color: #e9aeb4; background: #fff0f1; }

.text-btn { background: transparent; color: var(--primary); padding: 8px 0; }
.small { padding: 9px 12px; font-size: 12px; }

.view { display: none; }
.view.active { display: block; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stat-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 20px; box-shadow: var(--shadow);
}
.stat-card span { color: var(--muted); font-size: 12px; font-weight: 600; }
.stat-card strong { display: block; font-size: 28px; margin: 12px 0 6px; letter-spacing: -.04em; }
.stat-card small { color: #9aa4b6; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 22px; box-shadow: var(--shadow);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.panel-head.compact { margin-bottom: 16px; }
.panel-head h2 { margin: 0 0 4px; font-size: 18px; }
.panel-head p { margin: 0; color: var(--muted); font-size: 12px; }
.table-wrap { overflow-x: auto; position: relative; }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th {
  font-size: 10px; letter-spacing: .05em; text-transform: uppercase; color: #8a94a6;
  text-align: left; padding: 12px 10px; border-bottom: 1px solid var(--line);
}
td { padding: 14px 10px; border-bottom: 1px solid #edf0f4; font-size: 12px; vertical-align: middle; }
td strong { font-size: 12px; }
.client-cell small { color: var(--muted); display: block; margin-top: 3px; }
.badge { display: inline-flex; padding: 6px 9px; border-radius: 20px; font-size: 10px; font-weight: 800; }
.badge.Paid { background: #e9f8ef; color: #20884d; }
.badge.Unpaid { background: #fff5df; color: #b2790c; }
.badge.Overdue { background: #fdebed; color: #bf3040; }
.badge.Draft { background: #eef1f5; color: #657084; }
.row-actions { display: flex; gap: 7px; flex-wrap: wrap; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 7px 9px; color: #475469; font-size: 11px; }
.icon-btn:hover { background: #f4f6fa; }
.icon-btn.danger:hover { background: #fff0f1; color: var(--danger); border-color: #ffd4d7; }
.empty-state { text-align: center; padding: 42px 20px 26px; color: var(--muted); }
.empty-state h3 { color: var(--text); margin: 8px 0 6px; }
.empty-state p { margin: 0 0 16px; font-size: 12px; }
.empty-icon { width: 46px; height: 46px; border-radius: 50%; background: var(--soft); color: var(--primary); display: grid; place-items: center; margin: auto; font-size: 20px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.field-grid { display: grid; gap: 13px; margin-bottom: 13px; }
.field-grid.two { grid-template-columns: 1fr 1fr; }
.field-grid.three { grid-template-columns: repeat(3, 1fr); }
label { display: block; color: #4d596c; font-size: 11px; font-weight: 700; }
input, select, textarea {
  width: 100%; margin-top: 7px; border: 1px solid #dfe4ec; background: #fff; color: var(--text);
  border-radius: 9px; padding: 11px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: #8ca8ff; box-shadow: 0 0 0 3px rgba(36,87,230,.08); }
textarea { resize: vertical; }

.items-table { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.item-row { display: grid; grid-template-columns: minmax(220px, 1fr) 90px 140px 140px 44px; gap: 10px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.item-row:last-child { border-bottom: 0; }
.item-head { background: #f8f9fc; color: #8993a5; font-size: 10px; font-weight: 800; text-transform: uppercase; }
.item-row input { margin: 0; padding: 9px 10px; }
.item-amount { font-size: 12px; font-weight: 700; }
.remove-item { width: 32px; height: 32px; border: 0; border-radius: 8px; background: #fff0f1; color: #c8404c; }

.totals-area { display: grid; grid-template-columns: 1fr 320px; gap: 22px; margin-top: 20px; }
.totals-card { background: #f7f9fc; border-radius: 12px; padding: 16px; }
.totals-card > div, .inline-field { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 9px 0; font-size: 12px; color: #566276; }
.inline-field input { width: 95px; margin: 0; padding: 7px 8px; }
.grand-total { border-top: 1px solid #dfe4ec; margin-top: 5px; padding-top: 14px !important; color: var(--text) !important; font-size: 15px !important; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.history-tools { display: flex; gap: 10px; }
.history-tools input, .history-tools select { margin: 0; min-width: 180px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(8, 13, 23, .65); display: none; align-items: flex-start;
  justify-content: center; padding: 28px; overflow: auto; z-index: 1000;
}
.modal-backdrop.open { display: flex; }
.modal { width: min(920px, 100%); background: white; border-radius: 16px; box-shadow: 0 25px 80px rgba(0,0,0,.25); overflow: hidden; }
.modal-toolbar { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); background: #fafbfc; }
.modal-actions { display: flex; gap: 8px; }
.invoice-preview { padding: 48px; }
.preview-top { display: flex; justify-content: space-between; gap: 30px; margin-bottom: 45px; }
.preview-brand h2 { margin: 0 0 8px; font-size: 24px; }
.preview-brand p, .preview-meta p, .preview-client p { color: #6c788c; margin: 4px 0; font-size: 12px; line-height: 1.6; }
.preview-title { text-align: right; }
.preview-title h1 { margin: 0; font-size: 38px; letter-spacing: .08em; font-weight: 700; color: #1d2a3f; }
.preview-title .number { color: var(--primary); font-weight: 800; margin-top: 7px; }
.preview-info { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin-bottom: 30px; }
.preview-client h4, .preview-meta h4 { margin: 0 0 10px; font-size: 10px; color: #8792a5; text-transform: uppercase; letter-spacing: .1em; }
.preview-table { width: 100%; min-width: 0; margin-top: 25px; }
.preview-table th { background: #f7f9fc; padding: 12px; }
.preview-table td { padding: 12px; }
.preview-summary { margin-left: auto; width: 320px; margin-top: 22px; }
.preview-summary div { display: flex; justify-content: space-between; padding: 8px 0; color: #5f6b7e; font-size: 12px; }
.preview-summary .final { border-top: 1px solid #dfe4ec; padding-top: 14px; margin-top: 4px; color: #172033; font-size: 16px; font-weight: 800; }
.preview-notes { margin-top: 38px; border-top: 1px solid var(--line); padding-top: 22px; }
.preview-notes h4 { margin: 0 0 8px; font-size: 11px; }
.preview-notes p { margin: 0; color: #687489; font-size: 12px; line-height: 1.65; white-space: pre-line; }

.toast {
  position: fixed; right: 24px; bottom: 24px; background: #131d2d; color: white; padding: 13px 16px;
  border-radius: 10px; box-shadow: 0 12px 30px rgba(0,0,0,.18); opacity: 0; transform: translateY(10px);
  pointer-events: none; transition: .25s ease; font-size: 12px; z-index: 1100;
}
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1000px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .totals-area { grid-template-columns: 1fr; }
}
@media (max-width: 780px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; padding: 14px; }
  .brand { padding-bottom: 16px; }
  nav { grid-template-columns: repeat(3, 1fr); }
  .nav-link { justify-content: center; font-size: 0; }
  .nav-link span { font-size: 20px; }
  .sidebar-note { display: none; }
  .content { padding: 20px 14px 38px; }
  .topbar h1 { font-size: 24px; }
  .stats-grid { grid-template-columns: 1fr; }
  .field-grid.two, .field-grid.three { grid-template-columns: 1fr; }
  .item-row { grid-template-columns: 1fr 70px 100px 100px 36px; min-width: 680px; }
  .items-table { overflow-x: auto; }
  .panel-head.responsive { align-items: stretch; flex-direction: column; }
  .history-tools { flex-direction: column; }
  .history-tools input, .history-tools select { min-width: 0; }
  .invoice-preview { padding: 26px 20px; }
  .preview-top, .preview-info { grid-template-columns: 1fr; display: grid; }
  .preview-title { text-align: left; }
  .preview-summary { width: 100%; }
}
@media print {
  body * { visibility: hidden; }
  #invoicePreview, #invoicePreview * { visibility: visible; }
  #invoicePreview { position: absolute; left: 0; top: 0; width: 100%; }
  .invoice-preview { padding: 24px; }
  .no-print { display: none !important; }
}

.preview-logo { width: 190px; max-height: 92px; object-fit: contain; object-position: left center; display: block; margin-bottom: 10px; }
.pdf-download-hint { font-size: 10px; color: #8590a2; margin-top: 5px; }


/* Targeted responsive fixes that preserve the original desktop layout */
.brand > div { min-width: 0; }
.brand strong, .brand span { overflow: hidden; text-overflow: ellipsis; }
.modal-toolbar { gap: 10px; flex-wrap: wrap; }
.modal-actions { flex-wrap: wrap; justify-content: flex-end; }

@media (max-width: 780px) {
  .brand-logo { width: 76px; height: 42px; max-width: 76px; max-height: 42px; flex-basis: 76px; }
  .brand strong { font-size: 14px; }
  .topbar { flex-wrap: wrap; }
  .modal-backdrop { padding: 12px; }
  .modal-toolbar { align-items: stretch; }
  .modal-actions { width: 100%; }
  .modal-actions button { flex: 1 1 auto; }
  .preview-logo { width: 145px; max-width: 50vw; max-height: 72px; }
}

@media (max-width: 520px) {
  .topbar { align-items: stretch; flex-direction: column; }
  .topbar .primary-btn { width: 100%; }
  .panel { padding: 16px; }
  .modal-toolbar { flex-direction: column; }
  .modal-toolbar > button, .modal-actions, .modal-actions button { width: 100%; }
  .modal-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .invoice-preview { padding: 22px 15px; }
  .preview-title h1 { font-size: 30px; }
}

.native-download { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
.download-invoice { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
