/* Colors: cobalt marks the public key and encryption, raspberry the private
   key and decryption, ochre input that cannot round-trip. */
:root,
:root[data-theme="light"] {
  --paper: #f2f4fa;
  --surface: #ffffff;
  --surface-2: #e8ebf5;
  --ink: #17192e;
  --muted: #5a5f7d;
  --line: #d5d9ea;
  --public: #2b4edb;
  --public-soft: #e3e9ff;
  --private: #c42a63;
  --private-soft: #fbe4ee;
  --caution: #9a5b00;
  --caution-soft: #fff1d6;
  --on-accent: #ffffff;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --paper: #101329;
  --surface: #181c38;
  --surface-2: #20254a;
  --ink: #e8eaf7;
  --muted: #a0a6cb;
  --line: #2c3260;
  --public: #8ea2ff;
  --public-soft: #232c66;
  --private: #ff7fae;
  --private-soft: #4a1d35;
  --caution: #f0b44a;
  --caution-soft: #3b2c10;
  --on-accent: #101329;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #101329;
    --surface: #181c38;
    --surface-2: #20254a;
    --ink: #e8eaf7;
    --muted: #a0a6cb;
    --line: #2c3260;
    --public: #8ea2ff;
    --public-soft: #232c66;
    --private: #ff7fae;
    --private-soft: #4a1d35;
    --caution: #f0b44a;
    --caution-soft: #3b2c10;
    --on-accent: #101329;
    color-scheme: dark;
  }
}

:root {
  --font-sans: "Bricolage Grotesque", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "Martian Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius-tile: 10px;
  --radius-panel: 16px;
  --gutter: 16px;
  --track: 44px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 1.0625rem;
  line-height: 1.55;
  font-optical-sizing: auto;
  transition: background-color 0.25s ease, color 0.25s ease;
}

a {
  color: var(--public);
  text-underline-offset: 0.18em;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 3px solid var(--public);
  outline-offset: 2px;
}

sup {
  line-height: 0;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: var(--gutter);
  top: -100px;
  padding: 8px 14px;
  border-radius: var(--radius-tile);
  background: var(--public);
  color: var(--on-accent);
  z-index: 20;
}

.skip-link:focus {
  top: 12px;
}

.mono,
.num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Header */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px var(--gutter);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-decoration: none;
}

.brand-mark {
  width: 28px;
  height: 28px;
}

.mark-body {
  fill: var(--public);
}

.mark-shackle {
  fill: none;
  stroke: var(--private);
  stroke-width: 3;
}

.site-nav {
  display: flex;
  align-items: center;
  gap: 18px;
}

.site-nav a {
  color: var(--ink);
  font-weight: 500;
  text-decoration: none;
}

.site-nav a:hover {
  color: var(--public);
}

.theme-toggle {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  cursor: pointer;
}

.theme-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.theme-toggle .icon-moon {
  fill: currentColor;
  stroke: none;
}

:root[data-theme="dark"] .icon-moon,
:root:not([data-theme="dark"]) .icon-sun {
  display: none;
}

/* Hero */

.hero {
  max-width: 1180px;
  margin: 0 auto;
  padding: 40px var(--gutter) 48px;
}

.hero h1 {
  max-width: 13ch;
  margin: 0;
  font-size: clamp(2.6rem, 1.5rem + 5.2vw, 5.4rem);
  font-weight: 750;
  line-height: 0.98;
  letter-spacing: -0.035em;
}

.lede {
  max-width: 58ch;
  margin: 20px 0 32px;
  font-size: 1.15rem;
  color: var(--muted);
}

.message-input {
  display: block;
  width: 100%;
  max-width: 960px;
  padding: 6px 0 10px;
  border: 0;
  border-bottom: 3px solid var(--ink);
  border-radius: 0;
  background: transparent;
  font-size: clamp(1.75rem, 1.2rem + 2.6vw, 3rem);
  font-weight: 650;
  letter-spacing: -0.02em;
  caret-color: var(--public);
}

.message-input:focus {
  outline: none;
  border-bottom-color: var(--public);
}

.hint {
  margin: 10px 0 20px;
  font-size: 0.9rem;
  color: var(--muted);
}

/* The strip: one column per character, letter above its code and block */

.strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 112px;
}

