:root {
  color-scheme: light dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-2: #eef3f8;
  --text: #152033;
  --muted: #667085;
  --line: #d9e1ea;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --success: #157347;
  --warning: #9a6700;
  --danger: #b42318;
  --sidebar: #0b1220;
  --sidebar-text: #dbe7f5;
  --shadow: 0 12px 30px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
button, input, textarea { font: inherit; }
a { color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid #60a5fa; outline-offset: 2px; }

.landing {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
  background:
    radial-gradient(circle at 20% 10%, rgba(37,99,235,.16), transparent 30rem),
    radial-gradient(circle at 90% 85%, rgba(14,165,233,.13), transparent 28rem),
    #07101e;
}
.landing-card { width: min(100%, 34rem); padding: 3rem; border: 1px solid rgba(255,255,255,.12); border-radius: 1.4rem; background: rgba(11,18,32,.92); color: #f8fbff; box-shadow: 0 30px 80px rgba(0,0,0,.32); }
.brand-mark { width: 3.5rem; height: 3.5rem; display: grid; place-items: center; border-radius: .9rem; background: linear-gradient(145deg, #3b82f6, #0ea5e9); color: white; font-size: 1.5rem; font-weight: 800; }
.brand-mark.small { width: 2.2rem; height: 2.2rem; border-radius: .6rem; font-size: 1rem; flex: 0 0 auto; }
.eyebrow { margin: 1.5rem 0 .4rem; color: #8fb9ff; text-transform: uppercase; letter-spacing: .13em; font-size: .73rem; font-weight: 800; }
.landing h1 { margin: 0; font-size: clamp(2.2rem, 7vw, 3.6rem); letter-spacing: -.04em; }
.landing-copy { color: #c4d0df; line-height: 1.65; margin: 1rem 0 2rem; }
.security-note { color: #93a4b9; font-size: .85rem; line-height: 1.5; margin-top: 1.2rem; }
.inline-error { margin-top: 1rem; padding: .8rem; border-radius: .6rem; background: rgba(180,35,24,.2); color: #fecaca; }

.primary-action, .secondary-action, .danger-action, .link-button {
  border: 0; border-radius: .65rem; cursor: pointer; font-weight: 700; text-decoration: none;
}
.primary-action { display: inline-flex; justify-content: center; align-items: center; padding: .78rem 1.05rem; color: white; background: var(--accent); }
.primary-action:hover:not(:disabled) { background: var(--accent-strong); }
.secondary-action { padding: .62rem .85rem; background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.danger-action { padding: .7rem .9rem; background: var(--danger); color: #fff; }
.link-button { padding: 0; background: transparent; color: var(--accent); text-align: left; }
button:disabled { opacity: .46; cursor: not-allowed; }

.portal { min-height: 100vh; display: grid; grid-template-columns: 16.5rem minmax(0,1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 1.25rem 1rem; display: flex; flex-direction: column; background: var(--sidebar); color: var(--sidebar-text); }
.brand { display: flex; gap: .75rem; align-items: center; padding: .45rem .55rem 1.3rem; text-decoration: none; }
.brand strong, .brand small { display: block; }
.brand small { color: #8da0b8; font-size: .72rem; margin-top: .12rem; }
.sidebar nav { display: grid; gap: .18rem; }
.sidebar nav a { padding: .62rem .72rem; border-radius: .55rem; color: #adbed2; text-decoration: none; font-size: .92rem; }
.sidebar nav a:hover, .sidebar nav a.active { background: rgba(255,255,255,.09); color: #fff; }
.sidebar-footer { margin-top: auto; padding: 1rem .55rem .35rem; display: grid; gap: .7rem; color: #8da0b8; font-size: .76rem; }
.sidebar-footer .secondary-action { background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.14); color: #fff; }
.workspace { min-width: 0; padding: 1.5rem clamp(1rem,3vw,2.5rem) 4rem; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.4rem; }
.topbar .eyebrow { color: var(--accent); margin: 0 0 .25rem; }
.topbar h1 { margin: 0; font-size: 1.8rem; letter-spacing: -.025em; }

.status-pill, .status-badge { display: inline-flex; align-items: center; width: fit-content; border-radius: 999px; font-size: .75rem; font-weight: 800; text-transform: uppercase; letter-spacing: .03em; }
.status-pill { padding: .5rem .72rem; background: var(--surface-2); border: 1px solid var(--line); }
.status-badge { padding: .26rem .48rem; background: var(--surface-2); }
.status-pill.active, .status-badge.active { color: #067647; background: #ecfdf3; border-color: #abefc6; }
.status-pill.off, .status-pill.prepared, .status-pill.neutral { color: #475467; background: #f2f4f7; }
.status-pill.faulted, .status-badge.faulted, .status-badge.revoked { color: #b42318; background: #fef3f2; }
.status-badge.retired { color: #9a6700; background: #fffaeb; }

.metrics-grid { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 1rem; margin-bottom: 1rem; }
.metric-card, .panel, .state-card { background: var(--surface); border: 1px solid var(--line); border-radius: .9rem; box-shadow: var(--shadow); }
.metric-card { padding: 1.05rem; display: grid; gap: .35rem; min-height: 7.5rem; }
.metric-label { color: var(--muted); font-size: .8rem; font-weight: 700; }
.metric-value { font-size: 1.65rem; letter-spacing: -.03em; }
.metric-note { color: var(--muted); line-height: 1.4; }
.panel { padding: 1.1rem; margin-bottom: 1rem; overflow: hidden; }
.section-header { margin-bottom: 1rem; }
.section-header h2 { margin: 0; font-size: 1.05rem; }
.section-header p { margin: .35rem 0 0; }
.muted { color: var(--muted); font-size: .84rem; }
.warning-panel { border-left: 4px solid #f79009; }
.warning-panel h2 { margin-top: 0; }

.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .86rem; }
th { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; text-align: left; background: var(--surface-2); }
th, td { padding: .7rem .75rem; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; }
.wrap { overflow-wrap: anywhere; }
.jwk, .compact-json { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; font: .72rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--muted); }
.jwk { min-width: 15rem; max-width: 26rem; }

.definition-grid { display: grid; grid-template-columns: minmax(9rem,.5fr) minmax(0,1.5fr); gap: .65rem 1rem; margin: 0; }
.definition-grid dt { color: var(--muted); font-size: .82rem; }
.definition-grid dd { margin: 0; overflow-wrap: anywhere; }
.detail-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: .8rem; margin-bottom: 1rem; }
.detail-panel:empty { display: none; }

.form-panel { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: .9rem; align-items: end; }
.form-panel .section-header { grid-column: 1 / -1; margin-bottom: 0; }
.form-panel.wide textarea { min-height: 7rem; }
.field { display: grid; gap: .35rem; color: var(--muted); font-size: .78rem; font-weight: 700; }
input, textarea { width: 100%; padding: .65rem .72rem; border: 1px solid var(--line); border-radius: .55rem; background: var(--surface); color: var(--text); }
textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .8rem; }
.inline-form { display: grid; grid-template-columns: minmax(12rem,1fr) auto; gap: .7rem; align-items: end; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.inline-form small { grid-column: 1 / -1; }
.filter-row { display: grid; grid-template-columns: 1fr 1fr 7rem auto; gap: .6rem; margin-bottom: 1rem; }

.gate-notice { display: grid; gap: .18rem; margin-bottom: 1rem; padding: .78rem 1rem; border-radius: .7rem; border: 1px solid var(--line); font-size: .8rem; }
.gate-notice.allowed { background: #ecfdf3; color: #067647; border-color: #abefc6; }
.gate-notice.blocked { background: #fffaeb; color: #7a4f01; border-color: #fedf89; }
.flash { position: fixed; top: 1rem; right: 1rem; z-index: 10; max-width: 28rem; padding: .8rem 1rem; border-radius: .7rem; box-shadow: var(--shadow); background: var(--surface); border: 1px solid var(--line); }
.flash.success { border-left: 4px solid var(--success); }
.flash.error { border-left: 4px solid var(--danger); }
.loading-state, .empty-state { min-height: 8rem; display: flex; gap: .75rem; align-items: center; justify-content: center; color: var(--muted); }
.empty-state { flex-direction: column; border: 1px dashed var(--line); border-radius: .7rem; }
.spinner { width: 1.2rem; height: 1.2rem; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-state { padding: 1.5rem; }

@media (max-width: 1050px) {
  .portal { grid-template-columns: 13rem minmax(0,1fr); }
  .metrics-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .sidebar { padding-inline: .7rem; }
}
@media (max-width: 760px) {
  .portal { display: block; }
  .sidebar { position: static; height: auto; }
  .sidebar nav { grid-template-columns: repeat(2,minmax(0,1fr)); }
  .sidebar-footer { margin-top: 1rem; }
  .workspace { padding-top: 1rem; }
  .metrics-grid, .form-panel, .detail-grid { grid-template-columns: 1fr; }
  .filter-row, .inline-form { grid-template-columns: 1fr; }
  .definition-grid { grid-template-columns: 1fr; gap: .25rem; }
  .definition-grid dd { margin-bottom: .6rem; }
  .topbar { align-items: flex-start; }
}
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1422;
    --surface: #111c2d;
    --surface-2: #18263a;
    --text: #edf4fb;
    --muted: #9cafc5;
    --line: #2b3b50;
    --shadow: 0 12px 30px rgba(0,0,0,.2);
  }
  .status-pill.active, .status-badge.active { color: #75e0a7; background: #053321; border-color: #085d3a; }
  .status-pill.off, .status-pill.prepared, .status-pill.neutral { color: #c7d7e7; background: #1a293d; }
  .status-pill.faulted, .status-badge.faulted, .status-badge.revoked { color: #fda29b; background: #3b1514; }
  .gate-notice.allowed { background: #053321; color: #75e0a7; border-color: #085d3a; }
  .gate-notice.blocked { background: #342a08; color: #fedf89; border-color: #6b5305; }
}
