/* Default theme is dark, so a first paint without a cookie or before the
   head script runs does not flash light. Light is applied only when the
   html[data-theme="light"] attribute is present. */
:root,
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0a0a;
  --fg: #e5e5e5;
  --muted: #a3a3a3;
  --brand: #6b96bb;
  --heading: #e2b57a;
  --label: #8fb8b4;
  --border: #262626;
  --card: #171717;
  --input-bg: #22262a;
  --input-fg: #e8ecef;
  --input-border: #3d4349;
  --btn-bg: #7aa3c2;
  --btn-fg: #0a0a0a;
  --btn-bg-hover: #93b8d4;
  --btn-bg-active: #6593b5;
  --overlay: rgba(0, 0, 0, 0.6);
  --ok: #4ade80;
  --bad: #fca5a5;
  --jwt-header: #d9a88f;
  --jwt-payload: #91c9ad;
  --jwt-signature: #b4b4e6;
  --rail: 48px;
  --sidebar-hover: rgba(255, 255, 255, 0.05);
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #fafafa;
  --fg: #171717;
  --muted: #525252;
  --brand: #5784aa;
  --heading: #8a5c20;
  --label: #2f5d62;
  --border: #e5e5e5;
  --card: #ffffff;
  --input-bg: #eef1f3;
  --input-fg: #1b2326;
  --input-border: #c9ced3;
  --btn-bg: #3a6588;
  --btn-fg: #ffffff;
  --btn-bg-hover: #2f5674;
  --btn-bg-active: #24506e;
  --overlay: rgba(0, 0, 0, 0.45);
  --ok: #15803d;
  --bad: #b91c1c;
  --jwt-header: #8a4b2f;
  --jwt-payload: #2f6b4f;
  --jwt-signature: #45458a;
  --sidebar-hover: rgba(0, 0, 0, 0.04);
}

html[data-sidebar="open"] { --rail: 240px; }
html[data-sidebar="closed"] { --rail: 48px; }

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  padding-left: var(--rail);
  display: flex;
  flex-direction: column;
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
  text-rendering: optimizeLegibility;
  transition: padding-left 150ms ease;
}

main {
  flex: 1;
  width: min(760px, calc(100% - 32px));
  margin: 0 auto;
  padding-bottom: 1.5rem;
}

footer {
  width: min(760px, calc(100% - 32px));
  margin: 0 auto;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 1rem 0 2.5rem;
  border-top: 1px solid var(--border);
}
footer p { margin: 0; max-width: 68ch; }

a { color: var(--brand); }
a:hover { text-decoration-thickness: 2px; }

code, .mono { font-family: ui-monospace, monospace; }

/* Top bar: title | spacer | theme icon. The phone toggle sits at the left. */
.top {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  height: 48px;
  padding: 0 max(16px, calc((100% - 760px) / 2));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.top-spacer { flex: 1; }
.top-title {
  color: var(--fg);
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-toggle, .theme-toggle {
  margin: 0;
  flex: none;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  display: grid;
  place-items: center;
  background: transparent;
  color: var(--fg);
  border: 0;
  border-radius: 6px;
  cursor: pointer;
}
.sidebar-toggle:hover, .theme-toggle:hover { background: var(--sidebar-hover); }
.top-toggle { display: none; }

.theme-icon-sun { display: none; }
html[data-theme="dark"] .theme-icon-sun { display: block; }
html[data-theme="dark"] .theme-icon-moon { display: none; }

/* Docked rail: full viewport height on the left, never an overlay. */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 50;
  width: var(--rail);
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width 150ms ease;
}
.sidebar-header {
  flex: none;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.75rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  color: var(--fg);
  font-size: 0.875rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.sidebar-brand .logo-mark { display: block; flex: none; }
.sidebar-header .sidebar-toggle { margin-left: auto; }
/* The header control swaps shape with the rail: the logo opens a closed rail
   and the panel icon closes an open one. */
.sidebar-icon-closed,
.sidebar-icon-open { display: grid; place-items: center; }
html[data-sidebar="closed"] .sidebar-icon-open,
html:not([data-sidebar]) .sidebar-icon-open { display: none; }
html[data-sidebar="open"] .sidebar-icon-closed { display: none; }
html[data-sidebar="closed"] .sidebar-brand,
html:not([data-sidebar]) .sidebar-brand { display: none; }
html[data-sidebar="closed"] .sidebar-header,
html:not([data-sidebar]) .sidebar-header { justify-content: center; padding: 0; }
html[data-sidebar="closed"] .sidebar-header .sidebar-toggle,
html:not([data-sidebar]) .sidebar-header .sidebar-toggle { margin-left: 0; }

.sidebar-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 0.9rem 0.75rem;
  overflow-y: auto;
}
.sidebar-panel[hidden] { display: none; }

/* Group headings sit quietly between lists in the rail. */
.sidebar-group + .sidebar-group { margin-top: 0.85rem; }
.sidebar-group .sidebar-heading:first-child { margin-top: 0; }

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--overlay);
}
.sidebar-backdrop[hidden] { display: none; }

