
/* App-like mobile behaviour: no pinch/double-tap zoom, no page bounce */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  overscroll-behavior: none;
  touch-action: manipulation;
}

button, input, select, textarea, a {
  touch-action: manipulation;
}

input, select, textarea {
  font-size: 16px; /* prevents iPhone auto-zoom on focus */
}

:root {
  --ink: #111827;
  --muted: #6b7280;
  --line: #d1d5db;
  --soft-line: #e5e7eb;
  --surface: #ffffff;
  --canvas: #eef1f5;
  --accent: #1d4ed8;
  --accent-dark: #173ea8;
  --danger: #b42318;
  --success: #067647;
  --radius: 14px;
  --shadow: 0 14px 38px rgba(17, 24, 39, 0.12);
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--canvas); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--canvas);
}

button, input, textarea, select { font: inherit; }
button, label, summary, select { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: 68px;
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: rgba(255,255,255,0.95);
  border-bottom: 1px solid var(--soft-line);
  backdrop-filter: blur(12px);
}

.brand-block, .header-actions, .toolbar-row, .logo-row, .split-input, .check-row {
  display: flex;
  align-items: center;
}

.brand-block { gap: 11px; }
.app-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: var(--ink);
  color: white;
  font-weight: 850;
  font-size: 22px;
  box-shadow: 0 8px 20px rgba(17,24,39,.18);
}
.brand-block h1 { margin: 0; font-size: 17px; line-height: 1.15; letter-spacing: -.02em; }
.brand-block p { margin: 3px 0 0; font-size: 12px; color: var(--muted); }
.header-actions { gap: 9px; }
.save-status { font-size: 12px; color: var(--success); white-space: nowrap; }

.button {
  border: 1px solid transparent;
  border-radius: 10px;
  min-height: 38px;
  padding: 8px 13px;
  font-weight: 700;
  font-size: 13px;
  transition: transform .12s ease, background .12s ease, border-color .12s ease;
}
.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button.primary { background: var(--ink); color: white; }
.button.primary:hover { background: #263244; }
.button.secondary { background: white; border-color: var(--line); color: var(--ink); }
.button.secondary:hover { border-color: #9ca3af; }
.button.ghost { background: #eff6ff; color: var(--accent-dark); border-color: #bfdbfe; }
.button.full-width { width: 100%; margin-top: 10px; }

.text-button {
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 5px 4px;
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
}
.text-button:hover { text-decoration: underline; }
.text-button.danger { color: var(--danger); }
.file-label { position: relative; cursor: pointer; }
.file-label input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.mobile-tabs { display: none; }

.app-shell {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(380px, 470px) minmax(0, 1fr);
  align-items: start;
}

.editor-panel {
  height: calc(100vh - 68px);
  overflow-y: auto;
  padding: 16px;
  background: #f8fafc;
  border-right: 1px solid var(--soft-line);
  scrollbar-gutter: stable;
}

.toolbar-card, .form-section {
  background: white;
  border: 1px solid var(--soft-line);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(17,24,39,.035);
}
.toolbar-card { padding: 12px; margin-bottom: 12px; }
.toolbar-row { gap: 8px; }
.toolbar-row + .toolbar-row { margin-top: 8px; }
.toolbar-row select { min-width: 0; flex: 1; }
.toolbar-row.compact { min-height: 24px; }
.toolbar-spacer { flex: 1; }

.form-section { margin: 0 0 12px; overflow: clip; }
.form-section summary {
  list-style: none;
  padding: 14px 16px;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
}
.form-section summary::-webkit-details-marker { display: none; }
.form-section summary::after { content: "+"; color: var(--muted); font-size: 18px; line-height: 1; }
.form-section[open] summary::after { content: "–"; }
.form-section[open] summary { border-bottom: 1px solid var(--soft-line); }
.form-section > :not(summary) { margin-left: 14px; margin-right: 14px; }
.form-section > :last-child { margin-bottom: 14px; }

.form-grid {
  display: grid;
  gap: 11px;
  padding-top: 14px;
}
.form-grid.two { grid-template-columns: repeat(2, minmax(0,1fr)); }
.form-grid label, .item-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: #374151;
  font-size: 11px;
  font-weight: 750;
}
.form-grid .span-2 { grid-column: 1 / -1; }

input, textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: white;
  color: var(--ink);
  outline: none;
  transition: border-color .12s ease, box-shadow .12s ease;
}
input, select { min-height: 38px; padding: 8px 10px; }
textarea { resize: vertical; padding: 9px 10px; line-height: 1.45; }
input:focus, textarea:focus, select:focus {
  border-color: #5b8def;
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; accent-color: var(--ink); }
.check-row { flex-direction: row !important; gap: 9px !important; min-height: 32px; }
.split-input { gap: 6px; }
.split-input input { flex: 1; }
.split-input select { width: 95px; }

.logo-row { gap: 12px; padding-top: 14px; }
.logo-thumb {
  width: 70px;
  height: 70px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px dashed #9ca3af;
  border-radius: 12px;
  background: #f9fafb;
  font-size: 23px;
  font-weight: 850;
  letter-spacing: -.08em;
}
.logo-thumb img { width: 100%; height: 100%; object-fit: contain; padding: 5px; }
.logo-actions { display: flex; flex-wrap: wrap; gap: 5px 9px; align-items: center; }
.logo-actions small { flex-basis: 100%; color: var(--muted); font-size: 10px; }

.catalog-toolbar {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(160px, 1fr) auto;
  gap: 9px;
  align-items: end;
  padding-top: 14px;
}
.catalog-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: #374151;
  font-size: 11px;
  font-weight: 750;
}
.helper-text {
  margin-top: 8px;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 10.5px;
  line-height: 1.45;
}
.client-actions {
  justify-content: flex-start;
  align-items: end;
}
.client-actions .button {
  min-height: 38px;
}
.items-editor { padding-top: 14px; display: grid; gap: 10px; }
.item-card {
  border: 1px solid var(--soft-line);
  border-radius: 11px;
  padding: 11px;
  background: #fbfcfe;
}
.item-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 9px; }
.item-card-head strong { font-size: 12px; }
.item-card-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.item-card-grid { display: grid; grid-template-columns: 1.35fr .65fr; gap: 9px; }
.item-field.description { grid-column: 1 / -1; }
.item-line-total { margin-top: 8px; font-size: 11px; color: var(--muted); text-align: right; }
.item-line-total strong { color: var(--ink); }

