/* Reset */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
/* Elements toggled via the native `hidden` attribute (JS empty states, ad-type
   field toggling) must stay hidden even when they also carry an explicit
   `display` rule elsewhere — without this, author display rules of equal
   specificity beat the UA stylesheet's [hidden] default. */
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
img, svg { display: block; max-width: 100%; }
h1, h2, h3, h4, p, figure { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: inherit; }

/* Headings */
h1 { font-size: var(--text-2xl); font-weight: 700; line-height: var(--leading-tight); }
h2 { font-size: var(--text-xl); font-weight: 700; line-height: var(--leading-tight); }
h3 { font-size: var(--text-md); font-weight: 600; line-height: var(--leading-tight); }

/* Focus visibility — never remove, only restyle */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* Utilities */
.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;
}
.container {
  width: 100%;
  max-width: var(--container-max);
  margin-inline: auto;
  padding-inline: var(--space-5);
}
.text-muted { color: var(--color-text-muted); }
.text-subtle { color: var(--color-text-subtle); }

/* Buttons */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
  padding: 0 var(--space-5);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard),
              color var(--duration-fast) var(--ease-standard),
              box-shadow var(--duration-fast) var(--ease-standard);
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary { background: var(--color-primary); color: var(--color-text-on-primary); }
.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); }
.btn-secondary:hover:not(:disabled) { background: var(--color-surface-muted); }

.btn-ghost { background: transparent; color: var(--color-text-muted); }
.btn-ghost:hover:not(:disabled) { background: var(--color-surface-muted); color: var(--color-text); }

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

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

.btn-icon {
  /* Self-sufficient: several call sites use .btn-icon without .btn (e.g. the
     mobile nav toggle/close buttons, desktop table row actions), so this
     can't rely on inheriting .btn's reset — a bare <button> keeps the
     browser's default 3D border and top-alignment without it. */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--touch-target);
  min-height: var(--touch-target);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-muted);
  cursor: pointer;
}
.btn-icon:hover:not(:disabled) { background: var(--color-surface-muted); color: var(--color-text); }
/* Any inline SVG inside a button/link needs explicit sizing — an <svg> with
   only a viewBox (no width/height) defaults to a 300×150 intrinsic size in
   most browsers, and max-width:100% alone doesn't reliably prevent that
   inside a full-width (.btn-block) flex button. */
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-icon svg { width: 18px; height: 18px; }

.btn .spinner { display: none; }
.btn.is-loading .btn-label { visibility: hidden; }
.btn.is-loading .spinner {
  display: inline-block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Spinner */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn-secondary .spinner, .btn-ghost .spinner {
  border-color: var(--color-border-strong);
  border-top-color: var(--color-text);
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 1.4s; }
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.4;
}
.badge-neutral { background: var(--color-surface-muted); color: var(--color-text-muted); }
.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-info { background: var(--color-info-soft); color: var(--color-info); }
.badge-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor;
}

/* Form elements */
.field { display: flex; flex-direction: column; gap: var(--space-2); margin-bottom: var(--space-5); }
.field-label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
  display: flex;
  align-items: center;
  gap: var(--space-1);
}
.field-optional { font-weight: 400; color: var(--color-text-subtle); font-size: var(--text-xs); }
.field-help { font-size: var(--text-xs); color: var(--color-text-muted); }
.field-error {
  font-size: var(--text-xs);
  color: var(--color-danger);
  display: none;
  align-items: center;
  gap: var(--space-1);
}
.field.has-error .field-error { display: flex; }
.field.has-error .field-input { border-color: var(--color-danger); }

