:root {
  --ink: #1c1c1c;
  --ui-bg: #f2f3f5;
  --ui-panel: #ffffff;
  --ui-border: #dcdfe4;
  --ui-text: #24292f;
  --ui-muted: #6b7280;
  --accent: #1976b8;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--ui-bg);
  color: var(--ui-text);
  display: flex;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 340px;
  min-width: 340px;
  height: 100vh;
  overflow-y: auto;
  background: var(--ui-panel);
  border-right: 1px solid var(--ui-border);
  padding: 20px 18px 40px;
}
.sb-header h1 { font-size: 18px; margin: 0 0 4px; }
.sb-header p { font-size: 12px; color: var(--ui-muted); margin: 0 0 14px; }

details { border-top: 1px solid var(--ui-border); }
summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  padding: 10px 0;
  user-select: none;
}
.sec { padding: 2px 0 14px; }

label { display: block; font-size: 12px; font-weight: 500; margin-bottom: 10px; color: var(--ui-text); }
input[type="text"], input[type="number"], input[type="date"], select, textarea {
  width: 100%;
  margin-top: 4px;
  padding: 7px 9px;
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  background: #fff;
  color: var(--ui-text);
}
textarea { resize: vertical; }
input[type="file"] { margin-top: 4px; font-size: 12px; width: 100%; }
input[type="color"] { margin-top: 4px; width: 100%; height: 34px; padding: 2px; border: 1px solid var(--ui-border); border-radius: 6px; background: #fff; }
input[type="range"] { width: 100%; margin-top: 8px; }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.input-with-btn { display: flex; gap: 6px; margin-top: 4px; }
.input-with-btn input { margin-top: 0; flex: 1; min-width: 0; }
.btn.dice { width: 38px; padding: 4px 0; font-size: 15px; line-height: 1; flex: none; }
.hint { font-size: 11px; color: var(--ui-muted); margin: 2px 0 8px; }

.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 10px;
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  width: 100%;
}
.btn:hover { background: #f6f8fa; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.subtle { color: var(--ui-muted); }
.actions { display: grid; gap: 8px; padding-top: 16px; border-top: 1px solid var(--ui-border); }

/* line items */
.item {
  border: 1px solid var(--ui-border);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 10px;
  background: #fafbfc;
}
.item-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.item-tag { font-size: 10px; font-weight: 700; letter-spacing: 0.06em; padding: 2px 7px; border-radius: 999px; }
.item-tag.hourly { background: #e0f0fb; color: #14639c; }
.item-tag.extra { background: #fdeee0; color: #a05c1c; }
.icon-btn { border: none; background: none; cursor: pointer; color: var(--ui-muted); font-size: 13px; padding: 2px 6px; }
.icon-btn:hover { color: #c0392b; }
.item .calc { margin: 0; }
.inline-num input { max-width: 100%; }

/* AI auto-fill */
.ai-box {
  border: 1px dashed #b9c6d4;
  border-radius: 8px;
  padding: 10px;
  margin-top: 12px;
  background: #f6f9fc;
}
.ai-title { font-size: 12px; font-weight: 600; margin-bottom: 6px; }
.ai-box textarea { margin-top: 0; }
.ai-box .row2 { margin-top: 8px; }
.ai-box input { margin-top: 0; }
.ai-box .hint { margin: 8px 0 0; }
.ai-box .hint.err { color: #c0392b; }
.ai-box .btn[disabled] { opacity: .6; cursor: wait; }

/* ---------- Preview ---------- */
#previewWrap {
  flex: 1;
  height: 100vh;
  overflow: auto;
  display: flex;
  justify-content: center;
  padding: 28px;
}
#paperScale { transform-origin: top left; overflow: visible; }

#pages { display: flex; flex-direction: column; gap: 24px; width: 794px; }

.paper {
  width: 794px;   /* A4 @ 96dpi */
  height: 1116px; /* slightly under true A4 so print never spills onto a blank page */
  background: #faf7f0;
  color: var(--ink);
  font-family: 'Poppins', sans-serif;
  padding: 60px 78px 64px;
  box-shadow: 0 3px 24px rgba(0,0,0,.14);
  display: flex;
  flex-direction: column;
  position: relative;
}

.p-conthead {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .12em;
  text-align: right;
  margin-bottom: 28px;
}

.p-logo { text-align: center; min-height: 10px; margin-bottom: 44px; }
.p-logo img { max-height: 220px; }
.p-logo img[src=""] { display: none; }

.p-label { font-weight: 600; letter-spacing: .12em; font-size: 14px; margin-bottom: 8px; }
.p-lines { font-size: 13.5px; letter-spacing: .08em; line-height: 1.55; white-space: pre-wrap; }

.p-meta { display: flex; justify-content: space-between; margin-bottom: 46px; }
.p-invno { text-align: right; }
.p-no { font-weight: 600; letter-spacing: .1em; font-size: 14px; margin-bottom: 8px; }
.p-date { font-size: 13.5px; letter-spacing: .08em; }

.p-table { width: 100%; border-collapse: collapse; }
.p-table th {
  font-weight: 600;
  letter-spacing: .12em;
  font-size: 13.5px;
  padding: 16px 0 14px;
  border-top: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
}
.p-table .c-desc { text-align: left; width: 34%; }
.p-table .c-unit { text-align: center; width: 26%; }
.p-table .c-qty { text-align: center; width: 14%; }
.p-table .c-total { text-align: right; width: 26%; }
.p-table td {
  font-size: 13.5px;
  letter-spacing: .08em;
  line-height: 1.5;
  padding: 13px 0;
  vertical-align: top;
}
.p-table td.c-desc { padding-left: 12px; white-space: pre-wrap; }
.p-table td.c-date-group {
  padding: 22px 0 2px 2px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .18em;
  text-transform: uppercase;
  border-bottom: 1px solid rgba(0, 0, 0, .25);
}
.p-table tr:first-child td.c-date-group { padding-top: 14px; }
.p-table td.c-unit { text-align: center; }
.p-table td.c-qty { text-align: center; }
.p-table td.c-total { text-align: right; padding-right: 4px; }

.p-grandtotal {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1.5px solid var(--ink);
  border-bottom: 2px solid var(--ink);
  padding: 15px 4px 15px 0;
  font-weight: 600;
  letter-spacing: .12em;
  font-size: 14.5px;
  margin-top: 6px;
}

.p-footer {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}
.p-issued-by { max-width: 55%; }
.p-signoff {
  font-family: 'Mrs Saint Delafield', cursive;
  font-size: 64px;
  line-height: 1;
  padding-right: 8px;
  padding-bottom: 6px;
  transform: rotate(-4deg);
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  body { flex-direction: column; }
  #sidebar {
    width: 100%;
    min-width: 0;
    height: auto;
    max-height: 55vh;
    border-right: none;
    border-bottom: 1px solid var(--ui-border);
  }
  #previewWrap { height: auto; padding: 16px 8px 40px; }
}

/* ---------- Print ---------- */
@page { size: A4; margin: 0; }
@media print {
  body { background: none; display: block; }
  #sidebar { display: none; }
  #previewWrap { height: auto; overflow: visible; padding: 0; display: block; }
  #paperScale { transform: none !important; height: auto !important; }
  #pages { gap: 0; width: auto; }
  .paper {
    width: 210mm;
    height: 296mm;
    box-shadow: none;
    padding: 16mm 20.6mm 17mm;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
    break-after: page;
  }
  .paper:last-child { break-after: auto; }
}
