:root {
  /* Design tokens */
  --color-bg: #071028;
  --color-bg-alt: #041022;
  --color-panel: #0b1220;
  --color-card: #071227;
  --color-muted: #94a3b8;
  --color-text: #e6eef8;
  --color-accent: #7c3aed;
  --color-accent-2: #06b6d4;
  --color-success: #10b981;
  --color-danger: #ef4444;
  --glass-1: rgba(255, 255, 255, 0.02);
  --glass-2: rgba(255, 255, 255, 0.03);
  --glass-3: rgba(255, 255, 255, 0.01);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 14px;
  --shadow-1: 0 6px 18px rgba(2, 6, 23, 0.5);
  --shadow-2: 0 10px 30px rgba(2, 6, 23, 0.65);
  --max-width: 1100px;
  --container-padding: 20px;
  --spacing-xs: 6px;
  --spacing-sm: 12px;
  --spacing-md: 18px;
  --spacing-lg: 28px;
  --font-family-base: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI",
    Roboto, "Helvetica Neue", Arial, "Noto Color Emoji";
  --type-scale-1: 12px;
  --type-scale-2: 13px;
  --type-scale-3: 14px;
  --type-scale-4: 16px;
  --type-scale-5: 18px;
  --type-scale-6: 22px;
  --z-modal: 1100;
  --z-toast: 1200;
  --z-focus-ring: 1150;
  color-scheme: dark;
}

/* Mobile-first base reset and sensible defaults */
*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
html,
body {
  height: 100%;
  margin: 0;
  background: linear-gradient(
    180deg,
    var(--color-bg) 0%,
    var(--color-bg-alt) 60%
  );
  color: var(--color-text);
  font-family: var(--font-family-base);
  font-size: 16px;
  line-height: 1.45;
}
img,
video {
  max-width: 100%;
  height: auto;
  display: block;
}
a {
  color: var(--color-accent);
  text-decoration: none;
  outline-offset: 3px;
}
a:hover {
  text-decoration: underline;
}
:focus {
  outline: none;
}
:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--color-accent) 60%, white);
  outline-offset: 3px;
  border-radius: 8px;
}

/* Accessibility helpers */
.visually-hidden {
  position: absolute !important;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap;
  border: 0;
  padding: 0;
  margin: -1px;
}
.hidden {
  display: none !important;
  visibility: hidden !important;
}

/* Container and layout utilities */
.page {
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 48px 16px;
  box-sizing: border-box;
  gap: 24px;
}
.page.top-nav-layout {
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
}
.container {
  width: 100%;
  max-width: var(--max-width);
  padding: var(--container-padding);
  margin: 0 auto;
  box-sizing: border-box;
}
.shell {
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.02),
    rgba(255, 255, 255, 0.01)
  );
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  padding: 20px;
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
}

/* Larger shells used on admin/home pages ? support multi-column */
.shell.columns-2 {
  grid-template-columns: 1fr 420px;
  gap: 24px;
}

/* Admin-specific shell layout */
.admin-shell {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 24px;
}

/* Side navigation */
.side-nav {
  width: 180px;
  background: var(--color-panel);
  border-radius: var(--radius-md);
  padding: 20px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02);
}
.side-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.side-nav a {
  color: var(--color-text);
  text-decoration: none;
  font-size: 14px;
  padding: 6px 0;
  display: block;
}
.side-nav a:hover {
  text-decoration: underline;
}

/* Top navigation */
.top-nav {
  width: 100%;
  background: var(--color-panel);
  border-radius: var(--radius-md);
  padding: 10px 20px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.02);
}
.top-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.top-nav a {
  color: var(--color-text);
  text-decoration: none;
  font-size: 14px;
  padding: 6px 8px;
  display: block;
}
.top-nav a:hover {
  text-decoration: underline;
}