.sidebar-heading {
  margin: 0.2rem 0 0.35rem;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--heading);
}
.sidebar-tools {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sidebar-tools a {
  display: block;
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  color: var(--fg);
  font-size: 0.8rem;
  text-decoration: none;
}
.sidebar-tools a:hover { background: var(--sidebar-hover); }
.sidebar-tools a[aria-current="page"] {
  background: rgba(0, 0, 0, 0.06);
  border-radius: 6px;
  color: var(--fg);
  font-weight: 500;
}
html[data-theme="dark"] .sidebar-tools a[aria-current="page"] {
  background: rgba(255, 255, 255, 0.08);
}
.sidebar-tools a[aria-current="page"]:hover { background: rgba(0, 0, 0, 0.09); }
html[data-theme="dark"] .sidebar-tools a[aria-current="page"]:hover { background: rgba(255, 255, 255, 0.12); }

.sidebar-help {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border);
}
.sidebar-help a,
.sidebar-bottom a {
  display: block;
  padding: 0.35rem 0.55rem;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.8rem;
  text-decoration: none;
}
.sidebar-help a:hover,
.sidebar-bottom a:hover { color: var(--fg); background: var(--sidebar-hover); }

.sidebar-bottom {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border);
}

h1 {
  font-size: clamp(1.45rem, 4vw, 1.75rem);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin: 1.5rem 0 0.5rem;
}
h2 {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--heading);
  margin: 1.4rem 0 0.5rem;
}
.lede { color: var(--muted); max-width: 68ch; margin-top: 0; }

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.1rem;
  margin: 1rem 0 1.5rem;
}

label { display: block; font-size: 0.8rem; margin: 0.6rem 0 0.25rem; color: var(--label); }
input, select, textarea {
  width: 100%;
  font: inherit;
  font-size: 0.8rem;
  color: var(--input-fg);
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  background: var(--input-bg);
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
textarea { min-height: 5.5rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }

.regex-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 0.9rem;
  margin: 0.45rem 0 0;
}
.regex-flags label { margin: 0; display: flex; align-items: center; gap: 0.25rem; }
.field-row,
.qs-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.4rem;
}
.field-row button,
.qs-row button { margin-top: 0; }
.qr-preview { margin-top: 0.8rem; }
.qr-preview svg {
  display: block;
  width: min(100%, 20rem);
  height: auto;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem;
}
.img-preview { margin-top: 0.8rem; }
.img-preview img {
  display: block;
  max-width: min(100%, 24rem);
  height: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.diff-table td { vertical-align: top; }
.diff-table .delete { color: var(--bad); }
.diff-table .insert { color: var(--ok); }
.diff-gutter { display: inline-block; width: 1ch; color: var(--muted); }
.md-preview {
  margin-top: 0.35rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
}
.md-preview[hidden] { display: none; }
.md-preview h1,
.md-preview h2,
.md-preview h3 { margin-top: 0.6rem; color: var(--fg); text-transform: none; letter-spacing: 0; }
.md-preview p { margin: 0.4rem 0; }
.md-preview pre {
  overflow: auto;
  padding: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
}

button, .btn {
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  text-decoration: none;
}
/* Push buttons in the page (Check, Load example, and so on) are filled at
   rest. Header and sidebar icon buttons keep the transparent rule above. */
main button, main .btn {
  color: var(--btn-fg);
  background: var(--btn-bg);
  border-color: var(--btn-bg);
}
main button:hover, main .btn:hover {
  background: var(--btn-bg-hover);
  border-color: var(--btn-bg-hover);
}
main button:active, main .btn:active {
  background: var(--btn-bg-active);
  border-color: var(--btn-bg-active);
}
main button:focus-visible, main .btn:focus-visible {
  outline: 2px solid var(--btn-fg);
  outline-offset: 2px;
}
.panel button, .panel .btn { margin-top: 0.75rem; }
.btn { display: inline-block; }

.out { margin-top: 0.9rem; overflow-x: auto; }
.out table { width: 100%; border-collapse: collapse; }
.out td, .out th {
  text-align: left;
  padding: 0.35rem 0.25rem;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.ok { color: var(--ok); }
.bad { color: var(--bad); }

/* Contrast tool: each colour is a label, a square picker and a hex field. */
.contrast-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.5rem 0;
}
.contrast-row label { flex: 0 0 7rem; margin: 0; }
.contrast-row input[type="color"] {
  flex: none;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  cursor: pointer;
}
.contrast-row input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.contrast-row input[type="color"]::-webkit-color-swatch { border: none; border-radius: 5px; }
.contrast-row input[type="color"]::-moz-color-swatch { border: none; border-radius: 5px; }
.contrast-row input[type="text"] { flex: 1; min-width: 0; width: auto; }
.field-error { color: var(--bad); font-size: 0.8rem; margin: 0.15rem 0 0; }
.field-error[hidden] { display: none; }
.preview {
  margin-top: 0.9rem;
  min-height: 2.5rem;
  padding: 0.9rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
}
.ratio { margin: 0.5rem 0 0; color: var(--muted); font-size: 0.8rem; }

/* Index: a compact grid in named sections. The card carries the title only;
   a local preview may add a small muted host hint. */
.page-index h1 { margin-top: 1.1rem; }
.page-index .note { margin-bottom: 0; }
.index-group { margin-top: 1.25rem; }
.index-group h2 { margin-top: 0; margin-bottom: 0.45rem; }
.cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
}
.cards a {
  display: block;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.65rem;
  color: var(--fg);
  text-decoration: none;
}
.cards a:hover { border-color: var(--muted); }
.cards a strong { display: block; font-weight: 600; font-size: 0.82rem; line-height: 1.25; }
.cards .card-hint { display: block; color: var(--muted); font-size: 0.7rem; margin-top: 0.15rem; }