.field-input {
  width: 100%;
  min-height: var(--touch-target);
  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: 16px; /* prevents iOS Safari auto-zoom */
  color: var(--color-text);
  transition: border-color var(--duration-fast) var(--ease-standard), box-shadow var(--duration-fast) var(--ease-standard);
}
.field-input::placeholder { color: var(--color-text-subtle); }
.field-input:focus-visible {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}
textarea.field-input { min-height: 110px; resize: vertical; line-height: var(--leading-normal); }
select.field-input { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1L6 6L11 1' stroke='%23635F77' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right var(--space-4) center; padding-right: var(--space-8); }

.field-prefix-group { display: flex; align-items: stretch; }
.field-prefix {
  display: flex; align-items: center;
  padding: 0 var(--space-3);
  background: var(--color-surface-muted);
  border: 1px solid var(--color-border-strong);
  border-right: none;
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  white-space: nowrap;
}
.field-prefix-group .field-input { border-radius: 0 var(--radius-md) var(--radius-md) 0; }

.field-preview {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: var(--color-surface-muted);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  word-break: break-all;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* Checkbox / toggle */
.toggle-row { display: flex; align-items: center; gap: var(--space-3); }
.toggle {
  position: relative;
  width: 40px; height: 24px;
  flex-shrink: 0;
}
.toggle input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; width: 100%; height: 100%; }
.toggle-track {
  position: absolute; inset: 0;
  background: var(--color-border-strong);
  border-radius: var(--radius-pill);
  transition: background-color var(--duration-fast) var(--ease-standard);
}
.toggle-thumb {
  position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px;
  background: #fff;
  border-radius: 50%;
  box-shadow: var(--shadow-sm);
  transition: transform var(--duration-fast) var(--ease-standard);
}
.toggle input:checked ~ .toggle-track { background: var(--color-primary); }
.toggle input:checked ~ .toggle-thumb { transform: translateX(16px); }
.toggle input:focus-visible ~ .toggle-track { outline: 2px solid var(--color-accent); outline-offset: 2px; }

/* Card */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-pad { padding: var(--space-6); }

/* Flash messages */
.flash-region { display: flex; flex-direction: column; gap: var(--space-3); margin-bottom: var(--space-5); }
.flash {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  border: 1px solid transparent;
}
.flash-success { background: var(--color-success-soft); color: var(--color-success); border-color: currentColor; }
.flash-error { background: var(--color-danger-soft); color: var(--color-danger); border-color: currentColor; }
.flash-info { background: var(--color-info-soft); color: var(--color-info); border-color: currentColor; }
.flash-icon { flex-shrink: 0; width: 18px; height: 18px; margin-top: 1px; }
.flash-dismiss { margin-left: auto; background: transparent; border: none; cursor: pointer; color: inherit; opacity: 0.7; padding: 2px; }
.flash-dismiss:hover { opacity: 1; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-6);
  color: var(--color-text-muted);
}
.empty-state-icon {
  width: 44px; height: 44px;
  margin: 0 auto var(--space-4);
  color: var(--color-text-subtle);
}
.empty-state h3 { color: var(--color-text); margin-bottom: var(--space-2); }
.empty-state p { font-size: var(--text-sm); max-width: 40ch; margin-inline: auto; }
.empty-state .btn { margin-top: var(--space-5); }

/* Skeleton loading */
.skeleton {
  background: linear-gradient(90deg, var(--color-surface-muted) 25%, var(--color-surface-sunken) 37%, var(--color-surface-muted) 63%);
  background-size: 400% 100%;
  animation: skeleton-wave 1.4s ease infinite;
  border-radius: var(--radius-sm);
}
@keyframes skeleton-wave { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
@media (prefers-reduced-motion: reduce) {
  .skeleton { animation: none; opacity: 0.6; }
}

/* Dialog */
.dialog-overlay {
  position: fixed; inset: 0;
  background: rgba(20, 18, 31, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  z-index: 100;
  opacity: 0;
  transition: opacity var(--duration-base) var(--ease-standard);
}
.dialog-overlay.is-open { opacity: 1; }
.dialog-overlay[hidden] { display: none; }
.dialog {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 420px;
  padding: var(--space-6);
  transform: translateY(8px) scale(0.98);
  transition: transform var(--duration-base) var(--ease-standard);
}
.dialog-overlay.is-open .dialog { transform: translateY(0) scale(1); }
.dialog h2 { margin-bottom: var(--space-2); }
.dialog p { color: var(--color-text-muted); font-size: var(--text-sm); }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--space-3); margin-top: var(--space-6); }
@media (max-width: 480px) {
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions .btn { width: 100%; }
}

/* Responsive container padding */
@media (min-width: 768px) { .container { padding-inline: var(--space-8); } }