/* Panels & cards */
.panel,
.card {
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.01),
    rgba(255, 255, 255, 0)
  );
  border-radius: var(--radius-md);
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, 0.03);
  box-shadow: var(--shadow-1);
}
.panel h1,
.card h1,
.card h2,
.panel h2 {
  margin: 0 0 8px 0;
  color: var(--color-text);
  font-weight: 700;
}
.lead {
  color: var(--color-muted);
  font-size: var(--type-scale-3);
  margin: 0 0 12px 0;
}

/* Tables */
.table-wrapper {
  overflow: auto;
}
.table-wrapper table {
  width: 100%;
  min-width: 1200px;
  border-collapse: collapse;
}
.table-wrapper th,
.table-wrapper td {
  padding: 8px;
  text-align: left;
}

/* Brand */
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.logo {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--color-accent), #5b21b6);
  display: grid;
  place-items: center;
  font-weight: 800;
  color: #fff;
  box-shadow: 0 8px 26px rgba(124, 58, 237, 0.12);
  font-size: 18px;
  flex: 0 0 52px;
}

/* Headings and typography */
h1,
h2,
h3 {
  color: var(--color-text);
  line-height: 1.1;
  margin: 0;
}
h1 {
  font-size: var(--type-scale-6);
}
h2 {
  font-size: var(--type-scale-5);
}
h3 {
  font-size: var(--type-scale-4);
}
p {
  margin: 0 0 var(--spacing-sm) 0;
  color: var(--color-muted);
}

/* Form base */
form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
label {
  font-size: var(--type-scale-2);
  color: var(--color-muted);
  display: block;
  margin-bottom: 6px;
}
input[type="text"],
input[type="email"],
input[type="number"],
input[type="datetime-local"],
select,
textarea {
  -webkit-appearance: none;
  appearance: none;
  background: var(--glass-1);
  border: 1px solid rgba(255, 255, 255, 0.04);
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--color-text);
  font-size: 14px;
  transition: box-shadow 0.12s ease, transform 0.08s ease,
    border-color 0.12s ease;
  outline: none;
  width: 100%;
}
input::placeholder {
  color: color-mix(in srgb, var(--color-muted) 60%, transparent);
}
input:focus,
select:focus,
textarea:focus {
  box-shadow: 0 8px 26px rgba(124, 58, 237, 0.1);
  border-color: color-mix(in srgb, var(--color-accent) 60%, transparent);
  transform: translateY(-1px);
}

/* Specific form field classes required by JS & contract */
.input-email {
  font-family: inherit;
}
.input-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  letter-spacing: 0.06em;
  text-transform: none;
  font-size: 15px;
  padding: 12px;
}
.request-id {
  display: none;
} /* hidden input style; JS places value here */

/* Admin specific inputs */
.input-count,
.input-maxuses {
  max-width: 280px;
}
.select-tier {
  max-width: 340px;
}

/* Form layout helpers - fields stack on all viewports */
.fields {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 0;
  cursor: pointer;
  font-weight: 700;
  background: linear-gradient(90deg, var(--color-accent), #5b21b6);
  color: #fff;
  box-shadow: 0 10px 30px rgba(92, 32, 185, 0.14);
  transition: transform 0.08s ease, box-shadow 0.12s ease, opacity 0.12s ease;
}
.btn.secondary,
.btn.ghost {
  background: transparent;
  color: var(--color-muted);
  border: 1px solid rgba(255, 255, 255, 0.04);
  box-shadow: none;
  font-weight: 600;
}
.btn:focus {
  outline: 3px solid rgba(124, 58, 237, 0.12);
  outline-offset: 3px;
  border-radius: 8px;
  z-index: var(--z-focus-ring);
}
.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 40px rgba(2, 6, 23, 0.5);
}
.btn:active {
  transform: translateY(0);
}

/* Specific button variants referenced by JS/HTML selectors */
.btn-submit,
.btn-create {
  /* id'ed buttons used by JS */
  min-width: 160px;
  display: inline-flex;
  justify-content: center;
}
#redeem-submit.btn-submit {
  /* exact hook for home.js */
}
#admin-create.btn-create {
  /* exact hook for admin.js */
}

