:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #111827;
  --primary-hover: #000000;
  --soft: #f9fafb;
  --danger: #b91c1c;
  --success: #047857;
  --warning: #b45309;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(17, 24, 39, .06);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(145deg,#111827,#374151); }
.auth-card { width: min(420px, 100%); background: white; border-radius: 20px; padding: 34px; box-shadow: 0 24px 60px rgba(0,0,0,.25); }
.auth-card h1 { margin: 16px 0 6px; }
.auth-card p { margin: 0 0 24px; color: var(--muted); }
.auth-card label { display: block; margin: 14px 0; }
.brand-mark { width: 54px; height: 54px; border-radius: 16px; display: grid; place-items: center; background: var(--primary); color: white; font-weight: 800; font-size: 24px; }
.brand-mark.small { width: 38px; height: 38px; border-radius: 11px; font-size: 18px; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 250px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; background: #111827; color: white; padding: 20px 14px; display: flex; flex-direction: column; z-index: 20; }
.sidebar-brand { display: flex; gap: 12px; align-items: center; padding: 4px 8px 22px; }
.sidebar-brand strong, .sidebar-brand small { display: block; }
.sidebar-brand small { color: #9ca3af; margin-top: 3px; }
.sidebar nav { display: grid; gap: 6px; }
.sidebar nav button { border: 0; background: transparent; color: #d1d5db; text-align: left; padding: 12px 14px; border-radius: 10px; }
.sidebar nav button:hover, .sidebar nav button.active { background: #374151; color: white; }
.sidebar-footer { margin-top: auto; border-top: 1px solid #374151; padding: 16px 8px 0; display: grid; gap: 10px; }
.sidebar-footer small { color: #9ca3af; word-break: break-all; }

.main-content { min-width: 0; padding: 28px; }
.mobile-header { display: none; }
.page { display: none; }
.page.active { display: block; }
.page-heading { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 20px; }
.page-heading h2 { margin: 0 0 4px; }
.page-heading p { margin: 0; color: var(--muted); }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 20px; }
.panel h3 { margin-top: 0; }
.panel-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.two-column { display: grid; grid-template-columns: minmax(0,.85fr) minmax(0,1.6fr); gap: 20px; align-items: start; }

.card-grid { display: grid; grid-template-columns: repeat(4,minmax(0,1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card { background: white; border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.stat-card small { color: var(--muted); display: block; margin-bottom: 8px; }
.stat-card strong { font-size: 22px; display: block; line-height: 1.45; }
.stat-card .subline { margin-top: 7px; color: var(--muted); font-size: 13px; }

button { border-radius: 9px; padding: 10px 14px; border: 1px solid transparent; }
button.primary { background: var(--primary); color: white; }
button.primary:hover { background: var(--primary-hover); }
button.secondary { background: #e5e7eb; color: #111827; }
button.ghost { background: transparent; border-color: var(--line); color: inherit; }
button.small { padding: 7px 10px; font-size: 13px; }
button.full { width: 100%; }
.link-btn { background: transparent; padding: 0; color: #2563eb; }
.icon-btn { background: transparent; border: 0; color: white; font-size: 22px; }

label { display: grid; gap: 7px; color: #374151; font-size: 14px; }
input, select, textarea { width: 100%; border: 1px solid #d1d5db; border-radius: 9px; padding: 10px 11px; background: white; color: #111827; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(17,24,39,.13); border-color: #6b7280; }
.large-textarea { min-height: 270px; }
.form-panel { display: grid; gap: 16px; }
.form-grid { display: grid; gap: 12px; }
.form-grid.two { grid-template-columns: repeat(2,minmax(0,1fr)); }
.form-grid.three { grid-template-columns: repeat(3,minmax(0,1fr)); }
.section-title { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.section-title h4 { margin: 0; }
.stack { display: grid; gap: 10px; }
.dynamic-row { display: grid; grid-template-columns: 1.2fr 1fr 1fr 1.5fr auto; gap: 9px; align-items: end; padding: 12px; background: var(--soft); border: 1px solid var(--line); border-radius: 10px; }
.charge-row { grid-template-columns: 1.7fr 1fr auto; }
.row-remove { background: #fee2e2; color: var(--danger); padding: 10px 12px; }
.totals-box { background: #111827; color: white; border-radius: 12px; padding: 15px; display: grid; grid-template-columns: repeat(3,1fr); gap: 10px; }
.total-item small { color: #9ca3af; display:block; }
.total-item strong { font-size: 18px; }
.form-error { color: var(--danger); margin-top: 8px; min-height: 18px; }
.hint { color: var(--muted); margin-top: 10px; font-size: 14px; }

.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.filters label { min-width: 150px; flex: 1; }
.filters button { flex: 0 0 auto; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { text-align: left; padding: 11px 10px; border-bottom: 1px solid var(--line); vertical-align: top; font-size: 14px; }
th { color: #4b5563; font-weight: 600; background: #f9fafb; position: sticky; top: 0; }
td .muted, .muted { color: var(--muted); font-size: 12px; }
.money-lines { white-space: pre-line; line-height: 1.55; }
.badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 4px 8px; font-size: 12px; background: #e5e7eb; }
.badge.success { color: var(--success); background: #d1fae5; }
.badge.warning { color: var(--warning); background: #fef3c7; }
.badge.danger { color: var(--danger); background: #fee2e2; }

.summary-chips { display: flex; flex-wrap: wrap; gap: 10px; }
.summary-chip { border: 1px solid var(--line); background: var(--soft); border-radius: 11px; padding: 11px 13px; }
.summary-chip strong, .summary-chip small { display: block; }
.summary-chip small { color: var(--muted); margin-top: 4px; }

.inline-form { display: flex; gap: 10px; margin-bottom: 16px; }
.master-list { display: grid; gap: 8px; }
.master-item { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; }
.master-item.inactive { opacity: .55; }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 18px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.modal-card { position: relative; width: min(960px,100%); max-height: 90vh; overflow: auto; background: white; border-radius: 16px; padding: 24px; z-index: 1; }
.modal-close { position: sticky; float: right; top: 0; border: 0; background: #f3f4f6; font-size: 20px; padding: 5px 10px; }
.detail-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; }
.detail-item { background: #f9fafb; border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.detail-item small { color: var(--muted); display: block; margin-bottom: 5px; }
.invoice-preview { background: white; color: #111; padding: 24px; }
.invoice-preview h2 { margin-bottom: 4px; }
.invoice-meta { display: grid; grid-template-columns: repeat(2,1fr); gap: 6px 20px; margin: 16px 0; }
.invoice-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }

.toast { position: fixed; right: 22px; bottom: 22px; z-index: 100; background: #111827; color: white; border-radius: 10px; padding: 12px 16px; box-shadow: var(--shadow); }

@media (max-width: 1100px) {
  .card-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .two-column { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; left: -270px; transition: left .2s ease; width: 250px; }
  .sidebar.open { left: 0; }
  .main-content { padding: 16px; padding-top: 76px; }
  .mobile-header { display: flex; position: fixed; z-index: 15; top: 0; left: 0; right: 0; height: 58px; background: #111827; color: white; align-items: center; gap: 14px; padding: 0 14px; }
  .page-heading { align-items: flex-start; }
  .card-grid { grid-template-columns: 1fr; }
  .form-grid.two, .form-grid.three, .detail-grid { grid-template-columns: 1fr; }
  .dynamic-row, .charge-row { grid-template-columns: 1fr; }
  .totals-box { grid-template-columns: 1fr; }
  .filters { display: grid; grid-template-columns: 1fr 1fr; }
  .filters label, .filters button { min-width: 0; width: 100%; }
}
@media print {
  body * { visibility: hidden !important; }
  #printableInvoice, #printableInvoice * { visibility: visible !important; }
  #printableInvoice { position: absolute; left: 0; top: 0; width: 100%; }
  .invoice-actions { display: none !important; }
}
