/* ==========================================================================
   Components — buttons, fields, badges, cards, tables, menus
   Naming: block__element--modifier
   ========================================================================== */

/* ---- Button ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 2.5rem;
  padding: 0 var(--space-4);
  border: 0.0625rem solid transparent;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: var(--text-small);
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-standard),
    border-color var(--duration-fast) var(--ease-standard),
    box-shadow var(--duration-fast) var(--ease-standard),
    transform var(--duration-fast) var(--ease-standard);
}

.btn:active {
  transform: translateY(0.0625rem);
}

.btn--primary {
  background: var(--color-accent);
  color: var(--color-ink-on-accent);
  box-shadow: var(--shadow-xs);
}

.btn--primary:hover {
  filter: brightness(1.06);
}

.btn--secondary {
  background: var(--color-surface);
  border-color: var(--color-border-strong);
  color: var(--color-ink);
}

.btn--secondary:hover {
  border-color: var(--color-accent-ink);
  color: var(--color-accent-ink);
}

.btn--ghost {
  background: transparent;
  color: var(--color-ink-muted);
}

.btn--ghost:hover {
  background: var(--color-canvas-inset);
  color: var(--color-ink);
}

.btn--danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

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

.btn--sm {
  height: 2rem;
  padding: 0 var(--space-3);
  font-size: var(--text-small);
}

.btn--icon {
  width: 2.5rem;
  padding: 0;
}

/* Belt-and-suspenders: an <svg> with only a viewBox (no width/height attrs)
   falls back to the browser's default replaced-element box and renders
   oversized/inconsistent — this keeps every icon-button glyph at a uniform
   size regardless of whether the markup remembered to size it explicitly. */
.btn--icon svg {
  width: 1rem;
  height: 1rem;
}

.btn--block {
  width: 100%;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---- Field / Input ---- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.field__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.field__label {
  font-size: var(--text-small);
  font-weight: 600;
  color: var(--color-ink);
}

.field__hint {
  font-size: var(--text-small);
  color: var(--color-ink-faint);
}

.field__link {
  font-size: var(--text-small);
  font-weight: 600;
  color: var(--color-accent-ink);
}

.field__link:hover {
  text-decoration: underline;
}

.field__control {
  position: relative;
  display: flex;
  align-items: center;
}

.input,
.select,
.textarea {
  width: 100%;
  height: 2.75rem;
  padding: 0 var(--space-4);
  background: var(--color-surface);
  border: 0.0625rem solid var(--color-border-strong);
  border-radius: var(--radius-md);
  color: var(--color-ink);
  font-family: var(--font-body);
  font-size: var(--text-body);
  transition: border-color var(--duration-fast) var(--ease-standard),
    box-shadow var(--duration-fast) var(--ease-standard);
}

.textarea {
  height: auto;
  min-height: 6rem;
  padding: var(--space-3) var(--space-4);
  resize: vertical;
}

.input::placeholder,
.textarea::placeholder {
  color: var(--color-ink-faint);
}

.input:hover,
.select:hover,
.textarea:hover {
  border-color: var(--color-border-strong);
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--color-accent-ink);
  box-shadow: 0 0 0 0.1875rem var(--color-focus-ring);
}

/* Locked fields (e.g. profile login) — visually distinct from editable
   inputs so it's obvious at a glance the value can't be changed here. */
.input:disabled,
.select:disabled {
  background: var(--color-canvas-inset);
  color: var(--color-ink-faint);
  border-color: var(--color-border);
  cursor: not-allowed;
}

.field__control--has-suffix .input {
  padding-right: 2.75rem;
}

.field__suffix-btn {
  position: absolute;
  right: var(--space-1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border: none;
  background: transparent;
  color: var(--color-ink-faint);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.field__suffix-btn:hover {
  color: var(--color-ink-muted);
  background: var(--color-canvas-inset);
}

.field--error .input {
  border-color: var(--color-danger);
}

.field__error {
  font-size: var(--text-small);
  color: var(--color-danger);
}

.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 1.25rem) center, calc(100% - 1rem) center;
  background-size: 0.3125rem 0.3125rem, 0.3125rem 0.3125rem;
  background-repeat: no-repeat;
  color: var(--color-ink);
}

