/* ==========================================================================
   components.css — reusable UI: buttons, forms, cards, KPI, tables, badges,
   modal, side panel, toast, tabs, chips, avatars, empty states.
   ========================================================================== */

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  height: 38px; padding: 0 var(--sp-4); border-radius: var(--r-sm);
  font-family: var(--font-display); font-size: var(--fs-base); font-weight: 600;
  letter-spacing: var(--track-snug); white-space: nowrap; user-select: none;
  border: 1px solid transparent;
  transition: transform var(--dur-fast) var(--ease-out),
              background-color var(--dur) var(--ease-out),
              box-shadow var(--dur) var(--ease-out), color var(--dur) var(--ease-out);
}
.btn svg { width: 17px; height: 17px; }
.btn:active { transform: translateY(1px) scale(0.99); }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.btn-primary { background: var(--primary); color: var(--text-on-brand); }
.btn-primary:hover { background: var(--primary-hover); color: #fff; }
.btn-primary:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--border-strong); }

.btn-subtle { background: var(--surface-3); color: var(--text); }
.btn-subtle:hover { background: var(--border); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.94); }

.btn-sm { height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-sm); }
.btn-icon { width: 38px; height: 38px; padding: 0; border-radius: var(--r-sm); }
.btn-icon.btn-sm { width: 32px; height: 32px; }
.btn-block { width: 100%; }

/* ---------------- Forms ---------------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .form-label {
  font-family: var(--font-display); font-size: var(--fs-sm); font-weight: 600;
  color: var(--text); letter-spacing: var(--track-snug);
}
.field .hint { font-size: var(--fs-xs); color: var(--text-muted); }
.input, .select, .textarea {
  width: 100%; height: 40px; padding: 0 var(--sp-3);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  transition: border-color var(--dur) var(--ease-out), box-shadow var(--dur) var(--ease-out);
  font-size: var(--fs-base);
}
.textarea { height: auto; min-height: 84px; padding: var(--sp-3); line-height: 1.6; resize: vertical; }
.select { appearance: none; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); }
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }
.input:disabled, .select:disabled { background: var(--surface-3); color: var(--text-muted); cursor: not-allowed; }

.input-icon { position: relative; }
.input-icon svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--text-faint); pointer-events: none; }
.input-icon .input { padding-left: 36px; }

.checkbox { display: inline-flex; align-items: center; gap: var(--sp-2); cursor: pointer; font-size: var(--fs-base); }
.checkbox input { width: 17px; height: 17px; accent-color: var(--primary); }

/* ---------------- Surfaces / cards ---------------- */
.card {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); box-shadow: var(--shadow-sm);
}
.card-pad { padding: var(--sp-5); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--border-soft); }
.card-head h3 { font-size: var(--fs-lg); }
.card-body { padding: var(--sp-5); }