/* Spinner */
.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.06);
  border-top-color: var(--color-accent);
  animation: spin 1s linear infinite;
  display: inline-block;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Button spinner behavior for JS (.btn.is-loading toggles spinner visibility) */
.btn .spinner {
  display: none;
}
.btn.is-loading .spinner {
  display: inline-block;
}
.btn.is-loading {
  pointer-events: none;
  opacity: 0.75;
}

/* Status messages (aria-live) */
.status-message {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  border: 1px solid rgba(255, 255, 255, 0.03);
  background: transparent;
  color: var(--color-muted);
}
.status-message.is-info {
  background: transparent;
  color: var(--color-muted);
}
.status-message.is-success {
  background: linear-gradient(
    180deg,
    rgba(16, 185, 129, 0.04),
    rgba(16, 185, 129, 0.02)
  );
  color: var(--color-success);
  border-color: rgba(16, 185, 129, 0.06);
}
.status-message.is-error {
  background: linear-gradient(
    180deg,
    rgba(239, 68, 68, 0.035),
    rgba(239, 68, 68, 0.02)
  );
  color: var(--color-danger);
  border-color: rgba(239, 68, 68, 0.06);
}

/* Explicit status regions to match JS expectations and ARIA live hooks */
#redeem-status,
#admin-status {
  min-height: 40px;
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.03);
  background: transparent;
  color: var(--color-muted);
  margin: 0;
}
#redeem-status .status-message,
#admin-status .status-message {
  margin: 0;
  display: block;
}
#redeem-status:focus-visible,
#admin-status:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--color-accent) 60%, white);
  outline-offset: 3px;
  border-radius: 8px;
}

/* Admin list and batch items */
#batchesList {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 520px;
  overflow: auto;
  padding-right: 6px;
}
.batch-item {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.01), transparent);
  padding: 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.02);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.batch-item .badge {
  background: rgba(255, 255, 255, 0.03);
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--color-muted);
}

/* CSV download link (admin) - matches selector a#csv-download[data-batch-id] .csv-download-link */
a.csv-download-link,
a.csv-download-link:link,
a.csv-download-link:visited {
  color: var(--color-accent);
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.03);
  text-decoration: none;
  font-weight: 600;
}
a.csv-download-link:hover,
a.csv-download-link:focus {
  background: rgba(124, 58, 237, 0.06);
  color: color-mix(in srgb, var(--color-accent) 80%, white);
  outline-offset: 3px;
}

/* Explicit CSV download selector per contract */
a#csv-download[data-batch-id] {
  color: var(--color-accent);
  background: transparent;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.03);
  text-decoration: none;
  font-weight: 600;
}
a#csv-download[data-batch-id]:hover,
a#csv-download[data-batch-id]:focus {
  background: rgba(124, 58, 237, 0.06);
  color: color-mix(in srgb, var(--color-accent) 80%, white);
  outline-offset: 3px;
}

/* Small UI helpers */
.muted {
  color: var(--color-muted);
}
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
}
.hint {
  font-size: 13px;
  color: var(--color-muted);
}

/* Toasts / global messages */
#globalToast {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: var(--z-toast);
  pointer-events: none;
}
.toast {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(2, 6, 23, 0.6);
  color: var(--color-text);
  box-shadow: 0 8px 30px rgba(2, 6, 23, 0.6);
}

/* Focus ring utility for keyboard users */
:focus:not(:focus-visible) {
  outline: none;
}
:focus-visible {
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--color-accent) 60%, white);
}

/* State classes required by JS contract */
.is-loading {
  opacity: 0.75;
  pointer-events: none;
  filter: grayscale(0.02) contrast(0.98);
}
.is-success {
  box-shadow: 0 0 0 6px rgba(16, 185, 129, 0.05);
  border-radius: 8px;
}
.is-error {
  box-shadow: 0 0 0 6px rgba(239, 68, 68, 0.05);
  border-radius: 8px;
}

/* Small interactive affordances */
.link-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.04);
  color: var(--color-accent);
  padding: 8px 10px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}
.link-btn:hover {
  background: rgba(124, 58, 237, 0.04);
  transform: translateY(-1px);
}

