:root {
  --bg: #0b0f12;
  --bg-2: #11171c;
  --surface-1: #151d23;
  --surface-2: #1b252c;
  --surface-3: #222f37;
  --line: #2f3e47;
  --line-2: #3f525e;
  --text: #e7f0ec;
  --text-muted: #a9bab2;
  --accent: #21c08a;
  --accent-2: #5ed1b2;
  --danger: #e07b7b;
  --ok: #82d69f;
  --radius-sm: 10px;
  --radius-md: 14px;
  --font: "SF Pro Display", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(circle at 12% 10%, rgba(33, 192, 138, 0.16), transparent 28%),
    radial-gradient(circle at 84% 4%, rgba(94, 209, 178, 0.12), transparent 24%),
    linear-gradient(160deg, var(--bg) 0%, var(--bg-2) 55%, #0d1419 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 32px 32px;
  mask-image: radial-gradient(circle at 50% 20%, black 30%, transparent 85%);
}

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 18px 14px;
  border-right: 1px solid var(--line);
  background: linear-gradient(180deg, #121a20 0%, #10161b 100%);
}

.sidebar::after {
  content: "";
  position: absolute;
  right: -1px;
  top: 24px;
  width: 2px;
  height: 120px;
  background: linear-gradient(180deg, transparent, rgba(33, 192, 138, 0.8), transparent);
}

.sidebar h1 {
  margin: 4px 8px 12px;
  font-size: 34px;
  line-height: 1.03;
  font-weight: 820;
  letter-spacing: .15px;
  color: #edf7f2;
  text-shadow: 0 0 22px rgba(33, 192, 138, 0.22);
}

.nav-btn,
.topbar button,
.filters button {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: linear-gradient(180deg, var(--surface-2), #1a2329);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all .18s ease;
}

.nav-btn {
  width: 100%;
  margin: 0;
  padding: 10px 12px;
  text-align: left;
  text-decoration: none;
}

.nav-btn:hover {
  border-color: var(--line-2);
  background: linear-gradient(180deg, #253038, #1f2a31);
}

.nav-btn.active {
  border-color: rgba(94, 209, 178, 0.55);
  color: #eafff7;
  background: linear-gradient(90deg, rgba(33, 192, 138, 0.34), rgba(33, 192, 138, 0.15));
  box-shadow: inset 0 0 0 1px rgba(94, 209, 178, 0.2), 0 0 0 1px rgba(33, 192, 138, 0.08);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.content {
  position: relative;
  padding: 18px 24px 24px;
  min-width: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: 2px 0 14px;
}

.topbar h2 {
  margin: 0;
  font-size: 36px;
  font-weight: 820;
  letter-spacing: .2px;
  color: #eef9f4;
}

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.topbar button,
.filters button {
  padding: 8px 13px;
}

#refreshBtn {
  background: linear-gradient(180deg, #1a8a64, #17694f);
  border-color: #2d9e78;
  color: #f1fff8;
  box-shadow: 0 8px 24px rgba(33, 192, 138, 0.2);
}

#refreshBtn:hover {
  background: linear-gradient(180deg, #24a174, #1b7759);
  border-color: #3fbf93;
}

.topbar button:disabled,
.filters button:disabled {
  opacity: .72;
  cursor: not-allowed;
}

.topbar button.is-loading {
  position: relative;
  padding-left: 30px;
}

.topbar button.is-loading::before {
  content: "";
  position: absolute;
  left: 12px;
  top: 50%;
  width: 11px;
  height: 11px;
  border: 2px solid rgba(230,236,232,.35);
  border-top-color: #f7fbf8;
  border-radius: 50%;
  transform: translateY(-50%);
  animation: spin .72s linear infinite;
}

@keyframes spin { to { transform: translateY(-50%) rotate(360deg); } }
@keyframes fade-up {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

body.loading .content::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(6, 10, 12, 0.34);
  backdrop-filter: blur(1px);
  pointer-events: none;
}

.view { display: none; }
.view.active { display: block; }
.view.active .card { animation: fade-up .24s ease both; }
.view.active .card:nth-child(2) { animation-delay: .05s; }
.view.active .card:nth-child(3) { animation-delay: .1s; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
  align-items: center;
}

.filters label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-muted);
}

.filters input,
.filters select,
.api-grid input,
.settings-row input,
.settings-row select {
  min-height: 36px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: linear-gradient(180deg, #12181d, #0f1418);
  color: var(--text);
  outline: none;
}

.filters input:focus,
.filters select:focus,
.api-grid input:focus,
.settings-row input:focus,
.settings-row select:focus {
  border-color: #3cae87;
  box-shadow: 0 0 0 3px rgba(33, 192, 138, 0.16);
}

.range-btn.active {
  color: #e9fff6;
  border-color: #2f9b77;
  background: linear-gradient(180deg, #1f5b49, #21483d);
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.card,
.panel,
.settings-panel {
  position: relative;
  background: linear-gradient(180deg, rgba(25, 35, 42, 0.95), rgba(20, 29, 35, 0.95));
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03), 0 12px 28px rgba(0, 0, 0, 0.28);
  overflow: hidden;
  max-width: 100%;
}

.card::before,
.panel::before,
.settings-panel::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(94, 209, 178, 0.4), transparent);
}

.card.wide { grid-column: 1 / -1; }
.card h3,
.panel h3 {
  margin: 2px 0 9px;
  font-size: 24px;
  font-weight: 760;
  letter-spacing: .2px;
}

.summary-line,
.summary,
.token-status,
.auto-status {
  margin: 0 0 10px;
  color: var(--text-muted);
  font-size: 13px;
}

.big-number {
  font-size: 36px;
  font-weight: 820;
  color: #dffced;
  text-shadow: 0 0 24px rgba(33, 192, 138, 0.26);
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
  table-layout: auto;
}

th, td {
  padding: 9px 7px;
  text-align: left;
  border-bottom: 1px solid #2f3b43;
  vertical-align: middle;
}

th {
  color: #c2d1ca;
  font-weight: 680;
  background: rgba(94, 209, 178, 0.06);
  position: sticky;
  top: 0;
  backdrop-filter: blur(2px);
}

tbody tr:hover { background: rgba(33, 192, 138, 0.12); }

#giftRankTable,
#liveGiftTable {
  width: 100%;
  min-width: 0;
  table-layout: fixed;
}

#giftRankTable th:nth-child(1),
#giftRankTable td:nth-child(1),
#liveGiftTable th:nth-child(1),
#liveGiftTable td:nth-child(1) {
  width: 48px;
}