/* ---------------- KPI card ---------------- */
.kpi {
  position: relative; overflow: hidden; padding: var(--sp-5);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg);
  transition: border-color var(--dur) var(--ease-out);
}
.kpi:hover { border-color: var(--border-strong); }
/* Two-tone: KPI accents use the single blue accent (ignore per-card colors). */
.kpi::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--primary); }
.kpi .kpi-top { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.kpi .kpi-icon { width: 36px; height: 36px; border-radius: var(--r-sm);
  display: grid; place-items: center; color: var(--primary); background: var(--primary-soft); }
.kpi .kpi-icon svg { width: 19px; height: 19px; }
.kpi .kpi-label { font-size: var(--fs-sm); color: var(--text-muted); font-weight: 500; }
.kpi .kpi-value { font-family: var(--font-display); font-weight: 800; font-size: var(--fs-2xl);
  letter-spacing: var(--track-tight); color: var(--text-strong); line-height: 1.1; margin-top: var(--sp-3); }
.kpi .kpi-sub { display: flex; align-items: center; gap: var(--sp-2); margin-top: var(--sp-2); font-size: var(--fs-sm); }
.kpi .kpi-multi { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: var(--sp-3); }
.kpi .kpi-multi span { font-size: var(--fs-xs); color: var(--text-muted); }
.kpi .kpi-multi b { font-family: var(--font-display); color: var(--text); font-weight: 700; }

.delta { display: inline-flex; align-items: center; gap: 3px; font-weight: 600; font-size: var(--fs-sm);
  padding: 1px 7px; border-radius: var(--r-full); }
.delta svg { width: 13px; height: 13px; }
.delta.up { color: var(--success); background: var(--success-bg); }
.delta.down { color: var(--danger); background: var(--danger-bg); }
.delta.flat { color: var(--text-muted); background: var(--surface-3); }

/* ---------------- Badges ---------------- */
.badge { display: inline-flex; align-items: center; gap: 5px; height: 23px; padding: 0 9px;
  border-radius: var(--r-full); font-size: var(--fs-xs); font-weight: 600;
  font-family: var(--font-display); letter-spacing: var(--track-snug); white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.success { color: var(--success); background: var(--success-bg); }
.badge.warning { color: var(--warning); background: var(--warning-bg); }
.badge.danger  { color: var(--danger);  background: var(--danger-bg); }
.badge.info    { color: var(--info);    background: var(--info-bg); }
.badge.pending { color: var(--pending); background: var(--pending-bg); }
.badge.neutral { color: var(--text-muted); background: var(--surface-3); }
.badge.plain::before { display: none; }

/* ---------------- Chips / tabs ---------------- */
.chips { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.chip { height: 32px; padding: 0 var(--sp-3); border-radius: var(--r-full);
  background: var(--surface); border: 1px solid var(--border); color: var(--text-muted);
  font-size: var(--fs-sm); font-weight: 600; font-family: var(--font-display);
  display: inline-flex; align-items: center; gap: 6px;
  transition: background-color var(--dur) var(--ease-out), color var(--dur) var(--ease-out), border-color var(--dur) var(--ease-out); }
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip.active { background: var(--primary-soft); color: var(--primary); border-color: var(--primary-soft-border); }
.chip .count { font-size: var(--fs-xs); opacity: 0.75; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); }
.tab { position: relative; height: 42px; padding: 0 var(--sp-4); background: none;
  color: var(--text-muted); font-family: var(--font-display); font-weight: 600; font-size: var(--fs-base);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: color var(--dur) var(--ease-out), border-color var(--dur) var(--ease-out); }
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------------- Data table ---------------- */
.table-wrap { width: 100%; overflow-x: auto; border-radius: var(--r-lg); }
table.data { width: 100%; border-collapse: collapse; font-size: var(--fs-base); }
table.data thead th {
  position: sticky; top: 0; z-index: 1; text-align: left; white-space: nowrap;
  font-family: var(--font-display); font-size: var(--fs-xs); font-weight: 700;
  letter-spacing: var(--track-wide); text-transform: uppercase; color: var(--text-muted);
  background: var(--surface-2); padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
}
table.data thead th.sortable { cursor: pointer; user-select: none; }
table.data thead th.sortable:hover { color: var(--text); }
table.data thead th .th-sort { margin-left: 4px; opacity: 0.5; }
table.data tbody td { padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border-soft);
  color: var(--text); vertical-align: middle; }
table.data tbody tr { transition: background-color var(--dur-fast) var(--ease-out); }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr:last-child td { border-bottom: none; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Per-column filter row */
table.data tr.filters th { background: var(--surface); padding: 6px var(--sp-3) var(--sp-3); border-bottom: 1px solid var(--border); }
table.data tr.filters .input, table.data tr.filters .select { height: 32px; font-size: var(--fs-sm); }

.table-toolbar { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  flex-wrap: wrap; margin-bottom: var(--sp-4); }
.table-foot { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-1); flex-wrap: wrap; }
.pager { display: inline-flex; align-items: center; gap: 4px; }
.pager .btn-icon { width: 32px; height: 32px; }
.pager .page-info { font-size: var(--fs-sm); color: var(--text-muted); padding: 0 var(--sp-2); }

/* ---------------- Avatars ---------------- */
.avatar { width: 36px; height: 36px; border-radius: var(--r-full); display: grid; place-items: center;
  font-family: var(--font-display); font-weight: 700; font-size: var(--fs-sm); color: #fff;
  background: var(--accent-600); flex: none; }
.avatar.sm { width: 28px; height: 28px; font-size: var(--fs-xs); }
.avatar.lg { width: 56px; height: 56px; font-size: var(--fs-lg); }
.with-name { display: inline-flex; align-items: center; gap: var(--sp-3); }
.with-name .nm { font-weight: 600; color: var(--text-strong); }
.with-name .sub { font-size: var(--fs-xs); color: var(--text-muted); }

/* ---------------- Modal ---------------- */
.modal-backdrop { position: fixed; inset: 0; z-index: var(--z-modal); background: var(--overlay);
  backdrop-filter: blur(3px); display: grid; place-items: center; padding: var(--sp-4);
  animation: fade var(--dur) var(--ease-out) both; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(0.98); } to { opacity: 1; transform: none; } }
.modal { width: 100%; max-width: 560px; max-height: 90vh; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; animation: pop var(--dur-slow) var(--ease-spring) both; }
.modal.wide { max-width: 860px; } .modal.xwide { max-width: 1040px; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-3);
  padding: var(--sp-5) var(--sp-5) var(--sp-4); border-bottom: 1px solid var(--border-soft); }
.modal-head h3 { font-size: var(--fs-xl); }
.modal-head .sub { font-size: var(--fs-sm); color: var(--text-muted); margin-top: 2px; }
.modal-body { padding: var(--sp-5); overflow-y: auto; }
.modal-foot { display: flex; align-items: center; justify-content: flex-end; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5); border-top: 1px solid var(--border-soft); background: var(--surface-2); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-4); }
.form-grid .span-2 { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }

