/* ============================================================================
   Components: cards, buttons, inputs, badges, chips, modals, toasts,
   dropdowns, tables, empty states, skeletons.
   ========================================================================== */

/* --- card ---------------------------------------------------------------- */

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}

.card--flush { padding: 0; }
.card--pad { padding: var(--space-5); }

.card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border);
}

.card__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--text-strong);
}

.card__body { padding: var(--space-5); }
.card__footer {
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border);
}

/* --- buttons ------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0.5rem 0.875rem;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  line-height: 1.4;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
  transition: background var(--duration-fast) var(--ease),
              border-color var(--duration-fast) var(--ease),
              color var(--duration-fast) var(--ease),
              transform var(--duration-fast) var(--ease);
}

.btn:hover { text-decoration: none; }
.btn:active:not(:disabled) { transform: translateY(1px); }

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

:root[data-theme="dark"] .btn--primary { color: #0b1020; }

.btn--primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

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

.btn--secondary:hover:not(:disabled) { background: var(--bg-subtle); }

.btn--ghost { color: var(--text-body); }
.btn--ghost:hover:not(:disabled) { background: var(--bg-hover); }

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

:root[data-theme="dark"] .btn--danger { color: #0b1020; }
.btn--danger:hover:not(:disabled) { filter: brightness(1.08); }

.btn--danger-ghost {
  color: var(--danger);
  border-color: transparent;
}

.btn--danger-ghost:hover:not(:disabled) { background: var(--danger-soft); }

.btn--sm { padding: 0.3125rem 0.625rem; font-size: var(--text-xs); }
.btn--lg { padding: 0.6875rem 1.25rem; font-size: var(--text-base); }
.btn--block { width: 100%; }

.btn--icon {
  padding: 0.4375rem;
  border-radius: var(--radius-md);
  color: var(--text-muted);
}

.btn--icon:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-strong);
}

/* In-flight state: the label is swapped for a spinner and the button locked. */
.btn__spinner {
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}

.btn.is-busy .btn__spinner { display: inline-block; }
.btn.is-busy { pointer-events: none; opacity: 0.75; }

@keyframes spin { to { transform: rotate(360deg); } }

/* --- forms --------------------------------------------------------------- */

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

.field__label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-strong);
}

.field__hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.input,
.select,
.textarea {
  width: 100%;
  padding: 0.5625rem 0.75rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-strong);
  font-family: inherit;
  font-size: var(--text-base);
  line-height: 1.45;
  transition: border-color var(--duration-fast) var(--ease),
              box-shadow var(--duration-fast) var(--ease);
}

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

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

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

.textarea { min-height: 120px; resize: vertical; }

.select {
  appearance: none;
  padding-right: 2rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4.5 6 8.5 10 4.5' fill='none' stroke='%2364748b' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
}

.input--invalid,
.select--invalid,
.textarea--invalid,
.input.input-validation-error,
.select.input-validation-error,
.textarea.input-validation-error {
  border-color: var(--danger);
}

.field__error,
.field-validation-error {
  display: block;
  font-size: var(--text-xs);
  color: var(--danger);
  font-weight: var(--weight-medium);
}

.field-validation-valid:empty { display: none; }

.checkbox,
.radio {
  display: inline-flex;
  align-items: flex-start;
  gap: var(--space-2);
  font-size: var(--text-sm);
  cursor: pointer;
}

.checkbox input,
.radio input {
  width: 16px;
  height: 16px;
  margin: 0.15rem 0 0;
  accent-color: var(--accent);
  cursor: pointer;
  flex: none;
}

.validation-summary {
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius-md);
  color: var(--danger);
  font-size: var(--text-sm);
}

.validation-summary ul { padding-left: var(--space-4); }
.validation-summary li { list-style: disc; }
.validation-summary-valid { display: none; }

.color-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.color-swatch {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-full);
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}

.color-swatch[aria-pressed="true"] {
  border-color: var(--text-strong);
  box-shadow: 0 0 0 2px var(--bg-surface) inset;
}

/* --- badges and chips ---------------------------------------------------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0.1875rem 0.5rem;
  border-radius: var(--radius-full);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  white-space: nowrap;
}

.badge--pending { background: var(--bg-inset); color: var(--text-muted); }
.badge--progress { background: var(--info-soft); color: var(--info); }
.badge--done { background: var(--success-soft); color: var(--success); }
.badge--cancelled { background: var(--bg-inset); color: var(--text-faint); text-decoration: line-through; }
.badge--overdue { background: var(--danger-soft); color: var(--danger); }
.badge--today { background: var(--warning-soft); color: var(--warning); }
.badge--neutral { background: var(--bg-inset); color: var(--text-muted); }

.priority {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--text-muted);
}

.priority__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
}

.priority--low .priority__dot { background: var(--priority-low); }
.priority--medium .priority__dot { background: var(--priority-medium); }
.priority--high .priority__dot { background: var(--priority-high); }
.priority--critical .priority__dot { background: var(--priority-critical); }

.priority--high { color: var(--priority-high); }
.priority--critical { color: var(--priority-critical); font-weight: var(--weight-semibold); }

.cat-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: none;
  border: 1px solid rgba(0, 0, 0, 0.12);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0.25rem 0.5rem 0.25rem 0.625rem;
  background: var(--accent-soft);
  color: var(--accent-text);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.chip__label { color: inherit; opacity: 0.75; }

.chip__remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  line-height: 1;
  text-decoration: none;
}

.chip__remove:hover {
  background: rgba(0, 0, 0, 0.1);
  text-decoration: none;
}

/* --- tables -------------------------------------------------------------- */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table th {
  text-align: left;
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  white-space: nowrap;
}

