/*
 * DMS Gà Xay — Website Quản trị — CSS dùng chung (Sprint 12, WF-02).
 * Mobile-first: mặc định là bố cục điện thoại, mở rộng dần qua 2 breakpoint
 * (tablet 768px, desktop 1024px) — đúng yêu cầu kỹ thuật Sprint 12.
 */

:root {
  --color-bg: #f4f5f7;
  --color-surface: #ffffff;
  --color-border: #dfe3e8;
  --color-text: #1f2933;
  --color-text-muted: #64748b;
  --color-primary: #1d4ed8;
  --color-primary-dark: #1e3a8a;
  --color-danger: #dc2626;
  --color-danger-bg: #fee2e2;
  --color-warning: #d97706;
  --color-warning-bg: #fef3c7;
  --color-success: #16a34a;
  --color-success-bg: #dcfce7;
  --sidebar-width: 240px;
  --topbar-height: 56px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
}

* { box-sizing: border-box; }

/*
 * Bắt buộc [hidden] luôn thắng, bất kể class nào khác đặt display riêng
 * (.field{display:flex}, .btn{display:inline-block}...) — CSS tác giả (author
 * stylesheet) mặc định ĐÈ style UA `[hidden]{display:none}` dù độ đặc hiệu
 * ngang nhau, nên 1 phần tử vừa có `hidden` vừa có class set display riêng sẽ
 * KHÔNG bị ẩn nếu thiếu dòng này — phát hiện thật qua kiểm thử trình duyệt
 * (Sprint 12/13: #salesFilterField, #linkSalesPerf vẫn hiện dù có `hidden`).
 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0 0 0.5em; font-weight: 600; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }

button, input, select {
  font: inherit;
  color: inherit;
}

/* ---------- Khung dùng chung: topbar + sidebar + content (WF-02) ---------- */

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--topbar-height);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar__menu-toggle {
  border: none;
  background: transparent;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
}
.topbar__menu-toggle:hover { background: var(--color-bg); }

.topbar__brand {
  font-weight: 700;
  color: var(--color-primary-dark);
  white-space: nowrap;
}