/* Checkbox */
.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-small);
  color: var(--color-ink-muted);
  cursor: pointer;
}

.checkbox input {
  width: 1.125rem;
  height: 1.125rem;
  margin: 0;
  accent-color: var(--color-accent);
}

/* Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 2.5rem;
  height: 1.5rem;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch__track {
  position: absolute;
  inset: 0;
  background: var(--color-border-strong);
  border-radius: var(--radius-full);
  transition: background var(--duration-base) var(--ease-standard);
}

.switch__track::before {
  content: "";
  position: absolute;
  top: 0.1875rem;
  left: 0.1875rem;
  width: 1.125rem;
  height: 1.125rem;
  background: #fff;
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-xs);
  transition: transform var(--duration-base) var(--ease-standard);
}

.switch input:checked+.switch__track {
  background: var(--color-accent);
}

.switch input:checked+.switch__track::before {
  transform: translateX(1rem);
}

/* ---- Badge ---- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 1.5rem;
  padding: 0 var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-micro);
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  background: var(--color-canvas-inset);
  color: var(--color-ink-muted);
}

.badge::before {
  content: "";
  width: 0.375rem;
  height: 0.375rem;
  border-radius: var(--radius-full);
  background: currentColor;
}

.badge--success {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.badge--warning {
  background: var(--color-warning-soft);
  color: var(--color-warning);
}

.badge--danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

.badge--accent {
  background: var(--color-accent-soft);
  color: var(--color-accent-ink);
}

/* ---- Avatar ---- */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--color-teal), var(--color-violet));
  color: #fff;
  font-family: var(--font-display);
  font-size: var(--text-small);
  font-weight: 600;
  flex-shrink: 0;
}

.avatar--sm {
  width: 1.75rem;
  height: 1.75rem;
  font-size: 0.6875rem;
}

/* ---- Card ---- */
.card {
  background: var(--color-surface);
  border: 0.0625rem solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}

.card__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-5) var(--space-4);
}

.card__body {
  padding: 0 var(--space-5) var(--space-5);
}

/* Stacked direct children (fields, alerts, etc.) previously sat flush
   against each other with no vertical gap — this only spaces top-level
   children, so a body using its own grid/flex wrapper for a sub-layout is
   unaffected (that wrapper is itself a single direct child). */
.card__body > * + * {
  margin-top: var(--space-4);
}

.card__body--flush {
  padding: 0;
}

.card__footer {
  padding: var(--space-4) var(--space-5);
  border-top: 0.0625rem solid var(--color-border);
}

/* Knowledge group card */
.group-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: var(--space-4);
}

.group-card {
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.group-card__header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
}

.group-card__icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--radius-md);
  background: var(--color-accent-soft);
  color: var(--color-accent-ink);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.group-card__icon svg {
  width: 1.125rem;
  height: 1.125rem;
}

.group-card__title {
  flex: 1;
  min-width: 0;
  padding-top: 0.125rem;
}

.group-card__title strong {
  display: block;
  font-family: var(--font-display);
  font-weight: 600;
}

.group-card__desc {
  line-height: var(--leading-snug);
}

/* Stat card */
.stat {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-5);
}

.stat__value {
  font-family: var(--font-display);
  font-size: 1.75rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.stat__delta {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-family: var(--font-mono);
  font-size: var(--text-small);
  font-weight: 500;
}

.stat__delta--up {
  color: var(--color-success);
}

.stat__delta--down {
  color: var(--color-danger);
}

/* ---- Alert ---- */
.alert {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  border: 0.0625rem solid transparent;
  font-size: var(--text-small);
  line-height: var(--leading-snug);
}

.alert__icon {
  flex-shrink: 0;
  width: 1.125rem;
  height: 1.125rem;
  margin-top: 0.0625rem;
}

.alert__title {
  font-weight: 600;
  margin-bottom: 0.125rem;
}

.alert--info {
  background: var(--color-accent-soft);
  border-color: var(--color-accent-soft-strong);
  color: var(--color-accent-ink);
}

.alert--success {
  background: var(--color-success-soft);
  color: var(--color-success);
}

.alert--warning {
  background: var(--color-warning-soft);
  color: var(--color-warning);
}

.alert--danger {
  background: var(--color-danger-soft);
  color: var(--color-danger);
}

/* ---- Notification toast ----
   Auto-appearing/auto-dismissing card for new notifications (topbar bell),
   distinct from the ad-hoc error toasts in app.js's showErrorToast — this
   one has a shrinking TTL bar and a close button that marks the underlying
   notification read (auto-expiry deliberately does not). */
#notification-toast-stack {
  position: fixed;
  top: 1.25rem;
  right: 1.25rem;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 22rem;
}

