:root {
  color-scheme: dark;
  --bg: #0e1013;
  --panel: #161a21;
  --panel-2: #1b2029;
  --panel-3: #202631;
  --line: #303846;
  --text: #e7eaf0;
  --dim: #a1aaba;
  --accent: #bd4735;
  --accent-hover: #d0523d;
  --focus: #8abcf2;
  --s1: #69a5e3;
  --s2: #e6b14f;
  --s3: #ef705c;
  --s4: #e35c8d;
  --green: #55cd86;
  --red: #ff7479;
  --radius: 10px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { background: var(--bg); }
body {
  min-height: 100dvh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}

button, input, textarea, select { font: inherit; }
a { color: inherit; text-decoration: none; }

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

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

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  padding: 9px 12px;
  border-radius: 7px;
  background: var(--text);
  color: var(--bg);
  transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #151922, rgba(14, 16, 19, .98));
}

.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; white-space: nowrap; }
.brand h1 { margin: 0; font: inherit; font-weight: 700; }
.brand .tag {
  padding: 2px 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--dim);
  font-size: 11px;
}

.controls, .admin-tools, .dialog-actions, .pattern-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.controls { justify-content: flex-end; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: 7px 12px;
  border: 1px solid var(--accent);
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease, transform .1s ease;
}
.btn:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { cursor: not-allowed; opacity: .55; }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn.ghost:hover:not(:disabled) { background: var(--panel-3); border-color: #4a5667; }
.btn.danger { color: var(--red); }

select, input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--text);
}
select { width: auto; min-height: 34px; padding: 6px 30px 6px 10px; font-size: 13px; }
input, textarea { padding: 8px 10px; }
textarea { resize: vertical; }
input::placeholder, textarea::placeholder { color: #808a9b; }

main { max-width: 1240px; margin: 0 auto; padding: 16px 20px 60px; }

.statusbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px 0;
  min-height: 36px;
  margin-bottom: 14px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(22, 26, 33, .72);
  color: var(--dim);
  font-size: 12px;
}
.statusbar > span { padding: 0 10px; border-left: 1px solid var(--line); }
.statusbar > span:first-child { padding-left: 0; border-left: 0; }
.statusbar .status-primary { color: var(--text); font-weight: 700; }
.statusbar.is-scanning { border-color: var(--s2); }
.statusbar.is-scanning .status-primary { color: var(--s2); }
.statusbar.is-stale, .statusbar.has-error { border-color: rgba(255, 116, 121, .65); }
.status-error { flex-basis: 100%; padding: 4px 0 0 !important; border: 0 !important; color: var(--red); }

.cards {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  min-height: 80px;
  margin-bottom: 16px;
}
.cards > .loading-state, .funnel > .loading-state { grid-column: 1 / -1; }
.card {
  min-width: 0;
  padding: 13px 15px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}
.card .n { overflow: hidden; font-size: 25px; font-weight: 800; line-height: 1.2; text-overflow: ellipsis; }
.card .l { margin-top: 4px; color: var(--dim); font-size: 11px; }
.card.s1 .n { color: var(--s1); }
.card.s2 .n { color: var(--s2); }
.card.s3 .n { color: var(--s3); }
.card.s4 .n { color: var(--s4); }

.funnel {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
  min-height: 82px;
  margin-bottom: 18px;
}
.fstep {
  min-width: 0;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-top-width: 3px;
  border-radius: var(--radius);
  background: var(--panel);
}
.fstep .fh { color: var(--dim); font-size: 12px; }
.fstep .fn { margin-top: 1px; font-size: 21px; font-weight: 800; }
.fstep small { color: var(--dim); font-size: 11px; }
.fstep.s1 { border-top-color: var(--s1); }
.fstep.s2 { border-top-color: var(--s2); }
.fstep.s3 { border-top-color: var(--s3); }
.fstep.s4 { border-top-color: var(--s4); }

.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.tab {
  flex: 0 0 auto;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--dim);
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}
.tab:hover { color: var(--text); border-color: #4a5667; }
.tab.active { border-color: var(--accent); background: var(--accent); color: #fff; }

.panel {
  min-height: 260px;
  padding: 6px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { position: sticky; top: 0; z-index: 1; background: var(--panel); color: var(--dim); font-weight: 650; }
tbody tr:last-child td { border-bottom: 0; }
.clickable-row { cursor: pointer; }
.clickable-row:hover td, .clickable-row:focus td { background: var(--panel-2); }
.clickable-row:focus { outline: 2px solid var(--focus); outline-offset: -2px; }
.empty-row td { padding: 34px 12px; text-align: center; }

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 750;
  line-height: 1.2;
}
.b0 { background: rgba(161, 170, 186, .12); color: var(--dim); }
.b1 { background: rgba(105, 165, 227, .14); color: var(--s1); }
.b2 { background: rgba(230, 177, 79, .14); color: var(--s2); }
.b3 { background: rgba(239, 112, 92, .14); color: var(--s3); }
.b4 { background: rgba(227, 92, 141, .14); color: var(--s4); }
.stage-route { display: inline-flex; align-items: center; gap: 4px; }

.scorebar { position: relative; width: 90px; height: 17px; }
.scorebar progress, .sub progress {
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: 0;
  border-radius: 4px;
  appearance: none;
  background: var(--panel-3);
}
.scorebar progress::-webkit-progress-bar, .sub progress::-webkit-progress-bar { background: var(--panel-3); }
.scorebar progress::-webkit-progress-value, .sub progress::-webkit-progress-value { background: linear-gradient(90deg, var(--s2), var(--s3)); }
.scorebar progress::-moz-progress-bar, .sub progress::-moz-progress-bar { background: linear-gradient(90deg, var(--s2), var(--s3)); }
.scorebar span { position: absolute; inset: 0 auto auto 6px; font-size: 11px; font-weight: 750; line-height: 17px; color: var(--text); }

.up { color: var(--green); }
.down { color: var(--red); }
.muted { color: var(--dim); }
.flag {
  display: inline-block;
  margin-left: 5px;
  padding: 1px 4px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--dim);
  font-size: 10px;
  font-weight: 650;
}
.squeeze-flag { border-color: rgba(85, 205, 134, .55); color: var(--green); }

.mgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 9px; margin: 10px 0 14px; }
.mi { padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.mi .v { min-height: 25px; font-size: 17px; font-weight: 750; }
.mi .k { color: var(--dim); font-size: 11px; }

h2 { margin: 0 0 5px; font-size: 19px; }
h3 { margin: 20px 0 7px; color: var(--dim); font-size: 14px; }
p { margin: 7px 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chip { padding: 3px 9px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel-2); color: var(--dim); font-size: 12px; }
.active-chip { border-color: rgba(85, 205, 134, .55); color: var(--green); }

.pattern-list { padding: 4px; }
.pcard { margin-bottom: 9px; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); }
.pcard.active { border-color: var(--accent); }
.pattern-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.pattern-title { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; min-width: 0; }
.pattern-description { margin: 6px 0; font-size: 12px; }
.pattern-create { margin-top: 20px; }
.pattern-form { display: grid; gap: 12px; }
.admin-hint { margin: 8px 8px 14px; }

