:root {
  --bg: #ffffff;
  --fg: #111827;
  --muted: #6b7280;
  --muted-strong: #4b5563;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --panel: #ffffff;
  --panel-soft: #f9fafb;
  --panel-hover: #f3f4f6;
  --primary: #000035;
  --primary-dark: #000018;
  --primary-soft: #eef1ff;
  --accent: #0000a5;
  --accent-light: #8aa1ff;
  --focus: #0000e0;
  --error: #b91c1c;
  --error-soft: #fef2f2;
  --success: #166534;
  --success-soft: #dcfce7;
  --warning: #854d0e;
  --warning-soft: #fef9c3;
  --ins-bg: #dcfce7;
  --del-bg: #fee2e2;
  --code-bg: #f8fafc;
  --radius: 6px;
  --maxw: 80rem;
  color-scheme: light;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
}

body {
  font-size: 15px;
  line-height: 1.5;
}

body.public-page {
  --auth-logo-glow-y: 12rem;
  background:
    radial-gradient(ellipse 1200px 260px at 50% var(--auth-logo-glow-y), rgba(0, 0, 165, 0.9), transparent 72%),
    linear-gradient(
      180deg,
      var(--primary) 0%,
      #070744 38%,
      #1a1d4f 54%,
      #4b51a0 68%,
      #a3a8d6 80%,
      #f3f4f6 91%,
      #ffffff 100%
    );
  background-attachment: fixed;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.muted {
  color: var(--muted);
}

.error {
  color: var(--error);
  font-size: 0.92rem;
}

/* htmx normally injects these as inline CSS, but CSP forbids inline styles. */
.htmx-indicator {
  opacity: 0;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  opacity: 1;
  transition: opacity 200ms ease-in;
}

.topbar {
  min-height: 3.25rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--primary);
  color: #f9fafb;
}

.public-topbar {
  position: relative;
  z-index: 1;
  border-bottom: 0;
  background: transparent;
}

.topbar-inner {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-height: 3.25rem;
  padding: 0 1.5rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
}

.brand img {
  display: block;
  width: auto;
  height: 1.7rem;
}

