:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --ink: #14213d;
  --muted: #5b6779;
  --line: #d5dbe5;
  --accent: #0e7c7b;
  --accent-ink: #ffffff;
  --danger: #b3261e;
  --radius: 8px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--ink);
  color: #fff;
}

.app-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 650;
}

.view-toggle {
  display: none;
  gap: 4px;
  padding: 3px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
}

.toggle-btn {
  padding: 6px 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

.toggle-btn.is-active {
  background: #fff;
  color: var(--ink);
}

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 440px) 1fr;
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.panel-preview {
  position: sticky;
  top: 16px;
  align-self: start;
}

@media (max-width: 899px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 16px;
  }
  .view-toggle { display: flex; }
  .panel-preview { position: static; }
  .layout[data-view="form"] .panel-preview { display: none; }
  .layout[data-view="preview"] .panel-form { display: none; }
}

/* ---------- Form ---------- */

.panel-form {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 16px 16px;
}

fieldset {
  margin: 0;
  padding: 16px 0;
  border: 0;
  border-bottom: 1px solid var(--line);
}

fieldset:last-of-type { border-bottom: 0; }

legend {
  padding: 0;
  margin-bottom: 8px;
  font-weight: 650;
}

.field {
  display: block;
  margin-bottom: 12px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field-label {
  display: block;
  margin-bottom: 4px;
  font-size: 0.875rem;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

textarea { resize: vertical; }

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Item rows ---------- */

.items { margin-bottom: 8px; }

.item-row {
  display: grid;
  grid-template-columns: 90px 1fr auto;
  grid-template-areas:
    "desc desc desc"
    "qty  price remove";
  gap: 8px;
  align-items: end;
  padding: 12px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f8f9fb;
}

.item-row .field { margin-bottom: 0; }
.item-desc { grid-area: desc; }
.item-qty { grid-area: qty; }
.item-price { grid-area: price; }
.item-remove { grid-area: remove; }

.btn-secondary,
.item-remove {
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.btn-secondary {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.item-remove { color: var(--danger); }

.item-remove:disabled {
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.5;
}

/* ---------- Document preview ---------- */

.sheet {
  max-width: 760px;
  margin: 0 auto;
  padding: 40px;
  background: #fff;
  color: #14213d;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(20, 33, 61, 0.06);
}

.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  padding-bottom: 24px;
  border-bottom: 3px solid var(--accent);
}

.sheet-title {
  margin: 0;
  font-size: 2.25rem;
  line-height: 1.1;
  font-weight: 750;
  letter-spacing: -0.01em;
}

.sheet-number {
  margin: 6px 0 0;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.sheet-from { text-align: right; }
.sheet-from strong { display: block; font-size: 1.05rem; }

.sheet-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin: 24px 0;
}

.sheet-label {
  margin: 0 0 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.sheet-value { margin: 0; }

.placeholder { color: #9aa4b2; }

.table-wrap { overflow-x: auto; }

.sheet-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.sheet-table th,
.sheet-table td {
  padding: 10px 8px;
  text-align: right;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

.sheet-table th:first-child,
.sheet-table td:first-child { text-align: left; }

.sheet-table th {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.sheet-totals {
  width: min(100%, 320px);
  margin: 16px 0 0 auto;
  font-variant-numeric: tabular-nums;
}

.total-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 0;
}

.total-row.grand {
  margin-top: 6px;
  padding-top: 10px;
  border-top: 2px solid var(--ink);
  font-size: 1.2rem;
  font-weight: 750;
}

.sheet-notes {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

@media (max-width: 599px) {
  .sheet { padding: 20px; }
  .sheet-head { flex-direction: column; }
  .sheet-from { text-align: left; }
  .sheet-title { font-size: 1.75rem; }
  .sheet-meta { grid-template-columns: 1fr; gap: 16px; }
}
