/* ------------------------------------------------------------------ Tokens */
:root {
  color-scheme: light dark;

  --bg: #f4f5f9;
  --bg-soft: #eceef5;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --text: #14161c;
  --text-muted: #61697a;
  --border: #e2e5ee;
  --border-strong: #cdd2e0;

  --accent: #5457e5;
  --accent-hover: #4447cc;
  --accent-soft: #eef0ff;
  --accent-text: #3f42d4;
  --accent-contrast: #ffffff;

  --danger: #d33a4a;
  --danger-hover: #b62c3b;
  --danger-soft: #fff0f1;
  --danger-contrast: #ffffff;

  --success: #1a8a5c;

  --shadow-sm: 0 1px 2px rgba(20, 22, 28, 0.05), 0 1px 3px rgba(20, 22, 28, 0.04);
  --shadow-md: 0 12px 34px rgba(20, 22, 28, 0.09);
  --shadow-lg: 0 30px 80px rgba(20, 22, 28, 0.18);

  --radius: 14px;
  --radius-sm: 10px;

  --font:
    ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --mono:
    ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f14;
    --bg-soft: #12151c;
    --surface: #161a22;
    --surface-2: #1b202a;
    --text: #e8eaf0;
    --text-muted: #9aa2b1;
    --border: #262c38;
    --border-strong: #364051;

    --accent: #8486ff;
    --accent-hover: #9b9dff;
    --accent-soft: #23264a;
    --accent-text: #b9bbff;
    --accent-contrast: #0e1018;

    --danger: #ff6b78;
    --danger-hover: #ff8a94;
    --danger-soft: #3a1c22;
    --danger-contrast: #2b070c;

    --success: #4bd39b;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 12px 34px rgba(0, 0, 0, 0.42);
    --shadow-lg: 0 30px 80px rgba(0, 0, 0, 0.55);
  }
}

/* ----------------------------------------------------------------- Base */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1,
h2,
h3,
p {
  margin: 0;
}

a {
  color: inherit;
}

img {
  max-width: 100%;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.container {
  width: 100%;
  max-width: 1240px;
  margin: 0 auto;
  padding: 0 24px;
}

.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.icon {
  width: 16px;
  height: 16px;
  flex: none;
}

/* -------------------------------------------------------------- Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    color 0.15s ease,
    box-shadow 0.15s ease;
}

.btn:hover {
  background: var(--surface-2);
  border-color: var(--text-muted);
  text-decoration: none;
}

.btn:active {
  transform: translateY(0.5px);
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 6px 16px rgba(84, 87, 229, 0.22);
}

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

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

.btn-ghost:hover {
  background: var(--bg-soft);
  border-color: transparent;
  color: var(--text);
}

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

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

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

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

.btn-sm {
  padding: 6px 10px;
  font-size: 0.8rem;
  border-radius: 8px;
}

.btn-sm .icon {
  width: 13px;
  height: 13px;
}

/* ----------------------------------------------------------------- Brand */
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
}

.brand-mark {
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: #ffffff;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 55%, #06b6d4 100%);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.32);
}

.brand-mark svg {
  width: 20px;
  height: 20px;
}

.brand-name {
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-name > span {
  color: var(--accent-text);
}

.brand-lg {
  gap: 13px;
}

.brand-lg .brand-mark {
  width: 52px;
  height: 52px;
  border-radius: 15px;
}

.brand-lg .brand-mark svg {
  width: 30px;
  height: 30px;
}

.brand-lg .brand-name {
  font-size: 1.3rem;
}

/* ---------------------------------------------------------------- Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 64px;
}

.brand-link {
  text-decoration: none;
  border-radius: 10px;
}

.logout-form {
  margin: 0;
}

/* ------------------------------------------------------------ Page layout */
.page {
  padding-top: 32px;
  padding-bottom: 72px;
}

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 26px;
}

.page-head h1 {
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.035em;
  line-height: 1.15;
}

.lede {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 0.95rem;
  max-width: 62ch;
}

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

