:root {
  --bg: #f4f5f2; --card: #fff; --ink: #1d2320; --muted: #66706b; --line: #dfe3dd;
  --brand: #1f5f4a; --brand-2: #2c7a60; --brand-soft: #e3efe9;
  --warn: #9a5b00; --warn-bg: #fff4dc; --bad: #b3261e; --bad-bg: #fde8e6; --ok: #1f6f3f; --ok-bg: #e2f3e7;
  --info: #24527a; --info-bg: #e5eef7;
  --r: 12px; --tap: 48px;
  font-size: 16px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; line-height: 1.45; }
body { -webkit-text-size-adjust: 100%; }
a { color: var(--brand); }
h1 { font-size: 1.35rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 1.2rem 0 .5rem; }
h3 { font-size: 1rem; margin: .8rem 0 .4rem; }
.boot { padding: 40px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .86rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1 1 auto; }
.stack > * + * { margin-top: 10px; }
.hidden { display: none !important; }

/* ===== Khung ===== */
.shell { display: flex; min-height: 100vh; }
.side { display: none; }
.main { flex: 1; min-width: 0; padding: 12px 12px 96px; max-width: 1400px; margin: 0 auto; width: 100%; }
.topbar { position: sticky; top: 0; z-index: 20; background: var(--brand); color: #fff; display: flex; align-items: center; gap: 10px; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); }
.topbar .title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar button { background: transparent; border: 0; color: #fff; font-size: 1rem; min-height: 40px; padding: 0 8px; cursor: pointer; }
.bottomnav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; background: #fff; border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(5, 1fr); padding-bottom: env(safe-area-inset-bottom); }
.bottomnav a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 60px; text-decoration: none; color: var(--muted); font-size: .74rem; }
.bottomnav a .ic { font-size: 1.35rem; line-height: 1; }
.bottomnav a.on { color: var(--brand); font-weight: 600; }
.drawer { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); display: none; }
.drawer.open { display: block; }
.drawer nav { background: #fff; width: min(320px, 86vw); height: 100%; overflow: auto; padding: 12px 0; }
.navlink { display: flex; align-items: center; gap: 12px; padding: 13px 18px; color: var(--ink); text-decoration: none; font-size: 1rem; }
.navlink .ic { width: 24px; text-align: center; }
.navlink.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.navlink .badge { margin-left: auto; }
.navsep { border-top: 1px solid var(--line); margin: 8px 0; }

@media (min-width: 900px) {
  .topbar, .bottomnav { display: none; }
  .side { display: block; width: 240px; flex: 0 0 240px; background: #fff; border-right: 1px solid var(--line); position: sticky; top: 0; height: 100vh; overflow: auto; }
  .side .brand { padding: 18px; font-weight: 700; color: var(--brand); font-size: 1.1rem; }
  .side .me { padding: 12px 18px; border-top: 1px solid var(--line); font-size: .9rem; }
  .main { padding: 20px 28px 40px; }
}

/* ===== Thẻ, nút, ô nhập ===== */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; }
.card + .card { margin-top: 10px; }
.card.tight { padding: 10px 12px; }
.pagehead { display: flex; align-items: flex-start; gap: 10px; flex-wrap: wrap; margin: 4px 0 12px; }
.pagehead .grow { min-width: 180px; }
button, .btn { font: inherit; border-radius: 10px; border: 1px solid var(--line); background: #fff; color: var(--ink); min-height: var(--tap); padding: 0 16px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--brand-2); }
.btn-danger { color: var(--bad); border-color: #e9b9b5; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--brand); }
.btn-sm { min-height: 36px; padding: 0 10px; font-size: .9rem; }
.btn-block { width: 100%; }
.btn-big { min-height: 56px; font-size: 1.05rem; }
label.f { display: block; font-size: .88rem; color: var(--muted); }
label.f > span { display: block; margin-bottom: 4px; }
input, select, textarea { font: inherit; width: 100%; min-height: var(--tap); padding: 10px 12px; border: 1px solid #c9cfc8; border-radius: 10px; background: #fff; color: var(--ink); }
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: 0; border-color: var(--brand-2); }
input[type=checkbox] { width: 22px; min-height: 22px; height: 22px; accent-color: var(--brand); }
.check { display: flex; align-items: center; gap: 10px; min-height: var(--tap); }
input.numin { text-align: right; font-variant-numeric: tabular-nums; }
.grid { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
.grid.g3 { grid-template-columns: 1fr 1fr 1fr; }
.grid .full { grid-column: 1 / -1; }
@media (min-width: 700px) { .grid.g4 { grid-template-columns: repeat(4, 1fr); } .grid.g6 { grid-template-columns: repeat(6, 1fr); } }
@media (max-width: 420px) { .grid.g3 { grid-template-columns: 1fr 1fr; } }
.req::after { content: " *"; color: var(--bad); }

/* ===== Nhãn trạng thái ===== */
.tag { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: .8rem; font-weight: 600; background: #eef0ed; color: #47504b; white-space: nowrap; }
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }
.tag.info { background: var(--info-bg); color: var(--info); }
.badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: .75rem; padding: 1px 7px; font-weight: 700; }
.note { border-radius: 10px; padding: 10px 12px; font-size: .92rem; }
.note.warn { background: var(--warn-bg); color: #5b3a00; }
.note.bad { background: var(--bad-bg); color: #6d1712; }
.note.ok { background: var(--ok-bg); color: #164d2c; }
.note.info { background: var(--info-bg); color: #1b3d5c; }
.note ul { margin: 4px 0 0; padding-left: 18px; }

/* ===== Danh sách & bảng ===== */
.list { display: flex; flex-direction: column; gap: 8px; }
.item { background: #fff; border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px; display: block; color: inherit; text-decoration: none; }
.item:active { background: #f7f9f6; }
.item .t { font-weight: 600; }
.item .s { color: var(--muted); font-size: .88rem; }
.tablewrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--r); }
table.t { border-collapse: collapse; width: 100%; font-size: .92rem; }
table.t th, table.t td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; text-align: left; }
table.t th { background: #f6f7f4; font-weight: 600; color: #3d4641; position: sticky; top: 0; white-space: nowrap; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #f6faf8; }
table.t td.num, table.t th.num { text-align: right; }
table.t tfoot td { font-weight: 700; background: #f6f7f4; }
.only-desk { display: none; }
@media (min-width: 900px) { .only-desk { display: block; } .only-mob { display: none !important; } }
.filters { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; margin-bottom: 12px; }
.filters .full { grid-column: 1 / -1; }
@media (min-width: 900px) { .filters { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); } .filters .full { grid-column: span 2; } }

/* ===== Thống kê tổng quan ===== */
.tiles { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
@media (min-width: 900px) { .tiles { grid-template-columns: repeat(3, 1fr); } }
.tile { background: #fff; border: 1px solid var(--line); border-radius: var(--r); padding: 14px; text-decoration: none; color: inherit; display: block; }
.tile .k { color: var(--muted); font-size: .88rem; }
.tile .v { font-size: 1.6rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .v.small { font-size: 1.2rem; }
.tile.alert .v { color: var(--bad); }
.tile .sub { font-size: .82rem; color: var(--muted); margin-top: 4px; }

/* ===== Phiếu: dòng hàng ===== */
.line-card { border: 1px solid var(--line); border-radius: var(--r); padding: 12px; background: #fff; }
.line-card + .line-card { margin-top: 10px; }
.line-card .lhead { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.line-card .lno { background: var(--brand-soft); color: var(--brand); border-radius: 8px; padding: 2px 8px; font-weight: 700; font-size: .85rem; }
.calcbox { background: #f6f8f5; border-radius: 10px; padding: 10px 12px; font-size: .9rem; margin-top: 10px; }
.calcbox .big { font-weight: 700; }
.calcbox .steps { color: var(--muted); font-size: .82rem; margin-top: 4px; }
.counts { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 10px; }
.countrow { display: grid; grid-template-columns: 1fr 90px 40px; gap: 6px; margin-bottom: 6px; }
.countrow.wide { grid-template-columns: 1fr 90px 130px 40px; }
.sugg { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.sugg button { min-height: 34px; font-size: .85rem; padding: 0 10px; border-radius: 999px; background: var(--info-bg); border-color: transparent; color: var(--info); }
.sugg button.on { background: var(--ok-bg); color: var(--ok); }
.totalbar { position: sticky; bottom: 66px; z-index: 10; background: #fff; border: 1px solid var(--line); border-radius: var(--r); padding: 10px 12px; margin-top: 12px; box-shadow: 0 4px 18px rgba(0,0,0,.07); }
@media (min-width: 900px) { .totalbar { bottom: 10px; } }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.actions > * { flex: 1 1 160px; }

/* ===== Ảnh ===== */
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.thumb { width: 92px; height: 92px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: #f0f2ee; display: flex; align-items: center; justify-content: center; position: relative; font-size: .78rem; text-align: center; color: var(--muted); text-decoration: none; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .x { position: absolute; top: 2px; right: 2px; min-height: 26px; width: 26px; padding: 0; border-radius: 50%; font-size: .8rem; background: rgba(255,255,255,.9); }
.thumb .kind { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.55); color: #fff; font-size: .7rem; padding: 1px 3px; }
.uploader { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.split { display: grid; gap: 12px; }
@media (min-width: 1000px) { .split { grid-template-columns: minmax(320px, 42%) 1fr; align-items: start; } .split .sticky { position: sticky; top: 16px; } }
.viewer img { max-width: 100%; border-radius: 10px; border: 1px solid var(--line); display: block; margin-bottom: 8px; }
.viewer iframe { width: 100%; height: 70vh; border: 1px solid var(--line); border-radius: 10px; }
.viewer pre { white-space: pre-wrap; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-size: .85rem; max-height: 60vh; overflow: auto; }
.uncertain { background: var(--warn-bg) !important; border-color: #e5c27a !important; }
.missing { background: var(--bad-bg) !important; }

/* ===== Hộp thoại & thông báo ===== */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.modal .box { background: #fff; width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 18px; padding-bottom: max(18px, env(safe-area-inset-bottom)); }
@media (min-width: 700px) { .modal { align-items: center; } .modal .box { border-radius: 16px; } }
#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 82px; z-index: 80; display: flex; flex-direction: column; gap: 6px; width: min(92vw, 460px); pointer-events: none; }
#toast .m { background: #1d2320; color: #fff; border-radius: 10px; padding: 12px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.2); pointer-events: auto; }
#toast .m.ok { background: var(--ok); }
#toast .m.bad { background: var(--bad); }
@media (min-width: 900px) { #toast { bottom: 24px; } }
.auth { max-width: 420px; margin: 8vh auto; padding: 0 16px; }
.auth .logo { text-align: center; color: var(--brand); font-weight: 800; font-size: 1.5rem; margin-bottom: 16px; }
.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; padding-bottom: 2px; }
.tabs a { white-space: nowrap; padding: 8px 14px; border-radius: 999px; background: #fff; border: 1px solid var(--line); text-decoration: none; color: var(--ink); font-size: .92rem; }
.tabs a.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.hist { font-size: .85rem; }
.hist .e { padding: 8px 0; border-bottom: 1px solid var(--line); }
.hist details { margin-top: 4px; }
.hist pre { white-space: pre-wrap; font-size: .75rem; background: #f6f7f4; padding: 6px; border-radius: 6px; max-height: 240px; overflow: auto; }
.diff-pos { color: var(--bad); font-weight: 600; }
.diff-neg { color: var(--ok); font-weight: 600; }
.offline { background: var(--bad); color: #fff; text-align: center; padding: 6px; font-size: .88rem; }

.span2 { grid-column: span 2; }
/* Lưới tự co giãn, không tràn ngang */
.grid > *, .countrow > *, .row > * { min-width: 0; }
@media (min-width: 700px) {
  .grid.g4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
  .grid.g6 { grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); }
}
.main { overflow-x: hidden; }
@media (max-width: 600px) {
  .countrow { grid-template-columns: 1fr 1fr 40px !important; }
  .countrow > :first-child { grid-column: 1 / -1; }
}
