:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #242424;
  --muted: #73726e;
  --line: #e7e6e2;
  --line-strong: #d6d5d0;
  --active: #eeeeeb;
  --accent: #e43d4f;
  --accent-hover: #c92f40;
  --success: #277a52;
  --warning: #9a6512;
  --danger: #b32d3a;
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }

[hidden] { display: none !important; }

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(360px, 100%);
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.login-panel h1 { margin: 18px 0 26px; font-size: 22px; font-weight: 500; }
.login-panel label, .form-grid label, .form-stack label { display: grid; gap: 7px; }
.login-panel label + label { margin-top: 16px; }
.login-panel label span, .form-grid label span, .form-stack label span { color: var(--muted); font-size: 13px; }

.brand-mark {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  color: var(--accent);
  font-weight: 700;
  font-size: 18px;
}
.brand-mark.small { min-height: 20px; font-size: 13px; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
  padding: 9px 10px;
  outline: none;
}

input:focus, select:focus, textarea:focus { border-color: #8b8a85; box-shadow: 0 0 0 2px rgba(36, 36, 36, .08); }
textarea { resize: vertical; }

.button {
  min-height: 34px;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 7px 12px;
  background: var(--surface);
  color: var(--text);
  white-space: nowrap;
}
.button.primary { background: var(--text); color: #fff; }
.button.primary:hover { background: #000; }
.button.secondary { border-color: var(--line-strong); }
.button.secondary:hover { background: var(--active); }
.button.danger { border-color: #e6b8be; color: var(--danger); }
.button.full { width: 100%; margin-top: 20px; }
.button:disabled { cursor: not-allowed; opacity: .55; }
.button.compact { min-height: 28px; padding: 4px 8px; font-size: 12px; }

.text-button, .icon-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 4px;
}
.text-button:hover, .icon-button:hover { color: var(--text); }
.icon-button { width: 32px; height: 32px; font-size: 24px; line-height: 1; }

.form-error { min-height: 20px; margin: 12px 0 0; color: var(--danger); font-size: 13px; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 220px minmax(0, 1fr); }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  background: #f0f0ed;
  border-right: 1px solid var(--line);
}

.sidebar-brand { display: flex; align-items: center; gap: 9px; min-height: 40px; padding: 0 9px 12px; font-weight: 500; }
.main-nav { display: grid; gap: 2px; }
.nav-item {
  min-height: 36px;
  border: 0;
  border-radius: 5px;
  padding: 7px 10px;
  background: transparent;
  color: #52514e;
  text-align: left;
}
.nav-item:hover, .nav-item.active { background: #e2e2de; color: var(--text); }
.sidebar-footer { margin-top: auto; display: flex; justify-content: space-between; align-items: center; padding: 12px 9px 0; border-top: 1px solid var(--line-strong); color: var(--muted); font-size: 13px; }

.workspace { min-width: 0; padding: 26px 32px 56px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 20px; margin-bottom: 24px; }
.topbar h2 { margin: 3px 0 0; font-size: 23px; font-weight: 500; }
.eyebrow { margin: 0; color: var(--muted); font-size: 11px; }

.notice { margin-bottom: 18px; padding: 10px 12px; border: 1px solid var(--line); border-left: 3px solid var(--success); background: var(--surface); }
.notice.error { border-left-color: var(--danger); color: var(--danger); }

.view { display: none; }
.view.active-view { display: block; }

.metrics { display: grid; grid-template-columns: repeat(4, minmax(120px, 1fr)); gap: 10px; margin-bottom: 24px; }
.metric { min-height: 88px; padding: 15px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.metric-label { display: block; color: var(--muted); font-size: 12px; }
.metric-value { display: block; margin-top: 12px; font-size: 25px; font-weight: 500; }

.section-block { margin-top: 24px; }
.section-heading { display: flex; align-items: center; justify-content: space-between; min-height: 38px; }
.section-heading h3, .integration-row h3 { margin: 0; font-size: 15px; font-weight: 500; }

.bar-chart {
  min-height: 190px;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  align-items: end;
  gap: 12px;
  padding: 20px 18px 12px;
  border: 1px solid var(--line);
  background: var(--surface);
}
.bar-column { height: 150px; display: grid; grid-template-rows: 1fr auto; gap: 8px; align-items: end; text-align: center; }
.bar-track { height: 100%; display: flex; align-items: end; justify-content: center; }
.bar { width: min(34px, 70%); min-height: 2px; background: #3a7461; border-radius: 3px 3px 0 0; }
.bar-label { color: var(--muted); font-size: 11px; }
.bar-value { display: block; margin-bottom: 5px; color: var(--text); font-size: 11px; }

.toolbar { min-height: 44px; display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.toolbar .search-input { max-width: 360px; }
.result-count { margin: 0 0 10px; color: var(--muted); font-size: 12px; }

.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--line); background: var(--surface); }
table { width: 100%; border-collapse: collapse; min-width: 680px; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { background: #fafaf8; color: var(--muted); font-size: 11px; font-weight: 500; }
td { font-size: 13px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fbfbfa; }
.cell-title { display: block; font-weight: 500; }
.cell-subtitle { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }
.table-actions { display: flex; justify-content: flex-end; gap: 6px; }
.empty-cell { padding: 32px; color: var(--muted); text-align: center; }

.status-badge { display: inline-flex; align-items: center; min-height: 22px; padding: 2px 7px; border-radius: 4px; background: #ecece8; color: #55544f; font-size: 11px; }
.status-badge.active, .status-badge.success { background: #e3f1e9; color: var(--success); }
.status-badge.legacy, .status-badge.needs_review { background: #f7ead4; color: var(--warning); }
.status-badge.revoked, .status-badge.failed, .status-badge.deleted { background: #f6e3e5; color: var(--danger); }
.status-badge.released { background: #ecece8; color: var(--muted); }

.segmented { display: inline-flex; padding: 2px; margin-bottom: 12px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); }
.segment { min-height: 29px; border: 0; border-radius: 4px; padding: 4px 14px; background: transparent; color: var(--muted); }
.segment.active { background: var(--text); color: #fff; }

.integration-row { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 18px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.muted { margin: 6px 0 0; color: var(--muted); }
.integration-settings { max-width: 760px; }
.integration-actions { align-self: end; gap: 8px; }
.organization-section { max-width: 980px; }
.organization-heading { gap: 20px; margin-bottom: 10px; }
.organization-heading .search-input { max-width: 300px; }
.organization-tree { border-top: 1px solid var(--line); }
.organization-empty { padding: 32px 12px; color: var(--muted); text-align: center; }
.org-department { border-bottom: 1px solid var(--line); }
.org-department .org-department { margin-left: 22px; border-bottom: 0; border-top: 1px solid var(--line); }
.org-department summary { min-height: 44px; display: flex; align-items: center; gap: 9px; padding: 8px 10px; cursor: pointer; list-style: none; }
.org-department summary::-webkit-details-marker { display: none; }
.org-department summary::before { content: '›'; width: 14px; color: var(--muted); font-size: 18px; transform: rotate(0); transition: transform .15s ease; }
.org-department[open] > summary::before { transform: rotate(90deg); }
.org-department-name { font-weight: 500; }
.org-department-count { margin-left: auto; color: var(--muted); font-size: 12px; }
.org-employees { margin: 0 10px 8px 33px; border-left: 1px solid var(--line); }
.org-employee { min-height: 48px; display: grid; grid-template-columns: minmax(130px, 1fr) minmax(140px, 1.4fr) auto 112px; align-items: center; gap: 14px; padding: 7px 10px 7px 14px; }
.org-employee + .org-employee { border-top: 1px solid var(--line); }
.org-employee.access-denied { background: #fbf6f6; }
.org-employee-name { min-width: 0; font-weight: 500; }
.org-employee-meta { min-width: 0; overflow-wrap: anywhere; color: var(--muted); }
.org-primary { margin-left: 6px; color: var(--accent); font-size: 11px; font-weight: 400; }
.org-card-link { color: var(--text); text-decoration: none; font-size: 12px; }
.org-card-link:hover { text-decoration: underline; }
.org-access-select { width: 112px; min-height: 30px; padding: 4px 7px; font-size: 12px; }
.org-department summary > .org-access-select { margin-left: 3px; }

.modal { width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 40px); padding: 0; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); color: var(--text); }
.modal::backdrop { background: rgba(18, 18, 18, .38); }
.small-modal { width: min(460px, calc(100vw - 32px)); }
.modal form { padding: 20px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.modal-header h3 { margin: 0; font-size: 17px; font-weight: 500; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--line); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-stack { display: grid; gap: 15px; }
.check-row { display: flex !important; grid-column: 1 / -1; grid-template-columns: auto 1fr; align-items: center; }
.check-row input { width: auto; }
.form-actions { display: flex; justify-content: flex-end; }
.settings-form { max-width: 760px; padding-top: 4px; }

@media (max-width: 980px) {
  .metrics { grid-template-columns: repeat(2, minmax(120px, 1fr)); }
  .workspace { padding: 22px 22px 48px; }
}

@media (max-width: 720px) {
  .app-shell { display: block; }
  .sidebar { position: static; width: 100%; height: auto; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar-brand { padding-bottom: 8px; }
  .main-nav { display: flex; overflow-x: auto; padding-bottom: 4px; }
  .nav-item { flex: 0 0 auto; text-align: center; }
  .sidebar-footer { position: absolute; top: 14px; right: 14px; margin: 0; padding: 0; border: 0; gap: 8px; }
  .workspace { padding: 18px 14px 40px; }
  .topbar { margin-bottom: 18px; }
  .topbar h2 { font-size: 20px; }
  .metrics { grid-template-columns: 1fr 1fr; }
  .bar-chart { gap: 5px; padding-inline: 8px; }
  .toolbar { align-items: stretch; }
  .toolbar .search-input { max-width: none; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .wide, .check-row { grid-column: 1; }
  .organization-heading { align-items: stretch; flex-direction: column; }
  .organization-heading .search-input { max-width: none; }
  .org-department summary { flex-wrap: wrap; }
  .org-department-count { margin-left: 0; }
  .org-department summary > .org-access-select { margin-left: auto; }
  .org-employee { grid-template-columns: minmax(120px, 1fr) 1fr auto 112px; }
}

@media (max-width: 420px) {
  .login-panel { padding: 24px; }
  .metrics { grid-template-columns: 1fr; }
  .toolbar { flex-wrap: wrap; }
  .toolbar > * { flex: 1 1 100%; }
  .integration-row { align-items: flex-start; flex-direction: column; }
  .org-department .org-department { margin-left: 12px; }
  .org-employees { margin-left: 22px; }
  .org-employee { grid-template-columns: 1fr auto; gap: 5px 10px; }
  .org-employee-meta { grid-column: 1 / -1; grid-row: 2; }
  .org-employee .org-access-select { grid-column: 1 / -1; width: 100%; }
}
