:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eef1ec;
  --ink: #1b2522;
  --muted: #5d6a65;
  --line: #d6dcd6;
  --primary: #0d5c4d;
  --primary-ink: #ffffff;
  --primary-soft: #dcece6;
  --error: #b3261e;
  --error-soft: #fbe9e7;
  --warn: #8a5a00;
  --warn-soft: #fdf1d6;
  --ok: #1d6b3a;
  --ok-soft: #dff1e4;
  --yellow: #fff6bf;
  --radius: 8px;
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
p { margin: 0; line-height: 1.5; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }

.btn { border: 1px solid var(--line); background: var(--surface); padding: 7px 13px; border-radius: var(--radius); cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: var(--primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
.btn.danger { color: var(--error); }
.btn.small { padding: 3px 8px; font-size: .87rem; }
.btn.link { border: 0; background: none; color: var(--primary); padding: 2px 4px; text-decoration: underline; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }

/* Đăng nhập */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 28px; display: grid; gap: 14px; }
.login .unit { color: var(--muted); font-size: .85rem; letter-spacing: .03em; text-transform: uppercase; }
.login label { display: grid; gap: 4px; font-weight: 600; font-size: .92rem; }
.login input { padding: 9px 11px; border: 1px solid var(--line); border-radius: var(--radius); font-weight: 400; }
.form-error { color: var(--error); background: var(--error-soft); padding: 8px 10px; border-radius: 6px; font-size: .92rem; }

/* Khung ứng dụng */
.topbar { background: var(--primary); color: var(--primary-ink); padding: 9px 16px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar .brand { font-weight: 700; }
.topbar .sub { opacity: .85; font-size: .88rem; }
.topbar .spacer { flex: 1; }
.topbar .btn { background: transparent; border-color: rgba(255,255,255,.45); color: #fff; }
.save-state { font-size: .86rem; opacity: .9; }
.layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: calc(100vh - 50px); }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; }
.side button { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.side button:hover { background: var(--surface-2); }
.side button.active { background: var(--primary-soft); font-weight: 600; }
.side .name { flex: 1; min-width: 0; }
.badge { font-size: .76rem; font-weight: 600; padding: 1px 7px; border-radius: 999px; }
.badge.err { background: var(--error-soft); color: var(--error); }
.badge.wrn { background: var(--warn-soft); color: var(--warn); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.main { padding: 18px 20px 60px; min-width: 0; display: grid; gap: 14px; align-content: start; }
.page-head { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; }
.note { color: var(--muted); font-size: .9rem; max-width: 110ch; }
details.template-note summary { cursor: pointer; color: var(--primary); font-size: .9rem; }
details.template-note p { margin-top: 6px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; display: grid; gap: 10px; }
.banner { border-radius: 8px; padding: 10px 14px; display: grid; gap: 4px; }
.banner.err { background: var(--error-soft); color: #7c1a14; }
.banner.wrn { background: var(--warn-soft); color: #5f3e00; }
.banner.ok { background: var(--ok-soft); color: #14502b; }
.banner.info { background: var(--primary-soft); color: #0a4539; }
.banner ul { margin: 4px 0 0; padding-left: 20px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar .grow { flex: 1; }
.search { padding: 6px 10px; border: 1px solid var(--line); border-radius: 6px; min-width: 220px; }

/* Danh sách lỗi */
.issues { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.issues > summary { padding: 9px 14px; cursor: pointer; font-weight: 600; }
.issues .list { max-height: 260px; overflow: auto; border-top: 1px solid var(--line); }
.issue { display: grid; gap: 6px; padding: 7px 14px; border-bottom: 1px solid var(--surface-2); }
.issue button { all: unset; cursor: pointer; font-size: .92rem; line-height: 1.4; }
.issue button:hover { text-decoration: underline; }
.issue button:focus-visible { outline: 2px solid var(--primary); }
.issue .tag { font-weight: 700; margin-right: 6px; }
.issue.error .tag { color: var(--error); }
.issue.warn .tag { color: var(--warn); }
.issue textarea { width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; min-height: 34px; resize: vertical; font-size: .9rem; }

/* Bảng nhập */
.table-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; max-height: calc(100vh - 170px); }
table.grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
table.grid th { position: sticky; top: 0; z-index: 2; background: var(--surface-2); text-align: left; font-size: .82rem; font-weight: 600; padding: 7px 8px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); vertical-align: bottom; max-width: 190px; }
table.grid td { padding: 3px 4px; border-bottom: 1px solid var(--surface-2); border-right: 1px solid var(--surface-2); vertical-align: top; }
table.grid td.ro { padding: 7px 8px; font-size: .93rem; }
table.grid td.tt { text-align: right; color: var(--muted); padding: 7px 6px; font-size: .86rem; }
table.grid td.sticky, table.grid th.sticky { position: sticky; left: 0; background: var(--surface); z-index: 1; border-right: 1px solid var(--line); }
table.grid th.sticky { z-index: 3; background: var(--surface-2); }
table.grid tr.total td { font-weight: 700; background: var(--surface-2); padding: 7px 8px; }
.cell { position: relative; min-width: var(--w, 110px); width: var(--w, 110px); }
.cell input, .cell select, .cell textarea { width: 100%; padding: 5px 7px; border: 1px solid var(--line); border-radius: 5px; background: #fff; }
.cell textarea { min-height: 34px; resize: vertical; display: block; }
.cell input:disabled, .cell select:disabled, .cell textarea:disabled { background: var(--surface-2); color: var(--ink); border-color: transparent; opacity: 1; }
.cell.num input { text-align: right; }
.cell.is-error input, .cell.is-error select, .cell.is-error textarea { border-color: var(--error); background: var(--error-soft); }
.cell.is-warn input, .cell.is-warn select, .cell.is-warn textarea { border-color: #c98a00; background: var(--warn-soft); }
.cell .msg { display: none; position: absolute; z-index: 5; left: 0; top: 100%; width: max(240px, 100%); background: var(--ink); color: #fff; font-size: .82rem; line-height: 1.35; padding: 6px 8px; border-radius: 6px; }
.cell:focus-within .msg { display: block; }
.field-row { display: flex; gap: 14px; flex-wrap: wrap; align-items: flex-end; }
.field { display: grid; gap: 4px; font-size: .9rem; font-weight: 600; }
.field.yellow .cell input { background: var(--yellow); }
.check { display: flex; gap: 6px; align-items: center; font-size: .9rem; }
.check input { width: 17px; height: 17px; }
.check.is-error { color: var(--error); }
.hint { color: var(--muted); font-size: .86rem; }

/* Tổng quan */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; }
.stat b { display: block; font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .86rem; }
.chip { display: inline-block; font-size: .8rem; padding: 2px 9px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.chip.draft { background: var(--surface-2); color: var(--muted); }
.chip.returned { background: var(--error-soft); color: var(--error); }
.chip.submitted { background: var(--warn-soft); color: var(--warn); }
.chip.approved { background: var(--ok-soft); color: var(--ok); }
table.admin { border-collapse: collapse; width: 100%; background: var(--surface); }
table.admin th, table.admin td { padding: 7px 9px; border-bottom: 1px solid var(--line); text-align: left; font-size: .9rem; white-space: nowrap; }
table.admin td.r, table.admin th.r { text-align: right; }
table.admin th { background: var(--surface-2); font-size: .8rem; position: sticky; top: 0; }
.dialog-back { position: fixed; inset: 0; background: rgba(20,30,27,.45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.dialog { background: var(--surface); border-radius: 12px; padding: 18px 20px; width: min(640px, 100%); display: grid; gap: 12px; max-height: calc(100vh - 32px); overflow: auto; }
.dialog textarea { width: 100%; min-height: 170px; border: 1px solid var(--line); border-radius: 6px; padding: 8px; font-family: Consolas, monospace; font-size: .87rem; }
.dialog input { width: 100%; padding: 7px 10px; border: 1px solid var(--line); border-radius: 6px; }
.dialog .actions { display: flex; gap: 8px; justify-content: flex-end; }
.docs { display: grid; gap: 8px; }
.doc { display: grid; grid-template-columns: 32px minmax(0, 1fr) auto; gap: 10px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; }
.doc.is-error { border-color: var(--error); }
.doc .fname { color: var(--muted); font-size: .86rem; word-break: break-all; }
.doc input[type=file] { display: none; }

.print-only { display: none; }

@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .side { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .side button { width: auto; white-space: nowrap; }
  .main { padding: 14px 16px 50px; }
  .search { min-width: 0; width: 100%; }
}

@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; }
  .screen-only { display: none !important; }
  .print-only { display: block; }
  .sheet { page-break-after: always; font-size: 10pt; color: #000; }
  .sheet:last-child { page-break-after: auto; }
  .sheet .unit { font-weight: 700; }
  .sheet h2 { text-align: center; margin: 6px 0; font-size: 12pt; }
  .sheet table { border-collapse: collapse; width: 100%; }
  .sheet th, .sheet td { border: 1px solid #000; padding: 3px 4px; font-size: 8.5pt; vertical-align: top; }
  .sheet thead { display: table-header-group; }
  .sheet tr { page-break-inside: avoid; }
  .sign { display: flex; justify-content: space-between; margin-top: 14px; text-align: center; page-break-inside: avoid; }
  .sign div { min-width: 240px; min-height: 90px; }
}
