:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --line: #000000;
  --text: #000000;
  --muted: #555555;
  --primary: #000000;
  --primary-dark: #222222;
  --ok: #000000;
  --fail: #000000;
  --warn: #444444;
  --radius: 0px;
  --shadow: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #1e1e1e;
    --line: #ffffff;
    --text: #ffffff;
    --muted: #aaaaaa;
    --primary: #ffffff;
    --primary-dark: #dddddd;
    --ok: #ffffff;
    --fail: #ffffff;
    --warn: #cccccc;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
  border-radius: 0 !important;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2 { margin: 0; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; }
h2 { font-size: 1rem; margin-bottom: .75rem; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .7rem 1rem;
  padding-top: max(.7rem, env(safe-area-inset-top));
  background: #000000;
  color: #ffffff;
  border-bottom: 2px solid var(--line);
}

.brand { display: flex; align-items: center; gap: .65rem; min-width: 0; }
.logo { font-size: 1.6rem; line-height: 1; }
.topbar h1 { font-size: 1.05rem; white-space: nowrap; }
.sub { margin: 0; font-size: .78rem; opacity: .85; }
.topbar-actions { display: flex; align-items: center; gap: .4rem; }

.month-input {
  background: #000000;
  border: 1px solid #ffffff;
  color: #ffffff;
  border-radius: 0 !important;
  padding: .35rem .5rem;
  font: inherit;
  font-size: .85rem;
  color-scheme: dark;
}

