:root {
  --shell:      #e3e7e3;
  --shell-deep: #ced5ce;
  --ink:        #14181a;
  --ink-soft:   #5d6762;
  --key:        #fbfcfa;
  --key-edge:   #bfc7c0;
  --slate:      #38474e;
  --slate-edge: #222f34;
  --paper:      #fffdf7;
  --paper-edge: #e6e1d2;
  --red:        #c8102e;
  --focus:      #1f6f8b;
  --sans: "IBM Plex Sans", ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  --radius: 6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --shell:      #1f2523;
    --shell-deep: #161b1a;
    --ink:        #eef2ee;
    --ink-soft:   #97a29c;
    --key:        #2b3331;
    --key-edge:   #121716;
    --slate:      #4a5f68;
    --slate-edge: #2b3a41;
    --paper:      #232a28;
    --paper-edge: #141918;
    --red:        #ff6b7d;
    --focus:      #6fd0ef;
  }
}

* { box-sizing: border-box; }

/* display:grid/inline-flex на .sci и .seg перебивали атрибут hidden */
[hidden] { display: none !important; }

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

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--shell-deep);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
}

:where(a):focus-visible,
:where(button):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------- header ---------- */

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: var(--shell);
  border-bottom: 1px solid var(--key-edge);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.mark { width: 22px; height: 22px; flex: none; }
.mark-body { fill: none; stroke: currentColor; stroke-width: 1.6; }
.mark-line { fill: currentColor; }
.mark-neg  { fill: var(--red); }

.nav { display: flex; gap: 18px; }

.nav a {
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 0.9375rem;
}

.nav a:hover { color: var(--ink); text-decoration: underline; text-underline-offset: 3px; }

/* ---------- layout ---------- */

.layout {
  flex: 1;
  width: min(1040px, 100%);
  margin: 0 auto;
  padding: 28px 20px 40px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 28px;
  align-items: start;
}

/* ---------- machine ---------- */

.machine {
  background: var(--shell);
  border: 1px solid var(--key-edge);
  border-radius: var(--radius);
  padding: 18px;
}

.modes {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.seg {
  display: inline-flex;
  background: var(--shell-deep);
  border: 1px solid var(--key-edge);
  border-radius: var(--radius);
  padding: 2px;
  gap: 2px;
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 0.875rem;
  padding: 5px 13px;
  border-radius: 4px;
  cursor: pointer;
}

.seg-btn.is-on {
  background: var(--key);
  color: var(--ink);
  font-weight: 500;
}

.seg-angle .seg-btn { font-family: var(--mono); padding: 5px 10px; }

/* ---------- readout ---------- */

.readout {
  background: var(--shell-deep);
  border: 1px solid var(--key-edge);
  border-radius: var(--radius);
  padding: 14px 16px 10px;
  margin-bottom: 16px;
}

.readout-expr {
  font-family: var(--mono);
  font-size: 0.9375rem;
  color: var(--ink-soft);
  min-height: 1.5em;
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
}

.readout-expr::-webkit-scrollbar { display: none; }

.readout-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.readout-out {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  font-size: clamp(1.75rem, 6vw, 2.75rem);
  font-weight: 500;
  line-height: 1.2;
  letter-spacing: -0.02em;
  text-align: right;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
}

.readout-out::-webkit-scrollbar { display: none; }
.readout-out.is-neg { color: var(--red); }

.back {
  flex: none;
  width: 38px;
  height: 34px;
  display: grid;
  place-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--ink-soft);
  cursor: pointer;
}

.back:hover { color: var(--ink); border-color: var(--key-edge); }
.back svg { width: 21px; height: 21px; }
.back-body { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linejoin: round; }
.back-x { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; }

.hint {
  margin: 6px 0 0;
  min-height: 1.25em;
  font-size: 0.8125rem;
  color: var(--red);
  text-align: right;
}

/* ---------- keys ---------- */

.sci, .pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.sci { margin-bottom: 8px; }