.toast {
  background: var(--color-surface);
  border: 0.0625rem solid var(--color-border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}

.toast__body {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-3) var(--space-2);
}

.toast__message {
  flex: 1;
  font-size: var(--text-small);
  color: var(--color-ink);
  word-break: break-word;
}

.toast__close {
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--color-ink-faint);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.toast__close:hover {
  color: var(--color-ink-muted);
}

.toast__track {
  height: 0.1875rem;
  background: var(--color-border);
}

.toast__bar {
  height: 100%;
  width: 100%;
  background: var(--color-accent);
  transform-origin: left;
  animation-name: toast-shrink;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

@keyframes toast-shrink {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

/* ---- Table ---- */
.table-wrap {
  overflow-x: auto;
}

.table {
  width: 100%;
  min-width: 40rem;
  border-collapse: collapse;
  font-size: var(--text-small);
}

.table th {
  text-align: left;
  padding: var(--space-3) var(--space-5);
  font-size: var(--text-micro);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-ink-faint);
  border-bottom: 0.0625rem solid var(--color-border);
  white-space: nowrap;
}

.table td {
  padding: var(--space-4) var(--space-5);
  border-bottom: 0.0625rem solid var(--color-border);
  color: var(--color-ink);
  vertical-align: middle;
}

.table tbody tr {
  transition: background var(--duration-fast) var(--ease-standard);
}

.table tbody tr:hover {
  background: var(--color-canvas-inset);
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.table__primary-cell-content {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.table__primary-cell-text strong {
  display: inline-flex;
  font-weight: 600;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-ink);
}

.table__primary-cell-text span {
  display: inline-flex;
  align-items: center;
  font-size: var(--text-small);
  color: var(--color-ink-faint);
}

.table__actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-1);
}

/* ---- Pagination ---- */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  border-top: 0.0625rem solid var(--color-border);
}

.pagination__pages {
  display: flex;
  gap: var(--space-1);
}

.pagination__page {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: var(--text-small);
  color: var(--color-ink-muted);
  cursor: pointer;
}

.pagination__page:hover {
  background: var(--color-canvas-inset);
}

.pagination__page--current {
  background: var(--color-accent);
  color: var(--color-ink-on-accent);
}

.pagination__page--disabled {
  opacity: 0.4;
  pointer-events: none;
  cursor: default;
}

/* ---- Tabs ---- */
.tabs {
  display: flex;
  gap: var(--space-5);
  border-bottom: 0.0625rem solid var(--color-border);
}

.tabs__item {
  position: relative;
  padding: var(--space-3) 0;
  font-size: var(--text-small);
  font-weight: 600;
  color: var(--color-ink-faint);
  cursor: pointer;
}

.tabs__item:hover {
  color: var(--color-ink-muted);
}

.tabs__item--active {
  color: var(--color-ink);
}

.tabs__item--active::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -0.0625rem;
  height: 0.125rem;
  background: var(--color-accent);
  border-radius: var(--radius-full);
}

/* ---- Dropdown menu ---- */
.menu {
  position: relative;
}

/* Hidden by default — app.js's initMenus() toggles the [data-open]
   attribute on click; without this default-hide the dropdown (e.g. the
   topbar user menu / Sign out) would render permanently open. */
.menu__panel {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-2));
  min-width: 12rem;
  padding: var(--space-2);
  background: var(--color-surface-raised);
  border: 0.0625rem solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  z-index: 20;
}