/* Accessibility & ARIA helpers */
[role="status"] {
  outline: none;
}
[aria-busy="true"] {
  opacity: 0.9;
  pointer-events: none;
}

/* Forms: responsive layouts for admin panel (two column controls) */
.controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}
.controls-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.form-row {
  display: flex;
  gap: 10px;
  align-items: stretch;
  flex-wrap: wrap;
}
.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

/* Specific form/admin IDs layout rules (contract hooks) */
#redeem-form,
#admin-batch-form {
  display: block;
  padding: 0;
  margin: 0;
}
#redeem-form .fields,
#admin-batch-form .fields {
  gap: 12px;
}

/* Responsive adjustments for sidebar layout */
@media (max-width: 880px) {
  .page {
    flex-direction: column;
    align-items: stretch;
  }
  .side-nav {
    width: 100%;
  }
  .side-nav ul {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .side-nav a {
    padding: 6px 8px;
  }
}

/* Small screens: stack */
@media (max-width: 720px) {
  .shell {
    padding: 16px;
  }
  .card {
    padding: 16px;
  }
  .logo {
    width: 48px;
    height: 48px;
    font-size: 16px;
  }
  .brand {
    gap: 10px;
  }
  .spinner {
    width: 14px;
    height: 14px;
  }
}

/* Desktop touchups */
@media (min-width: 920px) {
  .container {
    padding: 28px;
  }
  .logo {
    width: 64px;
    height: 64px;
    font-size: 20px;
  }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001s !important;
    scroll-behavior: auto !important;
  }
}

/* Performance tactics: critical CSS above folded; provide fallback fonts (no @import) */
body {
  font-display: swap;
}

/* Cross-browser small fallbacks */
input::-ms-clear,
input::-ms-reveal {
  display: none;
  width: 0;
  height: 0;
}
button {
  -webkit-tap-highlight-color: transparent;
}

/* Utility grid & flex helpers */
.row {
  display: flex;
  gap: 12px;
  align-items: center;
}
.col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Specific exposed data-attribute styles used by JS hooks */
section[data-request-id],
section[data-tier],
section[data-code-hash] {
  /* non-visual, present for hooks */
}

/* Ensure interactive form controls have sufficient contrast on hover/focus */
input:focus,
select:focus {
  border-color: color-mix(in srgb, var(--color-accent) 60%, white);
  box-shadow: 0 10px 30px rgba(124, 58, 237, 0.08);
}

/* Buttons focus-visible specifics */
button:focus-visible,
a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--color-accent) 60%, white);
  outline-offset: 3px;
}

/* Helper: subtle divider */
.divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.02);
  border-radius: 2px;
  margin: 6px 0;
}

/* Ensure anchors with role button act like buttons visually */
a[role="button"] {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.03);
}

/* Additional admin small-screen collapse behavior */
@media (max-width: 720px) {
  .shell.columns-2 {
    grid-template-columns: 1fr;
    padding: 16px;
  }
  #batchesList {
    max-height: 300px;
  }
}

/* Ensure forms and critical interactive areas maintain good focus order */
form,
input,
button,
a,
select,
textarea {
  touch-action: manipulation;
}

/* Hover/focus states for inputs and buttons to meet accessibility */
button:hover,
button:focus {
  box-shadow: 0 14px 40px rgba(2, 6, 23, 0.45);
  transform: translateY(-1px);
}
input:hover {
  border-color: rgba(255, 255, 255, 0.06);
}

/* Ensure consistent small-print style */
footer small,
.small {
  font-size: 13px;
  color: var(--color-muted);
}

/* Provide utility for monospace badge displays used across pages */
.badge-mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  background: rgba(255, 255, 255, 0.02);
  padding: 6px 8px;
  border-radius: 8px;
  color: var(--color-text);
  font-size: 13px;
  display: inline-block;
}

/* Final safeguard: ensure state classes are declared (contract) */
.is-loading,
.is-success,
.is-error,
.hidden {
  /* declared above */
}
