/* ==========================================================================
   Components: buttons, forms, cards, alerts, badges, tables, states, dialogs
   ========================================================================== */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px; /* comfortable touch target */
  padding: 0 var(--space-5);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--transition), border-color var(--transition), color var(--transition),
    box-shadow var(--transition);
}
.btn:hover {
  text-decoration: none;
}
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn svg {
  width: 18px;
  height: 18px;
  flex: none;
}

.btn-primary {
  background: var(--color-primary);
  color: var(--white);
  box-shadow: var(--shadow-xs);
}
.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
}
.btn-primary:active:not(:disabled) {
  background: var(--color-primary-active);
}

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border-strong);
  box-shadow: var(--shadow-xs);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--gray-50);
  border-color: var(--gray-400);
}

.btn-dark {
  background: var(--color-ink);
  color: var(--white);
}
.btn-dark:hover:not(:disabled) {
  background: var(--gray-800);
}

.btn-ghost {
  background: transparent;
  color: var(--color-text-subtle);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--gray-100);
  color: var(--color-text);
}

.btn-danger {
  background: var(--color-surface);
  color: var(--color-danger);
  border-color: var(--red-200);
}
.btn-danger:hover:not(:disabled) {
  background: var(--red-50);
}

.btn-sm {
  min-height: 36px;
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
}
.btn-block {
  width: 100%;
}
.btn-icon {
  width: 44px;
  padding: 0;
}

/* Loading state: <button aria-busy="true"> */
.btn[aria-busy="true"] {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}
.btn[aria-busy="true"]::after {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-color: var(--white) var(--white) var(--white) transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn-secondary[aria-busy="true"]::after,
.btn-ghost[aria-busy="true"]::after,
.btn-danger[aria-busy="true"]::after {
  border-color: var(--gray-500) var(--gray-500) var(--gray-500) transparent;
}

/* ---------- Forms ---------- */
.form {
  display: grid;
  gap: var(--space-5);
}
.form-row {
  display: grid;
  gap: var(--space-4);
}
@media (min-width: 560px) {
  .form-row.cols-2 {
    grid-template-columns: 1fr 1fr;
  }
}
.field {
  display: grid;
  gap: var(--space-2);
}
.label {
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}
.label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-2);
}
.label-row a {
  font-size: var(--text-sm);
}

.input,
.select,
.textarea {
  width: 100%;
  min-height: 46px;
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  font-size: var(--text-base); /* 16px prevents iOS zoom on focus */
  transition: border-color var(--transition), box-shadow var(--transition);
}
.textarea {
  min-height: 96px;
  resize: vertical;
}
.input::placeholder,
.textarea::placeholder {
  color: var(--gray-400);
}
.input:hover,
.select:hover,
.textarea:hover {
  border-color: var(--gray-400);
}
.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: var(--focus-ring);
}
.input[aria-invalid="true"],
.select[aria-invalid="true"],
.textarea[aria-invalid="true"] {
  border-color: var(--color-danger);
}
.select {
  appearance: none;
  padding-right: var(--space-10);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 8 4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.field-hint {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}
.field-error {
  font-size: var(--text-sm);
  color: var(--color-danger);
  font-weight: var(--weight-medium);
}
.field-error:empty {
  display: none;
}

.checkbox {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
  cursor: pointer;
}
.checkbox input {
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  flex: none;
  accent-color: var(--color-primary);
}

/* Password field with show/hide toggle */
.password-wrap {
  position: relative;
}
.password-wrap .input {
  padding-right: 56px;
}
.password-toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  min-height: 34px;
  padding: 0 var(--space-2);
  border: 0;
  background: transparent;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.password-toggle:hover {
  color: var(--color-text);
  background: var(--gray-100);
}

/* Password requirement checklist */
.checklist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-1);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}
.checklist li {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.checklist li::before {
  content: "";
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1.5px solid var(--gray-300);
  flex: none;
}
.checklist li.met {
  color: var(--green-700);
}
.checklist li.met::before {
  border-color: var(--green-700);
  background: var(--green-700)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4.5 8.5 2.2 2.2 4.8-5'/%3E%3C/svg%3E")
    center/14px no-repeat;
}

/* ---------- Cards ---------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}
.card-body {
  padding: var(--space-6);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--color-border);
}
.card-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}
.card-grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}
.stat-label {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  font-weight: var(--weight-medium);
}
.stat-value {
  margin-top: var(--space-1);
  font-size: var(--text-2xl);
  font-weight: var(--weight-bold);
}

/* ---------- Alerts ---------- */
.alert {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-left-width: 3px;
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}
.alert svg {
  color: var(--alert-accent);
  width: 20px;
  height: 20px;
  flex: none;
  margin-top: 1px;
}
.alert-title {
  font-weight: var(--weight-semibold);
  color: var(--alert-accent);
}
.alert-error {
  border-left-color: var(--red-700);
  --alert-accent: var(--red-700);
}
.alert-success {
  border-left-color: var(--green-700);
  --alert-accent: var(--green-700);
}
.alert-info {
  border-left-color: var(--blue-700);
  --alert-accent: var(--blue-700);
}
.alert-warning {
  border-left-color: var(--amber-700);
  --alert-accent: var(--amber-700);
}
.alert a {
  color: inherit;
  text-decoration: underline;
}

/* ---------- Badges (status indicators) ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  line-height: 20px;
  white-space: nowrap;
}
.badge-dot::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.badge-neutral {
  color: var(--gray-600);
}
.badge-success {
  color: var(--green-700);
}
.badge-warning {
  color: var(--amber-700);
}
.badge-danger {
  color: var(--red-700);
}
.badge-info {
  color: var(--blue-700);
}
.badge-dark {
  color: var(--color-ink);
}

/* ---------- Tables (stack into cards on small screens) ---------- */
.table-wrap {
  overflow-x: auto;
  /* Rows switch to stacked cards based on the table's own width, not the
     screen's, so tables beside the sidebar never hide their action buttons. */
  container: table / inline-size;
}
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
.table th {
  text-align: left;
  padding: var(--space-3) var(--space-4);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--color-border);
}
.table td {
  padding: var(--space-4);
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}
.table th,
.table .cell-nowrap {
  white-space: nowrap;
}
.table tr:last-child td {
  border-bottom: 0;
}
.table tbody tr:hover {
  background: var(--gray-50);
}
.table .cell-primary {
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}
.table .cell-actions {
  text-align: right;
  white-space: nowrap;
}
.table .cell-actions .btn + .btn {
  margin-left: var(--space-2);
}

