:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-subtle: #f8fafb;
  --text: #17202a;
  --text-muted: #5d6875;
  --border: #dce2e8;
  --primary: #1769e0;
  --primary-strong: #0f52b8;
  --teal: #087f73;
  --success: #147d45;
  --warning: #986400;
  --danger: #bd2c2c;
  --focus: #8ab8ff;
  --shadow: 0 8px 24px rgba(28, 38, 50, 0.09);
  --radius: 8px;
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  letter-spacing: 0;
}
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, a { touch-action: manipulation; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.skip-link { position: fixed; left: 16px; top: -80px; z-index: 1000; background: var(--text); color: #fff; padding: 10px 16px; }
.skip-link:focus { top: 12px; }
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }

.topbar {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  background: var(--primary);
  color: #fff;
  flex: 0 0 auto;
}
.brand-title { font-weight: 700; font-size: 17px; white-space: nowrap; }
.brand-subtitle { color: var(--text-muted); font-size: 13px; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.user-chip { display: flex; align-items: center; gap: 9px; min-width: 0; margin-right: 4px; }
.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #d9e8fb;
  color: #144f91;
  font-weight: 700;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.btn {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 9px 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  transition: background-color 180ms ease, border-color 180ms ease, color 180ms ease;
}
.btn:hover { background: #eef2f5; border-color: #c9d1d9; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-strong); border-color: var(--primary-strong); }
.btn-danger { color: var(--danger); border-color: #e6bebe; }
.btn-small { min-height: 36px; padding: 6px 10px; font-size: 14px; }
.btn-icon { width: 44px; padding: 0; }
.btn svg { width: 18px; height: 18px; }

.portal-main { width: min(1180px, calc(100% - 40px)); margin: 0 auto; padding: 40px 0 64px; }
.page-heading { margin-bottom: 28px; }
.page-heading h1 { margin: 0 0 8px; font-size: 28px; line-height: 1.25; }
.page-heading p { margin: 0; color: var(--text-muted); }
.application-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}
.application-card {
  min-height: 176px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 20px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  transition: border-color 180ms ease, box-shadow 180ms ease, transform 180ms ease;
}
.application-card:hover { border-color: #9ebce2; box-shadow: var(--shadow); transform: translateY(-2px); }
.app-head { display: flex; align-items: flex-start; gap: 14px; }
.app-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #e7f0fc;
  color: var(--primary-strong);
  flex: 0 0 auto;
  overflow: hidden;
  font-weight: 700;
  font-size: 20px;
}
.app-icon img { width: 100%; height: 100%; object-fit: cover; }
.app-title { margin: 1px 0 4px; font-size: 17px; line-height: 1.35; }
.app-description { color: var(--text-muted); font-size: 14px; margin: 0; }
.app-open { display: flex; align-items: center; justify-content: space-between; color: var(--primary-strong); font-weight: 600; font-size: 14px; }
.app-open svg { width: 18px; height: 18px; }

.empty-state, .loading-state, .error-state {
  min-height: 220px;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 32px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.state-icon { width: 46px; height: 46px; margin: 0 auto 12px; color: var(--text-muted); }
.spinner { width: 30px; height: 30px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.admin-shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: calc(100dvh - 64px); }
.admin-nav { background: #1d2732; color: #e9eef3; padding: 20px 12px; }
.admin-nav h2 { font-size: 12px; color: #9fabb7; text-transform: uppercase; margin: 0 12px 12px; }
.nav-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.nav-button {
  width: 100%;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 12px;
  border: 0;
  border-radius: 6px;
  color: #dbe3ea;
  background: transparent;
  text-align: left;
}
.nav-button:hover { background: #2b3743; }
.nav-button.active { background: #e7f0fc; color: #123f73; font-weight: 700; }
.nav-button svg { width: 19px; height: 19px; }
.admin-main { min-width: 0; padding: 28px; }
.admin-view { display: none; }
.admin-view.active { display: block; }
.section-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.section-header h1 { margin: 0 0 5px; font-size: 24px; }
.section-header p { margin: 0; color: var(--text-muted); }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: 14px; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.metric-label { color: var(--text-muted); font-size: 14px; }
.metric-value { font-size: 28px; font-weight: 750; margin-top: 8px; font-variant-numeric: tabular-nums; }
.metric-accent { width: 32px; height: 4px; border-radius: 2px; margin-top: 14px; background: var(--teal); }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.search-input { min-width: 240px; }
.field { display: grid; gap: 6px; }
.field label { font-size: 14px; font-weight: 650; }
.field input, .field select, .field textarea, .search-input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 9px 11px;
}
.field textarea { min-height: 84px; resize: vertical; }
.field-help { color: var(--text-muted); font-size: 13px; }
.check-option { min-height: 48px; display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.check-option input { width: 20px; height: 20px; accent-color: var(--primary); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.field-full { grid-column: 1 / -1; }

.table-wrap { width: 100%; overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.data-table { width: 100%; border-collapse: collapse; min-width: 760px; }
.data-table th, .data-table td { padding: 13px 15px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.data-table th { background: var(--surface-subtle); color: #3e4955; font-size: 13px; font-weight: 700; }
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover { background: #f8fbfe; }
.table-actions { display: flex; gap: 6px; align-items: center; white-space: nowrap; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 650; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-active, .status-allow, .status-success { color: var(--success); }
.status-disabled, .status-deny, .status-fail { color: var(--danger); }
.status-pending { color: var(--warning); }

dialog { width: min(680px, calc(100% - 32px)); max-height: calc(100dvh - 40px); border: 0; border-radius: var(--radius); padding: 0; box-shadow: 0 24px 64px rgba(0,0,0,.24); color: var(--text); background: var(--surface); }
dialog::backdrop { background: rgba(16, 24, 32, .55); }
.dialog-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; font-size: 19px; }
.dialog-body { padding: 20px; overflow-y: auto; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 16px 20px; border-top: 1px solid var(--border); }

.toast { position: fixed; right: 20px; bottom: 20px; z-index: 100; max-width: min(420px, calc(100% - 40px)); padding: 13px 16px; border-radius: 6px; background: #1f2a35; color: #fff; box-shadow: var(--shadow); transform: translateY(16px); opacity: 0; pointer-events: none; transition: transform 180ms ease, opacity 180ms ease; }
.toast.show { transform: translateY(0); opacity: 1; }
.toast.error { background: #8f2424; }

.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 28px; background: #edf1f5; }
.login-layout { width: min(920px, 100%); display: grid; grid-template-columns: minmax(0, 1fr) 420px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); background: var(--surface); }
.login-context { padding: 48px; background: #1e2a36; color: #f5f7f9; display: flex; flex-direction: column; justify-content: space-between; min-height: 560px; }
.login-context .brand-mark { background: #fff; color: var(--primary); }
.login-context h1 { font-size: 34px; line-height: 1.25; margin: 0 0 14px; max-width: 360px; }
.login-context p { color: #bac5cf; margin: 0; max-width: 400px; }
.login-trust { display: flex; align-items: center; gap: 10px; color: #c9d2da; font-size: 14px; }
.login-panel { padding: 38px; display: flex; flex-direction: column; justify-content: center; }
.login-panel h2 { margin: 0 0 8px; font-size: 23px; }
.login-panel > p { margin: 0 0 24px; color: var(--text-muted); }
.qr-frame { width: 300px; height: 300px; max-width: 100%; margin: 0 auto 20px; display: grid; place-items: center; background: #f6f8fa; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.login-actions { display: grid; gap: 10px; }
.login-message { min-height: 24px; margin: 10px 0; color: var(--text-muted); text-align: center; font-size: 14px; }
.login-message.error { color: var(--danger); }
.dev-login { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding-top: 16px; margin-top: 8px; border-top: 1px solid var(--border); }
.denied-page { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.denied-content { max-width: 520px; text-align: center; }
.denied-content svg { width: 56px; height: 56px; color: var(--danger); }

@media (max-width: 900px) {
  .login-layout { grid-template-columns: 1fr; max-width: 520px; }
  .login-context { min-height: auto; padding: 28px; }
  .login-context h1 { font-size: 26px; margin-top: 34px; }
  .login-trust { margin-top: 34px; }
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .admin-shell { grid-template-columns: 1fr; }
  .admin-nav { position: sticky; top: 64px; z-index: 30; padding: 8px 12px; overflow-x: auto; }
  .admin-nav h2 { display: none; }
  .nav-list { display: flex; min-width: max-content; }
  .nav-button { width: auto; }
}

@media (max-width: 640px) {
  .topbar { padding: 0 14px; }
  .brand-subtitle, .user-name { display: none; }
  .portal-main { width: min(100% - 28px, 1180px); padding-top: 28px; }
  .page-heading h1 { font-size: 24px; }
  .application-grid { grid-template-columns: 1fr; }
  .admin-main { padding: 20px 14px 44px; }
  .section-header { align-items: stretch; flex-direction: column; }
  .metrics { grid-template-columns: 1fr 1fr; gap: 10px; }
  .metric { padding: 14px; }
  .metric-value { font-size: 23px; }
  .form-grid { grid-template-columns: 1fr; }
  .field-full { grid-column: auto; }
  .login-page { padding: 0; background: var(--surface); }
  .login-layout { border: 0; box-shadow: none; min-height: 100dvh; }
  .login-context { border-radius: 0; }
  .login-panel { padding: 28px 20px 40px; }
  .qr-frame { width: 280px; height: 280px; }
}

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

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111820;
    --surface: #18212b;
    --surface-subtle: #202b36;
    --text: #edf2f6;
    --text-muted: #a8b4c0;
    --border: #33404c;
    --primary: #68a7ff;
    --primary-strong: #8abaff;
    --teal: #4fc6b7;
    --success: #68ce91;
    --warning: #e0b65b;
    --danger: #ff8e8e;
    --focus: #99c4ff;
    --shadow: 0 8px 24px rgba(0,0,0,.28);
  }
  .brand-mark { background: #2f76d2; }
  .avatar { background: #274665; color: #b9d8fa; }
  .btn:hover, .data-table tbody tr:hover { background: #26313c; }
  .application-card:hover { border-color: #567aa5; }
  .app-icon { background: #263d58; color: #b9d8fa; }
  .admin-nav { background: #0c1218; }
  .nav-button.active { background: #294563; color: #eaf3ff; }
  .login-page { background: #0f151c; }
  .login-context { background: #0c1218; }
  .qr-frame { background: #fff; }
}