.icon-btn {
  background: #000000;
  border: 1px solid #ffffff;
  color: #ffffff;
  width: 34px;
  height: 34px;
  border-radius: 0 !important;
  font-size: 1rem;
  cursor: pointer;
}
.icon-btn:hover { background: #333333; }

/* ---------- Tabs ---------- */
.tabs {
  position: sticky;
  top: 56px;
  z-index: 20;
  display: flex;
  gap: .25rem;
  padding: .5rem 1rem 0;
  background: var(--bg);
  border-bottom: 2px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: 0 0 auto;
  background: none;
  border: 2px solid transparent;
  border-bottom: none;
  color: var(--text);
  padding: .55rem .9rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  border-radius: 0 !important;
}
.tab.active {
  color: var(--bg);
  background: var(--line);
  border: 2px solid var(--line);
  border-bottom: none;
  font-weight: 700;
}

main { padding: 1rem; max-width: 1200px; margin: 0 auto; padding-bottom: 3rem; }

.panel { display: none; }
.panel.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

/* ---------- Cards ---------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: .75rem;
  margin-bottom: 1rem;
}

.card {
  background: var(--surface);
  border: 2px solid var(--line);
  border-left: 6px solid var(--line);
  border-radius: 0 !important;
  padding: .85rem 1rem;
  box-shadow: none;
}
.accent-blue  { border-left-color: var(--line); }
.accent-green { border-left-color: var(--line); }
.accent-red   { border-left-color: var(--line); }
.accent-amber { border-left-color: var(--line); }

.card-label { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; }
.card-value { font-size: 1.9rem; font-weight: 800; line-height: 1.2; }
.card-foot { font-size: .78rem; color: var(--muted); }

/* ---------- Box ---------- */
.box {
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: 0 !important;
  padding: 1rem;
  box-shadow: none;
  margin-bottom: 1rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
}
.grid-2 > .box { margin-bottom: 0; }
.grid-2 + .grid-2 { margin-top: 1rem; }

.hint { font-size: .82rem; color: var(--muted); margin: .25rem 0 .75rem; }
.empty { color: var(--muted); font-size: .85rem; text-align: center; padding: 1rem 0; margin: 0; }

/* ---------- Donut ---------- */
.ratio-wrap { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.donut { width: 132px; height: 132px; flex: 0 0 auto; transform: rotate(-90deg); }
.donut-ring { fill: none; stroke: var(--line); stroke-width: 4.5; opacity: .2; }
.donut-seg {
  fill: none;
  stroke: var(--line);
  stroke-width: 4.5;
  stroke-linecap: square;
  transition: stroke-dasharray .5s ease;
}
.donut-num, .donut-cap {
  transform: rotate(90deg);
  transform-origin: 21px 21px;
  text-anchor: middle;
  fill: var(--text);
}
.donut-num { font-size: 7px; font-weight: 800; }
.donut-cap { font-size: 3px; fill: var(--muted); }

.legend { list-style: none; margin: 0; padding: 0; font-size: .88rem; }
.legend li { display: flex; align-items: center; gap: .45rem; padding: .2rem 0; }
.legend b { margin-left: auto; padding-left: 1rem; }
.dot { width: 10px; height: 10px; border-radius: 0 !important; display: inline-block; border: 1px solid var(--line); }
.dot.ok { background: var(--line); }
.dot.fail { background: var(--surface); border: 2px solid var(--line); }
.dot.muted { background: var(--muted); }

/* ---------- Bars ---------- */
.bars { display: flex; align-items: flex-end; gap: 3px; height: 140px; padding-top: .5rem; }
.bar-col { flex: 1 1 0; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; }
.bar {
  background: var(--line);
  border-radius: 0 !important;
  min-height: 3px;
  position: relative;
}
.bar-col span {
  font-size: .6rem;
  color: var(--muted);
  text-align: center;
  margin-top: 3px;
  overflow: hidden;
  white-space: nowrap;
}

/* ---------- Rank list ---------- */
.ranklist { display: flex; flex-direction: column; gap: .35rem; max-height: 290px; overflow-y: auto; }
.rank-item {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .45rem .6rem;
  border: 1px solid var(--line);
  border-radius: 0 !important;
  font-size: .88rem;
}
.rank-item .idx { color: var(--muted); font-size: .78rem; min-width: 1.1rem; }
.rank-item .plate { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rank-item .grow { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-item .meta { margin-left: auto; color: var(--muted); font-size: .8rem; text-align: right; white-space: nowrap; }
.rank-item.danger { border: 2px solid var(--line); background: color-mix(in srgb, var(--line) 10%, transparent); }

.spark { flex: 0 0 70px; height: 6px; background: var(--bg); border: 1px solid var(--line); border-radius: 0 !important; overflow: hidden; }
.spark i { display: block; height: 100%; background: var(--line); border-radius: 0 !important; }

/* ---------- Thống kê theo khu vực ---------- */
.kv-row { margin-bottom: .8rem; }
.kv-head { display: flex; justify-content: space-between; align-items: baseline; font-size: .88rem; gap: .5rem; }
.kv-head span { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.kv-bar { display: flex; height: 9px; border-radius: 0 !important; border: 1px solid var(--line); overflow: hidden; background: var(--bg); margin-top: .3rem; }
.kv-bar i.ok { background: var(--line); }
.kv-bar i.fail { background: var(--surface); }l); }

/* ---------- Forms ---------- */
textarea, input, select {
  width: 100%;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: 0 !important;
  padding: .55rem .7rem;
  font: inherit;
  color: var(--text);
  color-scheme: light dark;
}
textarea { resize: vertical; min-height: 120px; font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: .9rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--line); outline-offset: 1px; border-color: var(--line); }

.field { display: block; margin-bottom: .75rem; }
.field > span { display: block; font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; margin-bottom: .25rem; }
.field.grow { flex: 1 1 220px; }
.field.checkline { display: flex; align-items: center; gap: .5rem; }
.field.checkline input { width: auto; }
.field.checkline span { margin: 0; font-size: .85rem; color: var(--text); }

.row { display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1 1 220px; }
.filters { display: flex; gap: .75rem; flex-wrap: wrap; margin-bottom: .75rem; }
.filters .field { margin-bottom: 0; flex: 1 1 160px; }

.btn-row { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .5rem; }

.btn {
  border: 2px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 0 !important;
  padding: .55rem 1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  text-transform: uppercase;
  font-size: .82rem;
  letter-spacing: .02em;
}
.btn.primary { background: var(--line); color: var(--bg); border-color: var(--line); }
.btn.primary:hover:not(:disabled) { opacity: .85; }
.btn.ghost:hover { background: var(--bg); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.small { padding: .3rem .6rem; font-size: .78rem; }

.chip {
  display: inline-block;
  background: var(--line);
  color: var(--bg);
  border-radius: 0 !important;
  padding: .05rem .55rem;
  font-size: .75rem;
  font-weight: 700;
  vertical-align: middle;
}

/* ---------- Table ---------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tbl { width: 100%; border-collapse: collapse; font-size: .88rem; min-width: 640px; border: 1px solid var(--line); }
.tbl th, .tbl td { padding: .55rem .65rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl th { font-size: .75rem; color: var(--text); background: var(--bg); font-weight: 700; text-transform: uppercase; letter-spacing: .5px; white-space: nowrap; border-bottom: 2px solid var(--line); }
.tbl tbody tr:hover { background: color-mix(in srgb, var(--line) 6%, transparent); }
.tbl td.plate { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl input, .tbl select { border: 1px solid transparent; background: transparent; padding: .3rem .4rem; font-size: .85rem; border-radius: 0 !important; }
.tbl input:hover, .tbl select:hover { border-color: var(--line); }
.tbl td.dim { color: var(--muted); font-size: .8rem; line-height: 1.3; }
.tbl td.dim small { opacity: .8; }
.tbl tr.warn-row { background: color-mix(in srgb, var(--line) 8%, transparent); }

.badge { display: inline-block; padding: .15rem .6rem; border-radius: 0 !important; font-size: .75rem; font-weight: 700; white-space: nowrap; text-transform: uppercase; letter-spacing: .02em; border: 1px solid var(--line); }
.badge.ok { background: var(--line); color: var(--bg); }
.badge.fail { background: var(--surface); color: var(--text); border: 2px solid var(--line); }
.badge.open { background: var(--bg); color: var(--text); border: 1px dashed var(--line); }

.link-btn { background: none; border: none; color: var(--text); font: inherit; font-weight: 600; cursor: pointer; padding: .2rem; text-decoration: underline; }
.link-btn.danger { color: var(--text); text-decoration: underline; }

/* ---------- Export preview ---------- */
.export-preview { margin-top: 1rem; font-size: .88rem; }
.export-preview table { width: 100%; border-collapse: collapse; }
.export-preview td { padding: .35rem 0; border-bottom: 1px dashed var(--line); }
.export-preview td:last-child { text-align: right; font-weight: 700; }

/* ---------- Modal / toast / loading ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, .7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal[hidden] { display: none; }
.modal-box {
  background: var(--surface);
  border: 3px solid var(--line);
  border-radius: 0 !important;
  padding: 1.25rem;
  width: 100%;
  max-width: 460px;
  box-shadow: none;
  max-height: 90vh;
  overflow-y: auto;
}
.status { font-size: .82rem; margin: .5rem 0 0; font-weight: 600; }
.status.ok { color: var(--text); }
.status.err { color: var(--text); text-decoration: underline; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  z-index: 80;
  background: var(--line);
  color: var(--bg);
  padding: .65rem 1.1rem;
  border-radius: 0 !important;
  border: 2px solid var(--line);
  font-size: .88rem;
  font-weight: 600;
  max-width: 92vw;
  box-shadow: none;
}
.toast[hidden] { display: none; }
.toast.err { background: var(--surface); color: var(--text); border: 2px solid var(--line); }
.toast.ok { background: var(--line); color: var(--bg); }

.loading { position: fixed; inset: 0; z-index: 70; background: rgba(0, 0, 0, .4); display: grid; place-items: center; }
.loading[hidden] { display: none; }
.spinner {
  width: 38px; height: 38px;
  border: 4px solid var(--bg);
  border-top-color: var(--line);
  border-radius: 0 !important;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  main { padding: .75rem; }
  .topbar { padding: .6rem .75rem; }
  .topbar h1 { font-size: .95rem; }
  .tabs { top: 52px; padding: .4rem .5rem 0; }
  .tab { padding: .5rem .6rem; font-size: .85rem; }
  .card-value { font-size: 1.55rem; }
  .cards { grid-template-columns: repeat(2, 1fr); gap: .55rem; }
  .month-input { max-width: 130px; font-size: .78rem; }
  .sub .upd { display: none; }
  .donut { width: 108px; height: 108px; margin: 0 auto; }
  .ratio-wrap { justify-content: center; }
  .legend { width: 100%; }
}

/* ---------- Kho vật tư ---------- */
.kho-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
}

.kho-name { font-weight: 550; font-size: .85rem; line-height: 1.3; }

.kho-qty {
  white-space: nowrap;
  text-align: center !important;
}

.kho-val {
  display: inline-block;
  min-width: 24px;
  text-align: center;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  font-size: .95rem;
}

.kho-total {
  font-weight: 700;
  font-size: 1rem;
  text-align: center !important;
}

.kho-ok .kho-val { color: var(--text); font-weight: 700; }
.kho-low .kho-val { color: var(--text); font-weight: 700; }
.kho-zero .kho-val,
.kho-zero { color: var(--muted); }
.kho-zero .kho-val { opacity: .7; }

.adj-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 0 !important;
  font-size: .9rem;
  font-weight: 700;
  cursor: pointer;
  vertical-align: middle;
}
.adj-btn:hover { background: var(--line); color: var(--bg); }
.adj-btn:active { transform: scale(.92); }

/* ---------- Detail Modal ---------- */
.detail-box {
  max-width: 520px;
}
.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .75rem;
  margin-bottom: 1rem;
  padding-bottom: .75rem;
  border-bottom: 2px solid var(--line);
}
.detail-header h2 {
  font-size: 1.3rem;
  font-weight: 700;
  margin: 0;
  color: var(--text);
}
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .75rem;
  margin-bottom: 1.25rem;
}
.detail-item {
  display: flex;
  flex-direction: column;
  gap: .2rem;
  font-size: .88rem;
  background: var(--surface);
  padding: .55rem .75rem;
  border-radius: 0 !important;
  border: 1px solid var(--line);
}
.detail-item.full-width {
  grid-column: 1 / -1;
}
.detail-item span {
  font-size: .75rem;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .03em;
}
.detail-item strong {
  font-weight: 600;
  word-break: break-word;
}

