:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #0f766e;
  --accent-ink: #fff;
  --danger: #b91c1c;
  --ok: #166534;
  --font: "IBM Plex Sans", "Hiragino Sans", "Noto Sans JP", sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
}
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #d8f3ef 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #e8eef7 0%, transparent 50%),
    var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 100%;
  width: 100%;
}

#suite-bar-root {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
}
#suite-bar-root[hidden] { display: none !important; }

.app {
  flex: 1;
  min-height: 0;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

.auth-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.auth-gate[hidden] { display: none !important; }
.auth-gate-card {
  width: min(360px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 24px;
  text-align: center;
}
.brand-mark {
  font-weight: 700;
  letter-spacing: 0.18em;
  font-size: 13px;
  color: var(--accent);
}
.brand-sub {
  margin-top: 6px;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.auth-gate-status { margin: 18px 0 6px; color: var(--ink); }
.auth-gate-hint { margin: 0; color: var(--muted); font-size: 13px; }
.auth-gate-retry {
  margin-top: 16px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}

.app[hidden] { display: none !important; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand-inline { display: flex; align-items: baseline; gap: 10px; }
.brand-inline .brand-sub { font-size: 18px; margin: 0; }
.topbar-meta { display: flex; align-items: center; gap: 12px; font-size: 13px; }
.session-email { color: var(--muted); }
.app-version {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  letter-spacing: 0.02em;
  color: #94a3b8;
  user-select: all;
  white-space: nowrap;
}

.main {
  width: 100%;
  max-width: min(1280px, 100%);
  margin: 0 auto;
  padding: 24px 16px 48px;
  display: grid;
  gap: 20px;
  min-width: 0;
  box-sizing: border-box;
}
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.panel h1 {
  margin: 0 0 8px;
  font-size: 20px;
}
.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.hint { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.policy-hint { max-width: 52rem; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tab-row {
  display: inline-flex;
  gap: 4px;
  padding: 2px;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #f8fafc;
}
.tab-btn {
  appearance: none;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  cursor: pointer;
}
.tab-btn.active {
  background: #0f766e;
  color: #fff;
}
.toolbar input[type="search"] {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  min-width: 200px;
}

.btn {
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 9px 14px;
  font: inherit;
  cursor: pointer;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn.secondary { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
}
.btn-link {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.pager {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.pager-meta {
  font-size: 13px;
  color: var(--muted);
}
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
}
.matrix-table .action-col {
  text-align: center;
  min-width: 4rem;
}
.matrix-table .svc-cell.pending {
  color: #b45309;
}

.table-wrap,
.matrix-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: auto;
  max-height: calc(100vh - 260px);
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.matrix-table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}
.matrix-table th,
.matrix-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  white-space: nowrap;
}
.matrix-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 11px;
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 2;
}
.matrix-table .sticky-col {
  position: sticky;
  left: 0;
  background: var(--card);
  z-index: 1;
  min-width: 180px;
  max-width: 240px;
  box-shadow: 1px 0 0 var(--line);
}
.matrix-table thead .sticky-col { z-index: 3; }
.matrix-table .svc-col {
  text-align: center;
  min-width: 4.5rem;
  max-width: 5.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
}
.matrix-table .svc-extra {
  color: #94a3b8;
  font-weight: 500;
}
.matrix-table .svc-extra-first,
.matrix-table .svc-cell.svc-extra-first {
  border-left: 2px solid #cbd5e1;
}
.matrix-table .svc-cell {
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.matrix-table .svc-cell.on {
  color: var(--ok);
  font-weight: 700;
}
.matrix-table .svc-cell.level-admin {
  color: var(--accent);
}
.matrix-table .svc-cell.err {
  color: var(--danger);
}

/* L2 structured grants */
.matrix-table .l2-col {
  min-width: 14rem;
  max-width: 18rem;
  text-align: left;
  white-space: normal;
}
.matrix-table .l2-col-sub {
  display: block;
  margin-top: 2px;
  font-weight: 500;
  font-size: 10px;
  color: #94a3b8;
  letter-spacing: 0.02em;
}
.matrix-table .l2-cell {
  text-align: left;
  white-space: normal;
  vertical-align: top;
  min-width: 14rem;
  max-width: 18rem;
  font-weight: 400;
}
.matrix-table .l2-cell.on {
  color: var(--ink);
  font-weight: 400;
}
.l2-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.l2-grant {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  padding: 8px 9px;
}
.l2-grant.l2-grant-admin {
  border-color: #99f6e4;
  background: #f0fdfa;
}
.l2-grant.l2-grant-pending {
  border-color: #fde68a;
  background: #fffbeb;
}
.l2-grant.l2-grant-pending .l2-duty {
  color: #b45309;
}
.matrix-wrap.l2-view {
  max-height: calc(100vh - 300px);
}
.l2-grant-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 8px;
  margin-bottom: 6px;
}
.l2-duty {
  font-weight: 700;
  color: var(--accent);
  font-size: 12px;
}
.l2-label {
  font-size: 11px;
  color: var(--muted);
}
.l2-meta {
  margin: 0;
  display: grid;
  gap: 4px;
}
.l2-meta > div {
  display: grid;
  grid-template-columns: 2.4rem 1fr;
  gap: 6px;
  align-items: start;
}
.l2-meta dt {
  margin: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #94a3b8;
  padding-top: 2px;
}
.l2-meta dd {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.l2-chip {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--ink);
  line-height: 1.4;
}
.l2-empty {
  color: #cbd5e1;
  font-size: 11px;
}
.matrix-table .l2-col-unset .l2-col-sub {
  color: #f59e0b;
}
.matrix-table .l2-cell.l2-unset {
  text-align: center;
  vertical-align: middle;
  color: #92400e;
  background: #fffbeb;
  font-weight: 500;
}
.l2-unset-label {
  display: inline-block;
  font-size: 11px;
  line-height: 1.3;
  white-space: normal;
}
.email {
  font-weight: 600;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.muted { color: var(--muted); }
.small { font-size: 12px; margin-top: 2px; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: #f1f5f9;
}
.badge.role { font-family: ui-monospace, monospace; }
.badge.off { background: #fee2e2; color: var(--danger); }

.modal[hidden] { display: none !important; }
.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
}
.modal-card {
  position: relative;
  z-index: 1;
  width: min(440px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.12);
}
.modal-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
}
.edit-role-list {
  display: grid;
  gap: 8px;
  margin: 12px 0;
  max-height: 240px;
  overflow-y: auto;
}
.edit-role-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  background: #f8fafc;
}
.edit-role-item input { margin-top: 3px; }
.edit-role-item span { display: grid; gap: 2px; }
.form-error {
  min-height: 1.2em;
  margin: 0 0 8px;
  color: var(--danger);
  font-size: 13px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  background: #f1f5f9;
  padding: 1px 5px;
  border-radius: 4px;
}

@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; }
  .toolbar { width: 100%; flex-wrap: wrap; }
  .toolbar input[type="search"] { flex: 1; min-width: 0; }
}
