:root {
  --ink: #111827;
  --muted: #667085;
  --line: #d0d5dd;
  --soft: #f5f7fb;
  --panel: #ffffff;
  --blue: #173b74;
  --blue-2: #24579d;
  --danger: #b42318;
  --green: #087443;
  --amber: #b54708;
  --shadow: 0 18px 45px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; color: var(--ink); font-family: Arial, Helvetica, sans-serif; background: linear-gradient(180deg, #f7f9fc 0%, #eef3f8 100%); }
button, input, textarea, select { font: inherit; }
button, .file-button { border: 0; border-radius: 8px; padding: 10px 14px; font-weight: 700; cursor: pointer; }
.primary { background: var(--blue); color: white; }
.primary:hover { background: var(--blue-2); }
.secondary, .file-button { background: #eef2f7; color: var(--ink); border: 1px solid var(--line); }
.danger { color: var(--danger); }

.app-shell { display: flex; min-height: 100vh; }
.sidebar { width: 260px; background: #0f2748; color: #fff; border-right: 1px solid rgba(255,255,255,.08); padding: 18px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.brand-mark { width: 44px; height: 44px; display: grid; place-items: center; border: 1px solid rgba(255,255,255,.35); color: #fff; font-weight: 900; border-radius: 8px; background: rgba(255,255,255,.08); }
.brand span { display: block; color: rgba(255,255,255,.7); font-size: 13px; margin-top: 2px; }
.nav { display: grid; gap: 8px; }
.nav-btn { width: 100%; text-align: left; background: transparent; color: rgba(255,255,255,.82); border: 1px solid transparent; }
.nav-btn.active, .nav-btn:hover { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.16); }
.connection { margin-top: 22px; color: rgba(255,255,255,.72); font-size: 13px; line-height: 1.45; }

.main { flex: 1; min-width: 0; padding: 24px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 18px; }
.topbar h1 { margin: 0; font-size: clamp(24px, 4vw, 34px); letter-spacing: 0; }
.topbar p { margin: 6px 0 0; color: var(--muted); }
.top-actions, .button-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.view { display: none; }
.view.active { display: block; }

.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 16px; }
.hero-panel { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 18px; margin-bottom: 16px; padding: 22px; border: 1px solid #d8e1ef; border-radius: 8px; background: #fff; box-shadow: var(--shadow); }
.hero-panel h2 { margin: 4px 0 6px; max-width: 760px; font-size: clamp(24px, 3vw, 36px); letter-spacing: 0; }
.hero-panel p, .panel-note { margin: 0; color: var(--muted); line-height: 1.5; }
.eyebrow { display: inline-flex; color: var(--blue); font-weight: 900; text-transform: uppercase; font-size: 12px; letter-spacing: .08em; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.stat, .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 12px 28px rgba(16,24,40,.05); }
.stat { padding: 18px; }
.stat span { display: block; color: var(--muted); font-size: 14px; }
.stat strong { display: block; margin-top: 8px; font-size: 26px; }
.panel { padding: 16px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.panel h2 { margin: 0; font-size: 20px; }
.search-row { display: flex; gap: 8px; margin-bottom: 14px; }
.search-row input { min-width: 0; flex: 1; }
input, textarea, select { width: 100%; border: 1px solid var(--line); border-radius: 7px; padding: 10px 11px; background: #fff; color: var(--ink); }
textarea { min-height: 74px; resize: vertical; }

.editor-toolbar { position: sticky; top: 0; z-index: 10; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; background: rgba(247,249,252,.96); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 14px; box-shadow: 0 12px 24px rgba(16,24,40,.07); backdrop-filter: blur(10px); }
.save-state { font-weight: 700; color: var(--muted); margin-right: auto; }
#templateSelect { width: auto; min-width: 190px; padding: 9px 11px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.toggle input { width: auto; }
.editor-grid { display: grid; grid-template-columns: minmax(320px, 430px) minmax(0, 1fr); gap: 16px; align-items: start; }
.form-panel { max-height: calc(100vh - 140px); overflow: auto; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.field { display: grid; gap: 5px; }
.field label { font-size: 12px; color: var(--muted); font-weight: 800; text-transform: uppercase; letter-spacing: .02em; }
.field.full { grid-column: 1 / -1; }
.section-title { margin: 18px 0 10px; padding-top: 12px; border-top: 1px solid var(--line); color: var(--blue); font-weight: 900; }
.line-form { display: grid; gap: 10px; }
.line-card { border: 1px solid var(--line); border-radius: 8px; padding: 10px; background: #fbfcff; }
.line-card strong { display: block; margin-bottom: 8px; }
.line-grid { display: grid; grid-template-columns: 1fr 80px 1fr 90px 95px 95px; gap: 8px; }
.signature-tools { display: grid; gap: 10px; }
#signatureCanvas { width: 100%; height: 120px; background: white; border: 1px solid var(--line); border-radius: 8px; touch-action: none; }
.file-button { position: relative; overflow: hidden; display: inline-flex; align-items: center; }
.file-button input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.preview-wrap { min-width: 0; }
.preview-scroll { overflow: auto; padding: 8px; background: #dfe4ec; border-radius: 8px; }
.preview-wrap.layout-active .preview-scroll { background: #cbd5e1; }
.invoice-paper { width: 194mm; min-height: 280mm; margin: 0 auto; background: #fff; color: #111; font-family: "Times New Roman", Times, serif; padding: 0; box-shadow: 0 10px 30px rgba(16,24,40,.18); }
.invoice-box { border: 2px solid #111; min-height: 264mm; display: flex; flex-direction: column; }
.inv-header { text-align: center; padding: 18px 14px 7px; font-weight: 700; }
.company-name { font-size: 23px; font-weight: 900; letter-spacing: .4px; }
.company-line, .contact-line { font-size: 12.5px; line-height: 1.25; }
.tax-title { margin-top: 16px; font-size: 20px; font-weight: 900; text-decoration: underline; }
.customer-line { display: grid; grid-template-columns: 1fr auto; align-items: end; gap: 20px; margin-top: 3px; font-size: 16px; font-weight: 900; }
.customer-name { text-align: center; padding-left: 110px; }
.customer-gstin { font-size: 15px; white-space: nowrap; padding-right: 44px; }
.bill-meta { border-top: 2px solid #111; border-bottom: 2px solid #111; display: grid; grid-template-columns: 1.18fr .82fr; font-weight: 700; font-size: 12.5px; }
.meta-left, .meta-right { padding: 4px 10px; }
.meta-row { display: grid; grid-template-columns: 138px 1fr; align-items: center; min-height: 22px; }
.meta-right .meta-row { grid-template-columns: 78px 1fr; }
.driver-car { display: grid; grid-template-columns: minmax(38mm, 1.2fr) minmax(26mm, .7fr) minmax(34mm, 1fr); column-gap: 3mm; white-space: nowrap; align-items: center; }
.driver-car > span { min-width: 0; overflow: hidden; text-overflow: clip; }
.under { border-bottom: 1px solid #333; min-height: 19px; display: inline-block; width: 100%; }
.particular-head { display: grid; grid-template-columns: 1fr 31mm; border-bottom: 2px solid #111; font-weight: 900; text-align: center; font-size: 14px; }
.particular-head div:first-child { border-right: 2px solid #111; }
.items-area { display: grid; grid-template-columns: 1fr 31mm; min-height: 130mm; }
.left-items { border-right: 2px solid #111; padding: 4px 10px 0; display: flex; flex-direction: column; }
.amounts-col { display: flex; flex-direction: column; }
.item-row { display: grid; grid-template-columns: 42mm 16mm minmax(31mm, 1fr) 8mm 21mm 22mm; align-items: center; height: 5.2mm; flex: 0 0 5.2mm; font-weight: 700; font-size: 12.8px; column-gap: 1.2mm; }
.item-row .desc { white-space: nowrap; }
.invoice-paper input { min-width: 0; max-width: 100%; box-sizing: border-box; font-family: inherit; font-weight: inherit; color: #111; }
.item-row > *, .charge-row > *, .amount-cell { min-width: 0; }
.item-row input, .charge-row input, .amount-cell input, .editable-input { width: 100%; border: 0; border-bottom: 1px solid #333; border-radius: 0; padding: 0 3px; height: 19px; font-family: inherit; font-weight: inherit; background: transparent; text-align: right; }
.item-row input.text-input { text-align: left; }
.invoice-gap { flex: 0 0 auto; }
.main-gap { height: 25mm; }
.total-gap { flex: 1 1 auto; min-height: 27mm; }
.grand-gap { height: 10mm; }
.charge-row { display: grid; grid-template-columns: 52mm 8mm 22mm 1fr; height: 5.2mm; flex: 0 0 5.2mm; align-items: center; font-weight: 900; font-size: 12.8px; }
.charge-row .water { font-size: 12.5px; }
.summary-label { height: 5.2mm; flex: 0 0 5.2mm; display: flex; align-items: center; justify-content: flex-end; padding-right: 12mm; font-weight: 900; font-size: 12.8px; }
.summary-label.strong { font-size: 13.2px; }
.amount-spacer { flex: 0 0 auto; border-bottom: 0; }
.amount-spacer.total-gap { flex: 1 1 auto; min-height: 27mm; }
.amount-cell { height: 5.2mm; flex: 0 0 5.2mm; border-bottom: 0; display: flex; align-items: center; justify-content: flex-end; padding: 0 2mm; font-size: 12.8px; font-weight: 700; overflow: hidden; }
.amounts-col .amount-cell:nth-child(n+10) { border-top: 1px solid #111; border-bottom: 1px solid #111; }
.amount-cell input { border-bottom: 0; height: 18px; padding: 0; }
.amount-cell.label-cell { justify-content: flex-start; border-bottom: 0; padding-left: 0; }
.amount-row-label { min-height: 24px; display: flex; align-items: center; justify-content: flex-end; padding-right: 36px; font-weight: 900; }
.total-block { display: block; }
.total-labels { padding: 0 14px 0; }
.lower-left { display: grid; grid-template-columns: 1fr 250px; min-height: 34mm; padding: 0 14px; font-weight: 900; font-size: 12.8px; }
.duty { align-self: start; margin-top: 20px; }
.rupees { margin-top: 28px; }
.sign-zone { align-self: start; text-align: center; margin-top: 9mm; }
.signature-gap { height: 19mm; display: flex; align-items: center; justify-content: center; }
.sign-zone img { max-width: 150px; max-height: 18mm; display: block; }
.sign-top, .sign-bottom { min-height: 18px; }
.footer { margin-top: auto; border-top: 2px solid #111; padding-bottom: 8px; font-weight: 900; font-size: 10.5px; }
.footer-top { display: grid; grid-template-columns: 1fr 1fr 1fr; padding: 2px 8px 3px; font-size: 12px; }
.footer-lines { padding: 0 14px; line-height: 1.35; }
[contenteditable="true"] { outline: 1px dashed transparent; min-height: 1em; }
[contenteditable="true"]:focus { outline-color: #24579d; background: #eef5ff; }
.layout-target { position: relative; display: inline-block; z-index: 2; }
input.layout-target { display: block; }
.layout-active .layout-target { cursor: move; outline: 1px dashed rgba(181, 71, 8, .7); background: transparent !important; box-shadow: none !important; touch-action: none; }
.layout-active .layout-target:hover, .layout-active .layout-target.dragging { z-index: 9999; outline-color: #173b74; background: transparent !important; }
.layout-active .invoice-paper,
.layout-active .invoice-box,
.layout-active .inv-header,
.layout-active .bill-meta,
.layout-active .meta-left,
.layout-active .meta-right,
.layout-active .meta-row,
.layout-active .driver-car,
.layout-active .driver-car > span,
.layout-active .particular-head,
.layout-active .items-area,
.layout-active .left-items,
.layout-active .amounts-col,
.layout-active .item-row,
.layout-active .item-row > *,
.layout-active .charge-row,
.layout-active .charge-row > *,
.layout-active .amount-cell,
.layout-active .total-block,
.layout-active .lower-left,
.layout-active .footer,
.layout-active .footer-top,
.layout-active .footer-lines {
  overflow: visible !important;
}
.layout-active .driver-car > span,
.layout-active .amount-cell {
  contain: none;
}
.layout-active .invoice-paper::before {
  content: "Layout Mode: drag fields to reposition, then Save as Template";
  position: sticky;
  top: 0;
  z-index: 5;
  display: block;
  margin: -18px -18px 8px;
  padding: 6px 10px;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #9a3412;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  font-weight: 800;
}

.bill-list { display: grid; gap: 10px; }
.bill-card { display: grid; grid-template-columns: 1fr auto; gap: 12px; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.bill-card h3 { margin: 0 0 5px; }
.bill-card p { margin: 0; color: var(--muted); line-height: 1.45; }
.compact-list .bill-card { grid-template-columns: 1fr; }
.signature-preview img { max-width: 220px; max-height: 90px; border: 1px solid var(--line); background: #fff; }
.workspace-grid { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 16px; align-items: start; }
.template-list { display: grid; gap: 10px; }
.template-card { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.template-card h3 { margin: 0 0 5px; font-size: 18px; }
.template-card p { margin: 0; color: var(--muted); line-height: 1.45; }
.badge { display: inline-flex; align-items: center; min-height: 24px; padding: 3px 8px; border-radius: 999px; font-size: 12px; font-weight: 900; background: #ecfdf3; color: var(--green); border: 1px solid #abefc6; }
.template-tip { display: grid; gap: 8px; margin-top: 14px; padding: 14px; border-radius: 8px; background: #f8fafc; border: 1px solid var(--line); color: var(--muted); line-height: 1.5; }

@media (max-width: 900px) {
  .app-shell { display: block; }
  .sidebar { position: static; width: auto; height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .nav { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .nav-btn { text-align: center; padding: 9px 6px; }
  .main { padding: 14px; }
  .topbar, .panel-head { align-items: flex-start; flex-direction: column; }
  .stats-grid, .editor-grid, .hero-panel, .workspace-grid { grid-template-columns: 1fr; }
  .hero-actions { justify-content: stretch; }
  .hero-actions button { flex: 1; }
  .form-panel { max-height: none; }
  .form-grid, .line-grid { grid-template-columns: 1fr; }
  .search-row.filters, .search-row { flex-direction: column; }
  .sticky-save { flex: 1; }
  #templateSelect { width: 100%; }
}

@page { size: A4 portrait; margin: 10mm; }
@media print {
  html, body { width: 210mm; min-height: 297mm; background: #fff; }
  .sidebar, .topbar, .editor-toolbar, .form-panel, .panel, #dashboardView, #templatesView, #billsView, #settingsView { display: none !important; }
  .app-shell, .main, .view.active, .editor-grid, .preview-wrap, .preview-scroll { display: block !important; padding: 0 !important; margin: 0 !important; background: #fff !important; overflow: visible !important; }
  #editorView { display: block !important; }
  .invoice-paper { width: 194mm; min-height: 280mm; padding: 0; box-shadow: none; margin: 0 auto; }
  .invoice-box { min-height: 264mm; }
  .preview-scroll { border-radius: 0 !important; }
  .inv-header { padding-top: 18px; padding-bottom: 7px; }
  .company-name { font-size: 23px; }
  .company-line, .contact-line { font-size: 12.5px; }
  .tax-title { margin-top: 16px; font-size: 20px; }
  .customer-line { font-size: 16px; }
  .bill-meta { font-size: 12.5px; }
  .meta-left, .meta-right { padding: 4px 10px; }
  .driver-car { grid-template-columns: minmax(38mm, 1.2fr) minmax(26mm, .7fr) minmax(34mm, 1fr); column-gap: 3mm; white-space: nowrap; }
  .particular-head { grid-template-columns: 1fr 31mm; font-size: 14px; }
  .items-area { grid-template-columns: 1fr 31mm; min-height: 130mm; }
  .total-block { display: block; }
  .left-items { padding: 4px 10px 0; }
  .item-row { grid-template-columns: 42mm 16mm minmax(31mm, 1fr) 8mm 21mm 22mm; height: 5.2mm; flex-basis: 5.2mm; font-size: 12.8px; column-gap: 1.2mm; }
  .charge-row { grid-template-columns: 52mm 8mm 22mm 1fr; height: 5.2mm; flex-basis: 5.2mm; font-size: 12.8px; }
  .summary-label { height: 5.2mm; flex-basis: 5.2mm; padding-right: 12mm; font-size: 12.8px; }
  .summary-label.strong { font-size: 13.2px; }
  .main-gap { height: 25mm; }
  .total-gap { min-height: 27mm; }
  .grand-gap { height: 10mm; }
  .amount-cell { height: 5.2mm; flex-basis: 5.2mm; font-size: 12.8px; padding-left: 2mm; padding-right: 2mm; }
  .amount-spacer.total-gap { min-height: 27mm; }
  .lower-left { min-height: 34mm; font-size: 12.8px; }
  .sign-zone { margin-top: 9mm; }
  .signature-gap { height: 19mm; }
  .sign-zone img { max-height: 18mm; }
  .amount-row-label { min-height: 5.5mm; padding-right: 8mm; font-size: 12.8px; }
  .footer { font-size: 10.5px; }
  .footer-top { font-size: 12px; }
  .invoice-paper input {
    appearance: none;
    -webkit-appearance: none;
    background: transparent !important;
    box-shadow: none !important;
    outline: 0 !important;
    color: #111 !important;
    overflow: hidden;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .layout-active .invoice-paper::before { display: none !important; }
  .layout-active .layout-target { outline: 0 !important; background: transparent !important; }
  .item-row input, .charge-row input { border: 0 !important; border-bottom: 1px solid #333 !important; }
  .amount-cell input { border: 0 !important; text-align: right !important; }
  .amount-cell { padding-left: 6px; padding-right: 6px; }
  [contenteditable="true"] { outline: 0 !important; }
}
