:root {
  --sage: #c5d4a8;
  --ink: #222;
  --line: #bfbfbf;
  --bg: #f4f1ea;
  --card: #fff;
  --err: #b42318;
  --ok: #1f6b3a;
  --accent: #2f5d3a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

.hidden { display: none !important; }

.pin-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.pin {
  background: var(--card);
  padding: 2rem 1.6rem 1.5rem;
  border-radius: 10px;
  width: min(24rem, 92vw);
  text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}

.pin .eyebrow {
  margin: 0 0 0.35rem;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #666;
}

.pin h1 { font-size: 1.25rem; margin: 0 0 0.4rem; }
.pin p { color: #666; margin: 0 0 1rem; font-size: 0.9rem; }

.pin form {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

#pin {
  width: 8rem;
  letter-spacing: 0.35em;
  text-align: center;
  font-size: 1.2rem;
  padding: 0.45rem 0.4rem;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.err {
  color: var(--err);
  min-height: 1.2em;
  margin: 0.7rem 0 0;
  font-size: 0.85rem;
}

.chrome {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.7rem 1.25rem;
  background: #2a3a2e;
  color: #f6f3ec;
}

.chrome h1 {
  margin: 0;
  font-size: 1.05rem;
}

.chrome .sub {
  color: #c9d6c4;
  font-size: 0.8rem;
  margin-left: 0.6rem;
}

.layout {
  display: grid;
  grid-template-columns: 15.5rem minmax(24rem, 28rem) 1fr;
  gap: 1.25rem;
  padding: 1.25rem;
  align-items: start;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 0.75rem;
    gap: 0.9rem;
  }
}

.sidebar {
  background: var(--card);
  border: 1px solid #ddd4c4;
  border-radius: 8px;
  padding: 0.9rem 0.85rem 1rem;
}

.sidebar h2 {
  margin: 0 0 0.6rem;
  font-size: 0.92rem;
}

#invoice-list {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  max-height: 70vh;
  overflow: auto;
}

#invoice-list li {
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 0.45rem 0.5rem;
  margin: 0 0 0.4rem;
  cursor: pointer;
  font-size: 0.82rem;
}

#invoice-list li:hover,
#invoice-list li.on {
  background: #eef5e4;
  border-color: var(--sage);
}

#invoice-list .num { font-weight: 700; }
#invoice-list .meta { color: #666; display: block; }

.company-box {
  border: 1px solid #ddd4c4;
  border-radius: 8px;
  padding: 0.45rem 0.7rem 0.7rem;
  background: #fbfaf6;
}

.company-box summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 0.88rem;
  padding: 0.25rem 0;
}

.company-box label { margin-top: 0.45rem; }
.company-box button { margin-top: 0.6rem; }

form.editor {
  background: var(--card);
  padding: 1rem 1.1rem 1.15rem;
  border-radius: 8px;
  border: 1px solid #ddd4c4;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

form.editor label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.82rem;
  font-weight: 700;
}

form.editor input,
form.editor select,
form.editor textarea {
  font: inherit;
  font-weight: 400;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 6px;
}

form.editor textarea { min-height: 4.5rem; resize: vertical; }

.row {
  display: flex;
  gap: 0.4rem;
  align-items: stretch;
}

.row select { flex: 1; }

.lines-label {
  font-size: 0.82rem;
  font-weight: 700;
}

.line {
  display: grid;
  grid-template-columns: 1fr 7.2rem 2.1rem;
  grid-template-rows: auto auto;
  column-gap: 0.4rem;
  row-gap: 0.2rem;
  margin-bottom: 0.55rem;
  align-items: start;
}

.line .desc {
  grid-column: 1;
  grid-row: 1 / span 2;
  min-height: 3.4rem;
  overflow: hidden;
  resize: none;
  line-height: 1.35;
}

.line .amt-label {
  grid-column: 2;
  grid-row: 1;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.line .amt {
  grid-column: 2;
  grid-row: 2;
  width: 100%;
  appearance: textfield;
  -moz-appearance: textfield;
}

.line .amt::-webkit-outer-spin-button,
.line .amt::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.line .remove {
  grid-column: 3;
  grid-row: 2;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  min-height: 2.1rem;
  font-weight: 700;
  line-height: 1;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.35rem;
}

button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 0.45rem 0.85rem;
  font-weight: 700;
  cursor: pointer;
}

button.ghost {
  background: transparent;
  color: inherit;
  border: 1px solid currentColor;
}

button:disabled { opacity: 0.55; cursor: wait; }

#status {
  min-height: 1.2em;
  font-size: 0.88rem;
  margin: 0;
}

#status.ok { color: var(--ok); }
#status.bad { color: var(--err); }

.preview-wrap {
  overflow: auto;
}

.invoice {
  background: #fff;
  padding: 28px 32px;
  max-width: 720px;
  width: 100%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
  border: 1px solid #d7d7d7;
}

.invoice h2 {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 600;
}

.invoice hr {
  border: none;
  border-top: 2px solid #222;
  margin: 0 0 20px;
}

.inv-top {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  margin-bottom: 24px;
  font-size: 14px;
  line-height: 1.5;
}

.inv-top .left { flex: 1 1 14rem; min-width: 0; }
.inv-top .right { text-align: right; flex: 0 1 14rem; }
.inv-num,
.inv-date {
  white-space: nowrap;
}
#inv-billto {
  display: block;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}

.inv-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 14px;
}

.inv-table th {
  background: var(--sage);
  text-align: left;
  padding: 8px 10px;
  border: 1px solid var(--line);
}

.inv-table th.amt,
.inv-table td.amt {
  text-align: right;
  width: 140px;
}

.inv-table td {
  padding: 8px 10px;
  border: 1px solid var(--line);
  vertical-align: top;
  white-space: pre-wrap;
}

.total-row {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
  font-size: 14px;
}

.total-row span {
  padding: 8px 10px;
  font-weight: 700;
}

.total-row .amt {
  background: var(--sage);
  border: 1px solid var(--line);
  min-width: 140px;
  text-align: right;
}

@media (max-width: 640px) {
  .chrome {
    padding: 0.6rem 0.8rem;
    gap: 0.5rem;
  }
  .chrome h1 { font-size: 0.95rem; }
  .chrome .sub { display: block; margin-left: 0; }
  .row { flex-wrap: wrap; }
  .line {
    grid-template-columns: 1fr 2.1rem;
    grid-template-rows: auto auto auto;
  }
  .line .desc {
    grid-column: 1 / -1;
    grid-row: 1;
  }
  .line .amt-label {
    grid-column: 1;
    grid-row: 2;
  }
  .line .amt {
    grid-column: 1;
    grid-row: 3;
    max-width: 8.5rem;
  }
  .line .remove {
    grid-column: 2;
    grid-row: 3;
  }
  .invoice {
    padding: 18px 14px;
    box-shadow: none;
  }
  .invoice h2 { font-size: 22px; }
  .inv-top {
    flex-direction: column-reverse;
    gap: 1rem;
    margin-bottom: 16px;
  }
  .inv-top .left,
  .inv-top .right {
    flex: none;
    width: 100%;
    text-align: left;
  }
  .inv-table { font-size: 13px; }
  .inv-table th,
  .inv-table td { padding: 7px 8px; }
  .inv-table th.amt,
  .inv-table td.amt { width: 5.6rem; }
  .total-row .amt { min-width: 5.6rem; }
}

@media print {
  body { background: #fff; }
  .chrome, form.editor, .pin-wrap, .sidebar { display: none !important; }
  .layout { display: block; padding: 0; }
  .invoice { box-shadow: none; border: none; max-width: none; }
}
