/* ============================================================
   admin.css — Shared design system + theme + polish overlay.
   Loaded AFTER each page's inline <style> so it takes precedence
   and unifies every admin page. Each page keeps a self-contained
   <style> for layout; this file is the canonical source of truth
   for the palette, shared components and visual polish.
   ============================================================ */

/* ── Dark theme (default base) ──────────────────────────────── */
:root {
  --bg: #0f172a;
  --sidebar-bg: #1e293b;
  --card-bg: #1e293b;
  --card2-bg: #162032;
  --card-hover: #263449;
  --border: #334155;
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --text: #f1f5f9;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --tag-bg: #1e3a5f;

  /* Shared surfaces that used to be hardcoded per page */
  --input-bg: #0a1628;
  --thead-bg: #151f30;
  --selected-bg: #1d3557;
  --scroll-thumb: #334155;
  --scroll-thumb-hover: #475569;

  /* Elevation / shape / motion tokens */
  --shadow-sm: 0 1px 3px rgba(0,0,0,.35);
  --shadow-md: 0 8px 28px rgba(0,0,0,.45);
  --ring: 0 0 0 3px rgba(59,130,246,.30);
  --radius: 10px;
  --radius-sm: 6px;

  color-scheme: dark;
}

/* ── Light theme ──────────────────────────────────────────── */
[data-theme="light"] {
  --bg: #f0f4f8;
  --sidebar-bg: #ffffff;
  --card-bg: #ffffff;
  --card2-bg: #f8fafc;
  --card-hover: #f1f5f9;
  --border: #e2e8f0;
  --text-main: #1e293b;
  --text-muted: #475569;
  --text-dim: #94a3b8;
  --text: #1e293b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --tag-bg: #dbeafe;

  --input-bg: #f8fafc;
  --thead-bg: #f8fafc;
  --selected-bg: #dbeafe;
  --scroll-thumb: #cbd5e1;
  --scroll-thumb-hover: #94a3b8;

  --shadow-sm: 0 1px 3px rgba(15,23,42,.06);
  --shadow-md: 0 8px 28px rgba(15,23,42,.12);
  --ring: 0 0 0 3px rgba(59,130,246,.20);

  color-scheme: light;
}

/* ── System theme (follows OS preference) ──────────────────── */
@media (prefers-color-scheme: light) {
  [data-theme="system"] {
    --bg: #f0f4f8;
    --sidebar-bg: #ffffff;
    --card-bg: #ffffff;
    --card2-bg: #f8fafc;
    --card-hover: #f1f5f9;
    --border: #e2e8f0;
    --text-main: #1e293b;
    --text-muted: #475569;
    --text-dim: #94a3b8;
    --text: #1e293b;
    --tag-bg: #dbeafe;

    --input-bg: #f8fafc;
    --thead-bg: #f8fafc;
    --selected-bg: #dbeafe;
    --scroll-thumb: #cbd5e1;
    --scroll-thumb-hover: #94a3b8;

    --shadow-sm: 0 1px 3px rgba(15,23,42,.06);
    --shadow-md: 0 8px 28px rgba(15,23,42,.12);
    --ring: 0 0 0 3px rgba(59,130,246,.20);

    color-scheme: light;
  }
}

/* ── Theme transitions (smooth mode switch) ─────────────────── */
body, .sidebar, .topbar, .card, .card-head, .card-body, .table-wrap,
.modal, .confirm-box, .toast, .stat-card, .summary-card, thead th,
.filter-input, .filter-select, .form-input, .form-select, .form-textarea {
  transition: background-color .2s ease, border-color .2s ease, color .15s ease, box-shadow .2s ease;
}

/* Respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* ── Canonical button styles (override inline page styles) ──── */
.btn {
  padding: .4rem .9rem;
  border-radius: var(--radius-sm);
  font-size: .79rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  font-family: 'Inter', sans-serif;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .35rem;
  white-space: nowrap;
  transition: background .15s, box-shadow .15s, transform .05s;
  text-decoration: none;
  line-height: 1;
}
.btn:active { transform: translateY(1px); }
.btn-sm {
  height: auto;
  padding: .24rem .6rem;
  font-size: .72rem;
}
.btn-xs {
  height: auto;
  padding: .18rem .45rem;
  font-size: .68rem;
  border-radius: 4px;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:disabled:active { transform: none; }
.btn-primary { box-shadow: 0 1px 2px rgba(37,99,235,.25); }
.btn-primary:hover { box-shadow: 0 2px 8px rgba(37,99,235,.35); }

/* ── Accessible focus rings (keyboard nav) ──────────────────── */
.btn:focus-visible,
.filter-input:focus-visible, .filter-select:focus-visible,
.form-input:focus-visible, .form-select:focus-visible, .form-textarea:focus-visible,
.theme-btn:focus-visible, .nav-item:focus-visible, .nav-link:focus-visible {
  outline: none;
  box-shadow: var(--ring);
}

/* ── Shared form controls (unify surface across themes) ─────── */
.filter-input, .filter-select,
.form-input, .form-select, .form-textarea {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text-main);
}
.filter-input:focus, .filter-select:focus,
.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--primary);
}

/* ── Shared surfaces polish ─────────────────────────────────── */
.card { box-shadow: var(--shadow-sm); }
.table-wrap { box-shadow: var(--shadow-sm); }
.modal { box-shadow: var(--shadow-md); }
thead th { background: var(--thead-bg); }
tbody tr:hover { background: var(--card-hover); }

/* Subtle lift on interactive summary/stat cards */
.stat-card, .summary-card { transition: transform .12s ease, box-shadow .2s ease, background-color .2s ease; }
.stat-card:hover, .summary-card:hover { box-shadow: var(--shadow-sm); }

