/*
 * Reusable components, built on the tokens in tokens.css. See
 * docs/DESIGN_SYSTEM.md for usage rules and design-system.html for a live
 * catalogue of every variant in both themes.
 */

/* ---------- Card ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-1);
  margin-bottom: var(--space-4);
}

/* ---------- Headings / text ---------- */
h2 { font-size: var(--text-md); margin: 0 0 var(--space-3); }
h3 { font-size: var(--text-base); margin: var(--space-4) 0 var(--space-2); color: var(--ink-muted); font-weight: 600; }
.muted { color: var(--ink-muted); }
.small { font-size: var(--text-sm); }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* ---------- Buttons ---------- */
button, .btn {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-md);
  padding: 9px 16px;
  cursor: pointer;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  text-decoration: none;
  min-height: 44px;
}
button:hover, .btn:hover { border-color: var(--ink-muted); }
button:disabled, .btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.primary:hover { filter: brightness(1.05); border-color: var(--brand); }
.ghost { background: transparent; border-color: transparent; color: var(--ink-muted); }
.ghost:hover { color: var(--ink); border-color: var(--line); }
.btn-danger { background: var(--bad); color: var(--surface); border-color: var(--bad); }
.btn-danger:hover { filter: brightness(1.05); border-color: var(--bad); }

/* size modifier — a true size variant, not just ad hoc smaller text */
button.small, .btn.small { min-height: 36px; padding: 6px 12px; font-size: var(--text-sm); }

/* loading state: spinner + aria-busy, replaces swapping the label to "Đang…" */
button.loading, .btn.loading { position: relative; color: transparent !important; pointer-events: none; }
button.loading::after, .btn.loading::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  color: inherit;
  animation: btn-spin 0.6s linear infinite;
}
button.primary.loading::after, .btn.primary.loading::after { color: var(--brand-ink); }
button:not(.primary).loading::after, .btn:not(.primary).loading::after { color: var(--ink); }
@keyframes btn-spin { to { transform: rotate(360deg); } }

/* icon button: circular, requires an aria-label (enforced by convention, see DESIGN_SYSTEM.md) */
.icon-btn {
  width: 36px;
  height: 36px;
  min-height: 36px;
  border-radius: 50%;
  padding: 0;
  display: grid;
  place-items: center;
}

.row { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: center; }

/* ---------- Drop zone (file import) ---------- */
.drop {
  border: 2px dashed var(--line);
  border-radius: var(--radius-lg);
  padding: 28px var(--space-4);
  text-align: center;
  transition: var(--duration-fast);
}
.drop.over { border-color: var(--brand); background: var(--brand-soft); }
.drop strong { display: block; font-size: var(--text-md); margin-bottom: var(--space-1); }

/* ---------- Stat tile ---------- */
.stats { display: flex; gap: var(--space-3); flex-wrap: wrap; margin-top: var(--space-3); }
.stat { background: var(--surface-2); border-radius: var(--radius-md); padding: var(--space-2) var(--space-3); font-size: var(--text-base); }
.stat b { font-size: var(--text-md); font-family: var(--font-mono); }

/* ---------- Inline alert ---------- */
.alert { border-radius: var(--radius-md); padding: 10px 14px; font-size: var(--text-base); margin-top: var(--space-3); }
.alert.err { background: var(--bad-soft); color: var(--bad); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }

/* ---------- Tabs ---------- */
.tabs { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-md); overflow: hidden; margin-top: var(--space-1); }
.tabs .tab { border: 0; border-radius: 0; padding: 8px 14px; background: var(--surface); min-height: 40px; }
.tabs .tab.active { background: var(--brand); color: var(--brand-ink); }
.tab-panel { margin-top: var(--space-3); }

/* ---------- Form fields ---------- */
label.field { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-base); color: var(--ink-muted); margin-top: var(--space-3); }
label.field .field-error { color: var(--bad); font-size: var(--text-sm); }
input[type="email"], input[type="number"], input[type="password"], input[type="text"], select {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  min-height: 44px;
}
input.invalid, select.invalid { border-color: var(--bad); }
input[type="number"] { width: 110px; padding: 8px 10px; min-height: 40px; }

.toggle { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-base); cursor: pointer; min-height: 44px; }
.toggle input[type="checkbox"], .toggle input[type="radio"] { accent-color: var(--brand); width: 18px; height: 18px; }

/* ---------- Chips / filters ---------- */
.chips { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  padding: 6px 12px;
  font-size: var(--text-base);
  cursor: pointer;
  user-select: none;
  background: var(--surface);
  color: var(--ink);
  min-height: 36px;
}
.chip input { accent-color: var(--brand); margin: 0; }
.chip.on { background: var(--brand-soft); border-color: var(--brand); }
.chip.zero { opacity: 0.45; }