@container table (max-width: 860px) {
  .table thead {
    display: none;
  }
  .table,
  .table tbody,
  .table tr,
  .table td {
    display: block;
    width: 100%;
  }
  .table tr {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
  }
  .table tr:last-child {
    border-bottom: 0;
  }
  .table td {
    display: flex;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--space-2) 0;
    border: 0;
    text-align: right;
  }
  .table td::before {
    content: attr(data-label);
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    text-align: left;
  }
  .table td.cell-actions {
    justify-content: flex-end;
    padding-top: var(--space-3);
  }
  .table td.cell-actions::before {
    content: none;
  }
}

/* ---------- Empty, loading & error states ---------- */
.empty-state {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  padding: var(--space-12) var(--space-6);
  text-align: center;
}
.empty-icon {
  display: grid;
  place-items: center;
  color: var(--color-primary);
}
.empty-icon svg {
  width: 32px;
  height: 32px;
}
.empty-state h3 {
  font-size: var(--text-lg);
}
.empty-state p {
  max-width: 420px;
  color: var(--color-text-muted);
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.loading-block {
  display: grid;
  place-items: center;
  gap: var(--space-3);
  padding: var(--space-16) var(--space-6);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}
.skeleton {
  background: linear-gradient(90deg, var(--gray-100), var(--gray-200), var(--gray-100));
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
  border-radius: var(--radius-sm);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

/* ---------- Dialog (native <dialog>) ---------- */
.dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0;
  border: 0;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  color: var(--color-text);
}
.dialog::backdrop {
  background: rgba(10, 10, 10, 0.5);
}
.dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-6) var(--space-6) 0;
}
.dialog-title {
  font-size: var(--text-xl);
}
.dialog-desc {
  margin-top: var(--space-1);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}
.dialog-body {
  padding: var(--space-5) var(--space-6);
  display: grid;
  gap: var(--space-4);
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6) var(--space-6);
}
@media (max-width: 479px) {
  .dialog-footer {
    flex-direction: column-reverse;
  }
  .dialog-footer .btn {
    width: 100%;
  }
}

/* ---------- Toasts ---------- */
.toast-region {
  position: fixed;
  z-index: 50;
  right: var(--gutter);
  bottom: var(--gutter);
  left: var(--gutter);
  display: grid;
  justify-items: end;
  gap: var(--space-2);
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  max-width: 420px;
  padding: var(--space-3) var(--space-4);
  background: var(--color-ink);
  color: var(--white);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  pointer-events: auto;
  animation: toast-in 180ms ease-out;
}
.toast svg {
  width: 18px;
  height: 18px;
  flex: none;
  margin-top: 1px;
}
.toast-success svg {
  color: #4ade80;
}
.toast-error svg {
  color: #f87171;
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

/* ---------- Avatar ---------- */
.avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 50%;
  background: var(--color-ink);
  color: var(--white);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-border);
}
.toolbar .input,
.toolbar .select {
  min-height: 40px;
  padding-top: var(--space-2);
  padding-bottom: var(--space-2);
  font-size: var(--text-sm);
}
.toolbar .search {
  flex: 1 1 220px;
}
.toolbar .select {
  flex: 0 1 200px;
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

details.json-details summary {
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}
details.json-details pre {
  margin: var(--space-2) 0 0;
  padding: var(--space-3);
  max-width: 420px;
  overflow: auto;
  background: var(--gray-50);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}
