:root {
  --bg: #f2f4f6;
  --surface: #ffffff;
  --ink: #15202b;
  --muted: #566574;
  --line: #d3dae1;
  --accent: #17635f;
  --accent-ink: #ffffff;
  --wild: #5238c8;
  --wild-bg: #f1eefc;
  --danger: #a3271b;
  --focus: #1d6fd1;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 40px;
  --t-small: 0.8125rem; --t-body: 1rem; --t-lead: 1.125rem; --t-head: 1.5rem; --t-title: 2rem;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Noto Serif", Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1418; --surface: #151d23; --ink: #e6ecf0; --muted: #9aa9b5; --line: #2a363f;
    --accent: #5fc1b8; --accent-ink: #06201d; --wild: #b3a7ff; --wild-bg: #1c1a31;
    --danger: #ff8f80; --focus: #7db4f5;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: var(--t-body)/1.55 var(--sans);
  padding: var(--s5) var(--s4) var(--s6);
  max-width: 64rem; margin-inline: auto;
}
h1, h2, h3, p, ul, dl, dd, blockquote { margin: 0; padding: 0; }
ul { list-style: none; }
h1 { font: 600 var(--t-title)/1.1 var(--serif); letter-spacing: -0.01em; }
.han { font-weight: 400; margin-right: var(--s1); }
h2 { font: 600 var(--t-lead)/1.3 var(--sans); }
.lede { color: var(--muted); max-width: 38rem; margin-top: var(--s2); }
.top { display: flex; gap: var(--s4); justify-content: space-between; align-items: flex-start; margin-bottom: var(--s5); }

button, input, textarea, summary { font: inherit; color: inherit; }
button {
  min-height: 44px; padding: 0 var(--s4); border-radius: 6px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface);
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { cursor: default; opacity: 0.55; }
button.primary, #key-form button {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; padding: 0 var(--s5);
}
button.quiet { background: transparent; }
button.small { min-height: 36px; font-size: var(--t-small); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

label.field, label { display: block; font-weight: 600; margin-bottom: var(--s2); }
textarea, input[type="password"] {
  width: 100%; padding: var(--s3); border: 1px solid var(--line); border-radius: 6px;
  background: var(--surface); font-size: 16px; line-height: 1.5;
}
textarea { resize: vertical; min-height: 9rem; font-family: var(--serif); }
.count { text-align: right; font-size: var(--t-small); color: var(--muted); min-height: 1.2em; }
.hint { font-size: var(--t-small); color: var(--muted); }

.gate { max-width: 34rem; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: var(--s5); }
.gate p { margin: var(--s3) 0; }
.gate summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; gap: var(--s2); }
.gate form { display: grid; gap: var(--s3); justify-items: start; padding-bottom: var(--s2); }
.gate input { max-width: 100%; }

.layout { display: grid; gap: var(--s5); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 62rem) {
  .layout { grid-template-columns: minmax(0, 1fr) 17rem; gap: var(--s6); align-items: start; }
  .history { position: sticky; top: var(--s5); }
}

.moves { margin-top: var(--s3); border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.moves > summary, .history > details > summary {
  cursor: pointer; min-height: 44px; display: flex; flex-wrap: wrap; gap: var(--s1) var(--s3);
  align-items: center; padding: 0 var(--s4); font-weight: 600;
}
summary::-webkit-details-marker { display: none; }
summary::before { content: ""; flex: none; width: 0.45em; height: 0.45em; margin-right: var(--s1);
  border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(-45deg); }
details[open] > summary::before { transform: rotate(45deg); }
.moves .state { font-weight: 400; font-size: var(--t-small); color: var(--muted); }
.moves .hint, .moves-list, .moves > button { margin: 0 var(--s4) var(--s3); }
.moves-list { display: grid; gap: var(--s3); }
.move { border: 0; border-top: 1px solid var(--line); padding: var(--s3) 0 0; margin: 0; min-width: 0; }
.move legend { padding: 0; font-weight: 600; float: left; width: 100%; }
.move legend + * { clear: both; }
.move .prompt { font-family: var(--serif); margin-top: var(--s1); }
.move .meta { font-size: var(--t-small); color: var(--muted); margin-top: var(--s1); }
.seg { display: inline-flex; margin-top: var(--s2); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg label { margin: 0; font-weight: 400; position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.seg span { display: flex; align-items: center; min-height: 44px; padding: 0 var(--s4); font-size: var(--t-small); border-left: 1px solid var(--line); }
.seg label:first-child span { border-left: 0; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: -2px; }
.seg input:disabled + span { opacity: 0.45; }

.actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s3) var(--s4); margin-top: var(--s4); }
.status { font-size: var(--t-small); color: var(--muted); flex: 1 1 14rem; }
.status.error { color: var(--danger); font-size: var(--t-body); }
.working { display: inline-block; width: 0.9em; height: 0.9em; margin-right: var(--s2); vertical-align: -0.1em;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: turn 0.9s linear infinite; }
@keyframes turn { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .working { animation-duration: 3s; } }

.results { margin-top: var(--s6); }
.results-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--s1) var(--s4); }
.meta { font-size: var(--t-small); color: var(--muted); }
.input-echo {
  margin: var(--s3) 0 var(--s5); padding-left: var(--s4); border-left: 3px solid var(--line);
  color: var(--muted); font-family: var(--serif); white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 7.5rem; overflow: auto;
}
.persp {
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  padding: var(--s4); margin-bottom: var(--s4);
}
.persp:focus { outline: none; }
.persp dl { display: grid; gap: var(--s3); }
.persp dt { font-size: var(--t-small); font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.persp dd { overflow-wrap: anywhere; white-space: pre-wrap; }
.persp .f-move { font: 600 var(--t-head)/1.2 var(--serif); }
.persp .f-view { font: var(--t-lead)/1.6 var(--serif); }
.persp .f-blind { border-left: 3px solid var(--line); padding-left: var(--s3); color: var(--muted); }
.persp footer { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s3); margin-top: var(--s4); padding-top: var(--s3); border-top: 1px solid var(--line); }
.persp footer .note { font-size: var(--t-small); color: var(--muted); }
button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.persp.wild { border: 2px dashed var(--wild); background: var(--wild-bg); }
.badge {
  display: inline-block; margin-bottom: var(--s3); padding: 2px var(--s3); border-radius: 999px;
  background: var(--wild); color: var(--surface); font-size: var(--t-small); font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
}
.wild .badge-note { font-size: var(--t-small); color: var(--muted); margin: 0 0 var(--s3); }

.again { margin: var(--s5) 0 0; display: grid; gap: var(--s2); justify-items: start; }

.history-list { display: grid; gap: var(--s2); padding: 0 var(--s4) var(--s3); }
.history { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.history .hint { padding: 0 var(--s4) var(--s3); }
.history-list button { width: 100%; text-align: left; padding: var(--s2) var(--s3); display: grid; gap: 2px; min-height: 44px; border-color: transparent; background: transparent; border-bottom-color: var(--line); border-radius: 0; }
.history-list button[aria-current="true"] { border-left: 3px solid var(--accent); background: var(--bg); }
.history-list .snip { font-family: var(--serif); display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.history-list .when { font-size: var(--t-small); color: var(--muted); }

@media (max-width: 30rem) {
  body { padding-inline: var(--s4); }
  h1 { font-size: 1.625rem; }
  .top { flex-direction: column; }
  .persp .f-move { font-size: 1.3125rem; }
  button.primary { width: 100%; }
}
