:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1c2333;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #0f6b4f;
  --brand-dark: #0a4f3a;
  --accent: #e0b34a;
  --danger: #d64545;
  --ok: #1a9c5a;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.18);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}
.hidden { display: none !important; }
a { color: var(--brand); text-decoration: none; cursor: pointer; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(15, 107, 79, 0.25);
  border-color: var(--brand);
}
label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
table { border-collapse: collapse; width: 100%; background: var(--panel); }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: middle; }
th { background: #f8fafc; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
tr:hover td { background: #fafbfd; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 8px; padding: 7px 12px; font-size: 13px; font-weight: 600;
}
.btn:hover { border-color: #c9ced6; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: #f3c6c6; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

/* login */
#login-root {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #0a4f3a, #117a5a);
  padding: 16px;
}
.login-card {
  background: #fff; border-radius: 16px; padding: 34px 30px; width: 100%; max-width: 400px;
  box-shadow: var(--shadow-lg);
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-card p.sub { color: var(--muted); margin: 0 0 20px; font-size: 13px; }
.form-row { margin-bottom: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* layout */
#app-root { display: flex; min-height: 100vh; }
#sidebar {
  width: 218px; background: #12201a; color: #dce7e1; flex-shrink: 0;
  padding: 16px 10px 30px; overflow-y: auto; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 16px; }
.brand .logo {
  width: 34px; height: 34px; border-radius: 9px; background: var(--accent);
  color: #1c2333; font-weight: 800; display: flex; align-items: center; justify-content: center; font-size: 17px;
}
.brand .t { font-weight: 700; font-size: 16px; color: #fff; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: #6f8579; padding: 14px 12px 4px; }
.nav-item {
  display: flex; align-items: center; gap: 9px; padding: 8px 12px; border-radius: 8px;
  color: #c6d5cd; cursor: pointer; font-size: 13.5px; font-weight: 500; margin: 1px 0;
}
.nav-item:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.nav-item.active { background: var(--brand); color: #fff; }
.nav-item .ic { width: 18px; text-align: center; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 22px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
#topbar h2 { margin: 0; font-size: 18px; }
#view { padding: 20px 22px 60px; }

/* cards */
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card h3 { margin: 0 0 14px; font-size: 15px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px;
}
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 21px; font-weight: 700; margin-top: 4px; }
.stat .v.green { color: var(--ok); }
.stat .v.red { color: var(--danger); }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.right { text-align: right; }
.mono { font-variant-numeric: tabular-nums; }
.num { text-align: right; }
.muted { color: var(--muted); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.tag.green { background: #e3f6ec; color: var(--ok); }
.tag.red { background: #fdeaea; color: var(--danger); }
.tag.gray { background: #eef0f3; color: var(--muted); }
.tag.amber { background: #fdf3df; color: #a66b10; }
.unpaid { color: var(--danger); font-weight: 700; }

/* modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 15, 20, 0.5);
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50; overflow-y: auto;
}
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 640px; box-shadow: var(--shadow-lg); }
.modal.wide { max-width: 860px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-bottom: 1px solid var(--line);
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 20px; border-top: 1px solid var(--line); }

/* toast */
#toast-root { position: fixed; top: 16px; right: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #1c2333; color: #fff; border-radius: 10px; padding: 11px 16px;
  font-size: 13px; box-shadow: var(--shadow-lg); max-width: 340px;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* lines editor */
.lines { width: 100%; }
.lines input, .lines select { padding: 6px 8px; font-size: 13px; }
.inline-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.inline-form > div { min-width: 150px; flex: 1; }
.inline-form .short { min-width: 90px; flex: 0 0 110px; }

/* print */
#print-root { display: none; }
@media print {
  body > :not(#print-root) { display: none !important; }
  #print-root { display: block; }
  body { background: #fff; }
}
/* ---- print sheet (A4 designer output) ---- */
.printsheet { position: relative; width: 640px; min-height: 1123px; margin: 0 auto; background: #fff; padding: 10px; box-sizing: border-box; }
.printsheet .blk { cursor: move; box-sizing: border-box; }
.printsheet .ptable, .printsheet .ptable table { width: 100%; border-collapse: collapse; }
.printsheet .ptable th, .printsheet .ptable td { font-size: 12px; padding: 5px 6px; border-bottom: 1px solid #ddd; }
.printsheet .ptable th { background: #f5f5f5; font-weight: 700; }
.printsheet .ptotals { margin-left: auto; width: 250px; }
.printsheet .ptotals td { font-size: 12px; padding: 3px 6px; }
.printsheet table { border-collapse: collapse; }
.printsheet td.right, .printsheet th.right { text-align: right; }

@media print {
  @page { margin: 8mm; }
  body > :not(#print-root) { display: none !important; }
  #print-root { display: block; }
  .printsheet { width: 640px; min-height: auto !important; }
}

/* ---- layout designer editor ---- */
.ed-wrap { display: flex; gap: 14px; align-items: flex-start; }
.ed-main { flex: 1; min-width: 0; }
.ed-tools { margin-bottom: 8px; }
.ed-stage {
  border: 1px solid var(--line); border-radius: 8px; background: #eceef1;
  overflow: auto; padding: 4px;
}
.ed-canvas {
  transform-origin: top left; background: #fff; border: 1px solid #ccc;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12); position: relative; overflow: hidden;
  user-select: none; -webkit-user-select: none;
}
.ed-canvas .blk, .ed-canvas img { -webkit-user-drag: none; }
.ed-canvas .blk { user-select: none; -webkit-user-select: none; }
.ed-canvas .blk:hover { outline: 1px dashed #8ab8a6; }
.ed-side { width: 320px; flex-shrink: 0; }
.ed-side input, .ed-side select { padding: 3px 5px; font-size: 12px; }
.edtbl { width: 100%; }
.edtbl th { font-size: 10px; padding: 4px 3px; }
.edtbl td { padding: 3px; font-size: 12px; }
.edtbl input[type="number"] { width: 100%; min-width: 48px; }
.edtbl select { padding: 2px 2px; font-size: 12px; }
.ed-propcard { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fff; }

/* print layout designer */
.prow {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; margin-bottom: 6px; cursor: grab; user-select: none;
}
.prow.dragging { opacity: 0.45; }
.prow .grip { color: var(--muted); font-size: 15px; }
.prow .plabel { flex: 1; font-weight: 600; font-size: 13px; }
.prow .pshow { display: flex; align-items: center; gap: 5px; font-weight: 500; font-size: 12px; color: var(--muted); }
.prow .pshow input { width: auto; }

/* printed header blocks */
.pblock { text-align: center; margin-bottom: 10px; }
.pblock .co { font-size: 18px; font-weight: 800; }
.pblock .ptitle { font-size: 15px; font-weight: 800; letter-spacing: 0.04em; margin-top: 3px; }
.pblock > div { margin: 1px 0; }
.pblock .pnote { margin-top: 8px; font-size: 11px; color: #555; }

.receipt { width: 100%; max-width: 720px; margin: 0 auto; font-size: 13px; }
.receipt.narrow { max-width: 320px; font-size: 11px; }
.receipt.narrow .co { font-size: 15px; }
.receipt.narrow table td, .receipt.narrow table th { font-size: 10.5px; padding: 4px 4px; }
.receipt.narrow .totals { width: 100%; }
.receipt .hdr { text-align: center; margin-bottom: 12px; }
.receipt .hdr .co { font-size: 18px; font-weight: 800; }
.receipt table td, .receipt table th { font-size: 12px; padding: 5px 6px; }
.receipt .totals { margin-left: auto; width: 240px; }
.receipt .totals td:last-child { text-align: right; }
.receipt .b { font-weight: 700; }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
@media (max-width: 900px) {
  #sidebar { width: 58px; }
  .brand .t, .nav-item span.lbl { display: none; }
  .nav-item { justify-content: center; }
  #topbar h2 { font-size: 15px; }
}
