/* ===== Màu & kích thước chung ===== */
:root {
  --bg: #f4f5f2;
  --card: #ffffff;
  --text: #1d211b;
  --muted: #6a7166;
  --border: #dfe3db;
  --primary: #2f6b34;
  --primary-hover: #26582a;
  --primary-soft: #e7f1e7;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #9a5b00;
  --warn-soft: #fff4dc;
  --ok: #23803a;
  --radius: 12px;
  --topbar-h: 56px;
  --actionbar-h: 64px;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 12px;
}
h2 {
  font-size: 1.1rem;
  margin: 0 0 12px;
}
h3 {
  font-size: 1rem;
  margin: 0 0 8px;
}
p {
  margin: 0 0 10px;
}
a {
  color: var(--primary);
}

.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85rem;
}
.center {
  text-align: center;
}
.text-danger {
  color: var(--danger);
}
.ok {
  color: var(--ok);
  font-weight: 600;
}
.req {
  color: var(--danger);
}
.upper {
  text-transform: uppercase;
}
.upper::placeholder {
  text-transform: none;
}
.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0;
}
.loading {
  padding: 48px 16px;
  text-align: center;
  color: var(--muted);
}

/* ===== Thanh trên cùng ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--primary);
  color: #fff;
}
.topbar[hidden] {
  display: none;
}
.brand {
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}
.nav {
  display: flex;
  gap: 4px;
  flex: 1;
}
.nav a {
  color: #e4efe4;
  text-decoration: none;
  padding: 8px 12px;
  border-radius: 8px;
  font-weight: 600;
  white-space: nowrap;
}
.nav a.active,
.nav a:hover {
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}
.me {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  white-space: nowrap;
}
.link {
  background: none;
  border: none;
  color: #fff;
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
  padding: 4px;
}

@media (max-width: 720px) {
  .topbar {
    gap: 8px;
    padding: 0 10px;
  }
  .brand {
    display: none;
  }
  .nav a {
    padding: 8px 9px;
    font-size: 0.92rem;
  }
  #me-name {
    display: none;
  }
}

/* ===== Khung trang ===== */
.page {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}
.page.narrow {
  max-width: 720px;
}
.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.page-head h1 {
  margin: 0;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

/* ===== Form ===== */
label {
  display: block;
  font-weight: 600;
  font-size: 0.92rem;
  margin-bottom: 12px;
}
label small {
  font-weight: 400;
}
input,
select,
textarea {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 10px 12px;
  font: inherit;
  font-weight: 400;
  color: var(--text);
  background: #fff;
  border: 1px solid #cfd5ca;
  border-radius: 10px;
  min-height: 44px;
}
textarea {
  resize: vertical;
  font-variant-numeric: tabular-nums;
  line-height: 1.6;
}
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: var(--primary);
}
input:disabled,
select:disabled,
textarea:disabled {
  background: #f1f2ef;
  color: #555;
}
fieldset {
  border: none;
  margin: 0;
  padding: 0;
  min-width: 0;
}
label.check,
label.check-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-weight: 500;
  cursor: pointer;
}
label.check input,
label.check-row input {
  width: 22px;
  height: 22px;
  min-height: 0;
  margin: 1px 0 0;
  flex: none;
  accent-color: var(--primary);
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 14px;
}
.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0 12px;
}
@media (max-width: 560px) {
  .grid-2 {
    grid-template-columns: 1fr;
  }
  .grid-3 {
    grid-template-columns: 1fr 1fr;
  }
  .grid-3 label:last-child {
    grid-column: 1 / -1;
  }
}
.form-error {
  color: var(--danger);
  background: var(--danger-soft);
  padding: 8px 12px;
  border-radius: 8px;
}

