/* Layout: a workbench. Key and cipher controls sit in a left rail; the message goes in, comes out,
   and is dissected byte by byte on the right. Brass on graphite, like the hardware of a strongbox. */
:root {
  --bg: oklch(0.972 0.004 80);
  --surface: oklch(0.995 0.002 80);
  --sunken: oklch(0.952 0.005 80);
  --line: oklch(0.89 0.008 80);
  --line-strong: oklch(0.81 0.011 80);
  --fg: oklch(0.24 0.012 75);
  --muted: oklch(0.49 0.014 75);
  --accent: oklch(0.6 0.115 76);
  --accent-strong: oklch(0.53 0.11 72);
  --accent-ink: oklch(0.99 0.008 85);
  --accent-soft: oklch(0.6 0.115 76 / 0.16);
  --ok: oklch(0.52 0.13 152);
  --warn: oklch(0.57 0.15 48);
  --danger: oklch(0.53 0.19 27);
  --seg-header: oklch(0.62 0.11 76);
  --seg-salt: oklch(0.56 0.09 205);
  --seg-iv: oklch(0.55 0.12 292);
  --seg-ct: oklch(0.42 0.012 75);
  --seg-tag: oklch(0.54 0.12 152);
  --seg-ink: oklch(0.99 0.005 85);
  --shadow: 0 1px 0 oklch(0.3 0.02 75 / 0.04), 0 10px 30px -18px oklch(0.3 0.03 75 / 0.35);

  --font-display: 'Saira Stencil One', 'Arial Narrow', 'Roboto Condensed', sans-serif;
  --font-body: 'Hanken Grotesk', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'Cascadia Mono', 'SF Mono', Consolas, monospace;

  --radius: 12px;
  --radius-sm: 7px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: oklch(0.17 0.006 75);
    --surface: oklch(0.212 0.007 75);
    --sunken: oklch(0.185 0.006 75);
    --line: oklch(0.295 0.009 75);
    --line-strong: oklch(0.375 0.012 75);
    --fg: oklch(0.93 0.008 85);
    --muted: oklch(0.7 0.014 80);
    --accent: oklch(0.8 0.115 82);
    --accent-strong: oklch(0.86 0.11 85);
    --accent-ink: oklch(0.2 0.02 75);
    --accent-soft: oklch(0.8 0.115 82 / 0.16);
    --ok: oklch(0.77 0.14 152);
    --warn: oklch(0.78 0.13 58);
    --danger: oklch(0.71 0.17 25);
    --seg-header: oklch(0.79 0.11 82);
    --seg-salt: oklch(0.75 0.09 205);
    --seg-iv: oklch(0.74 0.11 295);
    --seg-ct: oklch(0.6 0.012 80);
    --seg-tag: oklch(0.76 0.13 152);
    --seg-ink: oklch(0.19 0.012 75);
    --shadow: 0 1px 0 oklch(1 0 0 / 0.03) inset, 0 12px 30px -20px oklch(0 0 0 / 0.7);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: oklch(0.17 0.006 75);
  --surface: oklch(0.212 0.007 75);
  --sunken: oklch(0.185 0.006 75);
  --line: oklch(0.295 0.009 75);
  --line-strong: oklch(0.375 0.012 75);
  --fg: oklch(0.93 0.008 85);
  --muted: oklch(0.7 0.014 80);
  --accent: oklch(0.8 0.115 82);
  --accent-strong: oklch(0.86 0.11 85);
  --accent-ink: oklch(0.2 0.02 75);
  --accent-soft: oklch(0.8 0.115 82 / 0.16);
  --ok: oklch(0.77 0.14 152);
  --warn: oklch(0.78 0.13 58);
  --danger: oklch(0.71 0.17 25);
  --seg-header: oklch(0.79 0.11 82);
  --seg-salt: oklch(0.75 0.09 205);
  --seg-iv: oklch(0.74 0.11 295);
  --seg-ct: oklch(0.6 0.012 80);
  --seg-tag: oklch(0.76 0.13 152);
  --seg-ink: oklch(0.19 0.012 75);
  --shadow: 0 1px 0 oklch(1 0 0 / 0.03) inset, 0 12px 30px -20px oklch(0 0 0 / 0.7);
  color-scheme: dark;
}

/* ---- Base */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 400 15px/1.5 var(--font-body);
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