@media (max-width: 900px) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .cards { grid-template-columns: minmax(0, 1fr); }
}

.prose { margin: 0 0 1.5rem; }
.prose p { color: var(--muted); max-width: 68ch; margin: 0 0 0.65rem; }
.prose ul { margin: 0.4rem 0 0.8rem; padding-left: 1.25rem; color: var(--muted); }
.prose li { margin: 0.15rem 0; }
.legal p { max-width: 68ch; }
.help-list { margin: 0.5rem 0 1rem; padding-left: 1.25rem; color: var(--muted); }
.help-list li { margin: 0.4rem 0; max-width: 68ch; }
.help-list strong { color: var(--fg); font-weight: 600; }
.note { color: var(--muted); font-size: 0.8rem; margin-top: 0.25rem; }

.ad-slot { display: none; min-height: 0; }

/* JWT tools: two plain columns on a wide screen, stacked on a phone. The three
   token parts keep calm colours in both themes. */
.jwt-layout,
.objwt-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 1.25rem;
}
.jwt-col,
.objwt-col { min-width: 0; }
.jwt-col h2:first-child,
.objwt-col h2:first-child { margin-top: 0; }
.jwt-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.jwt-actions button { margin-top: 0.5rem; }
.token-input { min-height: 7rem; line-height: 1.5; }
.token-view {
  margin-top: 0.5rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-all;
}
.token-view[hidden] { display: none; }
.tok-header { color: var(--jwt-header); }
.tok-payload { color: var(--jwt-payload); }
.tok-signature { color: var(--jwt-signature); }
.tok-dot { color: var(--muted); }
.json-box {
  margin: 0.25rem 0 0;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  overflow: auto;
  max-height: 16rem;
  font-size: 0.78rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.claims-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.5rem;
}
.claims-table td {
  vertical-align: top;
  padding: 0.3rem 0.25rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}
.claims-table td:first-child { width: 36%; color: var(--label); }
.status-line { margin: 0.6rem 0 0; font-size: 0.85rem; }
.status-line.ok { color: var(--ok); }
.status-line.bad { color: var(--bad); }

::selection { background: var(--brand); color: #ffffff; }

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.btn:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

/* Desktop only: no backdrop, no overlay. */
@media (min-width: 641px) {
  .sidebar-backdrop { display: none !important; }
}

@media (max-width: 640px) {
  html,
  html[data-sidebar="open"],
  html[data-sidebar="closed"] { --rail: 0px; }
  body { transition: none; }
  .top { padding: 0 1rem; }
  .top-toggle { display: grid; }
  body.sidebar-open { overflow: hidden; }

  .sidebar {
    z-index: 70;
    width: min(80vw, 280px);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 150ms ease, visibility 0s linear 150ms;
  }
  html[data-sidebar="open"] .sidebar {
    transform: translateX(0);
    visibility: visible;
    transition: transform 150ms ease;
  }

  .row { grid-template-columns: 1fr; }
  .jwt-layout,
  .objwt-layout { grid-template-columns: 1fr; }
  .contrast-row label { flex-basis: 5.5rem; }
}

@media (prefers-reduced-motion: reduce) {
  body, .sidebar, .sidebar-backdrop { transition: none; }
}
