:root {
  color-scheme: light;
  --background: #ffffff;
  --foreground: #09090b;
  --surface: #f4f4f5;
  --elevated: #e4e4e7;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --secondary: #3f3f46;
  --muted: #52525b;
  --subtle: #71717a;
  --faint: #a1a1aa;
  --accent: #047857;
  --accent-solid: #10b981;
  --accent-solid-hover: #34d399;
  --accent-foreground: #09090b;
  --emerald-tint: rgba(16, 185, 129, 0.1);
  --emerald-border: rgba(16, 185, 129, 0.3);
  --info: #4f46e5;
  --warning: #b45309;
  --amber-tint: rgba(245, 158, 11, 0.1);
  --amber-border: rgba(245, 158, 11, 0.3);
  --danger: #dc2626;
  --red-tint: rgba(239, 68, 68, 0.1);
  --red-border: rgba(239, 68, 68, 0.3);
  --overlay: rgba(9, 9, 11, 0.3);
  --grid-line: rgba(228, 228, 231, 0.8);
  --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.05), 0 8px 10px -6px rgba(0, 0, 0, 0.03);
  --shadow-menu: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.08);
  --font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

html.dark {
  color-scheme: dark;
  --background: #09090b;
  --foreground: #fafafa;
  --surface: #18181b;
  --elevated: #27272a;
  --border: #27272a;
  --border-strong: #3f3f46;
  --secondary: #d4d4d8;
  --muted: #a1a1aa;
  --subtle: #71717a;
  --faint: #52525b;
  --accent: #6ee7b7;
  --accent-solid: #10b981;
  --accent-solid-hover: #34d399;
  --accent-foreground: #09090b;
  --emerald-tint: rgba(16, 185, 129, 0.12);
  --emerald-border: rgba(16, 185, 129, 0.32);
  --info: #a5b4fc;
  --warning: #fcd34d;
  --amber-tint: rgba(245, 158, 11, 0.12);
  --amber-border: rgba(245, 158, 11, 0.32);
  --danger: #f87171;
  --red-tint: rgba(239, 68, 68, 0.12);
  --red-border: rgba(239, 68, 68, 0.32);
  --overlay: rgba(0, 0, 0, 0.6);
  --grid-line: rgba(39, 39, 42, 0.45);
  --shadow-card: 0 15px 30px -10px rgba(0, 0, 0, 0.45);
  --shadow-menu: 0 20px 30px -8px rgba(0, 0, 0, 0.6);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

::selection {
  background: rgba(16, 185, 129, 0.32);
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: var(--background);
  color: var(--foreground);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.bg-grid {
  position: fixed;
  inset: 0 0 auto 0;
  height: 480px;
  pointer-events: none;
  z-index: 0;
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: 32px 32px;
  -webkit-mask-image: radial-gradient(ellipse 80% 55% at 50% 0%, #000 30%, transparent 100%);
  mask-image: radial-gradient(ellipse 80% 55% at 50% 0%, #000 30%, transparent 100%);
}

/* ---- Logo Chip ---- */
.logo-chip {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 8px;
  border: 1px solid var(--emerald-border);
  background: var(--emerald-tint);
  color: var(--accent);
}

.logo-chip.lg {
  width: 40px;
  height: 40px;
  border-radius: 12px;
}

.logo-chip svg,
.logo-chip .logo-img {
  width: 18px;
  height: 18px;
  display: block;
}

.logo-chip.lg svg,
.logo-chip.lg .logo-img {
  width: 20px;
  height: 20px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--foreground);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease, opacity 140ms ease;
  outline: none;
}

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

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

.btn:focus-visible {
  box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.35);
}

.btn:disabled {
  opacity: 0.55;
  pointer-events: none;
}

.btn-primary {
  background: var(--accent-solid);
  border-color: rgba(16, 185, 129, 0.4);
  color: var(--accent-foreground);
  font-weight: 600;
  box-shadow: 0 0 0 1px rgba(16, 185, 129, 0.35), 0 8px 24px -8px rgba(16, 185, 129, 0.5);
}

.btn-primary:hover {
  background: var(--accent-solid-hover);
  border-color: rgba(52, 211, 153, 0.6);
}

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

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

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

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

.btn-sm {
  height: 30px;
  padding: 0 10px;
  font-size: 12px;
  gap: 6px;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
}

.btn-full {
  width: 100%;
}

/* ---- Inputs & Fields ---- */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

.field-error {
  margin: 0;
  font-size: 11px;
  color: var(--danger);
}

.input {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--background);
  color: var(--foreground);
  font: inherit;
  font-size: 13.5px;
  outline: none;
  transition: border-color 140ms ease, box-shadow 140ms ease, background-color 140ms ease;
}

.input::placeholder {
  color: var(--faint);
}

.input:focus {
  border-color: rgba(16, 185, 129, 0.6);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.15);
}