.plate-cell {
  cursor: pointer;
}
.plate-cell:hover {
  text-decoration: underline;
}

/* ---------- Mobile Layout Adaptations ---------- */
@media (max-width: 640px) {
  html, body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
  }

  main {
    padding: .5rem;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .box {
    padding: .75rem;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  .filters {
    flex-direction: column;
    gap: .5rem;
    width: 100%;
  }

  .filters .field {
    flex: 1 1 100%;
    width: 100%;
    margin-bottom: .25rem;
  }

  .table-scroll {
    overflow-x: hidden;
    width: 100%;
  }

  .tbl {
    min-width: 0 !important;
    width: 100% !important;
    table-layout: fixed;
  }

  /* 1. Mục Kho thiết bị dự phòng: Chỉ hiển thị Tên thiết bị và Số lượng tồn kho */
  #khoTable th:nth-child(1), /* STT */
  #khoTable th:nth-child(3), /* Mã thiết bị */
  #khoTable th.region-th,    /* Cột các vùng */
  #khoTable th:last-child,   /* Thao tác */
  #khoTable td:nth-child(1),
  #khoTable td:nth-child(3),
  #khoTable td.region-td,
  #khoTable td:last-child {
    display: none !important;
  }

  #khoTable th:nth-child(2) {
    display: table-cell !important;
    width: 70%;
  }
  #khoTable th.th-tong {
    display: table-cell !important;
    text-align: right !important;
    width: 30%;
  }
  #khoTable td.kho-name {
    display: table-cell !important;
    padding: .75rem .4rem;
    font-size: .88rem;
    word-break: break-word;
    white-space: normal;
    cursor: pointer;
  }
  #khoTable td.kho-total {
    display: table-cell !important;
    text-align: right !important;
    padding: .75rem .4rem;
    font-size: 1.05rem;
    font-weight: 700;
    cursor: pointer;
  }

  /* 2. Mục Danh sách: Chỉ hiển thị Biển số xe và Tình trạng hoàn thành */
  #listTable th,
  #listTable td {
    display: none !important;
  }

  #listTable th:first-child,
  #listTable td.plate-cell {
    display: table-cell !important;
    width: 55%;
    padding: .75rem .4rem;
    font-size: .92rem;
    font-weight: 700;
    color: var(--primary);
    word-break: break-word;
    white-space: normal;
  }

  #listTable th.th-result,
  #listTable td.badge-cell {
    display: table-cell !important;
    text-align: right !important;
    width: 45%;
    padding: .75rem .4rem;
    cursor: pointer;
  }

  .detail-grid {
    grid-template-columns: 1fr;
    gap: .55rem;
  }

  /* 3. Mục Nhật ký nhập xuất: Tối ưu trên mobile không bị cuộn ngang */
  #logTable th,
  #logTable td {
    display: none !important;
  }
  #logTable th:nth-child(3),
  #logTable th:nth-child(5),
  #logTable th:nth-child(6),
  #logTable td.td-log-loai,
  #logTable td.td-log-ten,
  #logTable td.td-log-qty {
    display: table-cell !important;
  }
  #logTable th:nth-child(3) { width: 25%; }
  #logTable th:nth-child(5) { width: 55%; }
  #logTable th:nth-child(6) { width: 20%; text-align: right; }
  #logTable td.td-log-qty { text-align: right; font-weight: 700; }
}

.badge.badge-nhap {
  border: 1px solid var(--line);
  background: var(--line);
  color: var(--bg);
  font-weight: 700;
}
.badge.badge-xuat {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--line);
  font-weight: 700;
}

