:root {
  color: #111318;
  background: #f5f6f8;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  font-kerning: normal;
  --blue: #002fa7;
  --blue-hover: #00237d;
  --blue-soft: #e8edfb;
  --ink: #111318;
  --muted: #565b65;
  --line: #d8dbe2;
  --line-strong: #aeb4c0;
  --surface: #fff;
  --surface-muted: #f5f6f8;
  --warning: #8b3517;
  --warning-soft: #fff0e8;
  --success: #1e6a46;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;
  --space-2xl: 32px;
  --ease-out: cubic-bezier(.22, 1, .36, 1);
}

* { box-sizing: border-box; }
html { background: var(--surface-muted); }
body { margin: 0; min-width: 320px; min-height: 100vh; color: var(--ink); }
button, input, select, textarea { color: inherit; font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 3px solid #3669e8;
  outline-offset: 2px;
}
[hidden] { display: none !important; }

.skip-link {
  position: fixed;
  z-index: 50;
  top: var(--space-md);
  left: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  color: #fff;
  background: var(--blue);
  transform: translateY(-160%);
}
.skip-link:focus { transform: translateY(0); }

.admin-layout {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  min-height: 100vh;
}
.admin-rail {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  border-right: 1px solid var(--line);
  background: var(--surface);
}
.brand {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  min-height: 96px;
  padding: var(--space-lg);
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}
.brand span {
  display: grid;
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  place-items: center;
  color: #fff;
  background: var(--blue);
  font-size: 1.125rem;
  font-weight: 700;
}
.brand strong { font-size: .9375rem; line-height: 1.4; }
.admin-rail nav { display: grid; overflow-y: auto; }
.admin-rail nav button {
  display: flex;
  align-items: center;
  min-height: 52px;
  padding: 0 var(--space-lg);
  border: 0;
  border-bottom: 1px solid var(--line);
  color: #30333a;
  background: var(--surface);
  font-size: .9375rem;
  text-align: left;
}
.admin-rail nav button:hover { background: var(--blue-soft); }
.admin-rail nav button.active { color: #fff; background: var(--blue); }
.rail-link {
  margin-top: auto;
  padding: var(--space-lg);
  border-top: 1px solid var(--line);
  color: var(--blue);
  font-size: .9375rem;
}

.admin-main {
  min-width: 0;
  padding: 0 clamp(20px, 3vw, 48px) 48px;
}
.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  min-height: 128px;
  border-bottom: 1px solid var(--ink);
  gap: var(--space-xl);
}
.page-header p, .page-header h1 { margin: 0; }
.page-header p { margin-bottom: var(--space-xs); color: var(--muted); font-size: .8125rem; }
.page-header h1 {
  margin-bottom: var(--space-xl);
  font-size: 2.25rem;
  font-weight: 650;
  letter-spacing: -.03em;
  line-height: 1.08;
  text-wrap: balance;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding-bottom: var(--space-xl);
}
#current-account { margin-right: var(--space-xs); font-size: .875rem; }

