@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; }

  html {
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font: var(--t-body);
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  h1, h2, h3 { margin: 0; letter-spacing: var(--track-title); }
  h1 { font: var(--t-title1); }
  h2 { font: var(--t-title2); }
  h3 { font: var(--t-headline); }
  p { margin: 0; }

  a { color: var(--accent-text); text-decoration: none; }
  a:hover { text-decoration: underline; }

  button { font: inherit; color: inherit; cursor: pointer; }

  code, pre, .mono { font-family: var(--font-mono); }

  ::-webkit-scrollbar { width: 10px; height: 10px; }
  ::-webkit-scrollbar-thumb {
    background: var(--scroll-thumb);
    border-radius: var(--r-full);
    border: 2px solid transparent;
    background-clip: content-box;
  }
  ::-webkit-scrollbar-thumb:hover { background: var(--scroll-thumb-hover); background-clip: content-box; }

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

  [hidden] { display: none !important; }
}

@layer layout {
  .shell { display: flex; min-height: 100dvh; }

  .sidebar {
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    border-right: 1px solid var(--line);
    background: var(--surface-sunken);
    padding: var(--s6) var(--s4);
    display: flex;
    flex-direction: column;
    gap: var(--s6);
    position: sticky;
    top: 0;
    height: 100dvh;
  }

  .brand { display: flex; align-items: center; gap: var(--s3); padding: 0 var(--s2); }

  .logo-icon {
    width: 30px; height: 30px;
    border-radius: var(--r-md);
    object-fit: contain;
    flex: 0 0 auto;
  }
  :root[data-theme="dark"] .logo-icon--light { display: none; }
  :root[data-theme="light"] .logo-icon--dark { display: none; }
  .brand-text { display: flex; flex-direction: column; line-height: 1.15; }
  .brand-text b { font: var(--t-headline); }
  .brand-text span { font: var(--t-caption); color: var(--text-tertiary); letter-spacing: var(--track-caption); text-transform: uppercase; }

  .nav { display: flex; flex-direction: column; gap: 2px; }
  .nav-item {
    display: flex; align-items: center; gap: var(--s3);
    padding: var(--s2) var(--s3);
    border-radius: var(--r-md);
    border: 0; background: transparent;
    font: var(--t-subhead);
    color: var(--text-secondary);
    text-align: left; width: 100%;
    transition: background var(--fast) var(--ease-soft), color var(--fast) var(--ease-soft);
  }
  .nav-item:hover { background: var(--ov-04); color: var(--text); }
  .nav-item[aria-current="page"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }

  .sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--s2); }

  .main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

  .topbar {
    height: var(--topbar-h);
    flex: 0 0 var(--topbar-h);
    border-bottom: 1px solid var(--line);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 var(--gutter);
    gap: var(--s4);
    background: var(--bg);
    position: sticky; top: 0; z-index: 10;
  }

  .content {
    padding: var(--s8) var(--gutter) var(--s10);
    max-width: var(--content-max);
    width: 100%;
  }

  .panel { display: flex; flex-direction: column; gap: var(--s6); }
  .panel[hidden] { display: none; }

  @media (max-width: 860px) {
    .shell { flex-direction: column; }
    .sidebar {
      width: 100%; flex: none; height: auto; position: static;
      border-right: 0; border-bottom: 1px solid var(--line);
      flex-direction: row; align-items: center; gap: var(--s4);
      padding: var(--s3) var(--s4);
      overflow-x: auto;
    }
    .nav { flex-direction: row; gap: var(--s1); }
    .sidebar-foot { margin-top: 0; flex-direction: row; }
    .brand-text { display: none; }
    :root { --gutter: var(--s4); }
    .content { padding: var(--s6) var(--gutter) var(--s9); }
  }
}