.sci button, .pad button {
  appearance: none;
  font: inherit;
  font-family: var(--mono);
  font-size: 1.0625rem;
  color: var(--ink);
  background: var(--key);
  border: 1px solid var(--key-edge);
  border-bottom-width: 2px;
  border-radius: var(--radius);
  padding: 14px 4px;
  cursor: pointer;
  user-select: none;
}

.sci button {
  font-size: 0.875rem;
  padding: 10px 4px;
  color: var(--ink-soft);
}

.sci button sup { font-size: 0.7em; }

.sci button:hover, .pad button:hover { background: color-mix(in srgb, var(--key) 88%, var(--ink)); }

.sci button:active, .pad button:active,
.sci button.is-hit, .pad button.is-hit {
  transform: translateY(1px);
  border-bottom-width: 1px;
  margin-bottom: 1px;
}

.pad .k-fn { color: var(--ink-soft); }

.pad .k-op, .pad .k-eq {
  background: var(--slate);
  border-color: var(--slate-edge);
  color: #f4f7f6;
  font-size: 1.1875rem;
}

.pad .k-op:hover, .pad .k-eq:hover { background: color-mix(in srgb, var(--slate) 78%, #000); }

.pad .k-eq { font-weight: 500; }

/* ---------- tape ---------- */

.tape {
  position: sticky;
  top: 20px;
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 14px 16px 0;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 60px);
  /* torn-off strip along the bottom edge */
  -webkit-mask: linear-gradient(#000 0 0) top/100% calc(100% - 7px) no-repeat,
                repeating-linear-gradient(90deg, #000 0 5px, transparent 5px 10px) bottom/100% 7px repeat-x;
          mask: linear-gradient(#000 0 0) top/100% calc(100% - 7px) no-repeat,
                repeating-linear-gradient(90deg, #000 0 5px, transparent 5px 10px) bottom/100% 7px repeat-x;
}

.tape-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--paper-edge);
}

.tape-head h2 {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.tape-clear {
  appearance: none;
  border: 0;
  background: none;
  font: inherit;
  font-size: 0.8125rem;
  color: var(--ink-soft);
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.tape-clear:hover { color: var(--ink); }

.tape-list {
  list-style: none;
  margin: 0;
  padding: 10px 0 24px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.875rem;
  flex: 1;
}

.tape-list li {
  padding: 5px 0;
  border-bottom: 1px dotted var(--paper-edge);
  cursor: pointer;
}

.tape-list li:hover { background: color-mix(in srgb, var(--paper) 90%, var(--ink)); }

.tape-expr {
  display: block;
  color: var(--ink-soft);
  font-size: 0.8125rem;
  overflow-wrap: anywhere;
}

.tape-res {
  display: block;
  text-align: right;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.tape-res.is-neg { color: var(--red); }

.tape-empty {
  margin: 0;
  padding: 12px 0 26px;
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

/* ---------- footer ---------- */

.foot {
  background: var(--shell);
  border-top: 1px solid var(--key-edge);
  padding: 18px 20px;
  font-size: 0.875rem;
  color: var(--ink-soft);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 20px;
  justify-content: space-between;
}

.foot p { margin: 0; }
.foot-links { display: flex; gap: 16px; }
.foot a { color: inherit; }

/* ---------- prose pages ---------- */

.prose {
  width: min(62ch, 100%);
  margin: 0 auto;
  padding: 36px 20px 56px;
}

.prose h1 {
  margin: 0 0 6px;
  font-size: clamp(1.5rem, 4vw, 2rem);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.prose .lede { margin: 0 0 28px; color: var(--ink-soft); }
.prose h2 { margin: 30px 0 8px; font-size: 1.0625rem; }
.prose p, .prose li { margin: 0 0 12px; }
.prose ul { padding-left: 20px; }
.prose a { color: var(--focus); }

/* ---------- responsive ---------- */

@media (max-width: 800px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 20px; }
  .tape { position: static; max-height: 340px; }
}

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