h1, h2, h3, p { margin: 0; }
h2, h3 { text-wrap: balance; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
code, kbd, pre { font-family: var(--font-mono); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

.i {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.page {
  max-width: 1240px;
  margin-inline: auto;
  padding-inline: clamp(16px, 3vw, 36px);
  padding-block: 18px 32px;
}

.hint { font-size: 12.5px; line-height: 1.45; color: var(--muted); }
.mono { font-family: var(--font-mono); font-size: 13px; line-height: 1.6; word-break: break-all; }

/* ---- Header */

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  padding-block: 6px 22px;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
}
.brand-mark .i { width: 22px; height: 22px; stroke-width: 1.9; }

.brand-text { display: flex; flex-direction: column; min-width: 0; }

.wordmark {
  font-family: var(--font-display);
  font-size: 27px;
  line-height: 1;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tagline { font-size: 12.5px; color: var(--muted); letter-spacing: 0.01em; }

.theme-toggle { justify-self: end; }

.op-switch {
  display: inline-flex;
  padding: 4px;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.op-option input, .seg input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.op-option span {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 22px;
  border-radius: 9px;
  font-weight: 650;
  font-size: 15px;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}
.op-option span:hover { color: var(--fg); }
.op-option input:checked + span { background: var(--accent); color: var(--accent-ink); }
.op-option input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Controls */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.btn .i { width: 16px; height: 16px; }
.btn:hover:not(:disabled) { background: var(--sunken); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); }

.btn-quiet { border-color: transparent; background: transparent; color: var(--muted); padding-inline: 8px; }
.btn-quiet:hover:not(:disabled) { color: var(--fg); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: none;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--muted);
  transition: color 0.15s, background-color 0.15s;
}
.icon-btn:hover { color: var(--fg); background: var(--sunken); }

.when-done { display: none; }
.copied .when-idle { display: none; }
.copied .when-done { display: block; color: var(--ok); }

.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--sunken);
}
.seg label { position: relative; display: flex; flex: 1; }
.seg span {
  flex: 1;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13.5px;
  font-weight: 600;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}
.seg span:hover { color: var(--fg); }
.seg input:checked + span { background: var(--surface); color: var(--fg); box-shadow: 0 1px 2px oklch(0 0 0 / 0.12), 0 0 0 1px var(--line); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg-sm span { padding: 4px 10px; font-size: 12.5px; }
.seg-wide { display: flex; }

input[type="password"], input[type="text"], select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--sunken);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input[type="password"], input[type="text"], select { height: 38px; padding: 0 12px; }
input[type="password"], input[type="text"] { font-family: var(--font-mono); font-size: 14px; }
select { appearance: auto; cursor: pointer; }
textarea { display: block; padding: 12px 14px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

fieldset { margin: 0; padding: 0; border: 0; min-width: 0; }
legend { padding: 0; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 650;
  white-space: nowrap;
}
.chip .i { width: 13px; height: 13px; stroke-width: 2; }
.chip-warn { color: var(--warn); background: color-mix(in oklch, var(--warn) 14%, transparent); }
.chip-ok { color: var(--ok); background: color-mix(in oklch, var(--ok) 14%, transparent); }

.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.push-end { margin-left: auto; }

/* ---- Workbench */

.workbench {
  display: grid;
  grid-template-columns: minmax(300px, 368px) minmax(0, 1fr);
  grid-template-rows: auto auto 1fr;
  grid-template-areas:
    "controls input"
    "controls output"
    "controls anatomy";
  gap: 18px;
  align-items: start;
}

.card {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.controls { grid-area: controls; }
.input-card { grid-area: input; }
.output-card { grid-area: output; }
.anatomy-card { grid-area: anatomy; }

.control-section { display: grid; gap: 14px; padding: 18px 20px 20px; }
.control-section + .control-section { border-top: 1px solid var(--line); }

.section-head, .io-head { display: flex; align-items: center; gap: 10px; min-height: 30px; }
.section-head { justify-content: space-between; }

.section-head h2, .io-head h2, legend, .field > label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}
.section-head h2, .io-head h2 { color: var(--fg); }
legend, .field > label { color: var(--muted); margin-bottom: 8px; display: block; }

.key-pane { display: grid; gap: 8px; }

.field-label { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.field-label label { font-size: 13px; font-weight: 600; }

.input-group { display: flex; gap: 6px; }
.input-group input { flex: 1; min-width: 0; }

.strength { display: flex; align-items: center; gap: 10px; }
.strength-bar { display: flex; gap: 3px; width: 92px; flex: none; }
.strength-bar i { flex: 1; height: 4px; border-radius: 2px; background: var(--line); transition: background-color 0.2s; }
.strength[data-level="1"] i:nth-child(-n + 1) { background: var(--danger); }
.strength[data-level="2"] i:nth-child(-n + 2) { background: var(--warn); }
.strength[data-level="3"] i:nth-child(-n + 3) { background: var(--ok); }
.strength[data-level="4"] i { background: var(--ok); }

#raw-key { min-height: 64px; }

.key-status { display: flex; gap: 6px; align-items: baseline; font-size: 12.5px; color: var(--muted); }
.key-status[data-state="ok"] { color: var(--ok); }
.key-status[data-state="error"] { color: var(--danger); }

/* Key matrix: the 32 key bytes laid out as 4 rows of 8. Brighter cells hold larger byte values. */
.matrix-figure { margin: 4px 0 0; display: grid; gap: 8px; }
.matrix-frame { position: relative; }
.matrix {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 3px;
  transition: filter 0.25s;
}
.matrix.is-hidden { filter: blur(6px); user-select: none; }
.matrix span {
  position: relative;
  display: grid;
  place-items: center;
  height: 26px;
  border-radius: 4px;
  background: var(--sunken);
  font: 500 11.5px/1 var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-transform: uppercase;
  isolation: isolate;
}
.matrix span::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: inherit;
  background: var(--accent);
  opacity: calc(var(--v, 0) * 0.62);
  transition: opacity 0.25s;
}
.matrix span.empty { color: var(--line-strong); background: transparent; box-shadow: inset 0 0 0 1px var(--line); }

.matrix-reveal {
  position: absolute;
  inset: 50% auto auto 50%;
  translate: -50% -50%;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  font-size: 12.5px;
  font-weight: 650;
  white-space: nowrap;
  box-shadow: var(--shadow);
}
.matrix-reveal .i { width: 15px; height: 15px; }

.matrix-figure figcaption { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); }
.matrix-figure figcaption button { border: 0; background: none; padding: 0; color: var(--muted); font-size: 12px; text-decoration: underline; text-underline-offset: 2px; }
.matrix-figure figcaption button:hover { color: var(--fg); }

