:root {
  color-scheme: light dark;
  --bg: #f4f0ea;
  --bg-elevated: #fffdf9;
  --panel: rgba(255, 253, 249, 0.88);
  --text: #1c1714;
  --muted: #6b6259;
  --border: #e2d8cc;
  --border-strong: #cfc3b4;
  --accent: #c45c26;
  --accent-hover: #a84d1e;
  --accent-soft: rgba(196, 92, 38, 0.12);
  --danger: #b42318;
  --danger-soft: #fdecec;
  --ok: #157f4b;
  --ok-soft: #e8f7ef;
  --shadow-sm: 0 1px 2px rgba(28, 23, 20, 0.05);
  --shadow-md: 0 8px 24px rgba(28, 23, 20, 0.08);
  --shadow-lg: 0 20px 48px rgba(28, 23, 20, 0.12);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --ring: 0 0 0 3px rgba(196, 92, 38, 0.22);
  --font: "DM Sans", "Segoe UI", system-ui, sans-serif;
  --transition: 160ms ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14110f;
    --bg-elevated: #1e1a17;
    --panel: rgba(30, 26, 23, 0.92);
    --text: #f5f0ea;
    --muted: #a89f95;
    --border: #3a332d;
    --border-strong: #4d453d;
    --accent-soft: rgba(232, 140, 80, 0.16);
    --danger-soft: rgba(180, 35, 24, 0.18);
    --ok-soft: rgba(21, 127, 75, 0.18);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.45);
  }

  select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23a89f95' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  }
}

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

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

.bg-pattern {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(196, 92, 38, 0.14), transparent 60%),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(196, 92, 38, 0.08), transparent 50%),
    linear-gradient(180deg, #ebe4da 0%, var(--bg) 45%);
  z-index: -1;
}

@media (prefers-color-scheme: dark) {
  .bg-pattern {
    background:
      radial-gradient(ellipse 80% 50% at 50% -10%, rgba(232, 140, 80, 0.1), transparent 60%),
      radial-gradient(ellipse 60% 40% at 100% 100%, rgba(232, 140, 80, 0.06), transparent 50%),
      linear-gradient(180deg, #1a1613 0%, var(--bg) 45%);
  }
}

.shell {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 20px 56px;
}

/* Header */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent) 0%, #e07a3a 100%);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

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

.header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 500;
}

/* Stats */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(12px);
}

.stat-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 11px;
  flex-shrink: 0;
}

.stat-icon svg {
  width: 20px;
  height: 20px;
}

.stat-total .stat-icon {
  background: var(--accent-soft);
  color: var(--accent);
}

.stat-active .stat-icon {
  background: var(--ok-soft);
  color: var(--ok);
}

.stat-inactive .stat-icon {
  background: var(--danger-soft);
  color: var(--danger);
}

.stat-body {
  min-width: 0;
}

.stat-value {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.stat-label {
  margin: 4px 0 0;
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--muted);
}

/* Panels */

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(12px);
}

.panel-elevated {
  box-shadow: var(--shadow-lg);
}

.panel-intro {
  margin-bottom: 20px;
}

.panel h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.muted {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

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

.panel-head .panel-intro {
  margin-bottom: 0;
}

/* Login */

.login-layout {
  display: flex;
  justify-content: center;
  padding: 24px 0 8px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  animation: fade-up 0.4s ease;
}

.login-intro {
  text-align: center;
  margin-bottom: 24px;
}

.login-icon {
  display: inline-grid;
  place-items: center;
  width: 52px;
  height: 52px;
  margin-bottom: 14px;
  border-radius: 14px;
  background: var(--accent-soft);
  color: var(--accent);
}

.login-icon svg {
  width: 26px;
  height: 26px;
}

.login-intro h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

.login-intro .muted {
  margin-top: 8px;
}

/* Forms */

.stack {
  display: grid;
  gap: 14px;
}

.grid-form {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 14px;
  align-items: end;
}

.field {
  display: grid;
  gap: 7px;
}

.field-label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.01em;
}

input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

input::placeholder {
  color: var(--muted);
  opacity: 0.65;
}

input:hover {
  border-color: var(--border-strong);
}

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

select {
  width: 100%;
  padding: 11px 36px 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background-color: var(--bg-elevated);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b6259' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

select:hover {
  border-color: var(--border-strong);
}

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

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background var(--transition),
    border-color var(--transition),
    transform var(--transition),
    opacity var(--transition);
}

.btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, #d06830 0%, var(--accent) 100%);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-hover) 100%);
}

.btn-secondary {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
}

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

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

.btn-ghost:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: rgba(28, 23, 20, 0.04);
}

@media (prefers-color-scheme: dark) {
  .btn-ghost:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.05);
  }
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid rgba(180, 35, 24, 0.2);
  padding: 7px 12px;
  font-size: 0.82rem;
}