.topbar__user {
  margin-left: auto;
  min-width: 0; /* cho phép flex item này co lại nhỏ hơn nội dung — nếu không tên dài sẽ đẩy nút Đăng xuất ra ngoài màn hình trên điện thoại (phát hiện qua kiểm thử responsive thật) */
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.topbar__user-name {
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar__user-role,
#logoutBtn { flex: 0 0 auto; }

.app-body {
  flex: 1 1 auto;
  display: flex;
  position: relative;
  min-height: 0;
}

.sidebar {
  position: fixed;
  top: var(--topbar-height);
  left: 0;
  bottom: 0;
  width: min(80vw, 280px);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 30;
}
.sidebar.is-open { transform: translateX(0); }

.sidebar__backdrop {
  position: fixed;
  inset: var(--topbar-height) 0 0 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 25;
  display: none;
}
.sidebar__backdrop.is-visible { display: block; }

.sidebar__menu {
  list-style: none;
  margin: 0;
  padding: 8px 0;
}

.sidebar__link {
  display: block;
  padding: 12px 20px;
  color: var(--color-text);
  border-left: 3px solid transparent;
}
.sidebar__link:hover { background: var(--color-bg); text-decoration: none; }
.sidebar__link.is-active {
  border-left-color: var(--color-primary);
  background: var(--color-bg);
  color: var(--color-primary-dark);
  font-weight: 600;
}

.sidebar__group-label {
  padding: 12px 20px 4px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
}

.content {
  flex: 1 1 auto;
  min-width: 0;
  padding: 16px;
}

/* Tablet trở lên: sidebar cố định, không cần overlay/hamburger */
@media (min-width: 768px) {
  .topbar__menu-toggle { display: none; }
  .sidebar {
    position: sticky;
    top: var(--topbar-height);
    height: calc(100vh - var(--topbar-height));
    transform: none;
    width: var(--sidebar-width);
    flex: 0 0 auto;
  }
  .sidebar__backdrop { display: none !important; }
  .app-body { display: flex; }
  .content { padding: 24px; }
}

@media (min-width: 1024px) {
  :root { --sidebar-width: 260px; }
  .content { padding: 32px; max-width: 1400px; }
}

/* ---------- Thành phần dùng chung ---------- */

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

.card--centered {
  max-width: 380px;
  margin: 10vh auto;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px;
}
.stat-card__label { font-size: 0.8rem; color: var(--color-text-muted); margin-bottom: 4px; }
.stat-card__value { font-size: 1.3rem; font-weight: 700; }
.stat-card__value--danger { color: var(--color-danger); }
.stat-card__value--success { color: var(--color-success); }

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  margin-bottom: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 160px;
  min-width: 0; /* cho phép co lại thay vì bám theo nội dung con — xem field select bên dưới */
}
.field label { font-size: 0.8rem; color: var(--color-text-muted); }
.field input, .field select {
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  min-width: 140px;
  /* width:100% BẮT BUỘC — <select> mặc định tự giãn theo <option> DÀI NHẤT bất
     kể min-width, không tôn trọng chiều rộng cha (bug thật phát hiện qua kiểm
     thử responsive: select liệt kê tên Sales dài làm tràn ngang 670px trên màn
     hình 375px, xem Sprint 20) — width:100% buộc select bám theo .field thay
     vì bám theo option, max-width chặn input cũng không vượt quá .field. */
  width: 100%;
  max-width: 100%;
}

.btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--color-primary);
  background: var(--color-primary);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { background: var(--color-primary-dark); border-color: var(--color-primary-dark); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.btn--ghost {
  background: transparent;
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn--ghost:hover { background: var(--color-bg); }

.btn--outline {
  background: transparent;
  color: var(--color-primary);
}
.btn--outline:hover { background: #eff4ff; }

.btn--block { width: 100%; }
.btn--sm { padding: 4px 10px; font-size: 0.85rem; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; margin-bottom: 4px; font-size: 0.85rem; font-weight: 600; }
.form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}

.banner {
  padding: 10px 14px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: 0.9rem;
}
.banner--error { background: var(--color-danger-bg); color: var(--color-danger); }
.banner--warning { background: var(--color-warning-bg); color: var(--color-warning); }
.banner--success { background: var(--color-success-bg); color: var(--color-success); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--color-bg);
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
}

.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  font-size: 0.9rem;
}
table th, table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  white-space: nowrap;
}
table th { color: var(--color-text-muted); font-weight: 600; background: var(--color-bg); }
table tbody tr:hover { background: var(--color-bg); }

.row-danger { background: var(--color-danger-bg) !important; }
.row-warning { background: var(--color-warning-bg) !important; }

.empty-state {
  text-align: center;
  color: var(--color-text-muted);
  padding: 32px 16px;
}

.bar-chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 160px;
  padding: 8px 4px 0;
  border-bottom: 1px solid var(--color-border);
  overflow-x: auto;
}
.bar-chart__col {
  flex: 0 0 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
}
.bar-chart__bar {
  width: 100%;
  background: var(--color-primary);
  border-radius: 3px 3px 0 0;
  min-height: 2px;
}
.bar-chart__label {
  font-size: 0.65rem;
  color: var(--color-text-muted);
  margin-top: 4px;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  max-height: 60px;
}

.link-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }

.page-loading, .page-error {
  text-align: center;
  padding: 40px 16px;
  color: var(--color-text-muted);
}

.text-muted { color: var(--color-text-muted); }
.text-right { text-align: right; }
.mt-0 { margin-top: 0; }

/* ---------- Dialog (Sprint 14/15) — <dialog> gốc HTML5, không thư viện ngoài ---------- */

dialog {
  border: none;
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(15, 23, 42, 0.25);
  padding: 20px;
  width: min(92vw, 560px);
  max-height: 85vh;
  overflow-y: auto;
  background: var(--color-surface);
  color: var(--color-text);
}
dialog::backdrop { background: rgba(15, 23, 42, 0.45); }

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.dialog-header h2 { margin: 0; }
.dialog-close {
  background: none;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 4px 8px;
}
.dialog-close:hover { color: var(--color-text); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.form-group textarea, .form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font: inherit;
}
.form-group textarea { resize: vertical; min-height: 60px; }
.form-hint { font-size: 0.8rem; color: var(--color-text-muted); margin-top: 4px; }