/* ---------------------------------------------------------------- Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}

.stat {
  position: relative;
  overflow: hidden;
  padding: 18px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.stat::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 3px;
  background: linear-gradient(180deg, #6366f1, #a855f7);
}

.stat:nth-child(2)::before {
  background: linear-gradient(180deg, #06b6d4, #14b8a6);
}

.stat:nth-child(3)::before {
  background: linear-gradient(180deg, #f59e0b, #f97316);
}

.stat-label {
  display: block;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.stat-value {
  display: block;
  margin-top: 4px;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.035em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

/* ---------------------------------------------------------------- Filters */
.filters {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 14px;
  padding: 16px 18px;
  margin-bottom: 24px;
}

.field {
  display: grid;
  gap: 6px;
  min-width: 210px;
}

.field.grow {
  flex: 1 1 260px;
  min-width: 240px;
}

.field label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

select,
input[type="search"],
input[type="text"],
textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.92rem;
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

select:hover,
input[type="search"]:hover,
input[type="text"]:hover,
textarea:hover {
  border-color: var(--text-muted);
}

select:focus,
input[type="search"]:focus,
input[type="text"]:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-muted);
  opacity: 0.75;
}

.filter-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 1px;
}

/* --------------------------------------------------------------- Results */
.results-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin-bottom: 16px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 0.88rem;
  cursor: pointer;
  user-select: none;
}