/* ---- Alerts ---- */
.alert {
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--secondary);
}

.alert-danger {
  border-color: var(--red-border);
  background: var(--red-tint);
  color: var(--danger);
}

.alert-emerald {
  border-color: var(--emerald-border);
  background: var(--emerald-tint);
  color: var(--foreground);
}

/* ---- Login Page ---- */
.login-shell {
  position: relative;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-top-right {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 20;
}

.login-card {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 384px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 24px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--background) 92%, transparent);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-card);
}

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

.login-brand h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.login-brand p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--subtle);
}

.login-foot {
  margin: 0;
  text-align: center;
  font-size: 11px;
  color: var(--faint);
}

/* ---- Sticky App Header (Layiha style) ---- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  border-bottom: 1px solid color-mix(in oklab, var(--border) 85%, transparent);
  background: color-mix(in oklab, var(--background) 82%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.header-inner {
  max-width: 1440px;
  margin: 0 auto;
  padding: 10px 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.brand-link {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--foreground);
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -0.01em;
}

/* Segmented View Switch */
.segmented {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface) 75%, transparent);
}

.seg-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--subtle);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease;
}

.seg-btn svg {
  width: 14px;
  height: 14px;
}

.seg-btn:hover {
  color: var(--secondary);
}

.seg-btn.active {
  background: var(--elevated);
  color: var(--accent);
}

.seg-count {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--faint);
}

/* Search Box */
.search-wrap {
  position: relative;
  flex: 1 1 240px;
  max-width: 440px;
  margin: 0 auto;
}

.search-wrap svg.search-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: var(--subtle);
  pointer-events: none;
}

.search-input {
  width: 100%;
  height: 36px;
  padding: 0 54px 0 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface) 65%, transparent);
  color: var(--foreground);
  font: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.search-input::placeholder {
  color: var(--subtle);
}

.search-input:focus {
  background: var(--surface);
  border-color: rgba(16, 185, 129, 0.5);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.14);
}

.search-input::-webkit-search-cancel-button {
  display: none;
}

.kbd-hint {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  padding: 2px 6px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--background);
  color: var(--faint);
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 1.2;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

/* ---- Dropdown Menus (Theme + User) ---- */
.menu-wrap {
  position: relative;
}

.avatar-pill {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--secondary);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
}

.dropdown-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 210px;
  padding: 4px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--background);
  box-shadow: var(--shadow-menu);
  z-index: 60;
}

.dropdown-header {
  padding: 8px 10px;
}

.dropdown-header .name {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--foreground);
  overflow: hidden;
  text-overflow: ellipsis;
}

.dropdown-header .sub {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--subtle);
  overflow: hidden;
  text-overflow: ellipsis;
}

.dropdown-sep {
  height: 1px;
  margin: 4px 0;
  background: var(--border);
}

.dropdown-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--secondary);
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.dropdown-item svg {
  width: 15px;
  height: 15px;
  color: var(--subtle);
  flex-shrink: 0;
}

.dropdown-item:hover {
  background: var(--elevated);
  color: var(--foreground);
}

.dropdown-item .check-mark {
  margin-left: auto;
  color: var(--accent);
}

