:root {
  --accent: #4f46e5;
  --bg: #f3f4f8;
  --panel: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
  font-size: 15px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app { min-height: 100vh; display: flex; flex-direction: column; }

/* ===== Toolbar ===== */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.toolbar__brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.05rem; }
.toolbar__logo { font-size: 1.3rem; }
.toolbar__actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #f7f7fb; }
.btn:active { transform: translateY(1px); }
.btn--ghost { background: transparent; }
.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn--primary:hover { filter: brightness(1.05); }
.btn--small { padding: 6px 12px; font-size: 0.8rem; }

/* ===== Layout ===== */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(360px, 460px) 1fr;
  gap: 20px;
  padding: 20px 24px 60px;
  max-width: 1500px;
  margin: 0 auto;
  width: 100%;
}

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
}

.panel { min-width: 0; }

/* ===== Editor cards ===== */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 16px;
}

.card__title {
  font-size: 0.95rem;
  font-weight: 700;
  margin: 0 0 14px;
  color: var(--text);
}

.card__header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.card__header-row .card__title { margin: 0; }

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

.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.8rem; font-weight: 600; color: var(--muted); min-width: 0; }
.field--wide { grid-column: 1 / -1; }

.field input, .field select, .field textarea {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 400;
  color: var(--text);
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fbfbfd;
  resize: vertical;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
}
.field input[type="color"] { padding: 3px; height: 38px; cursor: pointer; }

/* Logo upload */
.logo-upload { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.logo-drop {
  width: 96px;
  height: 96px;
  border: 1.5px dashed var(--border);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  background: #fbfbfd;
  flex-shrink: 0;
}
.logo-drop img { width: 100%; height: 100%; object-fit: contain; }
#logoPlaceholder { font-size: 0.72rem; color: var(--muted); text-align: center; padding: 4px; }
.link-btn {
  background: none;
  border: none;
  color: #dc2626;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}

/* Items table (editor) */
.items-table-wrap { overflow-x: auto; }
.items-table { width: 100%; border-collapse: collapse; }
.items-table th {
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  padding: 0 8px 8px;
  font-weight: 700;
}
.items-table td { padding: 4px; vertical-align: top; }
.items-table input {
  width: 100%;
  padding: 8px 9px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: #fbfbfd;
  font-size: 0.88rem;
  font-family: inherit;
}
.items-table input:focus { outline: none; border-color: var(--accent); background: #fff; }
.col-qty { width: 70px; }
.col-price { width: 110px; }
.col-amount { width: 110px; }
.col-remove { width: 36px; }
.amount-cell {
  padding: 8px 9px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.btn-remove-row {
  border: none;
  background: none;
  color: #9ca3af;
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
  padding: 6px;
}
.btn-remove-row:hover { color: #dc2626; }

/* ===== Preview ===== */
.preview-wrap {
  display: flex;
  justify-content: center;
}
.preview-scale {
  width: 100%;
  max-width: 800px;
}

.invoice {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 48px;
  min-height: 1000px;
}

.invoice__top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  padding-bottom: 28px;
  border-bottom: 2px solid var(--accent);
  margin-bottom: 28px;
}

.invoice__brand { display: flex; gap: 14px; align-items: flex-start; min-width: 0; }
.invoice__logo { width: 64px; height: 64px; object-fit: contain; border-radius: 8px; }
.invoice__biz-name { font-size: 1.15rem; font-weight: 800; margin-bottom: 4px; word-break: break-word; }
.invoice__muted { font-size: 0.82rem; color: var(--muted); line-height: 1.5; white-space: pre-line; }

.invoice__meta { text-align: right; flex-shrink: 0; }
.invoice__heading {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--accent);
  margin-bottom: 10px;
}
.invoice__meta-row {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 4px;
}
.invoice__meta-row strong { color: var(--text); font-weight: 700; min-width: 90px; text-align: right; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  background: var(--accent);
  color: #fff !important;
  font-size: 0.72rem !important;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.invoice__parties { margin-bottom: 26px; }
.invoice__label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 6px;
}
.invoice__party-name { font-size: 1rem; font-weight: 700; margin-bottom: 2px; }

.invoice__table { width: 100%; border-collapse: collapse; margin-bottom: 22px; }
.invoice__table thead th {
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #fff;
  background: var(--accent);
  padding: 10px 12px;
  font-weight: 700;
}
.invoice__table thead th.col-qty,
.invoice__table thead th.col-price,
.invoice__table thead th.col-amount { text-align: right; }
.invoice__table tbody td {
  padding: 10px 12px;
  font-size: 0.87rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.invoice__table tbody td.col-qty,
.invoice__table tbody td.col-price,
.invoice__table tbody td.col-amount { text-align: right; white-space: nowrap; }
.invoice__table tbody tr:last-child td { border-bottom: 2px solid var(--border); }

.invoice__summary { display: flex; justify-content: flex-end; margin-bottom: 30px; }
.invoice__summary-box { width: 280px; }
.invoice__summary-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.87rem;
  color: var(--muted);
  padding: 6px 0;
}
.invoice__summary-row--total {
  border-top: 2px solid var(--accent);
  margin-top: 6px;
  padding-top: 10px;
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--text);
}

.invoice__footer {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  border-top: 1px solid var(--border);
  padding-top: 20px;
}
.invoice__footer-block[hidden] { display: none; }

/* Print */
@media print {
  .toolbar, .editor { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .preview-wrap { display: block; }
  .preview-scale { max-width: none; }
  body { background: #fff; }
  .invoice { box-shadow: none; border-radius: 0; padding: 20px; }
}