.check input {
  width: 17px;
  height: 17px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.check-only {
  gap: 0;
}

.results-count {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.bulk-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

/* --------------------------------------------------------------- QR cards */
.qr-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.qr-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

.qr-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
}

.qr-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.qr-card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.badge {
  display: inline-block;
  max-width: 150px;
  overflow: hidden;
  padding: 4px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 650;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.flash-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.flash-badge svg {
  width: 12px;
  height: 12px;
  flex: none;
  color: #eab308;
}

@media (prefers-color-scheme: dark) {
  .flash-badge svg {
    color: #facc15;
  }
}

.badge.tone-0 {
  color: #4338ca;
  background: #eef0ff;
  border-color: #dde0ff;
}
.badge.tone-1 {
  color: #0f766e;
  background: #e7f8f5;
  border-color: #c9efe9;
}
.badge.tone-2 {
  color: #92400e;
  background: #fff7e6;
  border-color: #fde9c3;
}
.badge.tone-3 {
  color: #9f1239;
  background: #fff1f4;
  border-color: #ffd9e0;
}
.badge.tone-4 {
  color: #1d4ed8;
  background: #eaf1ff;
  border-color: #d3e2ff;
}
.badge.tone-5 {
  color: #166534;
  background: #e9f9ef;
  border-color: #c9eed7;
}

@media (prefers-color-scheme: dark) {
  .badge.tone-0 {
    color: #b9bbff;
    background: rgba(99, 102, 241, 0.16);
    border-color: rgba(139, 141, 255, 0.28);
  }
  .badge.tone-1 {
    color: #6ee7d7;
    background: rgba(20, 184, 166, 0.14);
    border-color: rgba(45, 212, 191, 0.26);
  }
  .badge.tone-2 {
    color: #fcd34d;
    background: rgba(245, 158, 11, 0.14);
    border-color: rgba(245, 158, 11, 0.28);
  }
  .badge.tone-3 {
    color: #fda4af;
    background: rgba(244, 63, 94, 0.14);
    border-color: rgba(244, 63, 94, 0.28);
  }
  .badge.tone-4 {
    color: #93c5fd;
    background: rgba(59, 130, 246, 0.14);
    border-color: rgba(59, 130, 246, 0.28);
  }
  .badge.tone-5 {
    color: #86efac;
    background: rgba(34, 197, 94, 0.13);
    border-color: rgba(34, 197, 94, 0.26);
  }
}

.qr-thumb {
  display: grid;
  place-items: center;
  padding: 7px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.qr-thumb img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  border-radius: 4px;
}

.qr-table {
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.qr-data {
  overflow: hidden;
  padding: 5px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 0.74rem;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.qr-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: auto;
  padding-top: 2px;
}

.qr-actions .btn-danger-ghost {
  margin-left: auto;
}

/* ----------------------------------------------------------- Empty state */
.empty {
  display: grid;
  place-items: center;
  gap: 8px;
  padding: 56px 24px;
  text-align: center;
}

.empty-icon {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  margin-bottom: 4px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-text);
}

.empty-icon svg {
  width: 28px;
  height: 28px;
}

.empty h2 {
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.empty p {
  max-width: 48ch;
  color: var(--text-muted);
  font-size: 0.92rem;
}

.empty .btn {
  margin-top: 8px;
}

/* ------------------------------------------------------------ Login/Error */
.login-body {
  display: grid;
  place-items: center;
  padding: 32px 20px;
  background:
    radial-gradient(900px 520px at 12% -10%, rgba(84, 87, 229, 0.17), transparent 62%),
    radial-gradient(800px 520px at 100% 112%, rgba(6, 182, 212, 0.14), transparent 58%),
    var(--bg);
}

.login-shell {
  width: min(480px, 100%);
}

.login-card {
  padding: 36px 32px 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-lg);
}

.login-card h1 {
  margin-top: 24px;
  font-size: 1.45rem;
  letter-spacing: -0.03em;
  line-height: 1.2;
}

.login-card .lede {
  margin-top: 7px;
  font-size: 0.92rem;
  line-height: 1.55;
}

.alert {
  margin-top: 18px;
  padding: 10px 14px;
  border: 1px solid rgba(211, 58, 74, 0.28);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.87rem;
  font-weight: 550;
}

.login-form {
  display: grid;
  gap: 9px;
  margin-top: 22px;
}

.login-form label {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.login-form textarea {
  min-height: 84px;
  resize: vertical;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.5;
  word-break: break-all;
}

.login-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.fineprint {
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.78rem;
}

.error-card {
  text-align: left;
}

.error-code {
  margin-top: 24px;
  color: var(--accent-text);
  font-size: 0.8rem;
  font-weight: 750;
  letter-spacing: 0.16em;
}

.error-card h1 {
  margin-top: 6px;
}

.error-card .lede {
  margin-bottom: 22px;
}

/* ---------------------------------------------------------------- Dialog */
.dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}

.dialog::backdrop {
  background: rgba(8, 10, 16, 0.55);
  backdrop-filter: blur(3px);
}

.dialog-body {
  display: grid;
  gap: 12px;
  padding: 26px;
}

.dialog-icon {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--danger-soft);
  color: var(--danger);
}

.dialog-icon svg {
  width: 22px;
  height: 22px;
}

.dialog h2 {
  font-size: 1.2rem;
  letter-spacing: -0.02em;
}

.dialog p {
  color: var(--text-muted);
  font-size: 0.92rem;
  line-height: 1.55;
}

.dialog p strong {
  color: var(--text);
}

.dialog .dialog-hint {
  font-size: 0.8rem;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

/* ----------------------------------------------------------------- Toasts */
.toast-region {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(380px, calc(100vw - 40px));
  pointer-events: none;
}

.toast {
  padding: 11px 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  background: #1c2029;
  color: #ffffff;
  font-size: 0.87rem;
  line-height: 1.4;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.22s ease-out;
}

.toast-error {
  background: #7f1d2a;
}

.toast-info {
  background: #2a3242;
}

.toast.is-leaving {
  opacity: 0;
  transform: translateY(6px);
  transition:
    opacity 0.25s ease,
    transform 0.25s ease;
}

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

/* ------------------------------------------------------------- Responsive */
@media (max-width: 720px) {
  .page-head {
    flex-direction: column;
    gap: 12px;
  }

  .bulk-actions {
    width: 100%;
    margin-left: 0;
  }

  .bulk-actions .btn {
    flex: 1 1 auto;
  }
}

@media (max-width: 640px) {
  .container {
    padding: 0 16px;
  }

  .page {
    padding-top: 24px;
    padding-bottom: 56px;
  }

  .page-head h1 {
    font-size: 1.65rem;
  }

  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .stat {
    padding: 12px 12px;
  }

  .stat-label {
    min-height: 2.5em;
    font-size: 0.62rem;
    line-height: 1.25;
    letter-spacing: 0.04em;
  }

  .stat-value {
    font-size: 1.4rem;
  }

  .filters {
    padding: 14px;
  }

  .field,
  .field.grow {
    min-width: 100%;
  }

  .filter-actions {
    width: 100%;
  }

  .filter-actions .btn {
    flex: 1 1 auto;
  }

  .qr-grid {
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  }

  .login-card {
    padding: 28px 22px 24px;
  }

  .login-actions .btn {
    flex: 1 1 auto;
  }

  .toast-region {
    right: 16px;
    bottom: 16px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
