:root {
  color-scheme: light;
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-soft: #f9fafb;
  --text: #111827;
  --muted: #6b7280;
  --line: #d1d5db;
  --line-soft: #e5e7eb;
  --accent: #111827;
  --danger: #b91c1c;
  --success: #166534;
  --warning: #92400e;
  --radius: 14px;
  --preview-scale: .72;
  --shadow: 0 12px 32px rgba(15, 23, 42, .08);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }

button {
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  transition: transform .12s ease, opacity .12s ease, background .12s ease;
}

button:hover { opacity: .9; }
button:active { transform: translateY(1px); }
button:disabled { cursor: not-allowed; opacity: .45; transform: none; }

button.secondary {
  color: var(--text);
  background: #fff;
  border-color: var(--line);
}

button.danger {
  color: var(--danger);
  background: #fff;
  border-color: #fecaca;
}

button.small { padding: 7px 10px; font-size: 12px; }
button.compact { padding: 7px 10px; font-size: 12px; }

.app-shell { min-height: 100vh; }

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

.brand-block { display: flex; align-items: center; gap: 11px; min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-family: Georgia, serif;
  font-size: 23px;
  font-weight: 800;
}

.brand-block h1 { margin: 0; font-size: 18px; line-height: 1.2; }
.brand-block p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border-color: var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  font-size: 18px;
}

.connection-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 6px 9px;
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
}