.menu__panel[data-open] {
  display: block;
}

/* Alpine-driven panels (x-show) control their own visibility via inline
   `style.display`, toggled directly by Alpine — not the [data-open]
   attribute above. Without this, the default `display: none` here always
   wins once Alpine clears its inline override to show the panel, since
   Alpine's x-show never sets [data-open]. */
.menu__panel[x-show] {
  display: block;
}

.menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: none;
  background: none;
  border-radius: var(--radius-sm);
  font-size: var(--text-small);
  text-align: left;
  color: var(--color-ink);
  cursor: pointer;
}

.menu__item:hover {
  background: var(--color-canvas-inset);
}

.menu__item--danger {
  color: var(--color-danger);
}

.menu__divider {
  margin: var(--space-2) calc(-1 * var(--space-2));
  border-top: 0.0625rem solid var(--color-border);
}

/* ---- Theme toggle ----
   A single compact icon button. The two glyphs are stacked and only one
   is ever displayed, so the button never grows past btn--icon's size. */
.theme-toggle svg {
  width: 1.125rem;
  height: 1.125rem;
}

.theme-toggle .icon-sun {
  display: none;
}

[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}

[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun {
    display: block;
  }

  :root:not([data-theme="light"]) .theme-toggle .icon-moon {
    display: none;
  }
}

/* ---- Progress ring (mini) ---- */
.progress-ring {
  --ring-value: 72;
  position: relative;
  width: 6rem;
  height: 6rem;
  border-radius: var(--radius-full);
  background: conic-gradient(var(--color-accent) calc(var(--ring-value) * 1%), var(--color-canvas-inset) 0);
  display: flex;
  align-items: center;
  justify-content: center;
}

.progress-ring::before {
  content: "";
  position: absolute;
  inset: 0.5rem;
  border-radius: var(--radius-full);
  background: var(--color-surface);
}

.progress-ring__label {
  position: relative;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--text-h3);
}

/* ---- Progress bar ---- */
.progress-bar {
  height: 0.5rem;
  border-radius: var(--radius-full);
  background: var(--color-canvas-inset);
  overflow: hidden;
}

.progress-bar__fill {
  height: 100%;
  border-radius: var(--radius-full);
  background: var(--color-accent);
}

/* ---- Empty state ---- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-3);
  padding: var(--space-9) var(--space-5);
  color: var(--color-ink-muted);
}

.empty-state__icon {
  width: 2.75rem;
  height: 2.75rem;
  border-radius: var(--radius-full);
  background: var(--color-accent-soft);
  color: var(--color-accent-ink);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- Modal (native <dialog>) ---- */
/* display is scoped to [open] so the browser's UA-stylesheet rule
   (dialog:not([open]) { display: none }) still hides the dialog until
   .showModal() is called — an unscoped `display` here would win the
   cascade over the UA rule (author styles beat UA styles regardless of
   specificity) and force every modal to render inline on the page. */
.modal {
  width: min(28rem, calc(100vw - 2rem));
  max-height: min(90vh, 40rem);
  margin: auto;
  padding: 0;
  border: none;
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  color: var(--color-ink);
  box-shadow: var(--shadow-lg);
}

/* Wider variant for content that needs more breathing room per-row (e.g. the
   kg-group files list: filename + doc-type badge + size + a group-move
   select all sharing one line) than the default modal width allows. */
.modal--lg {
  width: min(34rem, calc(100vw - 2rem));
}

.modal[open] {
  display: flex;
  flex-direction: column;
}

.modal::backdrop {
  background: rgba(15, 20, 24, 0.55);
  backdrop-filter: blur(0.125rem);
}

.modal__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5);
  border-bottom: 0.0625rem solid var(--color-border);
  flex-shrink: 0;
}

.modal__header h2 {
  margin-bottom: var(--space-1);
}

.modal__body {
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  overflow-y: auto;
}

.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-5);
  border-top: 0.0625rem solid var(--color-border);
  flex-shrink: 0;
}

/* ---- Check list (used inside forms, e.g. group access) ---- */
.check-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-4);
  border: 0.0625rem solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-canvas-inset);
}