.radio-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.radio-group label { display: flex; align-items: center; gap: 6px; font-weight: normal; }

.chip-list { display: flex; flex-wrap: wrap; gap: 8px; list-style: none; margin: 0 0 12px; padding: 0; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  font-size: 0.85rem;
}

tbody tr[data-clickable] { cursor: pointer; }

.section-divider { border: none; border-top: 1px solid var(--color-border); margin: 16px 0; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--color-border); margin-bottom: 16px; flex-wrap: wrap; }
.tab-btn {
  padding: 10px 16px;
  border: none;
  background: none;
  cursor: pointer;
  font-weight: 600;
  color: var(--color-text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab-btn.is-active { color: var(--color-primary-dark); border-bottom-color: var(--color-primary); }
.tab-panel[hidden] { display: none; }

/* ---------- Search-select combobox (Sprint 16/17) ---------- */

.search-select { position: relative; }
.search-select__input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font: inherit;
}
.search-select__menu {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  max-height: 240px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: var(--shadow);
}
.search-select__option { padding: 8px 12px; cursor: pointer; }
.search-select__option:hover { background: var(--color-bg); }

/* ---------- SLA / trạng thái (Sprint 16) ---------- */

.badge--danger { background: var(--color-danger-bg); color: var(--color-danger); border-color: transparent; }
.badge--warning { background: var(--color-warning-bg); color: var(--color-warning); border-color: transparent; }
.badge--success { background: var(--color-success-bg); color: var(--color-success); border-color: transparent; }

/* ---------- Dòng hàng đơn hàng (Sprint 17) — mobile-first, cuộn dọc ---------- */

.order-line {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
  background: var(--color-bg);
}
.order-line__row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.order-line__row .field { flex: 1 1 120px; }
.order-line__remove { flex: 0 0 auto; align-self: flex-end; }
.order-line__summary { font-size: 0.85rem; color: var(--color-text-muted); }

.sticky-save-bar {
  position: sticky;
  bottom: 0;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding: 12px;
  margin: 16px -16px -16px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  z-index: 10;
}
@media (min-width: 768px) {
  .sticky-save-bar { margin: 16px -24px -24px; }
}

/* ---------- Chuông thông báo trên topbar (Sprint 19) ---------- */

.notif-bell { position: relative; }
.notif-bell__btn {
  position: relative;
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 6px;
  line-height: 1;
}
.notif-bell__count {
  position: absolute;
  top: -4px;
  right: -2px;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 5px;
  min-width: 16px;
  text-align: center;
}
.notif-bell__menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  width: min(90vw, 340px);
  max-height: 400px;
  overflow-y: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 60;
}
.notif-bell__item { padding: 10px 14px; border-bottom: 1px solid var(--color-border); cursor: pointer; }
.notif-bell__item:hover { background: var(--color-bg); }
.notif-bell__item.is-unread { background: #eff4ff; }
.notif-bell__item-title { font-weight: 600; font-size: 0.85rem; margin-bottom: 2px; }
.notif-bell__item-body { font-size: 0.82rem; color: var(--color-text-muted); }
.notif-bell__empty { padding: 16px; color: var(--color-text-muted); text-align: center; font-size: 0.85rem; }
.notif-bell__viewall { display: block; padding: 10px 14px; text-align: center; font-size: 0.85rem; font-weight: 600; }

.notif-row {
  padding: 14px;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
}
.notif-row:last-child { border-bottom: none; }
.notif-row:hover { background: var(--color-bg); }
.notif-row.is-unread { background: #eff4ff; border-left: 3px solid var(--color-primary); }
.notif-row__title { font-weight: 600; margin-bottom: 4px; display: flex; align-items: center; gap: 8px; }
.notif-row__body { color: var(--color-text); margin-bottom: 4px; }
.notif-row__meta { font-size: 0.8rem; color: var(--color-text-muted); }