.column {
  display: grid;
  justify-items: center;
  gap: 4px;
  min-width: 3.4rem;
  padding: 10px 8px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-tile);
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.column:hover {
  border-color: var(--public);
}

.column[aria-pressed="true"] {
  border-color: var(--public);
  box-shadow: inset 0 0 0 1px var(--public);
}

.column-char {
  font-size: 1.9rem;
  font-weight: 650;
  line-height: 1.1;
  min-height: 1.1em;
}

.column-code {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--muted);
}

.column-block {
  display: inline-flex;
  overflow: hidden;
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--public-soft);
  color: var(--public);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 600;
}

.column-block .digit {
  display: inline-block;
}

.column.fresh .digit {
  animation: roll-in 0.42s cubic-bezier(0.2, 0.8, 0.2, 1) both;
  animation-delay: calc(var(--col, 0) * 28ms + var(--d, 0) * 40ms);
}

@keyframes roll-in {
  from {
    transform: translateY(110%);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

.column.too-big .column-block {
  background: var(--caution-soft);
  color: var(--caution);
}

.column.pending .column-code,
.column.pending .column-block {
  color: transparent;
  background: var(--surface-2);
  border-radius: 6px;
  min-width: 2.6rem;
}

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

.status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 24px 0 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s ease-in-out infinite;
}

.status[data-state="ready"] .status-dot {
  background: var(--public);
  animation: none;
}

.status[data-state="error"] {
  color: var(--caution);
}

.status[data-state="error"] .status-dot {
  background: var(--caution);
  animation: none;
}

@keyframes pulse {
  50% {
    opacity: 0.3;
  }
}

/* Page layout */

.layout {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 var(--gutter) 64px;
}

/* Key ring: a sticky bar on small screens, a sidebar on large ones */

.keyring {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin: 0 calc(-1 * var(--gutter)) 32px;
  padding: 10px var(--gutter);
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  border-block: 1px solid var(--line);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.key {
  display: flex;
  align-items: center;
  gap: 10px;
}

.key-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-size: 0.85rem;
  font-weight: 600;
}

.key-title svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.key-public .key-title {
  color: var(--public);
}

.key-private .key-title {
  color: var(--private);
}

.key-values {
  display: flex;
  gap: 12px;
  margin: 0;
}

.key-values div {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.key-values dt {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.key-values dd {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 600;
}

.key-public dd {
  color: var(--public);
}

.key-private dd {
  color: var(--private);
}

.key-note {
  display: none;
}

/* Steps: a numbered track down the left edge */

.steps {
  margin: 0;
  padding: 0;
  list-style: none;
}

.step {
  position: relative;
  padding: 0 0 56px var(--track);
}

.step::before {
  content: "";
  position: absolute;
  left: 14px;
  top: 38px;
  bottom: 8px;
  width: 2px;
  background: var(--line);
}

.step:last-child::before {
  display: none;
}

.step-title {
  position: relative;
  margin: 0 0 10px;
  font-size: clamp(1.45rem, 1.15rem + 1.2vw, 2rem);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
}

.step-number {
  position: absolute;
  left: calc(-1 * var(--track));
  top: 0.05em;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0;
}

.step-text {
  max-width: 62ch;
  margin: 0 0 20px;
}

.step-note {
  max-width: 62ch;
  margin: 12px 0 0;
  font-size: 0.95rem;
  color: var(--muted);
}

.step-note.warning {
  color: var(--caution);
}

/* Fields */

.prime-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  max-width: 520px;
}

.field label,
.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.95rem;
  font-weight: 600;
}

.stepper {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: var(--radius-tile);
  background: var(--surface);
  overflow: hidden;
}

.stepper:focus-within {
  border-color: var(--public);
}

.stepper input {
  width: 100%;
  min-width: 0;
  padding: 8px 4px;
  border: 0;
  background: transparent;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--private);
  -moz-appearance: textfield;
  appearance: textfield;
}

.stepper input:focus,
.field-inline input:focus {
  outline: none;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.step-button {
  flex: none;
  width: 44px;
  border: 0;
  background: var(--surface-2);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}

.step-button:hover {
  background: var(--public-soft);
  color: var(--public);
}

.field-error {
  min-height: 0;
  margin: 6px 0 0;
  font-size: 0.9rem;
  color: var(--caution);
}

.field-error:empty {
  display: none;
}

.field.invalid .stepper,
.field-inline.invalid input {
  border-color: var(--caution);
}

.text-input {
  display: block;
  width: 100%;
  max-width: 640px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-tile);
  background: var(--surface);
  font-size: 1.1rem;
}

