:root {
  color: #17212b;
  background: #f6f8fa;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Noto Sans SC", sans-serif;
  font-synthesis: none;
  --bg: #f6f8fa;
  --surface: #ffffff;
  --surface-soft: #f1f5f8;
  --text: #17212b;
  --muted: #5f6b76;
  --border: #d9e0e7;
  --primary: #175cd3;
  --primary-dark: #1249ad;
  --primary-soft: #eaf2ff;
  --success: #177245;
  --success-soft: #e8f5ee;
  --warning: #9a6700;
  --warning-soft: #fff6db;
  --danger: #b42318;
  --danger-soft: #fff0ee;
  --focus: #2e90fa;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); }
body { min-width: 320px; margin: 0; background: var(--bg); color: var(--text); }
body.embedded .topbar { display: none; }
body.embedded .shell { width: min(100% - 24px, 1080px); margin: 12px auto 24px; }
button, input { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .62; }
[hidden] { display: none !important; }

.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;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 64px;
  padding: 0 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, .96);
}

.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); font-weight: 760; text-decoration: none; }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 7px; background: var(--primary); color: #fff; font-size: 10px; font-weight: 800; letter-spacing: .06em; }
.topbar-actions { display: flex; align-items: center; gap: 18px; }
.service-state { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 4px var(--success-soft); }
.service-state[data-state="warning"] .status-dot, .service-state[data-state="uncertain"] .status-dot { background: var(--warning); box-shadow: 0 0 0 4px var(--warning-soft); }
.service-state[data-state="error"] .status-dot { background: var(--danger); box-shadow: 0 0 0 4px var(--danger-soft); }
.service-state[data-state="locked"] .status-dot, .service-state[data-state="loading"] .status-dot { background: #7b8794; box-shadow: 0 0 0 4px #edf0f3; }

.shell { width: min(1120px, calc(100% - 40px)); margin: 34px auto 60px; }
h1, h2, h3, p { margin-top: 0; letter-spacing: 0; }
h1 { margin-bottom: 0; font-size: clamp(25px, 3vw, 32px); line-height: 1.2; }
h2 { margin-bottom: 0; font-size: 19px; line-height: 1.3; }
h3 { margin-bottom: 3px; font-size: 15px; line-height: 1.35; }
.eyebrow { margin-bottom: 7px; color: var(--muted); font-size: 11px; font-weight: 760; letter-spacing: .08em; text-transform: uppercase; }
.muted, .metric-note { color: var(--muted); font-size: 12px; }
.text-button, .link-button { min-height: 44px; padding: 0 4px; border: 0; background: transparent; color: var(--primary); font-size: 13px; font-weight: 700; }
.text-button:hover, .link-button:hover { color: var(--primary-dark); text-decoration: underline; }
.button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 44px; padding: 0 15px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); color: var(--text); font-size: 13px; font-weight: 720; transition: background 160ms ease, border-color 160ms ease, transform 160ms ease; }
.button:hover { border-color: #b5c4d1; background: var(--surface-soft); }
.button:active { transform: translateY(1px); }
.button.primary { border-color: var(--primary); background: var(--primary); color: #fff; }
.button.primary:hover { border-color: var(--primary-dark); background: var(--primary-dark); }
.wide-button { width: 100%; }

.login-view { min-height: calc(100vh - 170px); display: grid; place-items: center; }
.login-panel { width: min(460px, 100%); padding: 32px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); box-shadow: 0 14px 34px rgba(23, 33, 43, .08); }
.login-intro { max-width: 36ch; margin: 12px 0 0; color: var(--muted); font-size: 14px; line-height: 1.55; }
.login-panel label { display: block; margin: 24px 0 8px; color: #3d4a56; font-size: 13px; font-weight: 700; }
.login-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.login-row input { min-width: 0; height: 44px; padding: 0 12px; border: 1px solid #b8c4cf; border-radius: 7px; outline: none; background: #fff; color: var(--text); }
.login-row input:focus { border-color: var(--focus); box-shadow: 0 0 0 3px #d7eaff; }
.form-error { margin: 10px 0 0; color: var(--danger); font-size: 13px; }

.loading-view { min-height: 380px; display: grid; place-items: center; }
.loading-card, .error-card { display: flex; align-items: center; gap: 10px; padding: 17px 20px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--muted); font-size: 13px; }
.error-view { min-height: 380px; display: grid; place-items: center; }
.error-card { display: block; width: min(420px, 100%); text-align: center; }
.error-card strong { display: block; margin-bottom: 6px; color: var(--text); font-size: 16px; }
.error-card p { margin-bottom: 16px; color: var(--muted); font-size: 13px; }
.spinner { width: 16px; height: 16px; border: 2px solid #c8d3dd; border-top-color: var(--primary); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.page-intro { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 24px; }
.intro-copy { max-width: 58ch; margin: 10px 0 0; color: var(--muted); font-size: 14px; line-height: 1.55; }
.intro-meta { display: grid; justify-items: end; gap: 5px; }
.updated-at { color: var(--muted); font-size: 12px; white-space: nowrap; }
.intro-caption { color: #7a8792; font-size: 11px; }

.metrics-grid { display: grid; grid-template-columns: 1.35fr repeat(3, 1fr); gap: 12px; }
.metric-card { min-width: 0; min-height: 142px; padding: 17px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); box-shadow: 0 2px 8px rgba(23, 33, 43, .035); }
.metric-total { border-top: 3px solid var(--primary); }
.metric-remaining { border-top: 3px solid var(--success); }
.metric-label { display: flex; align-items: center; gap: 6px; margin-bottom: 17px; color: var(--muted); font-size: 12px; font-weight: 700; }
.metric-card strong { display: block; overflow-wrap: anywhere; color: var(--text); font-size: clamp(23px, 2.8vw, 30px); font-variant-numeric: tabular-nums; line-height: 1.05; }
.metric-note { display: block; margin-top: 10px; line-height: 1.4; }
.metric-note b { color: var(--text); font-variant-numeric: tabular-nums; }
.direction { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 5px; font-size: 16px; line-height: 1; }
.direction.up { background: #eaf2ff; color: var(--primary); }
.direction.down { background: var(--success-soft); color: var(--success); }
.progress { height: 8px; margin-top: 20px; overflow: hidden; border-radius: 4px; background: #e8edf1; }
.progress span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--primary); transition: width 180ms ease; }

.section-block { margin-top: 34px; }
.section-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 18px; margin-bottom: 12px; }
.section-heading > div { min-width: 0; }
.client-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.client-card { display: flex; min-height: 210px; flex-direction: column; padding: 17px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); box-shadow: 0 2px 8px rgba(23, 33, 43, .035); }
.client-card-featured { border-top: 3px solid var(--primary); }
.client-card-head { display: flex; align-items: center; gap: 11px; min-width: 0; margin-bottom: 18px; }
.client-card-head p { margin: 0; color: var(--muted); font-size: 12px; }
.client-code { display: grid; place-items: center; flex: 0 0 auto; width: 36px; height: 36px; border: 1px solid #bfd3f7; border-radius: 7px; background: var(--primary-soft); color: var(--primary); font-size: 11px; font-weight: 800; letter-spacing: .06em; }
.client-secondary-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 44px; margin-top: 8px; }
.client-hint { color: var(--muted); font-size: 12px; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; min-width: 58px; min-height: 44px; padding: 0 9px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); color: var(--primary); font-size: 12px; font-weight: 700; }
.icon-button:hover { border-color: #b5c4d1; background: var(--primary-soft); }
.node-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.node-row { display: flex; align-items: center; justify-content: space-between; min-height: 88px; gap: 14px; padding: 15px 17px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.node-row p { margin-bottom: 0; color: var(--muted); font-size: 12px; }
.node-actions { display: flex; align-items: center; gap: 9px; }
.node-badge { padding: 5px 8px; border-radius: 5px; background: var(--success-soft); color: var(--success); font-size: 11px; font-weight: 760; white-space: nowrap; }
.node-badge[data-state="uncertain"] { background: var(--warning-soft); color: var(--warning); }
.node-badge[data-state="error"] { background: var(--danger-soft); color: var(--danger); }

.streaming-unlock { padding-top: 21px; border-top: 1px solid var(--border); }
.streaming-heading { align-items: flex-end; }
.streaming-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; }
.compact-button { min-height: 44px; }
.streaming-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.streaming-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 14px; min-height: 76px; padding: 13px 15px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.streaming-copy { min-width: 0; }
.streaming-copy h3 { overflow-wrap: anywhere; }
.streaming-meta { display: flex; align-items: center; gap: 7px; min-width: 0; margin: 0; color: var(--muted); font-size: 12px; }
.streaming-state-dot { flex: 0 0 auto; width: 7px; height: 7px; border-radius: 50%; background: var(--success); }
.streaming-row[data-health="fallback"] .streaming-state-dot, .streaming-row[data-health="pending"] .streaming-state-dot, .streaming-row[data-health="attention"] .streaming-state-dot { background: var(--warning); }
.streaming-row[data-health="disabled"] .streaming-state-dot { background: #8c9790; }
.streaming-row .switch b { min-width: 26px; }
.streaming-empty { grid-column: 1 / -1; padding: 22px; border: 1px dashed #bdc9d2; border-radius: 8px; color: var(--muted); font-size: 13px; text-align: center; }

.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-top: 34px; padding: 18px 20px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.settings-row p:last-child { max-width: 66ch; margin-bottom: 0; line-height: 1.5; }
.switch { display: inline-flex; align-items: center; flex: 0 0 auto; gap: 9px; color: #40505c; font-size: 12px; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch span { position: relative; width: 44px; height: 26px; border-radius: 13px; background: #aeb9c2; transition: background 160ms ease; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .2); transition: transform 160ms ease; }
.switch input:checked + span { background: var(--success); }
.switch input:checked + span::after { transform: translateX(18px); }

.modal { width: min(390px, calc(100% - 28px)); padding: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); box-shadow: 0 24px 70px rgba(20, 35, 27, .22); }
.modal::backdrop { background: rgba(18, 29, 23, .48); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid #e2e8ed; }
.modal-head h2 { font-size: 17px; }
.modal-head .eyebrow { margin-bottom: 4px; }
.close-button { min-width: 44px; width: 44px; padding: 0; font-size: 20px; }
.modal-body { display: grid; place-items: center; min-height: 310px; padding: 22px; }
.modal-body img { width: min(260px, 100%); aspect-ratio: 1; object-fit: contain; image-rendering: pixelated; }
.toast { position: fixed; left: 50%; bottom: 24px; z-index: 20; transform: translateX(-50%); min-width: 170px; max-width: calc(100% - 32px); padding: 11px 15px; border-radius: 7px; background: #17212b; color: #fff; font-size: 13px; text-align: center; box-shadow: 0 8px 24px rgba(0, 0, 0, .18); }

button:focus-visible, input:focus-visible, .qr-preview:focus-visible { outline: 3px solid #b9dcff; outline-offset: 2px; }

@media (max-width: 900px) {
  .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .client-grid { grid-template-columns: 1fr 1fr; }
  .client-card:last-child { grid-column: 1 / -1; }
}

@media (max-width: 700px) {
  .topbar { min-height: 58px; padding: 0 15px; }
  .topbar-actions { gap: 9px; }
  .service-state { font-size: 12px; }
  .shell { width: min(100% - 24px, 560px); margin-top: 20px; }
  .page-intro { align-items: flex-start; flex-direction: column; gap: 10px; margin-bottom: 19px; }
  .intro-meta { justify-items: start; }
  .metrics-grid { gap: 9px; }
  .metric-card { min-height: 128px; padding: 14px; }
  .client-grid, .node-grid, .streaming-grid { grid-template-columns: 1fr; }
  .client-card:last-child { grid-column: auto; }
  .section-block { margin-top: 28px; }
  .section-heading { align-items: flex-start; flex-direction: column; gap: 6px; }
  .section-heading > .muted { align-self: flex-start; }
  .streaming-heading { align-items: flex-start; }
  .streaming-actions { width: 100%; flex-wrap: wrap; justify-content: flex-start; gap: 8px; }
  .streaming-actions .updated-at { flex-basis: 100%; }
  .settings-row { align-items: flex-start; padding: 16px; }
  .login-row { grid-template-columns: 1fr; }
  .login-panel { padding: 24px 19px; }
}

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