:root {
  --green: #1f9d46;
  --green-dark: #17813a;
  --green-tint: #e6f7e9;
  --ink: #16231a;
  --muted: #5b6b60;
  --line: #d6e0d9;
  --bg: #f2f5f3;
  --card: #ffffff;
  --danger: #b3261e;
  --radius: 10px;
  --topbar-h: 68px;
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

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

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

h1, h2, p { margin: 0; }

[hidden] { display: none !important; }

/* ---- top bar ---------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: var(--topbar-h);
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 18px; font-weight: 700; line-height: 1.2; }
.brand p { font-size: 13px; color: var(--muted); }
.brand-bolt { flex: none; }

.actions { display: flex; gap: 10px; flex: none; }

/* ---- buttons ---------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 16px;
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 1px solid #b9c7be;
  border-radius: 8px;
  cursor: pointer;
}
.btn:hover { background: #f2f7f3; }
.btn:active { transform: translateY(1px); }
.btn-primary { color: #fff; background: var(--green); border-color: var(--green); }
.btn-primary:hover { background: var(--green-dark); }
.btn-small { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn-quiet { font-weight: 500; color: var(--muted); }
.btn-add { margin-top: 10px; color: var(--green-dark); border-style: dashed; }

button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible,
summary:focus-visible, a:focus-visible, label.btn:focus-within {
  outline: 3px solid rgba(31, 157, 70, 0.45);
  outline-offset: 1px;
}

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

.editor { display: grid; gap: 16px; min-width: 0; }

.notice {
  padding: 10px 14px;
  font-size: 14px;
  background: var(--green-tint);
  border: 1px solid #b9e0c1;
  border-radius: var(--radius);
}

.card {
  padding: 16px 18px 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.card > h2, .fold > summary h2 { font-size: 16px; font-weight: 700; }
.card > h2 { margin-bottom: 12px; }

.fold > summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
  list-style: none;
}
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary h2::before { content: "▸"; display: inline-block; width: 1.1em; color: var(--green-dark); }
.fold[open] > summary h2::before { content: "▾"; }
.fold > summary span { font-size: 13px; color: var(--muted); }
.fold[open] > summary { margin-bottom: 12px; }

/* ---- form fields ------------------------------------------------------ */
.grid.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.field { display: block; margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field > span { display: block; margin-bottom: 4px; font-size: 13px; font-weight: 600; color: #2d3d33; }
.field > span em { font-style: normal; font-weight: 400; color: var(--muted); }

input[type="text"], input[type="date"], textarea, select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid #b9c7be;
  border-radius: 8px;
}
textarea { resize: vertical; line-height: 1.35; }
input::placeholder, textarea::placeholder { color: #8a988f; }

.hint { margin-top: 6px; font-size: 13px; color: var(--muted); }
kbd { padding: 0 5px; font: inherit; font-size: 12px; background: #eef2ef; border: 1px solid var(--line); border-radius: 4px; }
code { padding: 0 4px; font-size: 12.5px; background: #eef2ef; border-radius: 4px; }

/* ---- items ------------------------------------------------------------ */
.item-row {
  display: grid;
  grid-template-columns: 84px 112px minmax(0, 1fr) auto;
  gap: 8px 10px;
  align-items: end;
  margin-bottom: 10px;
  padding: 10px 12px;
  background: #f7faf8;
  border: 1px solid var(--line);
  border-radius: 8px;
}
/* row 1: description + move/remove buttons; row 2: qty, unit price, price */
.item-row input.it-desc { grid-column: 1 / 4; grid-row: 1; }
.it-actions { grid-column: 4; grid-row: 1; display: flex; gap: 2px; justify-content: flex-end; }
.mini { display: block; min-width: 0; }
.mini > span { display: block; margin-bottom: 3px; font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.item-row input.it-qty, .item-row input.it-unit { text-align: center; }
.mini-total .it-total { display: block; min-height: 40px; line-height: 40px; font-weight: 700; font-variant-numeric: tabular-nums; }

.icon-btn {
  width: 28px;
  height: 36px;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:hover:not(:disabled) { color: var(--ink); background: #eef2ef; }
.icon-btn.remove:hover:not(:disabled) { color: var(--danger); background: #fdecea; }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* ---- totals summary --------------------------------------------------- */
.summary { margin: 0 0 14px; padding: 10px 14px; background: var(--green-tint); border-radius: 8px; }
.summary > div { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }
.summary dt, .summary dd { margin: 0; }
.summary dd { font-weight: 600; font-variant-numeric: tabular-nums; }
.summary .sum-grand { margin-top: 4px; padding-top: 7px; border-top: 1px solid rgba(0, 0, 0, 0.12); font-size: 17px; font-weight: 700; }

/* ---- logo ------------------------------------------------------------- */
.logo-row { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.logo-row img { flex: none; width: 150px; max-height: 60px; object-fit: contain; padding: 6px; background: var(--green-tint); border-radius: 8px; }
.logo-row .btn + .btn { margin-left: 6px; }

.saved { padding: 0 4px 24px; font-size: 13px; color: var(--muted); text-align: center; }

/* ---- preview ---------------------------------------------------------- */
.preview {
  position: sticky;
  top: calc(var(--topbar-h) + 16px);
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--topbar-h) - 32px);
  min-height: 420px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.preview-bar { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); font-size: 14px; }
.preview-bar #preview-status { flex: 1; font-size: 13px; color: var(--muted); }
.preview-bar a { color: var(--green-dark); font-weight: 600; }
#preview-frame { flex: 1; width: 100%; min-height: 0; border: 0; background: #e8ece9; }
.preview-fallback { padding: 8px 14px; font-size: 12.5px; color: var(--muted); border-top: 1px solid var(--line); }
.preview-error { color: var(--danger) !important; font-weight: 600; }

/* ---- narrow screens --------------------------------------------------- */
@media (max-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .preview { position: static; height: 80vh; }
}

@media (max-width: 640px) {
  :root { --topbar-h: 60px; }
  .topbar { padding: 8px 12px; }
  .brand > div { display: none; }
  .btn { padding: 0 12px; }
  .layout { padding: 12px; gap: 14px; }
  .grid.two { grid-template-columns: 1fr; gap: 0; }
  .grid.two .field { margin-bottom: 12px; }

  .item-row { grid-template-columns: minmax(0, 0.7fr) minmax(0, 1.1fr) minmax(0, 1.1fr) auto; }
  .logo-row { flex-direction: column; align-items: flex-start; }
}