#giftRankTable th:nth-child(3),
#giftRankTable td:nth-child(3) {
  width: 110px;
  text-align: right;
  white-space: nowrap;
}

#liveGiftTable th:last-child,
#liveGiftTable td:last-child {
  width: 120px;
  text-align: right;
  white-space: nowrap;
}

#giftRankTable th:nth-child(2),
#giftRankTable td:nth-child(2),
#liveGiftTable th:nth-child(2),
#liveGiftTable td:nth-child(2) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.panel.hidden,
.settings-panel.hidden { display: none; }

.settings-panel.invalid {
  border-color: #7c4a4f;
  background: linear-gradient(180deg, #2f2125, #251a1e);
}

.settings-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 8px;
}
.settings-row:last-child { margin-bottom: 0; }

.settings-row label {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--text-muted);
  font-size: 14px;
}

.token-row input { min-width: 420px; }

.api-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.api-grid label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--text-muted);
  font-size: 13px;
}

body.compact th,
body.compact td {
  padding: 5px 4px;
  font-size: 12px;
}

body.zebra tbody tr:nth-child(even) {
  background: rgba(76, 102, 91, 0.17);
}

body.reduced-motion * {
  animation: none !important;
  transition: none !important;
}

body.theme-contrast {
  --bg: #050607;
  --bg-2: #0a0d11;
  --surface-1: #0e1418;
  --surface-2: #121920;
  --surface-3: #182229;
  --line: #5a6c77;
  --line-2: #7d909b;
  --text: #f7fcfa;
  --text-muted: #d6e0db;
  --accent: #8fe9c8;
  --accent-2: #b4f2dd;
}

@media (max-width: 1200px) {
  .app { grid-template-columns: 220px 1fr; }
  .sidebar h1 { font-size: 30px; }
  .content { padding: 14px 16px 18px; }
  .topbar h2 { font-size: 30px; }
}