@layer components {

  .card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-xl);
    padding: var(--s6);
  }
  .card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s4); margin-bottom: var(--s5); }
  .card-head p { font: var(--t-footnote); color: var(--text-tertiary); margin-top: var(--s1); }

  .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--s4); }
  .stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: var(--s5); }
  .stat-label { font: var(--t-caption); letter-spacing: var(--track-caption); text-transform: uppercase; color: var(--text-tertiary); }
  .stat-value { font: var(--t-display); letter-spacing: var(--track-display); margin-top: var(--s2); font-variant-numeric: tabular-nums; }
  .stat-sub { font: var(--t-footnote); color: var(--text-tertiary); margin-top: var(--s1); }

  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
    padding: var(--s2) var(--s4);
    border-radius: var(--r-md);
    border: 1px solid transparent;
    font: var(--t-subhead); font-weight: 600;
    transition: background var(--fast) var(--ease-soft), border-color var(--fast) var(--ease-soft), opacity var(--fast);
    white-space: nowrap;
  }
  .btn-primary { background: var(--accent); color: var(--on-accent); }
  .btn-primary:hover { background: var(--accent-hover); }
  .btn-ghost { background: transparent; border-color: var(--line-strong); color: var(--text-secondary); }
  .btn-ghost:hover { background: var(--ov-04); color: var(--text); }
  .btn-danger { background: transparent; border-color: var(--red); color: var(--red-text); }
  .btn-danger:hover { background: var(--red-soft); }
  .btn-sm { padding: var(--s1) var(--s3); font: var(--t-footnote); font-weight: 600; }
  .btn:disabled { opacity: .5; cursor: not-allowed; }

  .token-row {
    display: flex; align-items: center; gap: var(--s4);
    padding: var(--s4) 0;
    border-bottom: 1px solid var(--line-faint);
    flex-wrap: wrap;
  }
  .token-row:last-child { border-bottom: 0; }
  .token-main { flex: 1 1 240px; min-width: 0; }
  .token-name { font: var(--t-headline); display: flex; align-items: center; gap: var(--s2); }
  .token-prefix {
    font-family: var(--font-mono); font-size: .78rem;
    color: var(--text-tertiary);
    background: var(--ov-04);
    padding: 2px var(--s2); border-radius: var(--r-sm);
    display: inline-block; margin-top: var(--s2);
    word-break: break-all;
  }
  .token-meta { font: var(--t-footnote); color: var(--text-tertiary); margin-top: var(--s2); }

  .scope-chips { display: flex; flex-wrap: wrap; gap: var(--s1); margin-top: var(--s2); }
  .chip {
    font: var(--t-caption); letter-spacing: 0;
    padding: 2px var(--s2); border-radius: var(--r-full);
    background: var(--accent-soft); color: var(--accent-text);
    font-family: var(--font-mono);
  }

  .badge { font: var(--t-caption); letter-spacing: var(--track-caption); text-transform: uppercase; padding: 2px var(--s2); border-radius: var(--r-full); }
  .badge-ok { background: var(--green-soft); color: var(--green-text); }
  .badge-warn { background: var(--amber-soft); color: var(--amber-text); }
  .badge-off { background: var(--ov-08); color: var(--text-tertiary); }

  .field { display: flex; flex-direction: column; gap: var(--s2); }
  .field label { font: var(--t-subhead); font-weight: 600; }
  .field-hint { font: var(--t-footnote); color: var(--text-tertiary); }
  .input, .select {
    width: 100%;
    padding: var(--s3) var(--s3);
    border-radius: var(--r-md);
    border: 1px solid var(--line-strong);
    background: var(--surface-sunken);
    color: var(--text);
    font: var(--t-body);
  }
  .input:focus, .select:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-ring); }

  .check { display: flex; gap: var(--s3); align-items: flex-start; padding: var(--s3); border: 1px solid var(--line); border-radius: var(--r-md); cursor: pointer; }
  .check:hover { background: var(--ov-02); }
  .check input { margin-top: 3px; accent-color: var(--accent); flex: 0 0 auto; }
  .check-body b { font: var(--t-subhead); font-weight: 600; font-family: var(--font-mono); }
  .check-body span { display: block; font: var(--t-footnote); color: var(--text-tertiary); margin-top: 2px; }

  .chart-wrap { overflow-x: auto; margin: 0 calc(var(--s2) * -1); padding: 0 var(--s2); }
  .chart { width: 100%; height: 172px; display: block; min-width: 380px; }
  .chart-bar { fill: var(--accent); transition: opacity var(--fast); }
  .chart-zero { fill: var(--text-quaternary); opacity: .5; }
  .chart-hit { fill: transparent; }
  .chart-col:hover .chart-bar { opacity: .75; }
  .chart-col:hover .chart-hit { fill: var(--ov-04); }
  .chart-grid { stroke: var(--line-faint); stroke-width: 1; }
  .chart-base { stroke: var(--line); stroke-width: 1; }
  .chart-label { fill: var(--text-quaternary); font: var(--t-caption); font-family: var(--font); letter-spacing: 0; }
  .chart-empty { color: var(--text-tertiary); font: var(--t-footnote); text-align: center; padding: var(--s9) 0; }

  .avatar {
    width: 32px; height: 32px; border-radius: var(--r-full);
    object-fit: cover; background: var(--ov-08);
    flex: 0 0 auto;
  }
  .who { display: flex; align-items: center; gap: var(--s3); }
  .who-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
  .who-text b { font: var(--t-subhead); font-weight: 650; }
  .who-text span { font: var(--t-caption); color: var(--text-tertiary); letter-spacing: 0; }

  .scrim {
    position: fixed; inset: 0; background: var(--scrim);
    z-index: var(--z-scrim); display: grid; place-items: center;
    padding: var(--s4);
  }
  .modal {
    background: var(--overlay); border: 1px solid var(--line);
    border-radius: var(--r-xl); box-shadow: var(--shadow-overlay);
    width: 100%; max-width: 520px; max-height: 90dvh; overflow-y: auto;
    padding: var(--s6);
    z-index: var(--z-modal);
  }
  .modal h2 { margin-bottom: var(--s2); }
  .modal-sub { font: var(--t-footnote); color: var(--text-tertiary); margin-bottom: var(--s5); }
  .modal-actions { display: flex; gap: var(--s3); justify-content: flex-end; margin-top: var(--s6); }

  .secret-box {
    background: var(--surface-sunken);
    border: 1px solid var(--accent-ring);
    border-radius: var(--r-md);
    padding: var(--s4);
    font-family: var(--font-mono); font-size: .8rem;
    word-break: break-all; user-select: all;
    margin: var(--s4) 0 var(--s3);
  }
  .warn-box {
    background: var(--amber-soft);
    border: 1px solid var(--amber);
    color: var(--amber-text);
    border-radius: var(--r-md);
    padding: var(--s3) var(--s4);
    font: var(--t-footnote);
  }
  .info-box {
    background: var(--ov-04);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    padding: var(--s4);
    font: var(--t-footnote);
    color: var(--text-secondary);
  }
  .info-box b { color: var(--text); }

  .toasts {
    position: fixed; bottom: var(--s5); right: var(--s5);
    z-index: var(--z-toast);
    display: flex; flex-direction: column; gap: var(--s2);
    pointer-events: none;
  }
  .toast {
    background: var(--overlay); border: 1px solid var(--line);
    box-shadow: var(--shadow-popover);
    border-radius: var(--r-md); padding: var(--s3) var(--s4);
    font: var(--t-subhead);
    animation: toast-in var(--base) var(--ease);
    max-width: 340px;
  }
  .toast-err { border-color: var(--red); color: var(--red-text); }
  .toast-ok { border-color: var(--green); color: var(--green-text); }
  @keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

  .gate {
    min-height: 100dvh; display: grid; place-items: center; padding: var(--s6);
    text-align: center;
  }
  .gate-card { max-width: 420px; display: flex; flex-direction: column; gap: var(--s4); align-items: center; }
  .gate-card .logo-icon { width: 48px; height: 48px; border-radius: var(--r-lg); }
  .gate-card p { color: var(--text-secondary); font: var(--t-body); }

  .spinner {
    width: 22px; height: 22px; border-radius: 50%;
    border: 2px solid var(--ov-16); border-top-color: var(--accent);
    animation: spin .7s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } }

  .empty { text-align: center; padding: var(--s8) var(--s4); color: var(--text-tertiary); font: var(--t-body); }
}