/* ===== Nút ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: #fff;
  border: 1px solid #cfd5ca;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover {
  background: #f5f7f3;
}
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover {
  background: var(--primary-hover);
}
.btn.primary.is-disabled {
  background: #9aa597;
  border-color: #9aa597;
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn.block {
  display: flex;
  width: 100%;
}
.btn.dashed {
  border-style: dashed;
  color: var(--primary);
  margin-bottom: 14px;
}
.btn.small {
  min-height: 36px;
  padding: 6px 10px;
  font-size: 0.88rem;
}
.btn.danger-outline {
  color: var(--danger);
  border-color: #e7b7b1;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
}
.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

/* ===== Nhãn trạng thái ===== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}
.badge-draft {
  background: #eceee9;
  color: #555b52;
}
.badge-checked {
  background: #e3effc;
  color: #1d5aa6;
}
.badge-exported {
  background: var(--primary-soft);
  color: var(--primary);
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  background: #eef1eb;
  font-size: 0.85rem;
  font-weight: 600;
}
.chip button {
  border: none;
  background: none;
  cursor: pointer;
  color: var(--muted);
  padding: 0 0 0 2px;
  font-size: 0.85rem;
}

/* ===== Thông báo nhỏ ===== */
.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(var(--actionbar-h) + 16px);
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 480px);
  pointer-events: none;
}
.toast {
  background: #2b2f29;
  color: #fff;
  padding: 12px 16px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  transition: opacity 0.3s, transform 0.3s;
}
.toast-success {
  background: var(--ok);
}
.toast-error {
  background: var(--danger);
}
.toast-warn {
  background: #8a5300;
}
.toast.hide {
  opacity: 0;
  transform: translateY(8px);
}

/* ===== Đăng nhập ===== */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px 16px;
}
.auth-card {
  width: min(100%, 400px);
  padding: 28px 24px;
}
.auth-logo {
  font-size: 2.4rem;
}

/* ===== Danh sách phiếu ===== */
.filters {
  display: grid;
  grid-template-columns: 1fr 170px 180px;
  gap: 10px;
  padding: 12px;
}
.filters input,
.filters select {
  margin: 0;
}
@media (max-width: 640px) {
  .filters {
    grid-template-columns: 1fr 1fr;
  }
  .filters input[type='search'] {
    grid-column: 1 / -1;
  }
}
.ticket-list {
  display: grid;
  gap: 10px;
  margin-top: 8px;
}
.ticket-item {
  display: block;
  color: inherit;
  text-decoration: none;
  margin: 0;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.ticket-item:hover {
  border-color: #b9c4b4;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
.ti-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.ti-customer {
  font-size: 1.1rem;
  font-weight: 700;
  margin: 4px 0;
}
.ti-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 0.9rem;
  color: #3d4339;
}
.ti-lines {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.ti-foot {
  margin-top: 6px;
}
.empty {
  text-align: center;
  padding: 32px 16px;
}

/* ===== Tạo phiếu ===== */
.big-choice {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  text-align: left;
  font: inherit;
  cursor: pointer;
  padding: 20px;
  margin-bottom: 12px;
  border-width: 2px;
}
.big-choice:hover {
  border-color: var(--primary);
}
.big-choice strong {
  display: block;
  font-size: 1.1rem;
}
.big-choice .muted {
  font-weight: 400;
}
.bc-icon {
  font-size: 2.2rem;
}
.note {
  background: var(--primary-soft);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.92rem;
}
.upload-list li {
  margin-bottom: 6px;
}

/* ===== MÀN HÌNH ĐỐI CHIẾU ===== */
body[data-page] main {
  min-height: calc(100vh - var(--topbar-h));
}
.edit {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  padding-bottom: calc(var(--actionbar-h) + 24px);
}
.edit-media {
  position: sticky;
  top: var(--topbar-h);
  z-index: 10;
  background: #20231f;
  display: flex;
  flex-direction: column;
  height: 46vh;
}
.edit-media.collapsed {
  height: auto;
}
.edit-media.collapsed .media-body {
  display: none;
}
.edit-media.collapsed .tab-collapse {
  transform: rotate(180deg);
}
.media-tabs {
  display: flex;
  gap: 4px;
  padding: 6px;
  background: #2b2f29;
}
.tab {
  flex: 1;
  min-height: 38px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #cfd6cb;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tab.active {
  background: #fff;
  color: var(--text);
}
.tab-collapse {
  width: 42px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  transition: transform 0.2s;
}
.media-body {
  position: relative;
  flex: 1;
  min-height: 0;
}
.media-image,
.media-preview {
  position: absolute;
  inset: 0;
}
.media-image[hidden],
.media-preview[hidden] {
  display: none;
}
.media-preview {
  overflow: auto;
  background: #50554d;
  padding: 12px;
}
.media-preview canvas {
  display: block;
  width: 100%;
  max-width: 640px;
  height: auto;
  margin: 0 auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
}
.viewer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  background: #20231f;
}
.viewer:active {
  cursor: grabbing;
}
.viewer img {
  position: absolute;
  left: 50%;
  top: 50%;
  transform-origin: center center;
  user-select: none;
  -webkit-user-select: none;
  max-width: none;
  pointer-events: none;
}
.viewer-tools {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: flex;
  gap: 6px;
  z-index: 2;
}
.viewer-tools button,
.vt-replace {
  min-width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.92);
  font: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
.vt-replace {
  font-size: 0.85rem;
}
.viewer-hint {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 20px);
  padding: 5px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 0.75rem;
  text-align: center;
  pointer-events: none;
  transition: opacity 0.6s;
}
.viewer-hint.fade {
  opacity: 0;
}
.no-image {
  height: 100%;
  display: grid;
  place-content: center;
  text-align: center;
  color: #e6ebe3;
  padding: 16px;
}