@media (max-width: 980px) {
  .app { grid-template-columns: 1fr; }

  .sidebar {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 8px;
    align-items: center;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px 12px;
    position: sticky;
    top: 0;
    z-index: 20;
    backdrop-filter: blur(8px);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar::after { display: none; }
  .sidebar h1 {
    width: auto;
    flex: 0 0 auto;
    margin: 0 4px 0 2px;
    font-size: 24px;
    white-space: nowrap;
  }
  .sidebar .nav-btn,
  .sidebar .settings-link {
    width: auto;
    min-width: max-content;
    flex: 0 0 auto;
    text-align: center;
    padding: 9px 10px;
    white-space: nowrap;
  }
  .sidebar-footer {
    margin-top: 0;
    margin-left: auto;
    padding-top: 0;
    border-top: 0;
    display: flex;
    flex: 0 0 auto;
  }
  .content { padding: 12px; }
  .topbar h2 { font-size: 28px; }
  .card,
  .panel,
  .settings-panel {
    padding: 10px;
  }
  .card,
  .panel {
    overflow-x: auto;
  }
  table {
    min-width: 680px;
  }
  .grid, .api-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }
  .topbar h2 {
    font-size: 26px;
    line-height: 1.15;
  }
  .topbar-actions {
    width: 100%;
    justify-content: flex-end;
  }
  .filters {
    gap: 8px;
    align-items: stretch;
  }
  .filters label {
    width: 100%;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 6px;
  }
  .filters input,
  .filters select {
    width: 100%;
    min-width: 0;
  }
  table {
    min-width: 620px;
  }
  #giftRankTable,
  #liveGiftTable {
    min-width: 0;
  }
  #giftRankTable th:nth-child(1),
  #giftRankTable td:nth-child(1),
  #liveGiftTable th:nth-child(1),
  #liveGiftTable td:nth-child(1) {
    width: 42px;
  }
  #giftRankTable th:nth-child(3),
  #giftRankTable td:nth-child(3),
  #liveGiftTable th:last-child,
  #liveGiftTable td:last-child {
    width: 92px;
  }
  #memberReplayTable th:nth-child(3),
  #memberReplayTable td:nth-child(3),
  #memberReplayTable th:nth-child(5),
  #memberReplayTable td:nth-child(5) {
    display: none;
  }
  #showGiftTable th:nth-child(5),
  #showGiftTable td:nth-child(5) {
    display: none;
  }
  #showsReplayTable th:nth-child(4),
  #showsReplayTable td:nth-child(4) {
    display: none;
  }
}

@media (max-width: 640px) {
  .app { min-height: 100dvh; }
  .sidebar {
    position: static;
    overflow: visible;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding: calc(10px + env(safe-area-inset-top)) 10px 10px;
  }
  .sidebar h1 {
    position: static;
    width: 100%;
    margin: 0 0 6px;
    font-size: 22px;
    padding-right: 0;
  }
  .sidebar .nav-btn,
  .sidebar .settings-link {
    width: 100%;
    min-width: 100%;
    font-size: 13px;
    line-height: 1.35;
    padding: 9px 10px;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }
  .sidebar-footer {
    width: 100%;
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
    display: flex;
  }
  .sidebar-footer .settings-link {
    width: 100%;
  }
  .content {
    padding:
      10px 10px
      calc(88px + env(safe-area-inset-bottom));
    scroll-padding-bottom: calc(88px + env(safe-area-inset-bottom));
  }
  .content::after {
    content: "";
    display: block;
    height: calc(28px + env(safe-area-inset-bottom));
  }
  .topbar h2 { font-size: 24px; }
  .topbar-actions { width: 100%; justify-content: flex-end; }
  .card h3, .panel h3 {
    font-size: 20px;
  }
  .summary-line,
  .summary,
  .token-status,
  .auto-status {
    font-size: 12px;
  }
  table {
    min-width: 560px;
    font-size: 12px;
  }
  #liveGiftTable {
    min-width: 0;
  }
  #liveGiftTable th:nth-child(1),
  #liveGiftTable td:nth-child(1) {
    width: 44px;
  }
  #liveGiftTable th:nth-child(3),
  #liveGiftTable td:nth-child(3) {
    width: 108px;
    text-align: right;
    white-space: nowrap;
  }
  #liveGiftTable th:nth-child(2),
  #liveGiftTable td:nth-child(2) {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #giftRankTable {
    min-width: 0;
    width: 100%;
    table-layout: fixed;
  }
  #giftRankTable th:nth-child(1),
  #giftRankTable td:nth-child(1) {
    width: 44px;
  }
  #giftRankTable th:nth-child(3),
  #giftRankTable td:nth-child(3) {
    width: 96px;
    text-align: right;
    white-space: nowrap;
  }
  #giftRankTable th:nth-child(2),
  #giftRankTable td:nth-child(2) {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  th, td { padding: 7px 6px; }
  .token-row input { min-width: 0; width: 100%; }
  th { position: static; }
}

@media (max-width: 480px) {
  .sidebar h1 { font-size: 20px; }
  .topbar h2 { font-size: 22px; }
  .card h3, .panel h3 { font-size: 18px; }
  .summary-line,
  .summary,
  .token-status,
  .auto-status {
    font-size: 11px;
  }
  th, td { padding: 6px 5px; }
}
