/* ==========================================================================
   KRManager — Modern Mobile-First Responsive UI + Desktop Spreadsheet Grid
   Uses CSS color-scheme + light-dark(), @container queries, native <dialog>,
   and scroll-snap navigation drawer with progressive enhancement.
   ========================================================================== */

:root {
  color-scheme: light dark;

  &:has(> head > meta[name="color-scheme"][content="dark"]) {
    color-scheme: dark;
  }
  &:has(> head > meta[name="color-scheme"][content="light"]) {
    color-scheme: light;
  }

  --bg: light-dark(#f1f5f9, #0b1120);
  --surface: light-dark(#ffffff, #111827);
  --surface-2: light-dark(#f8fafc, #1e293b);
  --surface-hover: light-dark(#eff6ff, #1f2b44);
  --border: light-dark(#e2e8f0, #293548);
  --border-strong: light-dark(#cbd5e1, #3b4b64);

  --text: light-dark(#0f172a, #f1f5f9);
  --text-muted: light-dark(#64748b, #94a3b8);

  --primary: light-dark(#2563eb, #3b82f6);
  --primary-hover: light-dark(#1d4ed8, #60a5fa);
  --primary-soft: light-dark(#dbeafe, #1e3a5f);
  --primary-text: light-dark(#1e40af, #93c5fd);

  --success-bg: light-dark(#dcfce7, #133929);
  --success-text: light-dark(#166534, #6ee7b7);
  --warn-bg: light-dark(#fef3c7, #3d2e10);
  --warn-text: light-dark(#92400e, #fcd34d);
  --danger-bg: light-dark(#fee2e2, #451a1f);
  --danger-text: light-dark(#991b1b, #fca5a5);
  --info-bg: light-dark(#e0e7ff, #252b54);
  --info-text: light-dark(#3730a3, #a5b4fc);

  --dirty-bg: light-dark(#fffbeb, #2c2410);
  --dirty-border: light-dark(#f59e0b, #d97706);

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.06);
  --shadow-md: 0 8px 24px rgb(0 0 0 / 0.14);
  --radius: 10px;
  --radius-sm: 6px;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.muted {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Theme toggle icon branching without JS-only classes */
.theme-icon-light { display: none; }
.theme-icon-dark { display: inline; }

@media (prefers-color-scheme: dark) {
  :root:not(:has(> head > meta[name="color-scheme"][content="light"])) .theme-icon-light { display: inline; }
  :root:not(:has(> head > meta[name="color-scheme"][content="light"])) .theme-icon-dark { display: none; }
}
:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-icon-light { display: inline; }
:root:has(> head > meta[name="color-scheme"][content="dark"]) .theme-icon-dark { display: none; }

/* ==========================================================================
   Buttons & Inputs
   ========================================================================== */
button, input, select, textarea {
  font: inherit;
  color: inherit;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.9rem;
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-2); border-color: var(--border-strong); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-hover); border-color: var(--primary); }
.btn-outline { background: transparent; border-color: var(--primary); color: var(--primary); }
.btn-outline:hover:not(:disabled) { background: var(--primary-soft); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.btn-danger { background: #dc2626; color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-success { background: #16a34a; color: #fff; }
.btn-success:hover:not(:disabled) { background: #15803d; }

.btn-sm { padding: 0.38rem 0.72rem; font-size: 0.8125rem; }
.btn-xs { padding: 0.22rem 0.5rem; font-size: 0.75rem; }
.btn-lg { padding: 0.7rem 1.2rem; font-size: 0.95rem; }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border-strong); }

.form-stack { display: flex; flex-direction: column; gap: 0.9rem; }
.form-row-2 { display: grid; grid-template-columns: 1fr; gap: 0.85rem; }
@media (min-width: 560px) {
  .form-row-2 { grid-template-columns: 1fr 1fr; }
}

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field label { font-size: 0.82rem; font-weight: 600; color: var(--text); }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 0.55rem 0.72rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  min-height: 38px;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: var(--primary);
}

.check-row {
  display: flex;
  align-items: flex-start;
  gap: 0.55rem;
  font-size: 0.875rem;
  cursor: pointer;
}
.check-row input[type="checkbox"] {
  margin-top: 0.2rem;
  accent-color: var(--primary);
  width: 16px;
  height: 16px;
}

.error-banner {
  margin: 0;
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-sm);
  background: var(--danger-bg);
  color: var(--danger-text);
  font-size: 0.85rem;
  font-weight: 500;
}
.info-banner {
  margin: 0;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-sm);
  background: var(--info-bg);
  color: var(--info-text);
  font-size: 0.875rem;
  font-weight: 600;
}

/* ==========================================================================
   Login View
   ========================================================================== */
.login-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1rem;
  background:
    radial-gradient(circle at 15% 15%, light-dark(#dbeafe, #172554) 0%, transparent 45%),
    radial-gradient(circle at 85% 85%, light-dark(#e0e7ff, #1e1b4b) 0%, transparent 45%),
    var(--bg);
}

.login-card {
  width: min(100%, 430px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.75rem;
  box-shadow: var(--shadow-md);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 1.4rem;
}
.login-brand h1 { margin: 0; font-size: 1.35rem; }
.login-brand p { margin: 0.15rem 0 0; font-size: 0.8rem; color: var(--text-muted); }

.brand-logo {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  color: #fff;
  font-weight: 800;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  letter-spacing: 0.5px;
}
.brand-logo.sm { width: 34px; height: 34px; font-size: 0.85rem; }

.login-hints {
  margin-top: 1.35rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--border);
}
.hint-title { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 0.55rem; }
.hint-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.45rem; }
.hint-chip {
  text-align: left;
  padding: 0.45rem 0.6rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 0.78rem;
  cursor: pointer;
}
.hint-chip:hover { border-color: var(--primary); background: var(--primary-soft); }

/* ==========================================================================
   App Layout (Mobile-First + Desktop Sidebar)
   ========================================================================== */
.app-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
}

.sidebar {
  display: none;
}

@media (min-width: 1024px) {
  .app-shell {
    grid-template-columns: 256px minmax(0, 1fr);
  }
  .sidebar {
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100dvh;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem 0.75rem;
    overflow-y: auto;
  }
  .mobile-only {
    display: none !important;
  }
}

.sidebar-brand, .brand-inline {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}
.sidebar-brand {
  padding: 0.25rem 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
}
.brand-title { font-weight: 800; font-size: 1rem; }
.brand-sub { font-size: 0.73rem; color: var(--text-muted); }

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  flex: 1;
}
.nav-group-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 0.75rem 0.65rem 0.25rem;
}
.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  padding: 0.58rem 0.7rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  text-align: left;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active {
  background: var(--primary-soft);
  color: var(--primary-text);
  font-weight: 700;
}
.nav-item-left { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.nav-badge {
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
}
.nav-badge:empty { display: none; }
.nav-item.active .nav-badge {
  background: var(--primary);
  color: #fff;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.user-card { display: flex; align-items: center; gap: 0.65rem; padding: 0.25rem; }
.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-text);
  font-weight: 800;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.user-meta { min-width: 0; }
.user-name { font-size: 0.85rem; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-role { font-size: 0.74rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.35rem; }

/* Main Column */
.main-wrap {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100dvh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.7rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
.page-title { margin: 0; font-size: 1.05rem; font-weight: 800; line-height: 1.2; }
.page-subtitle { margin: 0.1rem 0 0; font-size: 0.78rem; color: var(--text-muted); display: none; }
@media (min-width: 640px) {
  .topbar { padding: 0.75rem 1.35rem; }
  .page-title { font-size: 1.2rem; }
  .page-subtitle { display: block; }
}
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0; }
.trace-quick-form { display: none; }
@media (min-width: 760px) {
  .trace-quick-form { display: block; }
  .trace-input { min-width: 210px; padding: 0.35rem 0.65rem; font-size: 0.82rem; }
}
.link-btn {
  background: transparent;
  border: 0;
  color: var(--primary);
  font-weight: 700;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.grid-pagination {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  margin-top: 0.65rem;
  padding: 0.55rem 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.82rem;
}
.pager-controls { display: flex; align-items: center; gap: 0.4rem; }
.trace-section { margin-bottom: 1rem; }
.trace-section h2 { margin: 0 0 0.45rem; font-size: 0.9rem; font-weight: 800; color: var(--primary-text); }
.trace-mini-list { display: flex; flex-direction: column; gap: 0.4rem; }
.trace-mini-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  background: var(--surface-2);
  font-size: 0.82rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.4rem;
}

.main-content {
  flex: 1;
  padding: 0.9rem;
  padding-bottom: 5rem;
  min-width: 0;
}
@media (min-width: 1024px) {
  .main-content { padding: 1.25rem 1.35rem 2rem; }
}

/* Mobile bottom bar */
.mobile-bottom-bar {
  position: fixed;
  inset-inline: 0;
  inset-block-end: 0;
  z-index: 25;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.35rem 0.25rem calc(0.35rem + env(safe-area-inset-bottom));
}
.mobile-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.25rem;
  cursor: pointer;
}
.mobile-tab.active { color: var(--primary); }
.mobile-tab span:first-child { font-size: 1.05rem; }

/* ==========================================================================
   Mobile Navigation Drawer (<dialog> + scroll-snap + scroll-driven backdrop)
   ========================================================================== */
.Drawer {
  --drawer-width: min(20em, 80dvw, 100dvw - 24px);
  --drawer-backdrop: 0;
  inset: 0;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  background: transparent;
  border: 0;
  overflow: visible;
}
.Drawer::backdrop {
  background: #000;
  opacity: calc(var(--drawer-backdrop) / 2);
  overflow: hidden;
  overscroll-behavior: contain;
}
.Drawer-scroller {
  position: relative;
  display: grid;
  grid-template-columns: var(--drawer-width) 100%;
  overflow-x: scroll;
  overscroll-behavior: none;
  scrollbar-width: none;
  scroll-snap-type: x mandatory;
}
@media (prefers-reduced-motion: no-preference) {
  .Drawer-scroller { scroll-behavior: smooth; }
}
.Drawer-scroller::after {
  content: '';
  scroll-snap-align: end;
  scroll-initial-target: nearest;
}
.Drawer-sheet {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100svh;
  background: var(--surface);
  color: var(--text);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  scroll-snap-align: start;
  scrollbar-width: none;
  padding: 0.9rem 0.75rem;
}
.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.65rem;
}
.drawer-footer {
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

@supports (animation-timeline: scroll()) {
  .Drawer {
    timeline-scope: --drawer-fade;
    animation: fade-drawer-backdrop linear both;
    animation-timeline: --drawer-fade;
  }
  .Drawer-scroller {
    scroll-timeline: --drawer-fade x;
  }
  @property --drawer-backdrop {
    syntax: '<number>';
    inherits: true;
    initial-value: 0;
  }
  @keyframes fade-drawer-backdrop {
    0% { --drawer-backdrop: 1; }
    100% { --drawer-backdrop: 0; }
  }
}

/* ==========================================================================
   Native Modal Dialogs
   ========================================================================== */
dialog.modal {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-md);
  width: min(94vw, 560px);
  max-height: 90dvh;
  overflow: hidden;
}
dialog.modal.modal-sm { width: min(92vw, 420px); }
dialog.modal.modal-lg { width: min(95vw, 760px); }
dialog.modal.modal-xl { width: min(96vw, 960px); }

dialog.modal[open] {
  display: flex;
  flex-direction: column;
}
dialog:modal {
  overscroll-behavior: contain;
}
dialog.modal::backdrop {
  background: rgb(15 23 42 / 0.58);
  backdrop-filter: blur(3px);
  overflow: hidden;
  overscroll-behavior: contain;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
}
.modal-body {
  padding: 1.15rem 1.2rem;
  overflow-y: auto;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  padding-top: 0.75rem;
  margin-top: 0.25rem;
  border-top: 1px solid var(--border);
}
.modal-actions-inline { display: flex; flex-wrap: wrap; gap: 0.6rem; }
.divider { border: 0; border-top: 1px solid var(--border); margin: 1.1rem 0; }

/* ==========================================================================
   Dashboard Cards & Breakdowns
   ========================================================================== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 0.85rem;
  margin-bottom: 1.15rem;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  color: inherit;
  text-decoration: none;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.12s ease;
}
.kpi-card:hover { border-color: var(--primary); transform: translateY(-1px); }
.kpi-label { font-size: 0.78rem; font-weight: 600; color: var(--text-muted); }
.kpi-value { font-size: 1.7rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 0.76rem; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 0.4rem; }

.dash-panels {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
@media (min-width: 860px) {
  .dash-panels { grid-template-columns: 1fr 1fr; }
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow-sm);
}
.panel h2 { margin: 0 0 0.75rem; font-size: 0.98rem; font-weight: 800; }
.bar-list { display: flex; flex-direction: column; gap: 0.6rem; }
.bar-row { display: flex; flex-direction: column; gap: 0.25rem; }
.bar-meta { display: flex; justify-content: space-between; font-size: 0.83rem; }
.bar-track { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--primary); }
.bar-fill.success { background: #16a34a; }
.bar-fill.warn { background: #d97706; }
.bar-fill.danger { background: #dc2626; }

/* ==========================================================================
   Toolbar, Filters & Formula Legend
   ========================================================================== */
.grid-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  margin-bottom: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.85rem;
}
.toolbar-left, .toolbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}
.search-input {
  min-width: 210px;
  max-width: 100%;
  padding: 0.42rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  font-size: 0.85rem;
}
.search-input:focus { outline: 2px solid var(--primary); background: var(--surface); }

.filter-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.7rem;
}
.pill {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 0.26rem 0.7rem;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}
.pill.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.formula-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
  padding: 0.45rem 0.75rem;
  border-radius: var(--radius-sm);
  background: var(--info-bg);
  color: var(--info-text);
  font-size: 0.78rem;
}
.formula-strip code { font-family: var(--font-mono); font-weight: 700; }

/* ==========================================================================
   Responsive Table & Desktop Excel Spreadsheet Grid (@container)
   ========================================================================== */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: calc(100dvh - 265px);
  min-block-size: 320px;
  container-type: inline-size;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.kr-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.83rem;
}

.kr-table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 4;
}

.kr-table thead th {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 0.58rem 0.55rem;
  border-bottom: 2px solid var(--border-strong);
  border-right: 1px solid var(--border);
  white-space: nowrap;
  text-align: left;
  user-select: none;
}
.kr-table thead th.col-formula {
  background: var(--info-bg);
  color: var(--info-text);
}

/* Sticky row header column on desktop */
.kr-table tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 2;
  background: var(--surface);
  font-weight: 700;
  text-align: left;
  padding: 0.35rem 0.55rem;
  border-bottom: 1px solid var(--border);
  border-right: 2px solid var(--border-strong);
  white-space: nowrap;
}
.kr-table thead th.sticky-col {
  position: sticky;
  inset-inline-start: 0;
  z-index: 5;
  border-right: 2px solid var(--border-strong);
}

.kr-table tbody td {
  padding: 0.2rem 0.3rem;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
}

.kr-table tbody tr:hover td,
.kr-table tbody tr:hover th {
  background: var(--surface-hover);
}
.kr-table tbody tr.row-dirty td,
.kr-table tbody tr.row-dirty th {
  background: var(--dirty-bg);
}
.kr-table tbody tr.row-dup-phone td[data-key="phone"],
.kr-table tbody tr.row-dup-phone th {
  background: var(--danger-bg);
  color: var(--danger-text);
}

/* Spreadsheet inline inputs */
.grid-cell-input {
  width: 100%;
  min-width: 80px;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  padding: 0.32rem 0.42rem;
  border-radius: 4px;
  font-size: 0.83rem;
}
.grid-cell-input:hover {
  border-color: var(--border-strong);
  background: var(--surface);
}
.grid-cell-input:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  background: var(--surface);
  border-color: var(--primary);
}
.grid-cell-input.dirty {
  border-color: var(--dirty-border);
}
.grid-cell-input.w-xs { min-width: 58px; }
.grid-cell-input.w-sm { min-width: 96px; }
.grid-cell-input.w-md { min-width: 135px; }
.grid-cell-input.w-lg { min-width: 210px; }

.row-head-inner {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}
.row-idx {
  font-size: 0.72rem;
  color: var(--text-muted);
  min-width: 22px;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  white-space: nowrap;
}
.badge-success { background: var(--success-bg); color: var(--success-text); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge-danger { background: var(--danger-bg); color: var(--danger-text); }
.badge-info { background: var(--info-bg); color: var(--info-text); }
.badge-neutral { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }

/* Mobile Stacked Card Transformation using @container */
@container (width < 640px) {
  .kr-table,
  .kr-table thead,
  .kr-table tbody,
  .kr-table tr,
  .kr-table th,
  .kr-table td {
    display: block;
  }
  .kr-table thead {
    display: none;
  }
  .kr-table tbody {
    padding: 0.6rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
  }
  .kr-table tbody tr {
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
  }
  .kr-table tbody th {
    position: sticky;
    inset-block-start: 0;
    inset-inline-start: auto;
    z-index: 2;
    background: var(--surface-2);
    border-right: 0;
    border-bottom: 1px solid var(--border);
    padding: 0.6rem 0.75rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .kr-table tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.45rem 0.75rem;
    border-right: 0;
    border-bottom: 1px dashed var(--border);
    white-space: normal;
  }
  .kr-table tbody td:last-child { border-bottom: 0; }
  .kr-table tbody td[data-label]::before {
    content: attr(data-label) ": ";
    font-size: 0.76rem;
    font-weight: 700;
    color: var(--text-muted);
    flex-shrink: 0;
  }
  .kr-table tbody td .grid-cell-input {
    text-align: right;
    max-width: 65%;
  }
}

/* ==========================================================================
   RBAC Permission Matrix & User Cards
   ========================================================================== */
.export-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.55rem;
  margin-top: 0.65rem;
}
@media (min-width: 520px) {
  .export-grid { grid-template-columns: 1fr 1fr; }
  .export-grid button:first-child { grid-column: 1 / -1; }
}

.role-checks {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.5rem;
}
.role-check-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.65rem;
  background: var(--surface-2);
}

.matrix-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.35rem;
}
.matrix-quick { display: flex; gap: 0.35rem; }

.perm-matrix {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.65rem;
  max-height: 45dvh;
  overflow-y: auto;
  padding: 0.25rem;
}
@media (min-width: 680px) {
  .perm-matrix { grid-template-columns: 1fr 1fr; }
}
.perm-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.75rem;
  background: var(--surface-2);
}
.perm-group-title {
  font-size: 0.8rem;
  font-weight: 800;
  margin-bottom: 0.45rem;
  color: var(--primary-text);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.perm-items {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(125px, 1fr));
  gap: 0.35rem;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 0.85rem;
}
.entity-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.entity-card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 0.5rem; }
.entity-card-title { font-weight: 800; font-size: 0.95rem; }
.entity-card-actions { display: flex; gap: 0.4rem; margin-top: auto; padding-top: 0.5rem; border-top: 1px solid var(--border); }
.chip-list { display: flex; flex-wrap: wrap; gap: 0.35rem; }

/* Toasts */
.toast-container {
  position: fixed;
  inset-block-end: 1.25rem;
  inset-inline-end: 1rem;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(92vw, 380px);
}
.toast {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-inline-start: 4px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.95rem;
  box-shadow: var(--shadow-md);
  font-size: 0.85rem;
  font-weight: 600;
}
.toast.success { border-inline-start-color: #16a34a; }
.toast.error { border-inline-start-color: #dc2626; }
.toast.warn { border-inline-start-color: #d97706; }

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
}
