/* Mobile-first: viết cho màn hình điện thoại trước, mở rộng dần ở cuối file. */
:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-hover: #115e59;
  --primary-ink: #ffffff;
  --primary-soft: #ccfbf1;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --success: #15803d;
  --success-soft: #dcfce7;
  --info: #1d4ed8;
  --info-soft: #dbeafe;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 1px 3px rgb(15 23 42 / 8%);
  --topbar-h: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2b;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #1f2a3d;
    --primary: #2dd4bf;
    --primary-hover: #5eead4;
    --primary-ink: #042f2e;
    --primary-soft: #134e4a;
    --danger: #f87171;
    --danger-soft: #450a0a;
    --success: #4ade80;
    --success-soft: #052e16;
    --info: #93c5fd;
    --info-soft: #172554;
    --shadow: none;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary); text-decoration: none; }
h1, h2, p { margin: 0; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

/* ---------- Thanh trên ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  background: var(--primary);
  color: var(--primary-ink);
}
.brand {
  flex: 1;
  min-width: 0;
  padding: 0 8px;
  overflow: hidden;
  color: inherit;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: inherit;
  cursor: pointer;
  list-style: none;
}
.icon-btn::-webkit-details-marker { display: none; }
.icon-btn:active { background: rgb(255 255 255 / 18%); }

.avatar {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgb(255 255 255 / 22%);
  font-weight: 600;
}
.avatar--lg {
  width: 42px;
  height: 42px;
  background: var(--primary-soft);
  color: var(--primary);
}

.user-menu { position: relative; }
.user-menu__panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: min(280px, calc(100vw - 24px));
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 30px rgb(15 23 42 / 18%);
}
.user-menu__name { font-weight: 600; }
.user-menu__sub { margin-bottom: 12px; color: var(--muted); font-size: 14px; }

/* ---------- Khung trang ---------- */
.page {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.page--bare { display: grid; min-height: 100dvh; place-items: center; }

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.hello { margin: 4px 0 20px; }
.hello__name { font-size: 24px; line-height: 1.25; }

.section-title {
  margin: 20px 0 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.section-actions { display: flex; gap: 4px; margin-top: 10px; }

/* ---------- Ô báo cáo trên trang chủ ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.tile {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 112px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform .08s ease, border-color .15s ease;
}
.tile:active { transform: scale(.98); }
.tile__icon {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--primary-soft);
  color: var(--primary);
}
.tile__name { font-weight: 600; line-height: 1.3; }
.tile--admin .tile__icon { background: var(--info-soft); color: var(--info); }

/* ---------- Form ---------- */
.field { display: block; margin-bottom: 16px; }
.field__label { display: block; margin-bottom: 6px; font-size: 14px; font-weight: 600; }
.input {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* >=16px để iOS không tự phóng to */
}
.input:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: transparent; }
.input-group { position: relative; display: block; }
.input-group .input { padding-right: 72px; }
.input-group__btn {
  position: absolute;
  top: 0;
  right: 0;
  height: 48px;
  padding: 0 14px;
  border: 0;
  background: none;
  color: var(--primary);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 18px;
  border: 1px solid transparent;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { border-color: var(--border); background: transparent; color: var(--text); }
.btn-block { width: 100%; }
.btn-lg { min-height: 52px; font-size: 17px; }
.link-btn {
  min-height: 36px;
  padding: 4px 10px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--primary);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.alert { margin-bottom: 16px; padding: 12px 14px; border-radius: 10px; font-size: 15px; }
.alert-danger { background: var(--danger-soft); color: var(--danger); }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-info { background: var(--info-soft); color: var(--info); }

/* ---------- Đăng nhập ---------- */
.login { width: 100%; max-width: 400px; padding: 24px 16px; text-align: center; }
.login__logo {
  display: inline-grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin-bottom: 12px;
  border-radius: 18px;
  background: var(--primary);
  color: var(--primary-ink);
}
.login__title { font-size: 24px; }
.login__card { margin-top: 24px; padding: 20px; text-align: left; }

/* ---------- Danh sách ---------- */
.search { margin-bottom: 12px; }
.list { margin: 0; padding: 0; list-style: none; overflow: hidden; }
.list > li + li { border-top: 1px solid var(--border); }
.list__row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 10px 16px;
  color: var(--text);
}
.list__row:active { background: var(--bg); }
.list__main { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.list__title { font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.list__sub { color: var(--muted); font-size: 14px; }
.list__meta { flex: none; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-primary { background: var(--primary-soft); color: var(--primary); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

.profile { display: flex; align-items: center; gap: 12px; padding: 14px 16px; }
.profile > div { flex: 1; min-width: 0; }

/* Công tắc bật/tắt quyền */
.switch-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 8px 16px;
  cursor: pointer;
}
.switch-row__icon { display: inline-grid; color: var(--primary); }
.switch-row__icon svg { width: 22px; height: 22px; }
.switch-row__label { flex: 1; font-weight: 500; }
.switch {
  position: relative;
  flex: none;
  width: 50px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: var(--border);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  transition: background .15s ease;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
  transition: transform .15s ease;
}
.switch:checked { background: var(--primary); }
.switch:checked::after { transform: translateX(20px); }
.switch:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Thanh lưu dính đáy màn hình */
.with-savebar { padding-bottom: 88px; }
.savebar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 12px max(16px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.savebar .btn { display: flex; max-width: 928px; margin: 0 auto; }

.empty { padding: 32px 20px; text-align: center; }
.empty p + p { margin-top: 6px; }
.empty .btn { margin-top: 20px; }
.empty__icon { display: inline-grid; margin-bottom: 12px; color: var(--primary); }

/* ---------- Báo cáo: bộ lọc ---------- */
.filters { margin-bottom: 16px; }
.filters__summary {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 10px 16px;
  cursor: pointer;
  list-style: none;
}
.filters__summary::-webkit-details-marker { display: none; }
.filters__summary::after {
  content: "";
  flex: none;
  width: 10px;
  height: 10px;
  margin-right: 4px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform .15s ease;
}
.filters[open] > .filters__summary::after { transform: rotate(-135deg); }
.filters__icon { display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; background: var(--primary-soft); color: var(--primary); flex: none; }
.filters__text { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.filters__title { font-weight: 600; }
.filters__desc { color: var(--muted); font-size: 14px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.filters__form { padding: 16px; border-top: 1px solid var(--border); }
.filters__form .list--flat { margin-bottom: 16px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.grid-2 .field { margin-bottom: 16px; }
select.input {
  padding-right: 36px;
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0h10L5 6z' fill='%2364748b'/%3E%3C/svg%3E") no-repeat right 16px center;
}
.input--sm { min-height: 42px; margin-bottom: 8px; }
.list--flat { box-shadow: none; }
.switch-row__label small { display: block; color: var(--muted); font-size: 13px; font-weight: 400; }

/* Danh sách chọn nhiều (kho, nhóm hàng) */
.picker__summary { display: flex; align-items: center; cursor: pointer; list-style: none; }
.picker__summary::-webkit-details-marker { display: none; }
.picker__summary span { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.picker__summary::after { content: "▾"; margin-left: 8px; color: var(--muted); }
.picker__panel { margin-top: 6px; padding: 8px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); }
.picker__list { max-height: 280px; margin: 0; padding: 0; overflow-y: auto; list-style: none; overscroll-behavior: contain; }
.picker__empty { padding: 8px; }
.check-row { display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 4px 8px; border-radius: 8px; cursor: pointer; }
.check-row:active { background: var(--surface); }
.check-row input { flex: none; width: 20px; height: 20px; margin: 0; accent-color: var(--primary); }
.is-off { display: none !important; }

/* ---------- Báo cáo: kết quả ---------- */
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.stat { display: flex; flex-direction: column; padding: 12px 14px; }
.stat--wide { grid-column: 1 / -1; }
.stat__label { color: var(--muted); font-size: 13px; }
.stat__value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.3; overflow-wrap: anywhere; }

.search--sticky { position: sticky; top: calc(var(--topbar-h) + env(safe-area-inset-top)); z-index: 5; margin: 0 -4px 12px; padding: 4px; background: var(--bg); }

.rows > li { content-visibility: auto; contain-intrinsic-size: auto 96px; }
.row { display: flex; flex-direction: column; gap: 2px; padding: 12px 16px; color: var(--text); }
a.row:active { background: var(--bg); }
.row__head { display: flex; align-items: baseline; gap: 12px; }
.row__title { flex: 1; min-width: 0; font-weight: 600; line-height: 1.35; }
.row__qty { flex: none; font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.row__sub { color: var(--muted); font-size: 14px; }
.row__nums { display: flex; flex-wrap: wrap; gap: 2px 14px; margin-top: 4px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.row__nums b { color: var(--text); font-weight: 600; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-wrap: anywhere; }
.neg { color: var(--danger); }
.pos { color: var(--success); }

.detail-head { margin-bottom: 12px; padding: 14px 16px; }
.detail-head__title { font-size: 19px; line-height: 1.35; }
.detail-head .muted { font-size: 14px; }

/* ---------- Màn hình lớn hơn ---------- */
@media (min-width: 640px) {
  .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .page { padding-top: 24px; }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stat--wide { grid-column: auto; }
}
@media (min-width: 960px) {
  .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .tile:hover { border-color: var(--primary); }
  .list__row:hover { background: var(--bg); }
}