/* ---- Main Content ---- */
.container {
  position: relative;
  z-index: 10;
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px 20px 56px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.container-narrow {
  max-width: 780px;
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

.page-head h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.page-head p {
  margin: 3px 0 0;
  font-size: 13px;
  color: var(--subtle);
}

/* ---- Cards ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

.card {
  border-radius: 16px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--background) 92%, transparent);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card.tone-emerald {
  border-color: var(--emerald-border);
  background: linear-gradient(180deg, var(--emerald-tint), transparent 85%), var(--background);
}

.card.tone-amber {
  border-color: var(--amber-border);
  background: linear-gradient(180deg, var(--amber-tint), transparent 85%), var(--background);
}

.card.tone-danger {
  border-color: var(--red-border);
  background: linear-gradient(180deg, var(--red-tint), transparent 85%), var(--background);
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--foreground);
}

.card-desc {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--subtle);
}

.stat-label {
  font-size: 11.5px;
  font-weight: 500;
  color: var(--subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.stat-value {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.stat-meta {
  margin: 0;
  font-size: 12px;
  color: var(--subtle);
}

/* ---- Filter Chips ---- */
.chip-group {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 120ms ease;
}

.chip:hover {
  border-color: var(--border-strong);
  color: var(--foreground);
}

.chip.active {
  border-color: var(--emerald-border);
  background: var(--emerald-tint);
  color: var(--accent);
}

/* ---- Badges ---- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--secondary);
  font-size: 11px;
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
}

.badge-dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: currentColor;
}

.badge.available {
  border-color: var(--emerald-border);
  background: var(--emerald-tint);
  color: var(--accent);
}

.badge.none {
  border-color: var(--border-strong);
  background: var(--surface);
  color: var(--muted);
}

.badge.rate_limited {
  border-color: var(--amber-border);
  background: var(--amber-tint);
  color: var(--warning);
}

.badge.session_expired,
.badge.error {
  border-color: var(--red-border);
  background: var(--red-tint);
  color: var(--danger);
}

/* ---- Tables ---- */
.table-wrap {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--background);
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

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

tbody tr:hover {
  background: color-mix(in oklab, var(--surface) 55%, transparent);
}

th {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--subtle);
  background: color-mix(in oklab, var(--surface) 50%, transparent);
  white-space: nowrap;
}

td.mono {
  font-family: var(--font-mono);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

td.msg {
  max-width: 440px;
  word-break: break-word;
  color: var(--muted);
}

.empty-cell {
  text-align: center;
  padding: 36px 16px;
  color: var(--subtle);
}

/* ---- Collapsible Details ---- */
details.panel {
  border-radius: 16px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--background) 92%, transparent);
  padding: 14px 18px;
}

details.panel > summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--foreground);
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

details.panel > summary::-webkit-details-marker {
  display: none;
}

details.panel > summary::after {
  content: '▾';
  color: var(--subtle);
  transition: transform 140ms ease;
}

details.panel[open] > summary::after {
  transform: rotate(180deg);
}

.details-body {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.discovery-item {
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 12px;
}

.discovery-item pre {
  margin: 8px 0 0;
  max-height: 240px;
  overflow: auto;
  background: #09090b;
  color: #e4e4e7;
  border: 1px solid #27272a;
  padding: 10px;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---- Account & Tokens ---- */
.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
}

.token-create-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.token-create-row .input {
  max-width: 300px;
}

.token-secret-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--emerald-border);
  background: var(--emerald-tint);
}

.token-secret-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.token-secret-row code {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--background);
  font-family: var(--font-mono);
  font-size: 12px;
}

.token-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.token-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.token-list li:last-child {
  border-bottom: none;
}

.token-name {
  margin: 0;
  font-size: 13.5px;
  font-weight: 500;
}

.token-name.revoked {
  text-decoration: line-through;
  color: var(--subtle);
}

.token-meta {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--faint);
}

/* ---- Toast ---- */
.toast-stack {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--foreground);
  box-shadow: var(--shadow-menu);
  font-size: 13px;
}

.toast.error {
  border-color: var(--red-border);
  color: var(--danger);
}

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

.spin {
  animation: spin 0.8s linear infinite;
}

@media (max-width: 720px) {
  .search-wrap {
    order: 10;
    flex-basis: 100%;
    max-width: none;
  }
  .hide-mobile {
    display: none;
  }
}