.table td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  color: var(--text-body);
}

.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--bg-hover); }

.table th a {
  color: inherit;
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.table__sorted { color: var(--accent-text); }

/* --- empty / loading / error states -------------------------------------- */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-3);
  padding: var(--space-12) var(--space-6);
}

.empty-state__icon {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-full);
  background: var(--bg-inset);
  color: var(--text-muted);
}

.empty-state__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--text-strong);
}

.empty-state__body {
  max-width: 42ch;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.error-state {
  padding: var(--space-6);
  border: 1px solid var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius-md);
  color: var(--danger);
}

.skeleton {
  background: linear-gradient(90deg, var(--bg-inset) 25%, var(--bg-subtle) 37%, var(--bg-inset) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
  border-radius: var(--radius-sm);
}

.skeleton--line { height: 12px; margin-bottom: var(--space-2); }
.skeleton--title { height: 18px; width: 40%; }
.skeleton--row { height: 56px; margin-bottom: var(--space-2); border-radius: var(--radius-md); }

@keyframes shimmer {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* --- dropdown ------------------------------------------------------------ */

.dropdown { position: relative; }

.dropdown__panel {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  min-width: 260px;
  max-width: min(360px, calc(100vw - var(--space-8)));
  background: var(--bg-surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-drawer);
  overflow: hidden;
}

.dropdown__panel[hidden] { display: none; }

.dropdown__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--text-strong);
}

.dropdown__list { max-height: 340px; overflow-y: auto; }

.dropdown__item {
  display: block;
  width: 100%;
  padding: var(--space-3) var(--space-4);
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  text-align: left;
  color: var(--text-body);
  font-size: var(--text-sm);
  cursor: pointer;
  text-decoration: none;
}

.dropdown__item:last-child { border-bottom: none; }
.dropdown__item:hover { background: var(--bg-hover); text-decoration: none; }
.dropdown__item--unread { background: var(--accent-soft); }
.dropdown__footer { padding: var(--space-3) var(--space-4); border-top: 1px solid var(--border); }

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

.menu__item:hover { background: var(--bg-hover); text-decoration: none; }
.menu__divider { height: 1px; background: var(--border); margin: var(--space-2) 0; }

/* --- modal --------------------------------------------------------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: grid;
  place-items: center;
  padding: var(--space-4);
  background: var(--scrim);
  animation: fade-in var(--duration) var(--ease);
}

.modal[hidden] { display: none; }

.modal__dialog {
  width: min(440px, 100%);
  background: var(--bg-surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-6);
  animation: pop-in var(--duration) var(--ease);
}

.modal__title {
  font-size: var(--text-lg);
  margin-bottom: var(--space-2);
}

.modal__body {
  color: var(--text-muted);
  font-size: var(--text-sm);
  margin-bottom: var(--space-6);
}

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

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

/* --- toasts -------------------------------------------------------------- */

.toast-region {
  position: fixed;
  bottom: var(--space-4);
  right: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: min(380px, calc(100vw - var(--space-8)));
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-surface-raised);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-muted);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  color: var(--text-body);
  animation: toast-in var(--duration) var(--ease);
}

.toast--success { border-left-color: var(--success); }
.toast--error { border-left-color: var(--danger); }
.toast--info { border-left-color: var(--info); }
.toast.is-leaving { animation: toast-out var(--duration) var(--ease) forwards; }

.toast__close {
  border: none;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  padding: 0 0 0 var(--space-2);
  line-height: 1;
}

.toast__close:hover { color: var(--text-strong); }

@keyframes toast-in { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateX(16px); } }

/* --- pagination ---------------------------------------------------------- */

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.pager__pages { display: flex; align-items: center; gap: var(--space-1); }

.pager__link {
  display: inline-grid;
  place-items: center;
  min-width: 32px;
  height: 32px;
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  color: var(--text-body);
  font-size: var(--text-sm);
  background: var(--bg-surface);
}

.pager__link:hover { background: var(--bg-hover); text-decoration: none; }

.pager__link[aria-current="page"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: var(--weight-semibold);
}

:root[data-theme="dark"] .pager__link[aria-current="page"] { color: #0b1020; }

.pager__link[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }

/* --- progress ring ------------------------------------------------------- */

.ring { transform: rotate(-90deg); }
.ring__track { stroke: var(--bg-inset); }
.ring__value { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 600ms var(--ease); }

/* --- avatar -------------------------------------------------------------- */

.avatar {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  flex: none;
}

.avatar--lg { width: 44px; height: 44px; font-size: var(--text-sm); }