/* ---------- Segmented control ---------- */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-md); overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 8px 14px; background: var(--surface); min-height: 40px; }
.seg button.on { background: var(--brand); color: var(--brand-ink); }
.seg button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Badge ---------- */
.badge, .tag { font-size: var(--text-xs); padding: 3px 9px; border-radius: var(--radius-pill); background: var(--surface-2); color: var(--ink-muted); display: inline-block; }

/* ---------- Progress bar ---------- */
.progress { height: 6px; background: var(--surface-2); border-radius: var(--radius-pill); overflow: hidden; margin-bottom: var(--space-4); }
.progress > div { height: 100%; background: var(--brand); transition: width var(--duration-base); }
.bar { height: 8px; border-radius: var(--radius-pill); background: var(--surface-2); overflow: hidden; min-width: 80px; }
.bar > div { height: 100%; }

/* ---------- Score ring ---------- */
.ring { --p: 0; width: 120px; height: 120px; border-radius: 50%; background: conic-gradient(var(--brand) calc(var(--p) * 1%), var(--surface-2) 0); display: grid; place-items: center; }
.ring > div { width: 92px; height: 92px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; text-align: center; font-weight: 700; font-size: var(--text-xl); font-family: var(--font-mono); line-height: 1.1; }
.ring small { display: block; font-size: var(--text-xs); color: var(--ink-muted); font-weight: 500; font-family: var(--font-ui); }

/* ---------- Table / list rows ---------- */
table.data-table { width: 100%; border-collapse: collapse; font-size: var(--text-base); margin-top: var(--space-2); }
table.data-table td { padding: var(--space-2) 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data-table td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; font-family: var(--font-mono); }

.list-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: 10px 0;
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  padding: var(--space-8) var(--space-4);
  color: var(--ink-muted);
}
.empty-state .empty-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto var(--space-3);
  border-radius: 50%;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-size: var(--text-xl);
}
.empty-state h3 { color: var(--ink); margin: 0 0 var(--space-1); font-size: var(--text-md); }
.empty-state p { margin: 0 0 var(--space-3); }

/* ---------- Skeleton loader ---------- */
@keyframes skeleton-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}
.skeleton {
  background: var(--surface-2);
  border-radius: var(--radius-md);
  animation: skeleton-pulse 1.3s ease-in-out infinite;
}
.skeleton-line { height: 14px; margin-bottom: var(--space-2); }
.skeleton-line.w-60 { width: 60%; }
.skeleton-line.w-40 { width: 40%; }
.skeleton-line.w-80 { width: 80%; }
.skeleton-line.flex1 { margin: 0; flex: 1; }
.skeleton-card { padding: var(--space-5); }
.skeleton-row { display: flex; gap: var(--space-3); align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.skeleton-row:first-child { border-top: 0; }
.skeleton-pill { height: 32px; width: 90px; border-radius: var(--radius-pill); }

/* ---------- Toast ---------- */
#toastHost {
  position: fixed;
  left: 50%;
  bottom: var(--space-5);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: var(--z-toast);
  width: min(90vw, 420px);
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-base);
  box-shadow: var(--shadow-1);
  background: var(--ink);
  color: var(--bg);
  animation: toast-in var(--duration-base) ease-out;
}
.toast.ok { background: var(--ok); color: var(--surface); }
.toast.err { background: var(--bad); color: var(--surface); }
.toast.info { background: var(--info); color: var(--surface); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Confirm dialog ---------- */
.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: grid;
  place-items: center;
  z-index: var(--z-modal);
  padding: var(--space-4);
}
.dialog {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
  padding: var(--space-5);
  max-width: 420px;
  width: 100%;
}
.dialog h2 { margin: 0 0 var(--space-2); }
.dialog p { margin: 0 0 var(--space-4); color: var(--ink-muted); }
.dialog .row { justify-content: flex-end; }

/* ---------- Brand mark ---------- */
.logo { display: block; flex: none; width: 32px; height: 32px; }
.logo.logo-lg { width: 56px; height: 56px; margin: 0 auto var(--space-4); display: block; }

/* ---------- Account menu ---------- */
.account-menu { position: relative; }
.account-avatar {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: var(--line);
  font-weight: 700;
}
.account-dropdown {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-2));
  min-width: 200px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-1);
  padding: var(--space-3);
  z-index: var(--z-dropdown);
  flex-direction: column;
  gap: var(--space-2);
}
.account-menu.open .account-dropdown { display: flex; }
.account-email { margin: 0 0 2px; word-break: break-all; font-size: var(--text-sm); }

@media (max-width: 400px) {
  .dialog { padding: var(--space-4); }
}

/* ---------- Layout utilities (replace one-off inline styles in JS templates) ---------- */
.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-5 { margin-top: var(--space-5); }
.mb-0 { margin-bottom: 0 !important; }
.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.w-34 { width: 34%; }
.m-0 { margin: 0; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.items-end { align-items: flex-end; }
.flex-col { display: flex; flex-direction: column; }
.flex-1 { flex: 1; }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-5 { gap: var(--space-5); }
.min-w-220 { min-width: 220px; }
