*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #fff;
  --fg: #111;
  --muted: #767676;
  --rule: #e6e6e6;
  --desk: #f6f6f6;
  --label: 88px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e0e;
    --fg: #ececec;
    --muted: #8e8e8e;
    --rule: #262626;
    --desk: #161616;
  }
}

html {
  scrollbar-gutter: stable;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  color-scheme: light dark;
}

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  font-weight: 450;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
p {
  font-size: inherit;
  font-weight: inherit;
}

::selection {
  background: var(--fg);
  color: var(--bg);
}

:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

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

.muted {
  color: var(--muted);
}

/* Top bar */

.bar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 2rem;
  background: var(--bg);
  background-image: linear-gradient(to right, var(--rule) 50%, transparent 50%);
  background-size: 12px 1px;
  background-repeat: repeat-x;
  background-position: bottom;
}

.bar h1 {
  font-weight: 500;
}

.bar h1 .muted {
  margin-left: 0.5rem;
  font-weight: 450;
  transition: opacity 300ms ease-out;
}

.actions {
  display: flex;
  gap: 0.35rem;
}

.btn {
  min-height: 2rem;
  padding: 0.2rem 0.7rem;
  border: 1px solid var(--rule);
  border-radius: 0;
  background: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 120ms ease-out, border-color 120ms ease-out, color 120ms ease-out;
}

.btn.primary {
  border-color: var(--fg);
  background: var(--fg);
  color: var(--bg);
}

.btn:active {
  transform: translateY(0.5px);
}

.btn[aria-busy="true"] {
  cursor: progress;
  opacity: 0.6;
}

@media (hover: hover) and (pointer: fine) {
  .btn:hover {
    border-color: var(--muted);
  }

  .btn.primary:hover {
    border-color: var(--muted);
    background: var(--muted);
  }
}

/* Editor + preview */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 560px) minmax(0, 1fr);
  align-items: start;
}

.editor {
  padding: 2.5rem 2rem 4rem;
}

.row {
  display: grid;
  grid-template-columns: var(--label) minmax(0, 1fr);
  align-items: baseline;
}

.row + .row {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  background-image: linear-gradient(to right, var(--rule) 50%, transparent 50%);
  background-size: 12px 1px;
  background-repeat: repeat-x;
}

.row h2 {
  color: var(--muted);
}

.fields {
  display: grid;
  gap: 0.35rem;
}

.grid2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem 0.75rem;
}

.grid2 label {
  display: grid;
  gap: 0.15rem;
  color: var(--muted);
  font-size: 13px;
}

.grid2 label > * {
  color: var(--fg);
  font-size: 14px;
}

.pair {
  display: flex;
  gap: 0.35rem;
}

.pair > input {
  flex: 1;
  min-width: 0;
}

input,
textarea,
select {
  width: 100%;
  min-height: 2rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--rule);
  border-radius: 0;
  background: var(--bg);
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  transition: border-color 120ms ease-out;
}

textarea {
  display: block;
  resize: vertical;
  field-sizing: content;
}

select {
  appearance: none;
  padding-right: 1.5rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 12px) 50%, calc(100% - 8px) 50%;
  background-size: 4px 4px;
  background-repeat: no-repeat;
}

input::placeholder,
textarea::placeholder {
  color: var(--muted);
  opacity: 0.7;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--fg);
  outline: none;
}

input[type="date"]::-webkit-calendar-picker-indicator {
  opacity: 0.5;
}

.swatch {
  position: relative;
  flex: none;
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--rule);
  cursor: pointer;
}

.swatch input {
  position: absolute;
  inset: 3px;
  width: auto;
  height: auto;
  min-height: 0;
  padding: 0;
  border: 0;
  cursor: pointer;
}

.swatch input::-webkit-color-swatch-wrapper {
  padding: 0;
}

.swatch input::-webkit-color-swatch {
  border: 0;
}

.swatch:focus-within {
  border-color: var(--fg);
}

/* Repeating rows: fields, items, adjustments, payment */

[hidden] {
  display: none !important;
}

.list {
  display: grid;
  gap: 0.35rem;
  list-style: none;
}

.list:empty {
  display: none;
}

.list li {
  display: grid;
  gap: 0.35rem;
  align-items: start;
}

li.kv {
  grid-template-columns: 112px minmax(0, 1fr) 1.5rem;
}

li.kv input[data-k="label"] {
  color: var(--muted);
}

li.adj {
  grid-template-columns: minmax(0, 1fr) 92px 72px 72px 1.5rem;
}

li.adj input[data-k="value"] {
  text-align: right;
}

/* Item: name, (SAC), qty, rate on one line; description under the name */
li.item {
  grid-template-columns: minmax(0, 1fr) 56px 88px 1.5rem;
  grid-template-areas:
    "name qty rate x"
    "desc desc desc .";
}

.gst li.item {
  grid-template-columns: minmax(0, 1fr) 76px 56px 88px 1.5rem;
  grid-template-areas:
    "name sac qty rate x"
    "desc desc desc desc .";
}

li.item + li.item {
  margin-top: 0.4rem;
  padding-top: 0.75rem;
  background-image: linear-gradient(to right, var(--rule) 50%, transparent 50%);
  background-size: 6px 1px;
  background-repeat: repeat-x;
}

.i-name {
  grid-area: name;
}

.i-sac {
  grid-area: sac;
  display: none;
}

.gst .i-sac {
  display: block;
}

.i-qty {
  grid-area: qty;
  text-align: right;
}

.i-rate {
  grid-area: rate;
  text-align: right;
}

.item .remove {
  grid-area: x;
}

.i-desc {
  grid-area: desc;
  resize: none;
  color: var(--muted);
  font-size: 13px;
}

.remove {
  height: 2rem;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
}

.link {
  justify-self: start;
  margin-top: 0.25rem;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: var(--rule);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.22em;
  transition: text-decoration-color 150ms ease-out;
}

.items + .link {
  margin-top: 0.6rem;
}

.link::before {
  content: "+ ";
  color: var(--muted);
}

@media (hover: hover) and (pointer: fine) {
  .remove:hover {
    color: var(--fg);
  }

  .link:hover {
    text-decoration-color: currentColor;
  }
}

.split {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
}

.check {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  cursor: pointer;
}

.check input {
  width: auto;
  min-height: 0;
  accent-color: var(--fg);
  transform: translateY(1px);
}

.hint {
  grid-column: 1 / -1;
  font-size: 13px;
}

.warn {
  color: #c2264b;
}

.privacy {
  margin-top: 3rem;
  padding-left: var(--label);
  font-size: 13px;
}

.sum {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Preview: the real page, scaled to fit */

.preview {
  position: sticky;
  top: 4.1rem;
  height: calc(100vh - 4.1rem);
  overflow: auto;
  padding: 2.5rem 2rem;
  background: var(--desk);
}

.paper {
  max-width: 794px;
  margin: 0 auto;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 0 0 1px var(--rule);
}

.paper iframe {
  display: block;
  width: 794px;
  border: 0;
  transform-origin: top left;
  pointer-events: none;
}

@media (max-width: 1000px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .preview {
    position: static;
    height: auto;
    padding: 2rem 1rem;
  }
}

@media (max-width: 560px) {
  :root {
    --label: 0px;
  }

  .bar {
    padding: 0.75rem 1rem;
  }

  .editor {
    padding: 2rem 1rem 3rem;
  }

  .row {
    grid-template-columns: 1fr;
    gap: 0.4rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
