:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --text: #1a1a1a;
  --muted: #6b6b6b;
  --line: #e4e4e0;
  --accent: #1a1a1a;
  --danger: #b42318;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(246, 246, 244, 0.92);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1320px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { font-size: 16px; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
.topbar-actions { display: flex; gap: 8px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 500;
  border-radius: var(--radius);
  padding: 8px 14px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #333; }
.btn-secondary { background: var(--panel); border-color: var(--line); color: var(--text); }
.btn-secondary:hover { border-color: #c8c8c2; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-small { padding: 6px 10px; font-size: 13px; }

/* Layout */
.layout {
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px;
  display: grid;
  grid-template-columns: minmax(380px, 1fr) minmax(0, 1.15fr);
  gap: 24px;
  align-items: start;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; }
}

.panel { min-width: 0; }

/* Form */
.form-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 20px 20px;
}
.field-group { border: 0; padding: 0; margin: 16px 0 0; }
.field-group legend {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding: 0;
  margin-bottom: 8px;
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 10px;
}
.req { color: var(--danger); }
input, textarea {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
  min-width: 0;
}
textarea { resize: vertical; }
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
input.invalid { border-color: var(--danger); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }

/* Line items */
.line-items { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.line-item-head, .line-item {
  display: grid;
  grid-template-columns: 1fr 64px 92px 32px;
  gap: 6px;
  align-items: center;
}
.line-item-head span {
  font-size: 11px;
  color: var(--muted);
}
.line-item input { padding: 7px 8px; }
.remove-item {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
  height: 32px;
}
.remove-item:hover { color: var(--danger); background: #fbeceb; }
.remove-item:disabled { visibility: hidden; }

/* Preview sheet (A4 proportions) */
.preview-panel { position: sticky; top: 76px; }
.invoice-sheet {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.05);
  width: 100%;
  aspect-ratio: 210 / 297;
  padding: 7% 7.5%;
  color: var(--text);
  font-size: 13px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.invoice-sheet p { margin: 0; }
.invoice-header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 40px;
}
.pv-brand { font-size: 18px; font-weight: 600; margin: 0 0 6px; letter-spacing: -0.01em; }
.pv-muted { color: var(--muted); white-space: pre-line; }
.pv-strong { font-weight: 600; margin-bottom: 2px !important; }
.pv-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 4px !important;
}
.invoice-meta { text-align: right; }
.invoice-meta p { display: flex; justify-content: flex-end; gap: 10px; }
.invoice-meta .pv-label { margin: 0 !important; line-height: 1.9; }
.invoice-title {
  font-size: 22px;
  font-weight: 300;
  letter-spacing: 0.18em;
  margin: 0 0 10px;
}
.invoice-parties { margin-bottom: 32px; }

.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
.invoice-table th {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-align: left;
  padding: 8px 0;
  border-bottom: 1px solid var(--text);
}
.invoice-table td {
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  word-break: break-word;
}
.invoice-table .num { text-align: right; white-space: nowrap; padding-left: 12px; }
.invoice-table .empty-row td { color: var(--muted); font-style: italic; }

.invoice-totals { margin-left: auto; width: 55%; min-width: 200px; }
.totals-row { display: flex; justify-content: space-between; padding: 5px 0; }
.totals-grand {
  border-top: 1px solid var(--text);
  margin-top: 6px;
  padding-top: 10px;
  font-size: 16px;
  font-weight: 600;
}
.invoice-notes { margin-top: auto; padding-top: 32px; }
.invoice-notes #pvNotes { white-space: pre-line; color: var(--muted); }
.invoice-notes.hidden { display: none; }

/* Footer */
.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 8px 24px 32px; }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
  box-shadow: 0 6px 20px rgba(0,0,0,0.15);
}
.toast.error { background: var(--danger); }