.preview-panel {
  min-height: calc(100vh - 68px);
  padding: 14px 24px 34px;
  overflow: auto;
}
.preview-toolbar {
  max-width: 210mm;
  margin: 0 auto 9px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}
.paper {
  width: min(210mm, 100%);
  min-height: 297mm;
  margin: 0 auto;
  padding: 15mm 13mm 10mm;
  background: white;
  color: #0b0b0b;
  box-shadow: var(--shadow);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 11.5px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.document-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(235px, .85fr);
  gap: 22px;
  align-items: start;
  padding-bottom: 8px;
  border-bottom: 1.5px solid #151515;
}
.doc-brand { min-width: 0; }
.doc-logo-wrap { min-height: 69px; display: flex; align-items: center; }
.doc-logo { max-width: 175px; max-height: 72px; object-fit: contain; object-position: left center; }
.doc-monogram { font-family: Arial, sans-serif; font-size: 48px; font-weight: 300; letter-spacing: -.12em; line-height: 1; }
.doc-business-name { margin-top: 4px; font-family: Arial, sans-serif; font-size: 17px; letter-spacing: .12em; text-transform: uppercase; }
.doc-website { margin-top: 2px; font-family: Arial, sans-serif; font-size: 10px; color: #333; }
.doc-company-contact { text-align: right; font-family: Arial, sans-serif; font-size: 10.5px; line-height: 1.5; }
.doc-company-contact strong { letter-spacing: .02em; }

.doc-title-row {
  margin-top: 18px;
  display: grid;
  grid-template-columns: minmax(0,1fr) auto;
  gap: 20px;
  align-items: start;
}
.bill-to { padding: 28px 0 12px; }
.bill-to-label { margin-bottom: 6px; font-family: Arial, sans-serif; font-size: 8.5px; font-weight: 800; color: #555; letter-spacing: .12em; text-transform: uppercase; }
.bill-to-name { font-size: 15px; font-weight: 800; text-transform: uppercase; }
.bill-to-address { margin-top: 3px; max-width: 410px; white-space: pre-line; font-weight: 650; }
.bill-to-meta { margin-top: 4px; }
.bill-to-meta b { color: #0b45c8; }
.document-title { text-align: right; }
.document-title h2 { margin: 0; font-size: 23px; line-height: 1.15; letter-spacing: .02em; }
.document-title p { margin: 6px 0 0; font-size: 10.5px; font-weight: 700; }
.document-title .validity { font-weight: 400; color: #444; }

.doc-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  margin-top: 10px;
}
.doc-table th, .doc-table td { border: 1px solid #aaa; padding: 7px 6px; vertical-align: top; }
.doc-table th { padding-top: 9px; padding-bottom: 9px; text-align: center; font-size: 10.5px; font-weight: 800; }
.doc-table .col-no { width: 8%; text-align: center; }
.doc-table .col-description { width: 41%; }
.doc-table .col-code { width: 14%; text-align: center; }
.doc-table .col-price { width: 13%; text-align: right; }
.doc-table .col-qty { width: 9%; text-align: center; }
.doc-table .col-amount { width: 15%; text-align: right; }
.doc-table td.col-description { white-space: pre-line; }
.doc-table td.col-code { font-weight: 700; }

.totals-wrap { margin-top: 16px; margin-left: auto; width: 62%; }
.totals-table { width: 100%; border-collapse: collapse; }
.totals-table td { border: 1px solid #aaa; padding: 5px 8px; }
.totals-table td:first-child { text-align: right; }
.totals-table td:last-child { width: 30%; text-align: right; }
.totals-table .grand td { font-weight: 800; text-transform: uppercase; border-top: 1.5px solid #222; }
.amount-words { margin-top: 8px; text-align: right; font-size: 9.5px; font-style: italic; }

.doc-lower {
  display: grid;
  grid-template-columns: minmax(0,1.4fr) minmax(180px,.6fr);
  gap: 26px;
  margin-top: 22px;
  min-height: 155px;
}
.section-kicker { margin-bottom: 5px; font-family: Arial, sans-serif; font-size: 9px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.terms-text { white-space: pre-line; font-size: 10px; }
.payment-block { margin-top: 15px; }
.payment-lines { font-size: 10px; line-height: 1.55; }
.payment-lines b { display: inline-block; min-width: 84px; }
.signature-block { display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-end; text-align: left; }
.signature-flourish { margin: 20px 0 5px; font-family: "Brush Script MT", "Segoe Script", cursive; font-size: 27px; transform: rotate(-5deg); }
.signature-block .sign-for { margin-bottom: 8px; font-size: 9.5px; }
.signature-block strong { font-size: 11px; text-transform: uppercase; }
.signature-block small { color: #444; }

.doc-footer {
  margin-top: 18px;
  padding-top: 8px;
  border-top: 1px solid #aaa;
  text-align: center;
  font-size: 8.5px;
  font-weight: 650;
  line-height: 1.45;
}
.doc-footer b { text-transform: uppercase; }
.empty-value { color: #8a8a8a; font-style: italic; font-weight: 400; }

.catalog-dialog {
  width: min(760px, calc(100vw - 24px));
  max-height: min(88vh, 860px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: white;
  box-shadow: 0 24px 70px rgba(17,24,39,.28);
  color: var(--ink);
}
.catalog-dialog::backdrop { background: rgba(15,23,42,.55); backdrop-filter: blur(2px); }
.dialog-shell { display: flex; flex-direction: column; max-height: min(88vh, 860px); }
.dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--soft-line);
}
.dialog-header h2 { margin: 0; font-size: 18px; }
.dialog-header p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.dialog-close {
  width: 34px;
  height: 34px;
  border: 1px solid var(--soft-line);
  border-radius: 9px;
  background: white;
  color: var(--muted);
  font-size: 23px;
  line-height: 1;
}
.catalog-form {
  display: grid;
  grid-template-columns: 1.2fr .8fr .7fr;
  gap: 11px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--soft-line);
  background: #f8fafc;
}
.catalog-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: #374151;
  font-size: 11px;
  font-weight: 750;
}
.catalog-form .span-2 { grid-column: 1 / -1; }
.catalog-form-actions { display: flex; gap: 8px; align-items: end; }
.catalog-list-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 20px 8px;
  font-size: 12px;
}
.catalog-list-heading span { color: var(--muted); }
.catalog-list { overflow-y: auto; padding: 0 20px 14px; }
.catalog-list-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 11px 0;
  border-bottom: 1px solid var(--soft-line);
}
.catalog-list-main { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.catalog-list-main strong { font-size: 12px; overflow-wrap: anywhere; }
.catalog-list-main span { color: var(--muted); font-size: 10.5px; }
.catalog-list-actions { display: flex; align-items: center; gap: 6px; }
.catalog-empty {
  padding: 24px 14px;
  border: 1px dashed var(--line);
  border-radius: 11px;
  color: var(--muted);
  text-align: center;
  font-size: 12px;
}
.dialog-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-top: 1px solid var(--soft-line);
}
.dialog-footer .button { margin-left: auto; }

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 60;
  max-width: min(360px, calc(100vw - 40px));
  padding: 11px 14px;
  border-radius: 10px;
  background: #111827;
  color: white;
  font-size: 12px;
  box-shadow: 0 12px 30px rgba(0,0,0,.22);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1080px) {
  .app-shell { grid-template-columns: minmax(340px, 410px) minmax(0, 1fr); }
  .preview-panel { padding-left: 14px; padding-right: 14px; }
  .paper { padding-left: 9mm; padding-right: 9mm; }
}

@media (max-width: 820px) {
  .app-header { position: relative; min-height: 62px; padding: 9px 12px; }
  .brand-block p, .save-status { display: none; }
  .header-actions { gap: 6px; }
  .header-actions .button { min-height: 36px; padding: 7px 10px; }
  .mobile-tabs {
    position: sticky;
    top: 0;
    z-index: 25;
    display: grid;
    grid-template-columns: 1fr 1fr;
    padding: 6px;
    background: white;
    border-bottom: 1px solid var(--soft-line);
  }
  .mobile-tab {
    min-height: 38px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--muted);
    font-weight: 800;
  }
  .mobile-tab.active { background: var(--ink); color: white; }
  .app-shell { display: block; }
  .editor-panel { height: auto; min-height: calc(100vh - 112px); border-right: 0; padding: 12px; }
  .preview-panel { min-height: calc(100vh - 112px); padding: 10px 8px 24px; }
  .app-shell[data-mobile-panel="edit"] .preview-panel { display: none; }
  .app-shell[data-mobile-panel="preview"] .editor-panel { display: none; }
  .preview-toolbar { padding: 0 4px; }
  .paper { width: 100%; min-height: auto; padding: 7vw 5vw; box-shadow: 0 7px 24px rgba(17,24,39,.11); font-size: 10px; }
  .document-header { grid-template-columns: 1fr 1fr; gap: 10px; }
  .doc-logo-wrap { min-height: 50px; }
  .doc-logo { max-width: 125px; max-height: 52px; }
  .doc-monogram { font-size: 36px; }
  .doc-business-name { font-size: 12px; }
  .doc-company-contact { font-size: 8px; }
  .document-title h2 { font-size: 17px; }
  .doc-title-row { margin-top: 12px; gap: 8px; }
  .bill-to { padding-top: 18px; }
  .bill-to-name { font-size: 12px; }
  .doc-table { font-size: 8px; }
  .doc-table th, .doc-table td { padding: 5px 3px; }
  .doc-table th { font-size: 7.5px; }
  .totals-wrap { width: 72%; }
  .doc-lower { grid-template-columns: 1.2fr .8fr; gap: 12px; min-height: 110px; }
}

@media (max-width: 480px) {
  .brand-block h1 { font-size: 15px; }
  .app-mark { width: 36px; height: 36px; font-size: 19px; }
  #installBtn { display: none !important; }
  .toolbar-row:first-child { flex-wrap: wrap; }
  .toolbar-row:first-child .button { flex: 1; }
  .toolbar-row:first-child select { flex-basis: 100%; }
  .form-grid.two { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
  .catalog-toolbar { grid-template-columns: 1fr; }
  .catalog-toolbar .button { width: 100%; }
  .catalog-form { grid-template-columns: 1fr 1fr; padding: 14px; }
  .catalog-form .span-2 { grid-column: 1 / -1; }
  .catalog-form-actions { grid-column: 1 / -1; }
  .catalog-list-item { grid-template-columns: 1fr; }
  .catalog-list-actions { justify-content: flex-start; }
  .dialog-header, .dialog-footer { padding-left: 14px; padding-right: 14px; }
  .catalog-list { padding-left: 14px; padding-right: 14px; }
  .item-card-grid { grid-template-columns: 1fr 1fr; }
  .item-card-head { align-items: flex-start; }
  .item-card-actions { gap: 2px; }
  .catalog-form { grid-template-columns: 1fr; }
  .catalog-form .span-2, .catalog-form-actions { grid-column: auto; }
  .dialog-footer { flex-wrap: wrap; }
  .dialog-footer .button { width: 100%; margin-left: 0; }
  .item-field.description { grid-column: 1 / -1; }
  .document-header { grid-template-columns: 1fr; }
  .doc-company-contact { text-align: left; }
  .doc-title-row { grid-template-columns: 1fr; }
  .document-title { text-align: left; order: -1; }
  .bill-to { padding-top: 4px; }
  .doc-table .col-code { display: none; }
  .doc-table .col-description { width: 48%; }
  .doc-table .col-price { width: 16%; }
  .doc-table .col-qty { width: 10%; }
  .doc-table .col-amount { width: 18%; }
  .totals-wrap { width: 100%; }
  .doc-lower { grid-template-columns: 1fr; }
  .signature-block { min-height: 90px; }
}

@media print {
  @page { size: A4 portrait; margin: 0; }
  html, body { width: 210mm; min-width: 210mm; background: white; }
  body { margin: 0; }
  .no-print { display: none !important; }
  .app-shell, .preview-panel { display: block !important; width: 210mm; min-height: 297mm; margin: 0; padding: 0; overflow: visible; background: white; }
  .paper {
    width: 210mm;
    min-height: 297mm;
    margin: 0;
    padding: 15mm 13mm 10mm;
    box-shadow: none;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .doc-table tr, .totals-table tr, .doc-lower { break-inside: avoid; }
}