.text-input:focus {
  outline: none;
  border-color: var(--public);
  box-shadow: 0 0 0 3px var(--public-soft);
}

textarea.text-input {
  resize: vertical;
  font-size: 0.95rem;
  line-height: 1.6;
  word-break: break-all;
}

.presets,
.chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.presets {
  margin-top: 16px;
}

.presets-label {
  margin-right: 2px;
  font-size: 0.9rem;
  color: var(--muted);
}

.chip {
  min-height: 36px;
  padding: 4px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.92rem;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.chip:hover {
  border-color: var(--public);
}

.chip[aria-pressed="true"] {
  border-color: var(--public);
  background: var(--public);
  color: var(--on-accent);
}

.chips .chip {
  min-width: 48px;
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

.exponent-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 20px;
}

.field-inline {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-inline label {
  margin: 0;
}

.field-inline input {
  width: 96px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-tile);
  background: var(--surface);
  text-align: center;
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--public);
  appearance: textfield;
  -moz-appearance: textfield;
}

.field-inline input:focus {
  border-color: var(--public);
}

/* Equations under step 1 */

.equations {
  display: grid;
  gap: 6px;
  margin-top: 24px;
  font-family: var(--font-mono);
  font-size: clamp(0.95rem, 0.85rem + 0.5vw, 1.2rem);
}

.equation {
  margin: 0;
}

.equation .label {
  font-family: var(--font-sans);
  font-weight: 650;
}

.is-public {
  color: var(--public);
  font-weight: 600;
}

.is-private {
  color: var(--private);
  font-weight: 600;
}

.is-caution {
  color: var(--caution);
  font-weight: 600;
}

/* Panels and tables */

.panel {
  max-width: 720px;
  margin-top: 24px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius-panel);
  background: var(--surface);
}

.panel:empty {
  display: none;
}

.panel-title {
  margin: 0 0 6px;
  font-size: 1.12rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.panel-text {
  max-width: 62ch;
  margin: 0 0 14px;
  font-size: 0.95rem;
  color: var(--muted);
}

.table-wrap {
  overflow-x: auto;
  margin: 0 -4px;
  padding: 0 4px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.data-table th {
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  font-size: 0.85rem;
  font-weight: 600;
  text-align: right;
  white-space: nowrap;
  color: var(--muted);
}

.data-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--surface-2);
  text-align: right;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.data-table th:first-child,
.data-table td:first-child {
  text-align: left;
}

.data-table tr.final td {
  background: var(--private-soft);
}

.data-table th.col-d,
.data-table td.col-d {
  color: var(--private);
}

.data-table tr.used td {
  background: var(--public-soft);
}

.data-table.private tr.used td {
  background: var(--private-soft);
}

.data-table tr.unused td {
  color: var(--muted);
}

.result-line {
  margin: 14px 0 0;
  line-height: 1.7;
}

.result-line .num {
  font-size: 0.92em;
}

.detail-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 14px;
  margin-bottom: 12px;
}

.detail-char {
  font-size: 2.4rem;
  font-weight: 700;
  line-height: 1;
}

.detail-summary {
  margin: 0;
}

.bits {
  display: inline-flex;
  gap: 3px;
  vertical-align: middle;
}

.bit {
  display: inline-grid;
  place-items: center;
  width: 1.5em;
  height: 1.5em;
  border-radius: 5px;
  background: var(--surface-2);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--muted);
}

.bit.on {
  background: var(--public);
  color: var(--on-accent);
}

.private-bits .bit.on {
  background: var(--private);
}

/* Character pickers in steps 3 and 4 */

.char-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 16px;
}

.pick {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  min-height: 40px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-tile);
  background: var(--surface);
  cursor: pointer;
}

.pick:hover {
  border-color: var(--public);
}

.pick[aria-pressed="true"] {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--paper);
}

.pick-char {
  min-width: 0.8em;
  font-weight: 650;
  text-align: center;
}

.pick-num {
  font-family: var(--font-mono);
  font-size: 0.75rem;
}