.settings { display: grid; gap: 18px; }
.mode-note { margin-top: 8px; font-size: 12.5px; line-height: 1.45; color: var(--muted); }
.field .hint { margin-top: 6px; }

.detected { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 10px 16px; margin: 0; }
.detected dt { font-size: 12.5px; color: var(--muted); }
.detected dd { margin: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13.5px; font-weight: 550; }

/* ---- Input and output */

.io { display: grid; gap: 12px; padding: 14px 18px 18px; }
.io-head .count { margin-left: auto; font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.io-head .btn-quiet { margin-right: -8px; }

#input { min-height: 168px; font-size: 15px; line-height: 1.55; }
#input.mono { font-size: 13px; line-height: 1.6; }
#output { min-height: 128px; cursor: default; }
#output:not(.mono) { font-family: var(--font-body); font-size: 15px; line-height: 1.55; word-break: normal; overflow-wrap: anywhere; }
#output:placeholder-shown { resize: none; }
textarea::placeholder { color: var(--muted); opacity: 0.8; }

.input-hint {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 6px 6px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  font-size: 13.5px;
}

.status { margin-left: auto; display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
.status i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status[data-kind="ok"] { color: var(--ok); }
.status[data-kind="error"] { color: var(--danger); }
.status[data-kind="busy"] { color: var(--accent-strong); }
.status[data-kind="busy"] i { animation: pulse 0.9s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.25; } }

.error {
  display: flex;
  gap: 10px;
  min-height: 128px;
  padding: 14px 16px;
  border: 1px solid color-mix(in oklch, var(--danger) 45%, transparent);
  border-radius: var(--radius-sm);
  background: color-mix(in oklch, var(--danger) 8%, var(--surface));
}
.error > .i { color: var(--danger); margin-top: 1px; }
.error div { display: grid; gap: 10px; align-content: start; justify-items: start; }
.error p { font-size: 14.5px; line-height: 1.5; max-width: 60ch; }

.output-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; }
.meta { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 0; }
.actions { justify-content: flex-end; margin-left: auto; }

/* ---- Anatomy */

.anatomy-card { display: grid; gap: 14px; padding: 14px 18px 8px; }