.field { display: grid; gap: 5px; }
.field label { color: var(--text); font-size: 12px; font-weight: 650; }
.field-help { color: var(--dim); font-size: 11px; }
.form-error { margin: 2px 0 0; color: var(--red); font-size: 12px; }

.sub { margin: 10px 0; }
.sub .row { display: grid; grid-template-columns: 76px minmax(120px, 1fr) 32px; align-items: center; gap: 8px; margin: 6px 0; font-size: 12px; }
.sub progress { height: 8px; }
.sub-value { text-align: right; }
.sparkline { display: block; width: 100%; height: 64px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.sparkline polyline { stroke: var(--accent-hover); stroke-width: 2; }
.transition-list { display: flex; flex-wrap: wrap; gap: 8px 14px; font-size: 13px; }
.transition-item { display: inline-flex; align-items: center; gap: 4px; }
.coin-title { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; padding-right: 34px; }
.coin-score { color: var(--s3); font-size: 14px; }
.coin-meta { margin: 0 0 10px; }

.modal {
  width: min(760px, calc(100% - 32px));
  max-width: none;
  max-height: calc(100dvh - 40px);
  padding: 0;
  overflow: visible;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: var(--text);
}
.modal::backdrop { background: rgba(5, 7, 10, .76); }
.modal-box {
  position: relative;
  width: 100%;
  max-height: calc(100dvh - 40px);
  padding: 22px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(3, 6, 12, .55);
}
.modal-close {
  position: absolute;
  top: 9px;
  right: 10px;
  z-index: 2;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--dim);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { background: var(--panel-2); color: var(--text); }
.login-modal { width: min(430px, calc(100% - 32px)); }
.login-box { display: grid; gap: 12px; }
.dialog-actions { justify-content: flex-end; margin-top: 4px; }

.loading-state { display: grid; gap: 10px; padding: 18px 12px; }
.skeleton-row { display: grid; grid-template-columns: 1.2fr .7fr 1fr 1fr .8fr; gap: 14px; }
.skeleton-cell {
  height: 15px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--panel-2) 20%, var(--panel-3) 45%, var(--panel-2) 70%);
  background-size: 240% 100%;
  animation: skeleton 1.4s ease-in-out infinite;
}
@keyframes skeleton { to { background-position-x: -240%; } }

.empty-state { display: grid; justify-items: start; gap: 7px; padding: 30px 24px; }
.error-state { border-left: 3px solid var(--red); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 90;
  max-width: min(520px, calc(100% - 32px));
  padding: 10px 16px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: var(--red); }

.foot { padding: 18px; color: var(--dim); font-size: 12px; text-align: center; }
.foot > span { margin: 0 5px; }
.foot #intv { margin: 0; }

@media (max-width: 1050px) {
  .topbar { align-items: flex-start; }
  .cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .topbar { position: static; display: grid; padding: 12px 14px; }
  .brand { justify-content: space-between; font-size: 15px; }
  .brand .tag { display: none; }
  .controls { justify-content: flex-start; }
  .admin-tools { display: contents; }
  main { padding: 12px 12px 42px; }
  .statusbar { align-items: flex-start; }
  .statusbar > span { flex-basis: 50%; padding: 0 7px; border-left: 0; }
  .statusbar > span:nth-child(even) { border-left: 1px solid var(--line); }
  .statusbar .status-error { flex-basis: 100%; }
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .funnel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .pattern-head { display: grid; }
  .modal { width: calc(100% - 20px); max-height: calc(100dvh - 20px); }
  .modal-box { max-height: calc(100dvh - 20px); padding: 18px 14px; }
  .skeleton-row { grid-template-columns: 1.5fr 1fr 1fr; }
  .skeleton-cell:nth-child(n+4) { display: none; }
}

@media (max-width: 430px) {
  .cards, .funnel { grid-template-columns: 1fr 1fr; gap: 7px; }
  .card { padding: 11px 12px; }
  .card .n { font-size: 21px; }
  .controls .btn, .controls select { flex: 1 1 auto; }
  .sub .row { grid-template-columns: 68px minmax(90px, 1fr) 28px; }
}

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