.edit-form {
  padding: 16px;
  min-width: 0;
}
.edit-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}
.edit-head h1 {
  margin: 0 0 4px;
}
.status-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.save-state {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  padding-top: 6px;
}
.save-state.saved {
  color: var(--ok);
}
.save-state.error {
  color: var(--danger);
  white-space: normal;
}
.banner {
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
}
.banner .row {
  margin-bottom: 0;
}
.banner-info {
  background: var(--primary-soft);
  border: 1px solid #c4dbc5;
}

.group {
  border-left: 5px solid #c9cfc4;
}
.group.has-error {
  border-left-color: var(--danger);
}
.group.is-checked {
  border-left-color: var(--ok);
  background: #fbfdfa;
}
.group-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.group-no {
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}
.group-label {
  flex: 1;
  font-weight: 700;
  font-size: 1.05rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.group-actions {
  display: flex;
  gap: 4px;
}
.conv {
  margin: -4px 0 12px;
  font-size: 0.92rem;
}
.conv summary {
  cursor: pointer;
  color: var(--primary);
  font-weight: 600;
  padding: 4px 0 8px;
}
.wchips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: -4px 0 10px;
}
.wchip {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  padding: 3px 7px;
  border-radius: 7px;
  background: #eef1eb;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  font-size: 0.88rem;
}
.wchip i {
  font-style: normal;
  font-size: 0.7rem;
  color: var(--muted);
  font-weight: 600;
}
.wchip.warn {
  background: var(--warn-soft);
  color: var(--warn);
  outline: 1px solid #f0cf8f;
}
.wchip.invalid {
  background: var(--danger-soft);
  color: var(--danger);
  outline: 1px solid #f1b5ae;
}
.group-msgs {
  margin-bottom: 10px;
  font-size: 0.92rem;
}
.count-line {
  margin-bottom: 4px;
}
.msg {
  padding: 6px 10px;
  border-radius: 8px;
  margin-top: 4px;
}
.msg-error {
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 600;
}
.msg-warn {
  background: var(--warn-soft);
  color: var(--warn);
}
.check-row {
  background: #f3f5f1;
  border-radius: 10px;
  padding: 12px;
  margin: 0;
  font-size: 1rem;
}
.group.is-checked .check-row {
  background: #e3f1e4;
  color: var(--ok);
}

.sum-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.sum-table th,
.sum-table td {
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.sum-table .num {
  text-align: right;
}
.sum-table tfoot th {
  border-bottom: none;
  font-size: 1.05rem;
}
.history ul {
  margin: 8px 0 0;
  padding-left: 18px;
  font-size: 0.9rem;
}
.history summary {
  cursor: pointer;
  font-weight: 600;
}
.danger-zone {
  margin: 8px 0 20px;
}

.action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  min-height: var(--actionbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.06);
}
.blockers {
  flex: 1;
  font-size: 0.9rem;
  min-width: 0;
}
.action-bar .btn {
  min-width: 140px;
}