/* ---------------- Side detail panel ---------------- */
.panel-backdrop { position: fixed; inset: 0; z-index: var(--z-panel); background: var(--overlay);
  backdrop-filter: blur(2px); animation: fade var(--dur) var(--ease-out) both; }
.side-panel { position: fixed; top: 0; right: 0; bottom: 0; z-index: var(--z-panel);
  width: min(520px, 94vw); background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column;
  animation: slidein var(--dur-slow) var(--ease-out) both; }
.side-panel.lg { width: min(680px, 96vw); }
@keyframes slidein { from { transform: translateX(100%); } to { transform: none; } }
.panel-head { padding: var(--sp-5); border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-3); }
.panel-body { padding: var(--sp-5); overflow-y: auto; flex: 1; }
.panel-section { padding: var(--sp-4) 0; border-bottom: 1px solid var(--border-soft); }
.panel-section:last-child { border-bottom: none; }
.panel-section .sec-title { font-family: var(--font-display); font-weight: 700; font-size: var(--fs-sm);
  letter-spacing: var(--track-wide); text-transform: uppercase; color: var(--text-muted); margin-bottom: var(--sp-3); }

.dl { display: grid; grid-template-columns: auto 1fr; gap: var(--sp-2) var(--sp-4); }
.dl dt { color: var(--text-muted); font-size: var(--fs-sm); }
.dl dd { color: var(--text); text-align: right; font-weight: 500; }

/* ---------------- Toast ---------------- */
.toast-wrap { position: fixed; top: calc(var(--topbar-h) + 12px); right: var(--sp-5); z-index: var(--z-toast);
  display: flex; flex-direction: column; gap: var(--sp-3); width: min(360px, calc(100vw - 32px)); }
.toast { display: flex; align-items: flex-start; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4);
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--info);
  border-radius: var(--r-md); box-shadow: var(--shadow-lg); animation: pop var(--dur-slow) var(--ease-spring) both; }
.toast.success { border-left-color: var(--success); }
.toast.danger  { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
.toast .t-icon { color: var(--info); margin-top: 1px; } .toast.success .t-icon { color: var(--success); }
.toast.danger .t-icon { color: var(--danger); } .toast.warning .t-icon { color: var(--warning); }
.toast .t-icon svg { width: 18px; height: 18px; }
.toast .t-title { font-weight: 700; font-family: var(--font-display); color: var(--text-strong); font-size: var(--fs-base); }
.toast .t-msg { font-size: var(--fs-sm); color: var(--text-muted); }

/* ---------------- Empty state ---------------- */
.empty { text-align: center; padding: var(--sp-7) var(--sp-4); color: var(--text-muted); }
.empty .e-icon { width: 52px; height: 52px; margin: 0 auto var(--sp-3); border-radius: var(--r-lg);
  display: grid; place-items: center; background: var(--surface-3); color: var(--text-faint); }
.empty .e-icon svg { width: 24px; height: 24px; }
.empty h4 { color: var(--text); margin-bottom: 4px; }

/* ---------------- Progress / meter ---------------- */
.meter { height: 8px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: inherit; background: var(--primary); }

/* ---------------- File viewer (sheet/doc preview) ---------------- */
.fv-sheet table { border-collapse: collapse; width: 100%; font-size: var(--fs-sm); font-family: var(--font-mono); }
.fv-sheet td, .fv-sheet th { border: 1px solid var(--border); padding: 4px 8px; white-space: nowrap; }
.fv-sheet tr:first-child td { background: var(--surface-2); font-weight: 600; }
.fv-doc h1, .fv-doc h2, .fv-doc h3 { margin: 12px 0 6px; }
.fv-doc p { margin: 6px 0; }
.attach { display: inline-flex; align-items: center; gap: 6px; max-width: 220px; padding: 6px 10px; margin-top: 6px;
  border: 1px solid var(--border); border-radius: var(--r-sm); background: var(--surface-2); cursor: pointer; font-size: var(--fs-sm); }
.attach:hover { border-color: var(--border-strong); }
.attach img { width: 100%; border-radius: var(--r-sm); display: block; }
.msg .attach { background: rgba(255,255,255,0.12); border-color: rgba(255,255,255,0.2); }
.msg.them .attach { background: var(--surface); border-color: var(--border); }

/* ---------------- Misc ---------------- */
.divider { height: 1px; background: var(--border-soft); border: none; margin: var(--sp-4) 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.accent-stripe { height: 4px; border-radius: var(--r-full); background: var(--accent, var(--primary)); }
.skeleton { background: linear-gradient(90deg, var(--surface-3) 25%, var(--border) 37%, var(--surface-3) 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease-in-out infinite; border-radius: var(--r-sm); }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }
