/* ---------- base ---------- */
:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1a1d23;
  --ink-soft: #5c6470;
  --line: #e3e6ea;
  --accent: #3b5bdb;
  --tip: #ffdd57;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 20, 28, 0.08), 0 8px 24px rgba(16, 20, 28, 0.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

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

.hidden { display: none !important; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  clip: rect(0 0 0 0); overflow: hidden;
}

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

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { font-size: 28px; }
.brand-name { display: block; font-weight: 800; font-size: 19px; letter-spacing: -0.02em; }
.brand-tag { display: block; font-size: 12.5px; color: var(--ink-soft); }

.topbar-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  color: var(--ink);
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #f0f2f5; }
.btn:active { transform: translateY(1px); }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }

.btn.ghost { background: transparent; }
.btn.ghost:hover { background: #eef0f3; }

.btn.tip {
  background: var(--tip);
  border-color: #f2ce3a;
  color: #4a3b00;
}
.btn.tip:hover { filter: brightness(1.04); background: var(--tip); }

.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.danger { color: #c0392b; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 430px minmax(0, 1fr);
  gap: 24px;
  padding: 24px;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}

/* ---------- editor ---------- */
.editor { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 18px 16px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

.hint { font-weight: 500; text-transform: none; letter-spacing: 0; color: #9aa1ab; font-size: 12px; }

.field { display: block; margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field > span {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 5px;
}

.field input[type="text"],
.field input[type="date"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  background: #fbfcfd;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent);
  background: #fff;
}
.field textarea { resize: vertical; min-height: 40px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid-2.spaced { margin-top: 14px; }

/* logo */
.logo-row { display: flex; align-items: center; gap: 10px; }
.logo-thumb { height: 36px; max-width: 120px; object-fit: contain; border-radius: 4px; border: 1px solid var(--line); padding: 2px 6px; background: #fff; }

/* items editor */
.item-row {
  display: grid;
  grid-template-columns: 1fr 64px 92px 28px;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}
.item-row input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  background: #fbfcfd;
  outline: none;
}
.item-row input:focus { border-color: var(--accent); background: #fff; }
.item-row .item-remove {
  border: none;
  background: transparent;
  color: #b5bcc6;
  font-size: 18px;
  cursor: pointer;
  padding: 2px;
  border-radius: 6px;
  line-height: 1;
}
.item-row .item-remove:hover { color: #c0392b; background: #fdf0ee; }

.items-header {
  display: grid;
  grid-template-columns: 1fr 64px 92px 28px;
  gap: 8px;
  font-size: 11.5px;
  font-weight: 600;
  color: #9aa1ab;
  margin-bottom: 5px;
  padding: 0 2px;
}

/* style row */
.style-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.accents { display: flex; gap: 8px; }
.accent-swatch {
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  outline-offset: 2px;
}
.accent-swatch.active { border-color: var(--ink); box-shadow: inset 0 0 0 2px #fff; }
.paper-toggle { display: flex; gap: 6px; }
.paper-btn.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.privacy-note {
  font-size: 12.5px;
  color: var(--ink-soft);
  text-align: center;
  margin: 4px 0 0;
}

/* ---------- preview ---------- */
.preview-wrap {
  position: sticky;
  top: 86px;
  display: flex;
  justify-content: center;
  overflow: auto;
  max-height: calc(100vh - 110px);
  padding: 4px 4px 40px;
}

.sheet {
  --sheet-w: 794px;
  width: var(--sheet-w);
  min-width: var(--sheet-w);
  min-height: 1000px;
  background: #fff;
  box-shadow: var(--shadow);
  border-radius: 3px;
  padding: 56px 56px 48px;
  display: flex;
  flex-direction: column;
  font-size: 14px;
}
.sheet.paper-letter { --sheet-w: 816px; }

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

.pv-logo { max-height: 64px; max-width: 220px; object-fit: contain; margin-bottom: 12px; display: block; }
.pv-from-name { font-weight: 700; font-size: 17px; }
.pv-details { white-space: pre-line; color: var(--ink-soft); font-size: 13px; margin-top: 4px; }

.sheet-meta { text-align: right; flex-shrink: 0; }
.pv-title {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--accent);
  margin-bottom: 10px;
}
.meta-table { margin-left: auto; border-collapse: collapse; font-size: 13px; }
.meta-table td { padding: 2px 0 2px 18px; text-align: right; }
.meta-table td:first-child { color: var(--ink-soft); }
.meta-table td:last-child { font-weight: 600; }

.sheet-billto { margin-bottom: 28px; }
.label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--accent);
  margin-bottom: 4px;
}
.pv-to-name { font-weight: 700; font-size: 15px; }

.items-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}
.items-table th {
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #fff;
  background: var(--accent);
  padding: 9px 12px;
}
.items-table th.col-qty, .items-table th.col-rate, .items-table th.col-amt,
.items-table td.col-qty, .items-table td.col-rate, .items-table td.col-amt {
  text-align: right;
  white-space: nowrap;
}
.items-table th:first-child { border-radius: 4px 0 0 4px; }
.items-table th:last-child { border-radius: 0 4px 4px 0; }
.items-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.items-table td.col-desc { white-space: pre-line; }

.totals { display: flex; justify-content: flex-end; margin-bottom: 32px; }
.totals table { border-collapse: collapse; min-width: 260px; font-size: 14px; }
.totals td { padding: 6px 0 6px 24px; text-align: right; }
.totals td:first-child { color: var(--ink-soft); }
.totals .total-row td {
  border-top: 2px solid var(--ink);
  font-weight: 800;
  font-size: 18px;
  color: var(--ink);
  padding-top: 10px;
}
.totals .total-row td:first-child { color: var(--ink); }

.sheet-foot { margin-top: auto; display: flex; flex-direction: column; gap: 16px; }
.foot-block .pv-details { font-size: 12.5px; }

/* ---------- story ---------- */
.story {
  max-width: 720px;
  margin: 16px auto 60px;
  padding: 28px 32px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.story h2 { margin: 0 0 12px; font-size: 20px; letter-spacing: -0.02em; }
.story p { margin: 0 0 12px; color: #333a44; }
.story-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.fineprint { font-size: 12.5px; color: var(--ink-soft); }

/* ---------- responsive ---------- */
@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
  .preview-wrap { position: static; max-height: none; overflow-x: auto; justify-content: flex-start; }
  .brand-tag { display: none; }
}

/* ---------- print ---------- */
@media print {
  html, body { background: #fff; }
  .topbar, .editor, .story { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .preview-wrap { position: static; max-height: none; overflow: visible; padding: 0; display: block; }
  .sheet {
    width: auto;
    min-width: 0;
    min-height: 0;
    box-shadow: none;
    border-radius: 0;
    padding: 14mm 14mm 12mm;
    font-size: 13px;
  }
  .items-table th {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .sheet-head { border-bottom-color: var(--accent) !important; }
}