/* Máy tính: ảnh gốc bên trái, dữ liệu bên phải */
@media (min-width: 960px) {
  .edit {
    grid-template-columns: minmax(0, 1.1fr) minmax(420px, 1fr);
    align-items: start;
  }
  .edit-media {
    height: calc(100vh - var(--topbar-h) - var(--actionbar-h));
  }
  .edit-media.collapsed {
    height: calc(100vh - var(--topbar-h) - var(--actionbar-h));
  }
  .edit-media.collapsed .media-body {
    display: block;
  }
  .tab-collapse {
    display: none;
  }
  .edit-form {
    padding: 20px 24px;
  }
}

/* ===== Ảnh phiếu ===== */
.ticket-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.ticket-actions .btn {
  flex: 1 1 200px;
}
.ticket-canvas {
  background: #50554d;
  border-radius: var(--radius);
  padding: 14px;
}
.ticket-canvas canvas {
  display: block;
  width: 100%;
  height: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* ===== Cài đặt ===== */
.catalog {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}
@media (max-width: 640px) {
  .catalog {
    grid-template-columns: 1fr;
  }
}
.cat-items {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  min-height: 28px;
}
.cat-add {
  display: flex;
  gap: 6px;
}
.cat-add input {
  margin: 0;
}
.user-list {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
}
.user-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.user-row.inactive {
  opacity: 0.6;
}
.user-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.user-actions select {
  width: auto;
  margin: 0;
  min-height: 36px;
  padding: 6px 8px;
}
.new-user {
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
.error-card {
  text-align: center;
}

/* ===== GIAI ĐOẠN 2: AI ===== */
.grid-4 {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0 12px;
}
@media (max-width: 560px) {
  .grid-4 {
    grid-template-columns: 1fr 1fr;
  }
}

/* Lớp đè số AI lên ảnh gốc */
.viewer-stage {
  position: absolute;
  left: 50%;
  top: 50%;
  transform-origin: center center;
}
.viewer-stage img {
  position: static;
  display: block;
  width: 100%;
  height: 100%;
  transform: none;
}
.viewer-layer {
  position: absolute;
  inset: 0;
}
.ov-box {
  position: absolute;
  border: calc(2px * var(--inv, 1)) solid var(--c);
  border-radius: calc(3px * var(--inv, 1));
  background: color-mix(in srgb, var(--c) 8%, transparent);
}
.ov-label {
  position: absolute;
  left: 50%;
  bottom: 100%;
  transform: translateX(-50%);
  margin-bottom: calc(1px * var(--inv, 1));
  padding: calc(1px * var(--inv, 1)) calc(5px * var(--inv, 1));
  border-radius: calc(4px * var(--inv, 1));
  background: var(--c);
  color: #fff;
  font-size: calc(12px * var(--inv, 1));
  font-weight: 700;
  line-height: 1.3;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
.ov-box.warn .ov-label {
  background: #f59e0b;
  color: #1c1300;
}
.ov-box.warn {
  border-color: #f59e0b;
  border-style: dashed;
}
.ov-box.invalid .ov-label {
  background: var(--danger);
}
.ov-box.ok {
  opacity: 0.55;
}
.ov-box.flash {
  animation: ov-flash 1.4s ease 1;
}
@keyframes ov-flash {
  0%,
  60% {
    box-shadow: 0 0 0 calc(6px * var(--inv, 1)) rgba(250, 204, 21, 0.85);
  }
  100% {
    box-shadow: none;
  }
}
.vt-overlay {
  font-size: 0.85rem !important;
}
.vt-overlay.on {
  background: #fde68a !important;
}

/* Ô số ký: chạm để tìm trên ảnh */
.wchip {
  border: none;
  font-family: inherit;
  color: inherit;
}
.wchip.has-box {
  cursor: pointer;
  box-shadow: inset 0 -2px 0 var(--gc, #c9cfc4);
}
.wchip.has-box:hover {
  filter: brightness(0.95);
}
.wchip.flash {
  animation: chip-flash 1.4s ease 1;
}
@keyframes chip-flash {
  0%,
  60% {
    outline: 3px solid #facc15;
    outline-offset: 1px;
  }
  100% {
    outline: none;
  }
}
.group {
  border-left-color: var(--gc, #c9cfc4);
}
.group-no {
  color: var(--gc, var(--muted));
}

/* Thông báo AI */
.banner-ai {
  background: #eef4ff;
  border: 1px solid #c7d7fb;
}
.banner-ai-error {
  background: var(--warn-soft);
  border: 1px solid #f0cf8f;
}
.banner p:last-child {
  margin-bottom: 0;
}
.ai-busy {
  display: flex;
  align-items: center;
  gap: 12px;
}
.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid #c7d7fb;
  border-top-color: #1d4ed8;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  flex: none;
  display: inline-block;
}
.spinner.sm {
  width: 11px;
  height: 11px;
  border-width: 2px;
  vertical-align: -1px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
.ti-badges {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}
.badge-ai {
  background: #e3ecfd;
  color: #1d4ed8;
}
.badge-ai-error {
  background: var(--warn-soft);
  color: var(--warn);
}
.ai-status p {
  margin: 0 0 6px;
}
.table-wrap {
  overflow-x: auto;
  margin-bottom: 8px;
}
code {
  background: #eef1eb;
  padding: 1px 5px;
  border-radius: 4px;
}

/* ===== GIAI ĐOẠN 2B: DUYỆT NHANH & XUẤT HÀNG LOẠT ===== */
@media (max-width: 720px) {
  .nav a {
    padding: 8px 7px;
    font-size: 0.85rem;
  }
  .me .link {
    font-size: 0.85rem;
  }
}

/* Tab trạng thái */
.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  margin-bottom: 12px;
  padding-bottom: 2px;
}
.tab-btn {
  flex: none;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  min-height: 40px;
}
.tab-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.tab-count {
  display: inline-block;
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.08);
  font-size: 0.8rem;
  text-align: center;
}
.tab-btn.active .tab-count {
  background: rgba(255, 255, 255, 0.25);
}
.filters {
  grid-template-columns: 1fr 170px auto;
}
@media (max-width: 640px) {
  .filters {
    grid-template-columns: 1fr auto;
  }
}
.hint-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

/* Gom theo ngày + chọn phiếu */
.day-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 14px 0 2px;
  padding: 8px 4px;
  border-bottom: 2px solid var(--border);
}
.ti-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.ti-row .ticket-item {
  flex: 1;
  min-width: 0;
}
.ti-check,
.ti-check-space {
  flex: none;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  cursor: pointer;
}
.ti-check input {
  width: 24px;
  height: 24px;
  min-height: 0;
  margin: 0;
  accent-color: var(--primary);
}
body[data-page='list'] .page {
  padding-bottom: calc(var(--actionbar-h) + 30px);
}
.select-bar .btn {
  min-width: 0;
}

/* Thanh duyệt nhanh */
.review-bar {
  flex-wrap: wrap;
}
.rv-info {
  flex: 1;
  min-width: 180px;
}
.rv-pos {
  font-weight: 700;
}
.rv-buttons {
  display: flex;
  gap: 8px;
}
.review-bar .btn {
  min-width: 0;
}
#rv-approve {
  min-width: 190px;
}
@media (max-width: 560px) {
  .rv-buttons {
    width: 100%;
  }
  #rv-approve {
    flex: 1;
    min-width: 0;
  }
  body[data-page='review'] .edit {
    padding-bottom: 140px;
  }
}