button.primary, button.quiet, button.column-action, button.text-action {
  min-height: 44px;
  padding: 0 var(--space-lg);
  border-radius: 0;
  font-weight: 600;
  transition: color 150ms, background-color 150ms, border-color 150ms;
}
button.primary { border: 1px solid var(--blue); color: #fff; background: var(--blue); }
button.primary:hover { border-color: var(--blue-hover); background: var(--blue-hover); }
button.quiet { border: 1px solid var(--blue); color: var(--blue); background: var(--surface); }
button.quiet:hover { background: var(--blue-soft); }
button.column-action, button.text-action {
  min-height: 36px;
  padding: 0;
  border: 0;
  color: var(--blue);
  background: transparent;
  font-size: .8125rem;
}
button.column-action:hover, button.text-action:hover { color: var(--blue-hover); text-decoration: underline; }

.hierarchy-workspace { padding-top: var(--space-xl); }
.hierarchy-breadcrumbs { display: none; }
.hierarchy-grid {
  display: grid;
  grid-template-columns: minmax(250px, .85fr) minmax(250px, .8fr) minmax(360px, 1.25fr);
  min-height: 560px;
  height: calc(100dvh - 176px);
  max-height: 820px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
}
.hierarchy-column {
  display: flex;
  min-width: 0;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--line);
}
.hierarchy-column:last-child { border-right: 0; }
.column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 72px;
  padding: 0 var(--space-lg);
  border-bottom: 1px solid var(--line);
  gap: var(--space-md);
}
.column-header > div { display: flex; align-items: baseline; gap: var(--space-sm); min-width: 0; }
.column-header h2 { margin: 0; font-size: 1.125rem; line-height: 1.2; }
.column-header span { color: var(--muted); font-size: .75rem; font-variant-numeric: tabular-nums; }
.column-context {
  min-height: 41px;
  margin: 0;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  color: var(--muted);
  font-size: .75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entity-list, .member-list {
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-gutter: stable;
}
.entity-item, .member-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: stretch;
  min-height: 72px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.entity-item.is-selected { color: #fff; background: var(--blue); }
.entity-select, .member-content {
  display: grid;
  min-width: 0;
  align-content: center;
  gap: var(--space-xs);
  padding: var(--space-md) var(--space-lg);
  border: 0;
  color: inherit;
  background: transparent;
  text-align: left;
}
.entity-select:hover { background: var(--blue-soft); }
.is-selected .entity-select:hover { background: rgba(255, 255, 255, .08); }
.entity-title, .member-name {
  overflow: hidden;
  font-size: .9375rem;
  font-weight: 650;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.entity-meta, .member-meta {
  overflow: hidden;
  color: var(--muted);
  font-size: .75rem;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.is-selected .entity-meta { color: rgba(255, 255, 255, .8); }
.entity-edit, .member-edit {
  align-self: stretch;
  min-width: 64px;
  padding: 0 var(--space-md);
  border: 0;
  border-left: 1px solid var(--line);
  color: var(--blue);
  background: transparent;
  font-size: .75rem;
  font-weight: 600;
}
.entity-edit:hover, .member-edit:hover { background: var(--blue-soft); }
.is-selected .entity-edit { border-color: rgba(255, 255, 255, .3); color: #fff; }
.is-selected .entity-edit:hover { background: rgba(255, 255, 255, .12); }

.member-item { min-height: 68px; }
.member-content { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
.member-copy { display: grid; min-width: 0; gap: var(--space-xs); }
.role-label {
  justify-self: end;
  padding: 3px 7px;
  color: var(--blue);
  background: var(--blue-soft);
  font-size: .6875rem;
  font-weight: 700;
  white-space: nowrap;
}
.role-label.role-unset { color: var(--warning); background: var(--warning-soft); }
.member-item.is-inactive { background: var(--surface-muted); }
.member-item.is-inactive .member-name { color: var(--muted); text-decoration: line-through; }

.column-empty {
  display: grid;
  justify-items: start;
  align-content: center;
  min-height: 180px;
  padding: var(--space-xl);
  color: var(--muted);
}
.column-empty p { max-width: 32ch; margin: 0; line-height: 1.55; }

.data-tools {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  min-height: 88px;
  border-bottom: 1px solid var(--line);
}
.data-tools label {
  display: grid;
  gap: var(--space-sm);
  padding-bottom: var(--space-lg);
  color: var(--muted);
  font-size: .75rem;
}
.data-tools input {
  width: min(360px, 60vw);
  min-height: 36px;
  padding: var(--space-sm) 0;
  border: 0;
  border-bottom: 1px solid var(--ink);
  outline: 0;
  background: transparent;
}
.data-tools span { padding-bottom: var(--space-xl); font-size: .875rem; font-variant-numeric: tabular-nums; }
.table-frame { overflow: auto; background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td {
  min-width: 130px;
  padding: var(--space-lg) var(--space-md);
  border-bottom: 1px solid var(--line);
  font-size: .875rem;
  text-align: left;
  vertical-align: top;
}
th { color: var(--muted); font-size: .75rem; font-weight: 600; }
td:first-child, th:first-child { padding-left: 0; }
.status { color: var(--blue); }
.empty { padding: 48px 0; color: var(--muted); }

dialog { color: var(--ink); background: var(--surface); }
dialog::backdrop { background: rgba(17, 19, 24, .36); }
.drawer {
  width: min(500px, 100vw);
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0 0 0 auto;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--line-strong);
  animation: drawer-in 240ms var(--ease-out);
}
.drawer form { display: flex; min-height: 100%; flex-direction: column; }
.drawer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  min-height: 112px;
  padding: var(--space-xl);
  border-bottom: 1px solid var(--ink);
  gap: var(--space-lg);
}
.drawer-header p { margin: 0 0 var(--space-sm); color: var(--blue); font-size: .8125rem; }
.drawer-header h2 { margin: 0; font-size: 1.75rem; line-height: 1.15; letter-spacing: -.025em; }
.drawer-header-actions { display: flex; align-items: center; gap: var(--space-sm); }
.drawer-header-actions .quiet { min-height: 44px; white-space: nowrap; }
.refresh-status {
  margin: var(--space-lg) var(--space-xl) 0;
  color: var(--muted);
  font-size: .8125rem;
  line-height: 1.5;
}
.refresh-status.is-error { color: var(--warning); }
.icon-button {
  display: grid;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid var(--line-strong);
  color: var(--ink);
  background: var(--surface);
  font-size: 1.5rem;
  line-height: 1;
}
.icon-button:hover { color: var(--blue); background: var(--blue-soft); }
.form-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-content: start;
  gap: var(--space-lg);
  padding: var(--space-xl);
}
.form-fields label {
  display: grid;
  gap: var(--space-sm);
  color: var(--muted);
  font-size: .8125rem;
  line-height: 1.4;
}
.form-fields label.wide { grid-column: 1 / -1; }
.form-fields input, .form-fields select, .form-fields textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px;
  border: 1px solid var(--line-strong);
  border-radius: 0;
  background: var(--surface);
}
.form-fields textarea { min-height: 112px; resize: vertical; }
.form-fields label.checkbox-field { display: flex; align-items: center; min-height: 44px; gap: var(--space-sm); }
.form-fields label.checkbox-field input { width: 20px; min-height: 20px; margin: 0; }
.field-help { margin: 0; color: var(--muted); font-size: .75rem; line-height: 1.5; }
.error {
  margin: 0 var(--space-xl) var(--space-lg);
  padding: var(--space-md);
  color: var(--warning);
  background: var(--warning-soft);
  font-size: .875rem;
  font-weight: 600;
  line-height: 1.5;
}
.drawer footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-sm);
  margin-top: auto;
  padding: var(--space-lg) var(--space-xl) max(var(--space-lg), env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.credential-dialog {
  width: min(680px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--ink);
}
.credential-panel { padding: 28px; }
.credential-panel header { display: grid; grid-template-columns: 100px 1fr; align-items: end; border-bottom: 1px solid var(--ink); }
.credential-panel header p, .credential-panel header h2 { margin: 0 0 var(--space-lg); }
.credential-panel header p { color: var(--blue); }
.credential-panel header h2 { font-size: 1.75rem; }
.credential-panel dl { display: grid; grid-template-columns: 160px 1fr; margin: var(--space-xl) 0; border-top: 1px solid var(--line); }
.credential-panel dt, .credential-panel dd { margin: 0; padding: var(--space-md) 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.credential-panel dt { color: var(--muted); }

.toast {
  position: fixed;
  z-index: 40;
  right: var(--space-xl);
  bottom: var(--space-xl);
  max-width: min(420px, calc(100vw - 32px));
  padding: var(--space-md) var(--space-lg);
  color: #fff;
  background: var(--success);
  font-size: .875rem;
  animation: toast-in 180ms var(--ease-out);
}
.toast.is-error { background: var(--warning); }

@keyframes drawer-in {
  from { opacity: 0; transform: translateX(36px); }
  to { opacity: 1; transform: translateX(0); }
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-page { background: var(--surface); }
.login-grid { display: grid; grid-template-columns: 1.25fr .75fr; min-height: 100vh; }
.login-intro { display: flex; flex-direction: column; justify-content: space-between; padding: clamp(28px, 6vw, 80px); color: #fff; background: var(--blue); }
.login-intro .folio { margin: 0; font-size: .8125rem; }
.login-intro h1 { margin: auto 0; font-size: clamp(3.875rem, 10vw, 9.375rem); letter-spacing: -.04em; line-height: .8; }
.login-intro > p:last-child { max-width: 420px; font-size: 1.125rem; }
.login-form { align-self: center; display: grid; gap: var(--space-xl); padding: clamp(28px, 6vw, 80px); }
.login-form h2 { margin: 0 0 28px; font-size: 2.5rem; }
.login-form label { display: grid; gap: var(--space-sm); color: var(--muted); }
.login-form input { width: 100%; min-height: 44px; border: 0; border-bottom: 1px solid var(--ink); outline: 0; }

@media (max-width: 1080px) {
  .admin-layout { grid-template-columns: 210px minmax(0, 1fr); }
  .hierarchy-grid { grid-template-columns: minmax(220px, .85fr) minmax(220px, .8fr) minmax(320px, 1.15fr); }
}

@media (max-width: 900px) {
  .admin-layout { display: block; }
  .admin-rail {
    position: sticky;
    z-index: 20;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    width: 100%;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .brand { min-height: 64px; padding: var(--space-sm) var(--space-md); border-bottom: 0; }
  .brand span { width: 38px; height: 38px; flex-basis: 38px; }
  .brand strong { display: none; }
  .admin-rail nav { display: flex; overflow-x: auto; }
  .admin-rail nav button { flex: 0 0 auto; min-height: 64px; border-bottom: 0; border-left: 1px solid var(--line); padding: 0 var(--space-md); }
  .rail-link { display: grid; align-items: center; margin: 0; padding: var(--space-md); border-top: 0; border-left: 1px solid var(--line); white-space: nowrap; }
  .admin-main { padding: 0 var(--space-lg) var(--space-xl); }
  .page-header { min-height: 112px; }
  .page-header p { display: none; }
  .page-header h1 { margin-bottom: var(--space-lg); font-size: 1.75rem; }
  .header-actions { padding-bottom: var(--space-lg); }
  #current-account { display: none; }
  .hierarchy-breadcrumbs {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    min-height: 52px;
    overflow-x: auto;
  }
  .hierarchy-breadcrumbs button {
    min-height: 44px;
    padding: 0 var(--space-sm);
    border: 0;
    color: var(--muted);
    background: transparent;
    white-space: nowrap;
  }
  .hierarchy-breadcrumbs button.is-current { color: var(--blue); font-weight: 700; }
  .hierarchy-grid { display: block; height: calc(100dvh - 244px); min-height: 440px; }
  .hierarchy-column { display: none; height: 100%; border-right: 0; }
  .hierarchy-column.mobile-current { display: flex; }
}

@media (max-width: 620px) {
  .admin-rail { grid-template-columns: auto minmax(0, 1fr); }
  .rail-link { display: none; }
  .page-header { align-items: center; min-height: 96px; }
  .page-header h1 { margin: 0; font-size: 1.5rem; }
  .header-actions { padding: 0; }
  #logout-button { display: none; }
  #create-button { min-height: 40px; padding: 0 var(--space-md); }
  .hierarchy-workspace { padding-top: 0; }
  .drawer { width: 100vw; }
  .form-fields { grid-template-columns: 1fr; }
  .form-fields label.wide { grid-column: auto; }
  .drawer footer button { flex: 1 1 auto; }
  .data-tools input { width: min(240px, 60vw); }
  .login-grid { grid-template-columns: 1fr; }
  .login-intro { min-height: 40vh; }
  .login-intro h1 { font-size: 18vw; }
}

@media (pointer: coarse) {
  .entity-item, .member-item { min-height: 76px; }
  .entity-edit, .member-edit { min-width: 72px; }
}

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