/* ── Custom scrollbars (themed) ─────────────────────────────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--scroll-thumb-hover); }
* { scrollbar-width: thin; scrollbar-color: var(--scroll-thumb) transparent; }

/* ── Theme toggle dropdown ──────────────────────────────────── */
.theme-pill {
  position: relative;
  display: inline-flex;
  align-items: center;
}
.theme-btn {
  background: transparent;
  border: 1px solid var(--border, #334155);
  color: var(--text-muted, #94a3b8);
  border-radius: var(--radius-sm);
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: .92rem;
  transition: background .15s, color .15s, box-shadow .15s;
  flex-shrink: 0;
  outline: none;
}
.theme-btn:hover {
  background: var(--card-hover, #263449);
  color: var(--text-main, #f1f5f9);
}
.theme-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--card-bg, #1e293b);
  border: 1px solid var(--border, #334155);
  border-radius: 8px;
  padding: .25rem;
  min-width: 130px;
  z-index: 9000;
  box-shadow: var(--shadow-md);
  display: none;
  transform-origin: top right;
  animation: theme-menu-in .12s ease;
}
@keyframes theme-menu-in {
  from { opacity: 0; transform: scale(.96) translateY(-2px); }
  to   { opacity: 1; transform: none; }
}
.theme-menu.open { display: block; }
.theme-opt {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .4rem .65rem;
  border-radius: 5px;
  font-size: .78rem;
  color: var(--text-muted, #94a3b8);
  cursor: pointer;
  transition: background .1s, color .1s;
  user-select: none;
}
.theme-opt:hover {
  background: var(--card-hover, #263449);
  color: var(--text-main, #f1f5f9);
}
.theme-opt.active {
  color: var(--primary, #3b82f6);
  font-weight: 600;
}

/* ── Light theme element adjustments ───────────────────────── */
[data-theme="light"] thead th { color: #64748b; }
[data-theme="light"] .filter-select option { background: #fff; }
[data-theme="light"] .nav-item:hover { background: #f1f5f9; color: #1e293b; }
[data-theme="light"] .nav-link:hover { background: #f1f5f9; color: #1e293b; }
[data-theme="light"] .logo { color: #1e293b; }

/* ============================================================
   Canonical shell layout — sidebar / navbar / footer.
   Loaded after page <style> so it unifies every admin page that
   uses .sidebar (bespoke layouts như index/error-log không dùng
   class này nên không bị ảnh hưởng). Đây là nguồn chân lý chung
   cho cấu trúc header/navbar/sidebar/footer (admin-shell.js inject).
   ============================================================ */
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--sidebar-bg, #1e293b);
  border-right: 1px solid var(--border, #334155);
  display: flex;
  flex-direction: column;
  padding: 1rem .85rem;
  gap: .15rem;
  overflow-y: auto;
}
.sidebar .logo {
  font-size: .95rem;
  font-weight: 600;
  color: var(--text-main, #f1f5f9);
  padding: .4rem .5rem .9rem;
  border-bottom: 1px solid var(--border, #334155);
  margin-bottom: .5rem;
  display: flex;
  align-items: center;
  gap: .4rem;
}
.sidebar .logo span { color: var(--primary, #3b82f6); }
.sidebar .nav-section {
  font-size: .65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-dim, #94a3b8);
  padding: .6rem .5rem .3rem;
}
.sidebar .nav-item,
.sidebar .nav-link {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .55rem .75rem;
  border-radius: .45rem;
  cursor: pointer;
  font-size: .82rem;
  color: var(--text-muted, #94a3b8);
  transition: background .12s, color .12s, padding-left .12s;
  text-decoration: none;
}
.sidebar .nav-item:hover,
.sidebar .nav-link:hover {
  background: var(--card-hover, #263347);
  color: var(--text-main, #f1f5f9);
  padding-left: .95rem;
}
.sidebar .nav-item.active,
.sidebar .nav-link.active {
  background: var(--primary, #3b82f6);
  color: #fff;
  font-weight: 600;
}
.sidebar .nav-item.active:hover,
.sidebar .nav-link.active:hover {
  background: var(--primary, #3b82f6);
  color: #fff;
  padding-left: .75rem;
}
.sidebar .nav-icon { width: 1.1rem; text-align: center; flex-shrink: 0; }
.sidebar .sidebar-footer {
  margin-top: auto;
  font-size: .68rem;
  color: var(--text-dim, #94a3b8);
  padding-top: .9rem;
  border-top: 1px solid var(--border, #334155);
}
/* Tránh nhấp nháy: ẩn aside cho tới khi shell inject xong */
.sidebar[data-shell]:empty { visibility: hidden; }

/* ============================================================
   Responsive — mobile/tablet.
   admin-shell.js chèn nút .sidebar-toggle (hamburger) + .sidebar-backdrop.
   Desktop: nút ẩn, sidebar là cột 220px như thường.
   ≤820px: sidebar thành drawer off-canvas trượt; topbar/filters wrap;
   cards stack; bảng tự cuộn ngang trong .table-wrap.
   ============================================================ */
.sidebar-toggle { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 820px) {
  /* Hamburger nằm đầu topbar (shell tự chèn) */
  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    flex-shrink: 0;
    margin-right: .1rem;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.05rem;
    line-height: 1;
    transition: background .15s, color .15s;
  }
  .sidebar-toggle:hover { background: var(--card-hover); color: var(--text-main); }
  .sidebar-toggle:focus-visible { outline: none; box-shadow: var(--ring); }

  /* Sidebar → drawer off-canvas */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1000;
    width: min(82vw, 260px);
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 999;
    background: rgba(0,0,0,.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease;
  }
  .sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

  /* Topbar cho phép xuống dòng thay vì tràn */
  .topbar {
    height: auto;
    min-height: 52px;
    flex-wrap: wrap;
    padding: .45rem .9rem;
    row-gap: .45rem;
  }
  .topbar h1 { font-size: .88rem; }
  .filters, .topbar-actions { flex-wrap: wrap; }

  /* Content gọn hơn theo chiều ngang (giữ padding dọc của trang,
     vd canonical_form cần padding-bottom để chừa action-bar cố định) */
  .content { padding-left: .9rem; padding-right: .9rem; }

  /* Thẻ thống kê xếp 2 cột co giãn */
  .stats-bar, .summary-bar, .live-bar { gap: .6rem; }
  .stat-card, .summary-card, .live-card {
    min-width: calc(50% - .5rem);
    flex: 1 1 calc(50% - .5rem);
  }

  /* Hàng field 2 cột → xếp dọc (canonical_form) */
  .field-row { flex-direction: column; gap: .9rem; }

  /* Modal/drawer không vượt màn hình */
  .modal { max-width: 94vw; }
}

@media (max-width: 480px) {
  .stat-card, .summary-card, .live-card { min-width: 100%; flex-basis: 100%; }
  .content { padding-left: .7rem; padding-right: .7rem; }
  .topbar { padding: .4rem .7rem; }
  .btn { padding: .38rem .7rem; }
}