/* Nhóm thu gọn khi duyệt nhanh: chỉ hiện số */
.group.compact .grid-4,
.group.compact .g-fields,
.group.compact .conv,
.group.compact .wt-label,
.group.compact .check-row {
  display: none;
}
.group.compact .wchips {
  margin-top: 0;
}
.group.compact .wchip {
  font-size: 1rem;
  padding: 4px 9px;
}

/* Kết thúc duyệt */
.review-done {
  padding: 32px 20px;
}
.big-emoji {
  font-size: 3rem;
}
.center-row {
  justify-content: center;
}

/* Ảnh đã xuất */
.export-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.export-actions p {
  flex-basis: 100%;
  margin: 4px 0 0;
}
.thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.thumb {
  display: block;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  color: inherit;
  text-decoration: none;
  font-size: 0.8rem;
}
.thumb img {
  display: block;
  width: 100%;
  height: 160px;
  object-fit: cover;
  object-position: top;
  border-bottom: 1px solid var(--border);
}
.thumb span {
  display: block;
  padding: 6px 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Menu: chữ ngắn trên điện thoại */
.sm {
  display: none;
}
@media (max-width: 720px) {
  .lg {
    display: none;
  }
  .sm {
    display: inline;
  }
  .nav {
    gap: 2px;
  }
}
/* Ảnh thu nhỏ phiếu đã xuất: hiện trọn phiếu */
.thumb img {
  object-fit: contain;
  object-position: top center;
  background: #fff;
  height: 200px;
}
/* Duyệt nhanh: ẩn nút đổi thứ tự khi thu gọn để tên nhóm đủ chỗ; thông báo nằm trên thanh duyệt */
.group.compact [data-act='up'],
.group.compact [data-act='down'] {
  display: none;
}
@media (max-width: 560px) {
  body[data-page='review'] .toasts {
    bottom: 150px;
  }
}
/* Nút trên tiêu đề ngày + nút xóa */
.day-actions {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
}
.select-bar .danger-outline,
.review-bar .danger-outline {
  background: #fff;
}

/* ===== Excel: ô tìm khách (sheet KH), mã hàng, hộp thông báo ===== */
.field {
  display: block;
  font-weight: 600;
  font-size: 0.92rem;
  margin-bottom: 12px;
}
.field-label small {
  font-weight: 400;
}
.ac {
  position: relative;
}
.ac-list {
  position: absolute;
  z-index: 30;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  max-height: 280px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  font-weight: 400;
}
.ac-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  cursor: pointer;
  min-height: 40px;
}
.ac-item:hover,
.ac-item.active {
  background: var(--primary-soft);
}
.ac-empty {
  padding: 10px 12px;
}
.kh-hint {
  margin-top: 5px;
  font-weight: 400;
  min-height: 1em;
}
.warn-text {
  color: var(--warn);
}
.badge-excel {
  background: #e8f0fb;
  color: #1d5aa6;
  margin-right: 4px;
}
.dialog {
  border: none;
  border-radius: var(--radius);
  padding: 20px;
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.dialog::backdrop {
  background: rgba(0, 0, 0, 0.35);
}
.dialog ul,
.dialog ol {
  padding-left: 20px;
  margin: 6px 0 12px;
}
.map-table td {
  padding: 4px 6px 4px 0;
  vertical-align: middle;
}
.map-table input {
  margin-top: 0;
}
.kh-sugg {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.chip-btn {
  border: 1px solid #cfd8e8;
  background: #eef4fd;
  color: #1d5aa6;
  cursor: pointer;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 32px;
}
.chip-btn:hover {
  background: #dfeafb;
}
.ac-foot {
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  position: sticky;
  bottom: 0;
  background: #fff;
}
.ac-item small {
  text-align: right;
}
/* Mỗi nhóm dưa: ô Mã hàng Excel + chữ tài xế ghi trên giấy bên cạnh */
.g-code {
  display: grid;
  grid-template-columns: minmax(0, 280px) minmax(0, 1fr);
  gap: 0 16px;
  align-items: end;
}
.g-code .paper {
  margin-bottom: 14px;
  line-height: 1.45;
}
.g-code .paper:empty {
  display: none;
}
@media (max-width: 560px) {
  .g-code {
    grid-template-columns: 1fr;
  }
  .g-code .paper {
    margin-top: -6px;
  }
}
/* Danh sách mã hàng (sheet SP): rộng hơn ô nhập để thấy cả tên hàng */
.ac-sp .ac-list {
  width: 340px;
  max-width: calc(100vw - 32px);
}
/* Duyệt nhanh, nhóm thu gọn: hiện chữ giấy ghi cạnh mã hàng để so nhanh */
.paper-mini {
  display: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.group.compact .paper-mini {
  display: inline;
}
.group.compact .group-label {
  flex: 0 1 auto;
}
.group.compact .group-actions {
  margin-left: auto;
}
.conv-body {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.conv-body input {
  width: 90px;
}
.conv-body span {
  flex: 1;
  min-width: 200px;
}
.ac-item .ac-label {
  font-weight: 600;
  white-space: nowrap;
}
.settings-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: -4px 0 14px;
}
.settings-nav a {
  padding: 6px 12px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--border);
  text-decoration: none;
  color: var(--text);
  font-weight: 600;
  font-size: 0.9rem;
}
.settings-nav a:hover {
  background: var(--primary-soft);
}

/* ===== GIAI ĐOẠN 3N: 2 KHU TÁCH HẲN – GIAO KHÁCH (xanh lá) / NHẬP VƯỜN (cam đất) ===== */
body[data-zone='ncc'] {
  --primary: #a4521f;
  --primary-hover: #8a4318;
  --primary-soft: #fbeee3;
}
.zones {
  display: flex;
  flex: none;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}
.zones a {
  color: #f1f5ef;
  text-decoration: none;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  white-space: nowrap;
  font-size: 0.95rem;
}
.zones a:hover {
  background: rgba(255, 255, 255, 0.14);
}
.zones a.active {
  background: #fff;
  color: var(--primary);
}
@media (max-width: 720px) {
  .zones a {
    padding: 6px 7px;
    font-size: 0.82rem;
  }
}
/* Nhãn khu ở đầu trang danh sách / tạo phiếu */
.zone-tag {
  display: inline-block;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-soft);
  border-radius: 999px;
  padding: 2px 10px;
  margin-bottom: 4px;
}
/* Bảng số ký NCC: thẻ có viền màu cam */
body[data-zone='ncc'] .group {
  border-left-color: var(--gc, var(--primary));
}
.ncc-paper b {
  white-space: nowrap;
}
/* Điện thoại hẹp: menu chỉ còn biểu tượng để đủ chỗ cho nút chuyển khu + Thoát */
@media (max-width: 440px) {
  .xs-hide {
    display: none;
  }
  .topbar {
    gap: 6px;
    padding: 0 8px;
  }
  .nav a {
    padding: 8px 8px;
    font-size: 1rem;
  }
}

/* Phiếu cân nhà vườn: số ký bày thành lưới 10 cột giống trên giấy (cụm 5 cột / 5 hàng) */
.wchips.wgrids {
  display: block;
}
.wgrid {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 3px;
  margin-bottom: 8px;
}
.wgrid > :nth-child(10n + 6) {
  margin-left: 4px;
}
.wgrid > :nth-child(n + 41):nth-child(-n + 50) {
  margin-bottom: 4px;
}
.wgrid .wchip,
.group.compact .wgrid .wchip {
  justify-content: center;
  min-width: 0;
  padding: 3px 0;
  font-size: 0.74rem;
  letter-spacing: -0.02em;
  overflow: hidden;
}
.wcell-empty {
  min-height: 1.6em;
  border-radius: 7px;
  border: 1px dashed #d8dcd3;
}
@media (min-width: 700px) {
  .wgrid .wchip,
  .group.compact .wgrid .wchip {
    font-size: 0.9rem;
  }
}

/* Duyệt cả ngày (04/12/2026) */
.btn.approve-day {
  border-color: var(--ok);
  color: var(--ok);
  background: #fff;
}
.approve-box ul {
  margin: 8px 0 0;
  padding-left: 20px;
}
.approve-box li {
  margin: 4px 0;
}
.approve-box {
  position: relative;
  padding-right: 52px;
}
.approve-box [data-close-approve] {
  position: absolute;
  top: 8px;
  right: 8px;
}