.btn-danger:hover:not(:disabled) {
  background: rgba(180, 35, 24, 0.22);
}

.btn-ok {
  background: var(--ok-soft);
  color: var(--ok);
  border: 1px solid rgba(21, 127, 75, 0.2);
  padding: 7px 12px;
  font-size: 0.82rem;
}

.btn-ok:hover:not(:disabled) {
  background: rgba(21, 127, 75, 0.22);
}

.btn-block {
  width: 100%;
}

.btn.is-loading .btn-label {
  opacity: 0;
}

.btn.is-loading .btn-spinner {
  display: block;
  position: absolute;
}

.btn {
  position: relative;
}

.btn-spinner {
  display: none;
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
}

#refresh-btn.is-loading .refresh-icon {
  animation: spin 0.8s linear infinite;
}

/* Alerts */

.alert {
  margin: 14px 0 0;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
}

.alert-error {
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid rgba(180, 35, 24, 0.18);
}

/* Key box */

.key-box {
  margin-top: 20px;
  padding: 18px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid rgba(196, 92, 38, 0.22);
  animation: fade-up 0.3s ease;
}

.key-box-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}

.key-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--accent);
  flex-shrink: 0;
}

.key-icon svg {
  width: 18px;
  height: 18px;
}

.key-label {
  margin: 0;
  font-weight: 600;
  font-size: 0.92rem;
}

.key-hint {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

#created-key {
  display: block;
  word-break: break-all;
  font-family: ui-monospace, "Cascadia Code", "SF Mono", monospace;
  font-size: 0.88rem;
  padding: 12px 14px;
  margin-bottom: 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  line-height: 1.6;
}

/* Table */

.table-toolbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  margin-bottom: 12px;
  align-items: end;
}

.search-field {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

.search-field:focus-within {
  border-color: var(--accent);
  box-shadow: var(--ring);
}

.search-field svg {
  width: 16px;
  height: 16px;
  color: var(--muted);
  flex-shrink: 0;
}

.search-field input {
  flex: 1;
  min-width: 0;
  width: auto;
  border: 0;
  padding: 11px 0;
  background: transparent;
  box-shadow: none;
}

.search-field input:hover,
.search-field input:focus {
  border-color: transparent;
  box-shadow: none;
}

.field-inline {
  width: 160px;
  min-width: 160px;
}

.filter-count {
  margin: 0 0 12px;
  font-size: 0.86rem;
  color: var(--muted);
}

.table-loading {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.table-wrap {
  overflow-x: auto;
  margin: 0 -4px;
  padding: 0 4px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th,
td {
  padding: 13px 12px;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border);
}

.th-actions {
  text-align: right;
}

tbody tr {
  transition: background var(--transition);
}

tbody tr:hover {
  background: rgba(196, 92, 38, 0.04);
}

@media (prefers-color-scheme: dark) {
  tbody tr:hover {
    background: rgba(232, 140, 80, 0.06);
  }
}

td {
  border-bottom: 1px solid var(--border);
}

tbody tr:last-child td {
  border-bottom: 0;
}

td:last-child {
  text-align: right;
}

.empty-state {
  text-align: center;
  padding: 36px 16px !important;
  color: var(--muted);
}

.empty-state-inner {
  display: grid;
  gap: 6px;
  justify-items: center;
}

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

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

.empty-state strong {
  color: var(--text);
  font-weight: 600;
}

/* Status badges */

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: capitalize;
  letter-spacing: 0.01em;
}

.status::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.85;
}

.status-active {
  background: var(--ok-soft);
  color: var(--ok);
}

.status-disabled,
.status-expired {
  background: var(--danger-soft);
  color: var(--danger);
}

/* Dashboard animation */

.dashboard:not(.hidden) .stats-grid,
.dashboard:not(.hidden) .panel {
  animation: fade-up 0.35s ease backwards;
}

.dashboard:not(.hidden) .panel:nth-child(2) {
  animation-delay: 0.05s;
}

.dashboard:not(.hidden) .panel:nth-child(3) {
  animation-delay: 0.1s;
}

.hidden {
  display: none !important;
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

/* Responsive */

@media (max-width: 760px) {
  .shell {
    padding: 20px 14px 40px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .table-toolbar {
    grid-template-columns: 1fr;
  }

  .search-field,
  .field-inline {
    width: 100%;
    min-width: 0;
  }

  .grid-form {
    grid-template-columns: 1fr;
  }

  .panel-head {
    flex-direction: column;
    align-items: stretch;
  }

  .panel-head .btn {
    align-self: flex-start;
  }

  th:nth-child(5),
  td:nth-child(5) {
    display: none;
  }
}

@media (max-width: 520px) {
  .brand-sub {
    display: none;
  }

  th:nth-child(6),
  td:nth-child(6) {
    display: none;
  }
}
