:root {
  --bg: #f5f7fb;
  --panel: #fff;
  --ink: #152238;
  --muted: #697990;
  --line: #e3eaf3;
  --primary: #3267e3;
  --primary-dark: #234fb8;
  --primary-soft: #eaf0ff;
  --success: #11805a;
  --success-soft: #e7f7f0;
  --danger: #cf4853;
  --danger-soft: #feedef;
  --warning: #966113;
  --warning-soft: #fff4da;
  --shadow: 0 18px 52px rgba(25, 48, 86, .09);
  --radius: 17px;
}

* { box-sizing: border-box; }
html { color-scheme: light; }
body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0, rgba(50, 103, 227, .1), transparent 25rem),
    radial-gradient(circle at 95% 98%, rgba(44, 180, 140, .06), transparent 30rem),
    var(--bg);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .65; }
a { color: inherit; }
.hidden { display: none !important; }
.mono { font-family: "SFMono-Regular", Consolas, monospace; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  height: 66px; padding: 0 clamp(18px, 5vw, 72px);
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(255,255,255,.84); border-bottom: 1px solid rgba(224,232,242,.95);
  backdrop-filter: blur(14px);
}
.brand { display: flex; align-items: center; gap: 11px; font-weight: 800; letter-spacing: -.02em; text-decoration: none; }
.brand-mark { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 11px; color: #fff; background: linear-gradient(145deg, #4778ec, #2559d0); box-shadow: 0 8px 18px rgba(50,103,227,.25); font-size: 14px; }
.top-actions { display: flex; gap: 8px; align-items: center; }

.btn {
  min-height: 40px; padding: 0 15px; border: 1px solid var(--line); border-radius: 11px;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  background: #fff; color: var(--ink); font-weight: 680; text-decoration: none; transition: .18s ease;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); border-color: #c9d4e4; box-shadow: 0 7px 18px rgba(26,51,92,.08); }
.btn:focus-visible, .input:focus-visible, .select:focus-visible, .textarea:focus-visible { outline: 3px solid rgba(50,103,227,.18); outline-offset: 1px; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-soft { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.btn-danger { background: var(--danger-soft); color: var(--danger); border-color: #f6ccd0; }
.btn-sm { min-height: 34px; padding: 0 11px; border-radius: 9px; font-size: 13px; }
.icon-btn { width: 38px; padding: 0; }

.input, .select, .textarea {
  width: 100%; min-height: 44px; padding: 10px 13px;
  border: 1px solid #d9e2ee; border-radius: 11px; outline: none;
  background: #fff; color: var(--ink); transition: .16s ease;
}
.textarea { min-height: 82px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--primary); }
.field { margin-bottom: 16px; }
.field label { display: block; margin-bottom: 8px; font-size: 13px; font-weight: 720; }
.field-help { margin-top: 6px; color: var(--muted); font-size: 12px; line-height: 1.5; }
.input-group { display: flex; gap: 9px; }
.input-group .input { flex: 1; min-width: 0; }
.form-error { margin: -5px 0 13px; color: var(--danger); font-size: 13px; line-height: 1.5; }

.auth-page { min-height: calc(100vh - 66px); display: grid; place-items: center; padding: 34px 18px; }
.auth-card { width: min(100%, 450px); padding: 34px; border: 1px solid var(--line); border-radius: 25px; background: rgba(255,255,255,.95); box-shadow: var(--shadow); }
.auth-icon { width: 54px; height: 54px; display: grid; place-items: center; border-radius: 17px; background: var(--primary-soft); color: var(--primary); font-weight: 850; font-size: 20px; }
.auth-card h1 { margin: 21px 0 8px; font-size: 28px; letter-spacing: -.035em; }
.auth-card > p { margin: 0 0 24px; color: var(--muted); line-height: 1.7; }
.auth-note { margin-top: 17px; padding: 11px 13px; border-radius: 11px; background: #f7f9fc; color: var(--muted); font-size: 12px; line-height: 1.6; }

.page { width: min(1280px, calc(100% - 40px)); margin: 0 auto; padding: 32px 0 62px; }
.welcome { display: flex; align-items: flex-start; justify-content: space-between; gap: 22px; margin-bottom: 22px; }
.welcome h1 { margin: 0 0 7px; font-size: clamp(24px, 3.4vw, 34px); letter-spacing: -.04em; }
.welcome p { margin: 0; color: var(--muted); line-height: 1.55; }
.sync-meta { text-align: right; color: var(--muted); font-size: 12px; }
.sync-meta .btn { margin-bottom: 7px; }

.notice { margin-bottom: 18px; padding: 13px 15px; display: flex; gap: 11px; align-items: flex-start; border: 1px solid #dfe7f5; border-radius: 13px; background: #f9fbff; font-size: 13px; line-height: 1.55; }
.notice-warning { background: var(--warning-soft); border-color: #f1dcad; color: #72501e; }
.notice-danger { background: var(--danger-soft); border-color: #f4ccd0; color: #8a343b; }
.notice-success { background: var(--success-soft); border-color: #bfe7d8; color: #126548; }
.notice > .btn { margin-left: auto; flex: 0 0 auto; }

.grid { display: grid; gap: 17px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); box-shadow: 0 8px 30px rgba(26,51,92,.045); }
.card-pad { padding: 21px; }
.card-head { padding: 19px 21px; display: flex; justify-content: space-between; align-items: center; gap: 14px; border-bottom: 1px solid var(--line); }
.card-head > * { min-width: 0; }
.card-head h2, .card-head h3 { margin: 0; font-size: 17px; }
.metric-label { display: flex; justify-content: space-between; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.metric { margin: 13px 0 6px; font-size: 30px; font-weight: 790; letter-spacing: -.04em; }
.metric small { font-size: 14px; font-weight: 650; letter-spacing: 0; }
.metric-help { color: var(--muted); font-size: 12px; line-height: 1.5; }
.progress { height: 7px; margin-top: 14px; overflow: hidden; border-radius: 20px; background: #eef2f7; }
.progress > i { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #3d70e8, #4b91f1); }
.status { display: inline-flex; align-items: center; gap: 6px; padding: 5px 9px; border-radius: 99px; font-size: 12px; font-weight: 720; white-space: nowrap; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-ok { color: var(--success); background: var(--success-soft); }
.status-off { color: var(--danger); background: var(--danger-soft); }
.status-wait { color: var(--warning); background: var(--warning-soft); }

.section { margin-top: 21px; }
.section-title { margin-bottom: 12px; display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; }
.section-title h2 { margin: 0 0 4px; font-size: 19px; }
.section-title p { margin: 0; color: var(--muted); font-size: 13px; }
.credentials { padding: 3px 21px; }
.cred-row { padding: 16px 0; display: grid; grid-template-columns: 110px minmax(0,1fr) auto; gap: 13px; align-items: center; border-bottom: 1px solid var(--line); }
.cred-row:last-child { border-bottom: 0; }
.cred-label { color: var(--muted); font-size: 13px; }
.cred-value { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 13px; }
.cred-actions { display: flex; gap: 7px; }

.filters { padding: 14px 17px; display: grid; grid-template-columns: minmax(190px,1.3fr) repeat(4,minmax(130px,.65fr)); gap: 10px; border-bottom: 1px solid var(--line); }
.admin-key-filters { grid-template-columns: minmax(180px,1fr) 180px; }
.filters .input, .filters .select { min-height: 39px; padding: 8px 11px; font-size: 13px; }
.compact-field { display:grid; gap:5px; color:var(--muted); font-size:12px; }
.table-wrap { position: relative; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { padding: 14px 17px; text-align: left; border-bottom: 1px solid var(--line); font-size: 13px; }
th { color: var(--muted); background: #fbfcfe; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:hover { background: #fafcff; }
tbody tr:last-child td { border-bottom: 0; }
.model-cell, .cost { font-weight: 740; }
.empty-state { padding: 43px 20px; text-align: center; color: var(--muted); }
.empty-state strong { display: block; margin-bottom: 7px; color: var(--ink); }
.pagination { padding: 13px 17px; display: flex; justify-content: flex-end; align-items: center; gap: 9px; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.skeleton { position: relative; overflow: hidden; border-radius: 8px; background: #edf1f6; color: transparent !important; }
.skeleton::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, rgba(255,255,255,.78), transparent); animation: shine 1.2s infinite; }
@keyframes shine { 100% { transform: translateX(100%); } }

.admin-layout { min-height: calc(100vh - 66px); display: grid; grid-template-columns: 225px minmax(0,1fr); }
.sidebar { padding: 27px 15px; border-right: 1px solid var(--line); background: rgba(255,255,255,.72); }
.sidebar-label { padding: 0 11px 9px; color: var(--muted); font-size: 11px; font-weight: 760; letter-spacing: .09em; }
.nav-btn { width: 100%; margin-bottom: 4px; padding: 11px 12px; border: 0; border-radius: 10px; background: transparent; color: #4e5e75; text-align: left; font-weight: 660; }
.nav-btn.active { background: var(--primary-soft); color: var(--primary); }
.admin-main { min-width: 0; padding: 29px clamp(21px,4vw,45px) 58px; }
.admin-tab { min-width: 0; }
.admin-actions { display: flex; gap: 9px; flex-wrap: wrap; }
.mini-kpi { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.mini-kpi strong { display: block; margin-top: 7px; font-size: 25px; letter-spacing: -.03em; }
.kpi-icon { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 13px; background: var(--primary-soft); color: var(--primary); font-weight: 820; }
.switch { width: 42px; height: 24px; padding: 3px; border: 0; border-radius: 99px; background: #cbd4e0; transition: .2s; }
.switch::before { content: ""; display: block; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.15); transition: .2s; }
.switch.on { background: var(--success); }
.switch.on::before { transform: translateX(18px); }
.row-actions { display: flex; gap: 7px; align-items: center; flex-wrap: wrap; }
.ledger-key-summary { min-width: 0; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.ledger-key-summary > div { min-width: 0; }
.ledger-key-summary strong { display: block; margin: 7px 0 4px; overflow-wrap: anywhere; }
.ledger-key-summary .mono { overflow-wrap: anywhere; }
.ledger-account-meta { margin-top: 11px; display: flex; justify-content: flex-end; gap: 18px; flex-wrap: wrap; color: var(--muted); font-size: 12px; }
.ledger-account-meta strong { color: var(--ink); }
.card-head p { margin: 5px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.gate-grid { padding: 17px; display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
.gate-card { min-width: 0; padding: 16px; border: 1px solid var(--line); border-radius: 13px; background: #fbfcfe; }
.gate-code { display: block; margin-top: 14px; color: var(--ink); font-size: 12px; overflow-wrap: anywhere; }
.gate-card p { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; overflow-wrap: anywhere; }
.sync-list { list-style: none; margin: 0; padding: 3px 21px; }
.sync-list li { padding: 14px 0; display: grid; grid-template-columns: 18px minmax(0,1fr) auto; gap: 10px; align-items: start; border-bottom: 1px solid var(--line); }
.sync-list li:last-child { border-bottom: 0; }
.dot { width: 10px; height: 10px; margin-top: 4px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 4px var(--success-soft); }
.dot.error { background: var(--danger); box-shadow: 0 0 0 4px var(--danger-soft); }
.sync-list strong { display: block; margin-bottom: 4px; font-size: 13px; }
.sync-list small { color: var(--muted); }

.modal-backdrop { position: fixed; inset: 0; z-index: 100; padding: 18px; display: grid; place-items: center; background: rgba(15,27,46,.5); }
.modal { width: min(100%, 560px); max-height: 90vh; overflow: auto; border-radius: 20px; background: #fff; box-shadow: 0 26px 90px rgba(9,21,40,.26); }
.modal-head { padding: 19px 21px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 18px; }
.modal-body { padding: 21px; }
.modal-foot { padding: 15px 21px; display: flex; justify-content: flex-end; gap: 9px; border-top: 1px solid var(--line); }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.detail-item { padding: 12px 13px; border-radius: 11px; background: #f7f9fc; }
.detail-item span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 11px; }
.one-time-secret { display: flex; gap: 9px; align-items: center; }
.one-time-secret input { min-width: 0; flex: 1; background: #f7f9fc; }
.toast { position: fixed; left: 50%; bottom: 28px; z-index: 120; padding: 11px 16px; border-radius: 11px; background: #17243a; color: #fff; box-shadow: var(--shadow); font-size: 13px; opacity: 0; pointer-events: none; transform: translate(-50%,20px); transition: .24s; }
.toast.show { opacity: 1; transform: translate(-50%,0); }

@media (max-width: 900px) {
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .gate-grid { grid-template-columns: 1fr; }
  .admin-layout { grid-template-columns: 1fr; }
  .sidebar { display: flex; gap: 4px; padding: 9px 15px; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar-label { display: none; }
  .nav-btn { width: auto; margin: 0; white-space: nowrap; }
}

@media (max-width: 650px) {
  .topbar { height: 60px; padding: 0 15px; }
  .brand-name { display: none; }
  .top-actions .btn { min-height: 35px; padding: 0 10px; font-size: 12px; }
  .page { width: calc(100% - 26px); padding-top: 23px; }
  .welcome { display: block; }
  .sync-meta { margin-top: 15px; text-align: left; }
  .card-pad { padding: 18px; }
  .cred-row { grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
  .cred-label { grid-column: 1 / -1; }
  .cred-value { font-size: 12px; }
  .filters { grid-template-columns: 1fr; }
  .admin-key-filters { grid-template-columns: 1fr; }
  .section-title { align-items: flex-start; flex-direction: column; }
  .auth-card { padding: 26px 21px; }
  .admin-main { padding: 23px 13px 48px; }
  .admin-actions { width: 100%; }
  .admin-actions .btn { flex: 1; }
  .detail-grid { grid-template-columns: 1fr; }
  .one-time-secret { align-items: stretch; flex-direction: column; }
  .notice { flex-wrap: wrap; }
  .notice > .btn { margin-left: 0; }
  .ledger-key-summary { align-items: flex-start; flex-direction: column; }
  .ledger-account-meta { justify-content: flex-start; }
  .gate-grid { padding: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