@layer utilities {
  .row { display: flex; align-items: center; gap: var(--s3); }
  .row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--s4); flex-wrap: wrap; }
  .col { display: flex; flex-direction: column; gap: var(--s4); }
  .col-sm { display: flex; flex-direction: column; gap: var(--s2); }
  .muted { color: var(--text-tertiary); }
  .small { font: var(--t-footnote); }
  .grow { flex: 1 1 auto; min-width: 0; }
  .mono { font-family: var(--font-mono); }
  .nowrap { white-space: nowrap; }
  .sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
}

@layer components {
  .boot {
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: var(--s6);
    background:
      radial-gradient(60rem 40rem at 50% -10%, var(--accent-soft), transparent 70%),
      var(--bg);
  }

  .boot-inner {
    width: 100%;
    max-width: 460px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: var(--s4);
  }

  .boot-mark {
    position: relative;
    display: grid;
    place-items: center;
    width: 84px;
    height: 84px;
  }
  .boot-mark .logo-icon {
    width: 56px;
    height: 56px;
    border-radius: var(--r-lg);
    animation: boot-breathe 2.6s var(--ease-soft) infinite;
  }
  .boot-ring {
    position: absolute;
    inset: 0;
    border-radius: var(--r-full);
    border: 2px solid transparent;
    border-top-color: var(--accent);
    border-right-color: var(--accent-ring);
    animation: spin 1.1s linear infinite;
  }

  .boot-title {
    font: var(--t-title2);
    letter-spacing: var(--track-title);
  }

  .boot-status {
    font: var(--t-footnote);
    color: var(--text-tertiary);
    min-height: 1.2em;
  }

  .boot-track {
    width: 200px;
    height: 3px;
    border-radius: var(--r-full);
    background: var(--ov-08);
    overflow: hidden;
  }
  .boot-fill {
    display: block;
    height: 100%;
    width: 0;
    border-radius: var(--r-full);
    background: var(--accent);
    transition: width 0.5s var(--ease);
  }

  .boot-tip {
    margin: var(--s4) 0 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: var(--s3);
    animation: boot-rise 0.4s var(--ease) both;
  }
  .boot-code {
    margin: 0;
    text-align: left;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    padding: var(--s4);
    font-family: var(--font-mono);
    font-size: 0.75rem;
    line-height: 1.6;
    color: var(--text-secondary);
    overflow-x: auto;
    white-space: pre;
  }
  .boot-code .c-key { color: var(--accent-text); }
  .boot-code .c-str { color: var(--green-text); }
  .boot-code .c-dim { color: var(--text-quaternary); }
  .boot-tip figcaption {
    font: var(--t-footnote);
    color: var(--text-tertiary);
  }

  @keyframes boot-breathe {
    0%, 100% { transform: scale(1); opacity: .92; }
    50%      { transform: scale(1.05); opacity: 1; }
  }
  @keyframes boot-rise {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: none; }
  }

  .skel {
    position: relative;
    overflow: hidden;
    background: var(--ov-06);
    border-radius: var(--r-sm);
    color: transparent !important;
    user-select: none;
  }
  .skel::after {
    content: "";
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg, transparent, var(--ov-08), transparent);
    animation: shimmer 1.4s infinite;
  }
  .skel-text { height: 0.9em; width: 100%; }
  .skel-line { height: 12px; border-radius: var(--r-full); }
  .skel-block { height: 100%; width: 100%; border-radius: var(--r-lg); }
  .skel-circle { border-radius: var(--r-full); }

  @keyframes shimmer { to { transform: translateX(100%); } }

  @media (prefers-reduced-motion: reduce) {
    .skel::after { animation: none; }
    .boot-mark .logo-icon { animation: none; }
    .boot-ring { animation-duration: 3s; }
  }
}

@layer components {
  .facts {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: var(--s4);
  }
  .facts li {
    font: var(--t-subhead);
    color: var(--text-tertiary);
    line-height: 1.55;
    padding-left: var(--s4);
    position: relative;
  }
  .facts li::before {
    content: "";
    position: absolute;
    left: 0;
    top: .58em;
    width: 5px;
    height: 5px;
    border-radius: var(--r-full);
    background: var(--accent);
    opacity: .7;
  }
  .facts b { color: var(--text); font-weight: 600; }
}