.connection-badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.connection-badge.online { color: var(--success); background: #dcfce7; }
.connection-badge.offline { color: var(--warning); background: #fef3c7; }
.connection-badge.checking { color: #4b5563; background: #f3f4f6; }

.app-nav {
  position: sticky;
  top: 70px;
  z-index: 14;
  display: flex;
  gap: 6px;
  padding: 8px 22px;
  background: rgba(255, 255, 255, .96);
  border-bottom: 1px solid var(--line-soft);
  backdrop-filter: blur(14px);
}

.nav-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 120px;
  padding: 9px 14px;
  color: #4b5563;
  background: transparent;
  border: 1px solid transparent;
  font-size: 13px;
}

.nav-item.active {
  color: var(--text);
  background: var(--panel-soft);
  border-color: var(--line-soft);
}

.nav-icon { font-size: 16px; line-height: 1; }

.workspace {
  display: grid;
  gap: 18px;
  padding: 18px 22px 24px;
  min-height: calc(100vh - 123px);
}

.panel {
  min-width: 0;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.document-panel, .preview-panel, .properties-panel, .archive-panel { display: none; }

.workspace[data-view="document"] {
  grid-template-columns: minmax(500px, 680px) minmax(0, 1fr);
}

.workspace[data-view="document"] .document-panel,
.workspace[data-view="document"] .preview-panel { display: block; }

.workspace[data-view="preview"] { grid-template-columns: minmax(0, 1fr); }
.workspace[data-view="preview"] .preview-panel { display: block; }
.workspace[data-view="properties"] .properties-panel { display: block; }
.workspace[data-view="archive"] .archive-panel { display: block; }

.properties-panel, .archive-panel { max-width: 1050px; width: 100%; margin: 0 auto; }

.document-panel {
  max-height: calc(100vh - 142px);
  overflow: auto;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 17px 18px;
  border-bottom: 1px solid var(--line-soft);
}

.sticky-heading {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(255, 255, 255, .97);
  backdrop-filter: blur(10px);
}

.panel-heading h2 { margin: 0; font-size: 20px; }
.panel-heading p { margin: 3px 0 0; color: var(--muted); font-size: 12px; line-height: 1.35; }
.primary-action { white-space: nowrap; }

.form-stack { padding: 8px 14px 18px; }

.form-card {
  margin: 10px 0;
  overflow: hidden;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: #fff;
}

.form-card[open] { box-shadow: 0 1px 0 rgba(0, 0, 0, .02); }

.form-card summary {
  padding: 13px 14px;
  background: var(--panel-soft);
  cursor: pointer;
  font-size: 14px;
  font-weight: 800;
  user-select: none;
}

.card-body { padding: 5px 14px 15px; }

label, .group-label {
  display: block;
  margin: 11px 0 5px;
  color: #374151;
  font-size: 12px;
  font-weight: 700;
}

input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  outline: none;
  background: #fff;
  color: var(--text);
}

input:focus, select:focus, textarea:focus {
  border-color: #6b7280;
  box-shadow: 0 0 0 3px rgba(17, 24, 39, .08);
}

input[readonly] { color: #4b5563; background: var(--panel-soft); }
textarea { resize: vertical; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.invoice-number-control {
  display: flex;
  align-items: stretch;
  overflow: hidden;
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
}

.invoice-number-prefix {
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  padding: 9px 6px 9px 10px;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--panel-soft);
  color: #4b5563;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.invoice-serial-input {
  flex: 0 0 78px;
  width: 78px;
  min-height: 38px;
  padding: 9px;
  border: 0;
  border-radius: 0;
  text-align: center;
  box-shadow: none !important;
}

.date-helper { display: flex; align-items: center; gap: 6px; min-height: 34px; margin-top: 6px; }
.due-offset-label, .due-offset-unit { margin: 0; color: #4b5563; font-size: 12px; white-space: nowrap; }
.due-offset-input { width: 62px; min-height: 33px; padding: 5px 7px; text-align: center; }

.rows-editor-list { display: grid; gap: 10px; margin-top: 10px; }

.row-editor {
  display: grid;
  grid-template-columns: minmax(150px, 2fr) 70px minmax(100px, 1fr) 110px auto auto auto;
  gap: 7px;
  align-items: end;
  padding: 10px;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: var(--panel-soft);
}

.row-editor label { margin-top: 0; }
.row-editor input, .row-editor select { min-height: 36px; padding: 7px 8px; font-size: 12px; }

.row-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  gap: 5px;
  padding: 0 3px;
  color: #374151;
  font-size: 11px;
  white-space: nowrap;
}

.row-toggle input { width: 17px; min-height: 17px; height: 17px; margin: 0; padding: 0; }
.row-delete { width: 36px; height: 36px; padding: 0; align-self: end; }

.notice {
  margin: 12px 14px 0;
  padding: 10px 12px;
  border: 1px solid;
  border-radius: 10px;
  font-size: 12px;
  line-height: 1.45;
}

.notice.warning { color: #78350f; border-color: #fde68a; background: #fffbeb; }
.notice.info { color: #1e3a8a; border-color: #bfdbfe; background: #eff6ff; }

.validation-message {
  display: none;
  margin: 12px 14px 0;
  padding: 10px 12px;
  border: 1px solid #fecaca;
  border-radius: 10px;
  background: #fff1f2;
  color: #7f1d1d;
  font-size: 13px;
  line-height: 1.4;
}

.validation-message.show { display: block; }
.validation-message strong { display: block; margin-bottom: 5px; }
.validation-message ul { margin: 0 0 0 18px; padding: 0; }

.preview-panel { overflow: hidden; }
.preview-toolbar { display: none; }
.workspace[data-view="preview"] .preview-toolbar { display: flex; }

.preview-wrap {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: calc(100vh - 142px);
  overflow: auto;
  padding: 16px 8px 28px;
  background: #e5e7eb;
}

.workspace[data-view="preview"] .preview-wrap { height: calc(100vh - 218px); min-height: 620px; }

.sheet-frame {
  position: relative;
  flex: 0 0 auto;
  width: calc(210mm * var(--preview-scale));
  height: calc(297mm * var(--preview-scale));
}

.sheet {
  position: relative;
  width: 210mm;
  min-height: 297mm;
  padding: 10mm 4mm 7mm;
  transform: scale(var(--preview-scale));
  transform-origin: top left;
  background: #fff;
  box-shadow: 0 14px 40px rgba(15, 23, 42, .20);
  color: #000;
  font-family: "Times New Roman", Times, serif;
  font-size: 12pt;
  line-height: 1.05;
}

.doc-title { margin: 0 0 5mm; text-align: center; font-size: 18pt; font-weight: 700; }
.top-table, .date-table, .items-table { width: 100%; border-collapse: collapse; }

.top-table td { padding: 2mm 1mm; border: 1.4pt solid #000; vertical-align: top; }
.top-table .address-cell { width: 50%; height: 45mm; }
.top-table .doc-cell { width: 50%; height: 11mm; padding: 1mm; text-align: center; vertical-align: middle; }
.top-table .label { margin-bottom: 6mm; font-weight: 700; }
.doc-label { display: block; font-weight: 400; }
.doc-value { display: block; font-size: 12.5pt; font-weight: 700; }

.date-table td { width: 50%; height: 11mm; padding: 1mm; border: 1.4pt solid #000; text-align: center; vertical-align: middle; }
.date-table td.period-date-cell { width: auto; height: 11mm; padding: .7mm 1mm; }
.note-line { margin: 1.6mm 1mm 3mm; white-space: pre-wrap; }

.items-table { margin-top: 0; table-layout: fixed; }
.items-table th { padding: 1mm; border: 1.4pt solid #000; background: #fff; font-weight: 400; text-align: left; }
.items-table th:nth-child(1) { width: 46%; text-align: center; }
.items-table th:nth-child(2), .items-table th:nth-child(3), .items-table th:nth-child(4) { width: 18%; }
.items-table td { height: 5.5mm; padding: 1.2mm 1mm 0; border: 0; }
.items-table td.money { text-align: right; white-space: nowrap; }
.items-table tfoot td { padding-top: .6mm; border-top: 1.4pt solid #000; font-weight: 400; }

.payable { display: grid; grid-template-columns: 33% 1fr; align-items: baseline; margin: 10mm 18mm 0 24mm; }
.payable .amount { text-align: center; font-size: 17pt; font-weight: 700; }
.amount-words { margin-top: 9mm; text-align: center; }
.bottom-info-note { margin: 23mm 20mm 0; font-size: 9.8pt; line-height: 1.25; }
.bottom-info-note p { margin: 0 0 1.5mm; }

.footer {
  position: absolute;
  right: 4mm;
  bottom: 5mm;
  left: 4mm;
  display: flex;
  justify-content: space-between;
  gap: 8mm;
  padding: 1mm 1mm 0;
  border-top: 1.4pt solid #000;
  font-size: 10.5pt;
}

.property-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; padding: 16px; }

.property-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 150px;
  padding: 14px;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: var(--panel-soft);
}

.property-card.active { border-color: #9ca3af; box-shadow: inset 0 0 0 1px #9ca3af; }
.property-card h3 { margin: 0; font-size: 16px; }
.property-card p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.property-prefix { align-self: flex-start; padding: 4px 7px; border-radius: 7px; background: #e5e7eb; font: 700 11px ui-monospace, monospace; }
.property-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: auto; }

.archive-filter { padding: 15px 18px 2px; }
.filter-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.field-note { margin: 5px 0 0; color: var(--muted); font-size: 11px; line-height: 1.4; }
.count-badge { padding: 6px 9px; border-radius: 999px; background: #f3f4f6; font-size: 12px; font-weight: 800; white-space: nowrap; }

.archive-list { display: grid; gap: 9px; margin: 0; padding: 14px 18px 18px; list-style: none; }
.archive-list li { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 10px; padding: 12px; border: 1px solid var(--line-soft); border-radius: 11px; }
.archive-list li.void { border-color: #fecaca; background: #fff7f7; }
.archive-title { font-weight: 800; }
.archive-meta { margin-top: 3px; color: var(--muted); font-size: 12px; line-height: 1.45; }
.archive-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.archive-empty { color: var(--muted); font-size: 13px; }
.status-pill { display: inline-block; margin-left: 6px; padding: 2px 6px; border-radius: 999px; color: #991b1b; background: #fee2e2; font-size: 10px; font-weight: 800; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15px;
  background: rgba(15, 23, 42, .42);
}

.modal[hidden] { display: none; }

.modal-card {
  width: min(720px, 100%);
  max-height: calc(100vh - 30px);
  overflow: auto;
  padding: 18px;
  border-radius: 15px;
  background: #fff;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .3);
}

.modal-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.modal-heading h2 { margin: 0; font-size: 21px; }
.modal-card ol { margin: 14px 0 16px 22px; padding: 0; line-height: 1.55; }
.modal-card li { margin: 6px 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.document-details { display: grid; gap: 8px; margin-top: 14px; }
.detail-row { display: grid; grid-template-columns: 145px 1fr; gap: 10px; padding-bottom: 7px; border-bottom: 1px solid var(--line-soft); font-size: 13px; }
.detail-label { color: var(--muted); font-weight: 700; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  z-index: 70;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  transform: translateX(-50%) translateY(18px);
  border-radius: 999px;
  background: #111827;
  box-shadow: 0 12px 35px rgba(15, 23, 42, .25);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}

.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

@media (max-width: 1180px) {
  .workspace[data-view="document"] { grid-template-columns: minmax(460px, 610px) minmax(0, 1fr); }
  .row-editor { grid-template-columns: 2fr 70px 100px 110px; }
  .row-toggle { justify-content: flex-start; }
}

@media (max-width: 920px) {
  .workspace[data-view="document"] { grid-template-columns: 1fr; }
  .workspace[data-view="document"] .preview-panel { display: none; }
  .document-panel { max-height: none; overflow: visible; }
  .workspace[data-view="preview"] .preview-wrap { height: auto; min-height: calc(100vh - 260px); }
}

@media (max-width: 680px) {
  body { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }

  .app-header {
    position: sticky;
    min-height: 62px;
    padding: calc(9px + env(safe-area-inset-top)) 12px 9px;
  }

  .brand-mark { width: 36px; height: 36px; font-size: 20px; }
  .brand-block h1 { max-width: 170px; overflow: hidden; font-size: 15px; white-space: nowrap; text-overflow: ellipsis; }
  .brand-block p { display: none; }
  .connection-badge { padding: 5px 7px; }
  .connection-badge span { display: none; }
  #installBtn { display: none !important; }

  .app-nav {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0;
    padding: 5px 5px calc(5px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line-soft);
    border-bottom: 0;
  }

  .nav-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    padding: 6px 3px;
    border-radius: 9px;
    font-size: 10px;
  }

  .nav-icon { font-size: 18px; }
  .workspace { min-height: calc(100vh - 62px); padding: 10px; }
  .panel { border-radius: 12px; box-shadow: 0 6px 20px rgba(15, 23, 42, .06); }
  .panel-heading { align-items: flex-start; padding: 14px; }
  .panel-heading h2 { font-size: 18px; }
  .panel-heading .primary-action { padding: 9px 10px; font-size: 12px; }
  .form-stack { padding: 5px 9px 14px; }
  .form-card summary { padding: 12px; }
  .card-body { padding: 4px 11px 13px; }
  .grid-2 { grid-template-columns: 1fr; gap: 0; }

  .row-editor { grid-template-columns: 1fr 1fr; gap: 8px; }
  .row-editor .row-name { grid-column: 1 / -1; }
  .row-editor .row-toggle { justify-content: flex-start; }
  .row-delete { justify-self: end; }

  .preview-toolbar { display: flex; }
  .preview-wrap { height: auto; min-height: calc(100vh - 250px); padding: 12px 5px 20px; }
  .property-list { grid-template-columns: 1fr; padding: 12px; }
  .filter-row { grid-template-columns: 1fr; }
  .archive-list { padding: 12px; }
  .archive-list li { grid-template-columns: 1fr; }
  .archive-actions { justify-content: flex-start; }
  .detail-row { grid-template-columns: 1fr; gap: 3px; }
  .modal-actions { flex-wrap: wrap; }
  .toast { bottom: calc(82px + env(safe-area-inset-bottom)); border-radius: 12px; text-align: center; }
}

@media print {
  body { padding: 0; background: #fff; }
  .app-header, .app-nav, .document-panel, .properties-panel, .archive-panel, .preview-toolbar, .modal, .toast { display: none !important; }
  .app-shell, .workspace, .preview-panel, .preview-wrap { display: block !important; min-height: 0; height: auto; margin: 0; padding: 0; overflow: visible; border: 0; border-radius: 0; box-shadow: none; background: #fff; }
  .sheet-frame { position: static; width: auto; height: auto; }
  .sheet { width: 210mm; height: 297mm; min-height: 297mm; padding: 10mm 4mm 7mm; transform: none; box-shadow: none; }
  @page { size: A4; margin: 0; }
}