.pick.too-big {
  border-color: var(--caution);
}

/* Ciphertext output */

.cipher-output {
  max-width: 720px;
  margin-top: 24px;
}

.cipher-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.cipher-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin: 8px 0 0;
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 600;
  color: var(--public);
}

.cipher-blocks span {
  padding: 3px 5px;
  border-radius: 5px;
  background: var(--public-soft);
}

.cipher-blocks span:nth-child(even) {
  background: var(--surface-2);
}

.cipher-blocks span.too-big {
  color: var(--caution);
  background: var(--caution-soft);
}

.cipher-field {
  max-width: 720px;
}

.link-button {
  padding: 4px 2px;
  border: 0;
  background: none;
  color: var(--public);
  font-size: 0.92rem;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 0.18em;
  cursor: pointer;
}

.plaintext {
  max-width: 720px;
  margin: 20px 0 0;
  font-size: 1.15rem;
}

.plaintext:empty {
  display: none;
}

.plaintext strong {
  color: var(--private);
}

.match {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 10px;
  border-radius: 999px;
  background: var(--private-soft);
  color: var(--private);
  font-size: 0.85rem;
  font-weight: 600;
}

.match.mismatch {
  background: var(--caution-soft);
  color: var(--caution);
}

/* Why this is only a toy */

.toy {
  padding-top: 8px;
}

.toy h2 {
  margin: 0 0 18px;
  font-size: clamp(1.45rem, 1.15rem + 1.2vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.toy-grid {
  display: grid;
  gap: 20px;
}

.toy-item {
  padding-top: 14px;
  border-top: 3px solid var(--caution);
}

.toy-item h3 {
  margin: 0 0 6px;
  font-size: 1.12rem;
  font-weight: 700;
}

.toy-item p {
  max-width: 52ch;
  margin: 0;
}

.toy-footnote {
  max-width: 62ch;
  margin: 24px 0 0;
  font-size: 0.95rem;
  color: var(--muted);
}

/* Footer */

.site-footer {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px var(--gutter) 40px;
  border-top: 1px solid var(--line);
  font-size: 0.9rem;
  color: var(--muted);
}

.site-footer p {
  max-width: 70ch;
  margin: 0 0 10px;
}

.footer-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 20px;
}

/* Small phones */

@media (max-width: 420px) {
  :root {
    --track: 40px;
  }

  .site-nav {
    gap: 12px;
  }

  .key-title {
    font-size: 0;
    gap: 0;
  }

  .panel {
    padding: 14px 12px;
  }

  .data-table {
    font-size: 0.74rem;
  }

  .data-table th,
  .data-table td {
    padding: 6px 5px;
  }

  .data-table th {
    font-size: 0.78rem;
  }

  .column {
    min-width: 3rem;
    padding: 8px 6px 6px;
  }

  .column-char {
    font-size: 1.6rem;
  }
}

/* Tablets */

@media (min-width: 720px) {
  :root {
    --gutter: 28px;
    --track: 60px;
  }

  .hero {
    padding-top: 64px;
    padding-bottom: 64px;
  }

  .step-number {
    width: 38px;
    height: 38px;
    font-size: 1.05rem;
  }

  .step::before {
    left: 18px;
    top: 44px;
  }

  .toy-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 32px;
  }

  .panel {
    padding: 22px 24px;
  }
}

/* Laptops: key ring becomes a sidebar */

@media (min-width: 1080px) {
  .layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 280px;
    column-gap: 56px;
    align-items: start;
  }

  .steps,
  .toy {
    grid-column: 1;
  }

  .keyring {
    grid-column: 2;
    grid-row: 1 / span 2;
    top: 24px;
    display: grid;
    gap: 14px;
    margin: 0;
    padding: 0;
    background: none;
    border: 0;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .key {
    display: grid;
    gap: 8px;
    padding: 16px 18px;
    border: 1px solid var(--line);
    border-left-width: 4px;
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .key-public {
    border-left-color: var(--public);
  }

  .key-private {
    border-left-color: var(--private);
  }

  .key-title {
    font-size: 1rem;
  }

  .key-values dd {
    font-size: 1.35rem;
  }

  .key-note {
    display: block;
    margin: 0;
    font-size: 0.88rem;
    color: var(--muted);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