.brand:hover,
.topbar a:hover {
  text-decoration: none;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.topnav a {
  color: #d1d5db;
  font-size: 0.87rem;
  padding: 0.35rem 0.45rem;
}

.topnav a:hover {
  color: #ffffff;
}

.topnav.public {
  justify-content: flex-end;
}

.sidebar-toggle,
.sidebar-toggle-button,
.sidebar-backdrop {
  display: none;
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex: 0 0 16rem;
  flex-direction: column;
  align-self: stretch;
  width: 16rem;
  padding: 1.5rem;
  border-right: 1px solid rgba(255, 255, 255, 0.12);
  background: linear-gradient(135deg, #000070 0%, #000035 60%);
  color: #e0e0e0;
}

.sidebar-brand {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  margin: 0 0 1.75rem;
}

.sidebar-brand:hover {
  text-decoration: none;
}

.sidebar-brand img {
  display: block;
  width: auto;
  height: 1.85rem;
}

.sidebar h2 {
  margin: 0 0 1rem;
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.side-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.side-nav a {
  display: block;
  padding: 0.5rem 0.65rem;
  border-radius: var(--radius);
  color: #d1d5db;
  font-size: 0.92rem;
  font-weight: 500;
}

.side-nav a:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #ffffff;
  text-decoration: none;
}

.sidebar-signout {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin-top: auto;
  padding-top: 1.5rem;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.sidebar-user .user {
  overflow: hidden;
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.86rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-signout button.link,
.sidebar-signout button[type="submit"].link {
  color: #d1d5db;
  text-align: left;
}

.sidebar-signout button.link:hover,
.sidebar-signout button[type="submit"].link:hover {
  color: #ffffff;
}

.content-shell {
  flex: 1;
  min-width: 0;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse 900px 240px at 70% -12%, rgba(138, 161, 255, 0.18), transparent 72%),
    linear-gradient(180deg, #f8f9ff 0%, #ffffff 22rem);
}

.content-inner {
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  overflow-x: auto;
}

.public-shell {
  position: relative;
  isolation: isolate;
  min-height: calc(100vh - 3.25rem);
  padding: 2rem 1rem;
  background: transparent;
}

.public-shell::before,
.public-shell::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}

.public-shell::before {
  background-image:
    radial-gradient(1px 1px at 12% 18%, rgba(255, 255, 255, 0.75), transparent 60%),
    radial-gradient(1px 1px at 27% 72%, rgba(255, 255, 255, 0.55), transparent 60%),
    radial-gradient(1.5px 1.5px at 44% 33%, rgba(255, 255, 255, 0.85), transparent 60%),
    radial-gradient(1px 1px at 61% 81%, rgba(180, 200, 255, 0.6), transparent 60%),
    radial-gradient(1px 1px at 73% 22%, rgba(255, 255, 255, 0.7), transparent 60%),
    radial-gradient(2px 2px at 85% 55%, rgba(255, 255, 255, 0.9), transparent 60%);
  background-repeat: repeat;
  background-size: 620px 420px;
  opacity: 0.72;
}

.public-shell::after {
  background: linear-gradient(180deg, transparent 0, transparent 44%, rgba(255, 255, 255, 0.24) 72%, rgba(255, 255, 255, 0.82) 92%, #ffffff 100%);
}

.container {
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 0 0 1.25rem;
}

.page-head h1 {
  margin: 0;
  color: #111827;
  font-size: 1.55rem;
  font-weight: 700;
  line-height: 1.2;
}

.page-head p {
  margin: 0.35rem 0 0;
}

.actions,
.form-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.form-actions {
  justify-content: flex-end;
  margin-top: 1rem;
}

.card {
  margin-bottom: 1rem;
  padding: 1.5rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(0, 0, 53, 0.05);
}

.card.narrow {
  max-width: 28rem;
  margin: 3rem auto;
  padding: 2rem;
  border-color: rgba(255, 255, 255, 0.28);
  box-shadow: 0 24px 60px rgba(0, 0, 53, 0.28);
}

.card h1,
.card h2 {
  margin: 0 0 1rem;
  color: #111827;
  line-height: 1.2;
}

.card h1 {
  font-size: 1.55rem;
}

.card.narrow h1 {
  text-align: center;
}

.auth-brand {
  display: flex;
  justify-content: center;
  margin: -0.25rem 0 1rem;
}

.auth-brand img {
  display: block;
  width: 4rem;
  height: 4rem;
  border-radius: 14px;
  box-shadow: 0 14px 30px rgba(0, 0, 165, 0.3);
}

.card h2 {
  font-size: 1.05rem;
}

.card.narrow form + .divider,
.card.narrow .button.google + .muted {
  margin-top: 1.25rem;
}

.card.narrow > .muted:last-child {
  margin: 1.25rem 0 0;
  text-align: center;
}

.oauth-form {
  margin: 0;
}

label {
  display: block;
  margin: 0.85rem 0;
  color: var(--muted-strong);
  font-size: 0.9rem;
  font-weight: 500;
}

input,
textarea,
select {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.58rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--fg);
  font: inherit;
}

select {
  min-height: 2.25rem;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgba(0, 0, 224, 0.15);
  outline: none;
}

textarea {
  min-height: 14rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
}

button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.25rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: #ffffff;
  color: #111827;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
}

button:hover,
.button:hover {
  background: var(--panel-hover);
  text-decoration: none;
}

button[type="submit"],
.button.primary {
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
}

button[type="submit"]:hover,
.button.primary:hover {
  border-color: var(--accent);
  background: var(--accent);
}

button.link,
button[type="submit"].link {
  display: inline;
  width: auto;
  min-height: auto;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 0.86rem;
  font-weight: 500;
  cursor: pointer;
}

button.link:hover,
button[type="submit"].link:hover {
  background: transparent;
  color: var(--focus);
  text-decoration: underline;
}

.button.google {
  display: flex;
  width: 100%;
  gap: 0.65rem;
  border-color: var(--border-strong);
  background: #ffffff;
  color: var(--primary);
}

.button.google:hover {
  background: #f9fafb;
}

button[type="submit"].google,
button[type="submit"].google:hover {
  color: var(--primary);
}

.google-icon {
  width: 1.15rem;
  height: 1.15rem;
  flex: 0 0 auto;
}

.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.doc-list {
  overflow: hidden;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(0, 0, 53, 0.05);
  list-style: none;
}

.doc-list li + li {
  border-top: 1px solid var(--border);
}

.doc-list a {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 3.35rem;
  padding: 0.85rem 1rem;
  color: var(--fg);
}

.doc-list a:hover {
  background: var(--panel-soft);
  text-decoration: none;
}

.doc-list .title {
  font-weight: 600;
}

.doc-list .slug {
  flex: 0 1 auto;
  overflow: hidden;
  font-size: 0.85rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

pre.content,
pre.diff {
  overflow: auto;
  margin: 0;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--code-bg);
  color: #111827;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

pre.diff .ins {
  display: inline;
  background: var(--ins-bg);
}

pre.diff .del {
  display: inline;
  background: var(--del-bg);
  text-decoration: line-through;
}

table.users,
table.versions,
table.audit {
  width: 100%;
  overflow: hidden;
  border-collapse: separate;
  border-spacing: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  font-size: 0.9rem;
  box-shadow: 0 1px 2px rgba(0, 0, 53, 0.05);
}

.card table.users,
.card table.versions,
.card table.audit {
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

table.users th,
table.users td,
table.versions th,
table.versions td,
table.audit th,
table.audit td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

table.audit td {
  vertical-align: top;
}

table.users th,
table.versions th,
table.audit th {
  background: var(--panel-soft);
  color: var(--muted-strong);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

table.users tr:last-child td,
table.versions tr:last-child td,
table.audit tr:last-child td {
  border-bottom: 0;
}

table.users tbody tr:hover,
table.versions tbody tr:hover,
table.audit tbody tr:hover {
  background: var(--panel-soft);
}

table.audit code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.8rem;
}

table.users .row-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

table.users .row-actions form {
  margin: 0;
}

.pager {
  display: flex;
  justify-content: flex-end;
  margin-top: 1rem;
}

.banner {
  padding: 0.65rem 1.5rem;
  border-bottom: 1px solid #fde68a;
  background: var(--warning-soft);
  color: var(--warning);
  font-size: 0.9rem;
  text-align: center;
}

.banner a {
  color: #713f12;
  font-weight: 700;
}

.notice {
  margin: 0 0 1rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius);
  background: var(--success-soft);
  color: var(--success);
  font-size: 0.9rem;
}

.card.danger {
  border-color: #fecaca;
  background: #fffafa;
}

.card.danger h2 {
  color: var(--error);
}

button.destructive,
.button.destructive,
button[type="submit"].destructive {
  border-color: var(--error);
  background: var(--error);
  color: #ffffff;
}

button.destructive:hover,
.button.destructive:hover,
button[type="submit"].destructive:hover {
  border-color: #991b1b;
  background: #991b1b;
}

button.link.destructive {
  background: transparent;
  color: var(--error);
}

button.link.destructive:hover {
  background: transparent;
  color: #991b1b;
}

label.checkbox {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

label.checkbox input {
  width: auto;
  margin: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.35rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1;
}

.badge.admin {
  background: #eef1ff;
  color: var(--accent);
}

.badge.superadmin {
  background: var(--primary);
  color: #ffffff;
}

.badge.user {
  background: #e5e7eb;
  color: #374151;
}

.badge.active {
  background: var(--success-soft);
  color: var(--success);
}

.badge.pending {
  background: var(--warning-soft);
  color: var(--warning);
}

.badge.inactive {
  background: var(--error-soft);
  color: #991b1b;
}

.inline-form {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.inline-form select {
  width: auto;
  min-width: 7.75rem;
  margin: 0;
  padding: 0.28rem 0.45rem;
  font-size: 0.85rem;
}

.inline-form .link {
  color: var(--accent);
}

@media (max-width: 992px) {
  .topbar-inner {
    padding: 0 1rem;
  }

  .topnav:not(.public) {
    display: none;
  }

  .app-shell {
    display: block;
    min-height: 100vh;
  }

  .sidebar-toggle {
    position: fixed;
    top: 0.4rem;
    left: 0.4rem;
    z-index: 61;
    display: block;
    width: 2.25rem;
    height: 2.25rem;
    margin: 0;
    opacity: 0;
  }

  .sidebar-toggle-button {
    position: fixed;
    top: 0.4rem;
    left: 0.4rem;
    z-index: 60;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    margin: 0;
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-radius: var(--radius);
    background: linear-gradient(135deg, #000070 0%, #000035 60%);
    box-shadow: 0 12px 28px rgba(0, 0, 53, 0.24);
    cursor: pointer;
    transition: left 180ms ease, transform 180ms ease;
  }

  .sidebar-toggle-button span,
  .sidebar-toggle-button span::before,
  .sidebar-toggle-button span::after {
    display: block;
    width: 1.15rem;
    height: 2px;
    border-radius: 999px;
    background: #ffffff;
    transition: transform 150ms ease, background 150ms ease;
  }

  .sidebar-toggle-button span {
    position: relative;
  }

  .sidebar-toggle-button span::before,
  .sidebar-toggle-button span::after {
    content: "";
    position: absolute;
    left: 0;
  }

  .sidebar-toggle-button span::before {
    transform: translateY(-0.38rem);
  }

  .sidebar-toggle-button span::after {
    transform: translateY(0.38rem);
  }

  .sidebar-toggle:focus-visible + .sidebar-toggle-button {
    outline: 3px solid rgba(138, 161, 255, 0.75);
    outline-offset: 2px;
  }

  .sidebar-toggle:checked + .sidebar-toggle-button span {
    background: transparent;
  }

  .sidebar-toggle:checked + .sidebar-toggle-button span::before {
    transform: rotate(45deg);
  }

  .sidebar-toggle:checked + .sidebar-toggle-button span::after {
    transform: rotate(-45deg);
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: block;
    background: rgba(0, 0, 24, 0.42);
    opacity: 0;
    pointer-events: none;
    transition: opacity 150ms ease;
  }

  .sidebar-toggle:checked ~ .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 50;
    width: min(20rem, calc(100vw - 3rem));
    height: 100vh;
    min-height: 0;
    overflow-y: auto;
    border-right: 1px solid rgba(255, 255, 255, 0.12);
    border-bottom: 0;
    transform: translateX(-100%);
    transition: transform 180ms ease;
  }

  .sidebar-toggle:checked ~ .app-shell .sidebar {
    transform: translateX(0);
  }

  .sidebar-toggle:checked,
  .sidebar-toggle:checked + .sidebar-toggle-button {
    left: min(17.25rem, calc(100vw - 5.75rem));
  }

  .sidebar-signout {
    margin-top: auto;
    padding-top: 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.12);
  }

  .content-shell {
    padding: 4rem 1rem 1rem;
  }

  .banner {
    padding-left: 4rem;
    text-align: left;
  }

  .page-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .actions,
  .form-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 640px) {
  body {
    font-size: 14px;
  }

  body.public-page {
    --auth-logo-glow-y: 8.6rem;
  }

  .brand img {
    height: 1.45rem;
  }

  .sidebar {
    padding: 1rem;
  }

  .sidebar-brand {
    margin-bottom: 1rem;
  }

  .sidebar-brand img {
    height: 1.55rem;
  }

  .public-shell {
    padding: 1rem;
  }

  .card,
  .card.narrow {
    padding: 1.1rem;
  }

  .card.narrow {
    margin: 1.5rem auto;
  }

  .doc-list a {
    align-items: flex-start;
    flex-direction: column;
    gap: 0.15rem;
  }

  table.users,
  table.versions,
  table.audit {
    min-width: 42rem;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --fg: #f9fafb;
    --muted: #9ca3af;
    --muted-strong: #d1d5db;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.16);
    --panel: rgba(255, 255, 255, 0.04);
    --panel-soft: rgba(255, 255, 255, 0.03);
    --panel-hover: rgba(255, 255, 255, 0.07);
    --primary-soft: rgba(138, 161, 255, 0.12);
    --accent: #8aa1ff;
    --focus: #a0a0ff;
    --code-bg: #0f172a;
    color-scheme: dark;
  }

  body.public-page {
    background:
      radial-gradient(ellipse 1200px 260px at 50% var(--auth-logo-glow-y), rgba(0, 0, 165, 0.85), transparent 72%),
      linear-gradient(180deg, var(--primary) 0%, #000018 100%);
  }

  .topbar {
    border-bottom-color: rgba(255, 255, 255, 0.12);
    background: var(--primary);
  }

  .public-topbar {
    border-bottom: 0;
    background: transparent;
  }

  .content-shell {
    background:
      radial-gradient(ellipse 900px 240px at 70% -12%, rgba(0, 0, 165, 0.22), transparent 72%),
      #000000;
  }

  .public-shell::after {
    background: linear-gradient(180deg, transparent 0, transparent 28rem, rgba(0, 0, 0, 0.78) 38rem, #000000 100%);
  }

  .page-head h1,
  .card h1,
  .card h2,
  pre.content,
  pre.diff {
    color: var(--fg);
  }

  input,
  textarea,
  select,
  button,
  .button,
  .button.google,
  .card,
  .doc-list,
  table.users,
  table.versions,
  table.audit {
    background: var(--panel);
    color: var(--fg);
  }

  button:hover,
  .button:hover,
  .button.google:hover,
  .doc-list a:hover,
  table.users tbody tr:hover,
  table.versions tbody tr:hover,
  table.audit tbody tr:hover {
    background: var(--panel-hover);
  }

  button[type="submit"],
  .button.primary {
    border-color: #ffffff;
    background: #ffffff;
    color: var(--primary);
  }

  button[type="submit"]:hover,
  .button.primary:hover {
    border-color: var(--accent-light);
    background: var(--accent-light);
    color: var(--primary);
  }

  button[type="submit"].google {
    border-color: var(--border-strong);
    background: #ffffff;
    color: var(--primary);
  }

  button[type="submit"].google:hover {
    border-color: var(--accent-light);
    background: var(--accent-light);
    color: var(--primary);
  }

  button[type="submit"].link,
  button[type="submit"].link:hover {
    border: 0;
    background: transparent;
    color: var(--accent);
  }

  .doc-list a {
    color: var(--fg);
  }

  .card.danger {
    background: #2a1518;
  }

  .badge.user {
    background: #374151;
    color: #e5e7eb;
  }
}