.anatomy-bar { display: flex; gap: 2px; height: 36px; border-radius: 8px; overflow: hidden; }
.anatomy-bar:empty { display: none; }

.piece {
  display: grid;
  place-items: center;
  min-width: 34px;
  flex: 1 1 0;
  background: var(--piece);
  color: var(--seg-ink);
  font: 600 11.5px/1 var(--font-mono);
  font-variant-numeric: tabular-nums;
  transition: opacity 0.15s;
}
.piece[data-seg="ciphertext"] {
  background: repeating-linear-gradient(-45deg, var(--seg-ct) 0 7px, color-mix(in oklch, var(--seg-ct) 82%, var(--surface)) 7px 14px);
}
.anatomy-bar.has-active .piece:not(.is-active) { opacity: 0.35; }

[data-seg="header"] { --piece: var(--seg-header); }
[data-seg="salt"] { --piece: var(--seg-salt); }
[data-seg="iv"] { --piece: var(--seg-iv); }
[data-seg="ciphertext"] { --piece: var(--seg-ct); }
[data-seg="tag"] { --piece: var(--seg-tag); }

.anatomy-list { list-style: none; margin: 0; padding: 0; }
.anatomy-list li {
  display: grid;
  grid-template-columns: 12px minmax(0, 15rem) 4.2rem minmax(0, 1fr) auto;
  grid-template-areas: "swatch name size hex copy";
  align-items: center;
  gap: 4px 14px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}
.anatomy-list li.is-active { background: linear-gradient(90deg, var(--accent-soft), transparent 70%); }
.swatch { grid-area: swatch; width: 12px; height: 12px; border-radius: 3px; background: var(--piece); align-self: start; margin-top: 4px; }
.seg-name { grid-area: name; display: grid; gap: 1px; }
.seg-name strong { font-size: 13.5px; font-weight: 650; }
.seg-name span { font-size: 12px; color: var(--muted); line-height: 1.4; }
.seg-size { grid-area: size; text-align: right; font: 500 12px var(--font-mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.seg-hex { grid-area: hex; font: 12px/1.5 var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg); }
.anatomy-list .icon-btn { grid-area: copy; width: 30px; height: 30px; border-color: transparent; background: transparent; }
.anatomy-list .icon-btn .i { width: 16px; height: 16px; }
.anatomy-empty { padding-bottom: 10px; }

/* ---- Docs */

.docs { margin-top: 44px; display: grid; gap: 22px; }
.docs > h2 { font-size: 21px; font-weight: 700; letter-spacing: -0.01em; }
.docs-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 22px 36px; }
.docs article { display: grid; gap: 8px; align-content: start; }
.docs h3 { font-size: 15px; font-weight: 700; }
.docs p { font-size: 14.5px; line-height: 1.6; color: color-mix(in oklch, var(--fg) 82%, var(--muted)); max-width: 65ch; }

.spec { border-top: 1px solid var(--line); }
.spec summary { padding: 16px 0; font-weight: 650; cursor: pointer; }
.spec summary:hover { color: var(--accent-strong); }
.spec-body { display: grid; gap: 16px; padding-bottom: 8px; }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; font-size: 13.5px; min-width: 480px; }
th { text-align: left; font-size: 11.5px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
th, td { padding: 8px 18px 8px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
td:first-child { font-family: var(--font-mono); font-size: 12.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
code { font-size: 0.9em; }

pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--sunken);
  font-size: 12.5px;
  line-height: 1.6;
}
pre code { font-size: inherit; }

.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 24px;
  margin-top: 36px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 12.5px;
  color: var(--muted);
}
kbd {
  padding: 1px 5px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface);
  font-size: 11px;
}

/* ---- Narrow screens */

@media (max-width: 960px) {
  .workbench {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "input" "controls" "output" "anatomy";
  }
}

@media (max-width: 720px) {
  .topbar { grid-template-columns: 1fr auto; grid-template-areas: "brand theme" "op op"; }
  .brand { grid-area: brand; }
  .theme-toggle { grid-area: theme; }
  .op-switch { grid-area: op; display: flex; }
  .op-option { flex: 1; }
  .op-option span { width: 100%; justify-content: center; }

  .anatomy-list li {
    grid-template-columns: 12px minmax(0, 1fr) auto auto;
    grid-template-areas: "swatch name size copy" ". hex hex hex";
  }
  .actions { margin-left: 0; justify-content: flex-start; }
}

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