:root{
  --bg: #12352a;
  --bg-deep: #0b241c;
  --tape-bg: #0a231b;
  --felt-line: rgba(244,235,217,0.14);
  --tile-bg: #f4ebd9;
  --tile-bg-hover: #fff8ea;
  --tile-ink: #241b10;
  --tile-shadow: #a9925c;
  --op-bg: #c9a227;
  --op-bg-hover: #ddb830;
  --op-ink: #241b10;
  --op-shadow: #7c631a;
  --success: #4c9a6a;
  --error: #c1503a;
  --paper: #f4ebd9;
  --muted: #a9c4b3;
}

*{ box-sizing: border-box; }

/* Rapid double-tapping a button (e.g. mashing Backspace) was being
   interpreted as a double-tap-to-zoom gesture on mobile browsers.
   `manipulation` disables that specifically on interactive elements
   while leaving normal pinch-zoom/panning intact everywhere else. */
button{ touch-action: manipulation; }

html, body{
  margin:0;
  padding:0;
  min-height:100vh;
  background:
    radial-gradient(ellipse at top, #17422f 0%, var(--bg) 45%, var(--bg-deep) 100%);
  font-family: 'Inter', system-ui, sans-serif;
  color: var(--paper);
}

body{
  display:flex;
  flex-direction:column;
  align-items:center;
  padding: 48px 20px 80px;
}

header{
  display:flex;
  flex-direction:column;
  align-items:center;
  margin-bottom: 36px;
  position: relative;
}

.eyebrow{
  font-family:'JetBrains Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--op-bg);
  margin-bottom: 10px;
}

h1{
  font-family:'Space Grotesk', sans-serif;
  font-weight:700;
  font-size: clamp(2.2rem, 5vw, 3.4rem);
  margin: 0 0 16px;
  letter-spacing: -0.01em;
}

.help-btn{
  font-family:'JetBrains Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.06em;
  background: transparent;
  border: 1px solid rgba(244,235,217,0.35);
  color: var(--paper);
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  display:inline-flex;
  align-items:center;
  gap: 6px;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.help-btn:hover{ border-color: var(--op-bg); background: rgba(201,162,39,0.08); }
.help-btn .mark{
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--op-bg);
  color: var(--op-ink);
  font-weight: 700;
  font-size: 11px;
  display:flex; align-items:center; justify-content:center;
}

.help-panel{
  margin-top: 14px;
  max-width: 480px;
  background: var(--tape-bg);
  border: 1px solid var(--felt-line);
  border-radius: 10px;
  padding: 16px 18px;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--muted);
}
.help-panel strong{ color: var(--paper); }
.help-panel[hidden]{ display:none; }

.rig{
  width: 100%;
  max-width: 620px;
}

/* ===== Paper tape (equation line) ===== */
.tape-wrap{
  position: relative;
  margin-bottom: 36px;
}

.tape{
  background: var(--tape-bg);
  border-radius: 10px;
  padding: 26px 24px 20px;
  min-height: 74px;
  display:flex;
  align-items:center;
  flex-wrap: wrap;
  box-shadow: inset 0 2px 10px rgba(0,0,0,0.4);
  border: 1px solid var(--felt-line);
  border-bottom: none;
  position: relative;
  cursor: text;
}

.tape-token{
  font-family:'JetBrains Mono', monospace;
  font-weight:600;
  font-size: 26px;
  color: var(--paper);
  padding: 4px 2px;
  line-height:1;
  animation: dropIn 0.18s ease-out;
}

.tape-token.op{ color: var(--op-bg); margin: 0 4px; }

.tape-placeholder{
  font-family:'JetBrains Mono', monospace;
  font-size: 15px;
  color: rgba(244,235,217,0.35);
  letter-spacing: 0.02em;
}

/* Positioned by JS (left/top/height) after every render — never part of
   the flex flow, so moving it can never push tokens around or change
   spacing. See positionCursor() in script.js. */
.cursor{
  position: absolute;
  width: 2px;
  background: var(--paper);
  animation: blink 1.1s step-start infinite;
  pointer-events: none;
}
/* Briefly forces the cursor solid-visible right after a move, so it can
   never happen to land mid-"off" in the blink cycle and look like it
   didn't move at all. */
.cursor.just-moved{
  animation: none;
  opacity: 1;
}

@keyframes blink{ 50%{ opacity: 0; } }
@keyframes dropIn{
  from{ transform: translateY(-6px); opacity:0; }
  to{ transform: translateY(0); opacity:1; }
}

.result-badge{
  position:absolute;
  top: -16px;
  right: -14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size: 22px;
  font-weight:700;
  color: var(--paper);
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
  opacity: 0;
  transform: scale(0.5) rotate(-12deg);
  transition: opacity 0.25s ease, transform 0.25s cubic-bezier(.34,1.56,.64,1);
  pointer-events:none;
}
.result-badge.show{ opacity:1; transform: scale(1) rotate(0deg); }
.result-badge.good{ background: var(--success); }
.result-badge.bad{ background: var(--error); }

.status-line{
  font-family:'JetBrains Mono', monospace;
  font-size: 13px;
  min-height: 18px;
  margin-top: 10px;
  text-align:center;
  color: var(--muted);
}
.status-line.good{ color: var(--success); }
.status-line.bad{ color: #e08d7c; }

/* ===== Tile racks ===== */
.rack{
  margin-bottom: 22px;
}

.rack-label{
  font-family:'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 10px;
  display:block;
}

.tiles{
  display:flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tile{
  font-family:'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 22px;
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: var(--tile-bg);
  color: var(--tile-ink);
  box-shadow: 0 4px 0 var(--tile-shadow), 0 6px 10px rgba(0,0,0,0.35);
  transition: transform 0.08s ease, box-shadow 0.08s ease, opacity 0.2s ease;
}
.tile:hover{ background: var(--tile-bg-hover); }
.tile:active,
.tile.pressed{
  transform: translateY(4px);
  box-shadow: 0 0 0 var(--tile-shadow), 0 2px 4px rgba(0,0,0,0.3);
}

.tile.op{
  background: var(--op-bg);
  box-shadow: 0 4px 0 var(--op-shadow), 0 6px 10px rgba(0,0,0,0.35);
}
.tile.op:hover{ background: var(--op-bg-hover); }
.tile.op:active,
.tile.op.pressed{
  box-shadow: 0 0 0 var(--op-shadow), 0 2px 4px rgba(0,0,0,0.3);
}

.tile.used{
  opacity: 0;
  pointer-events: none;
  transform: scale(0.6);
}

/* ===== Controls ===== */
.controls{
  display:flex;
  gap: 12px;
  margin-top: 30px;
  flex-wrap: wrap;
}

.btn{
  font-family:'Inter', sans-serif;
  font-weight: 600;
  font-size: 14px;
  padding: 13px 22px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}
.btn:active{ transform: translateY(2px); }

.btn-ghost{
  background: transparent;
  border-color: rgba(244,235,217,0.3);
  color: var(--paper);
}
.btn-ghost:hover{ border-color: rgba(244,235,217,0.6); }

.btn-submit{
  background: var(--op-bg);
  color: var(--op-ink);
  margin-left: auto;
}
.btn-submit:hover{ background: var(--op-bg-hover); }
.btn-submit:disabled{
  opacity: 0.4;
  cursor: not-allowed;
}

footer{
  margin-top: 46px;
  font-family:'JetBrains Mono', monospace;
  font-size: 11px;
  color: rgba(169,196,179,0.6);
  text-align:center;
  max-width: 480px;
  line-height:1.6;
}

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

:focus-visible{
  outline: 2px solid var(--op-bg);
  outline-offset: 2px;
}

/* ===== Timer ===== */
.timer-pill{
  position: absolute;
  top: -14px;
  left: 16px;
  background: var(--op-bg);
  color: var(--op-ink);
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 13px;
  padding: 5px 12px;
  border-radius: 999px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.35);
  z-index: 1;
}

/* ===== Achievements panel ===== */
.sheet-backdrop{
  position: fixed;
  inset: 0;
  background: rgba(6, 20, 15, 0.72);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  animation: fadeIn 0.2s ease-out;
}
.sheet-backdrop[hidden]{ display: none; }

.sheet-card{
  background: var(--paper);
  color: var(--tile-ink);
  border-radius: 16px;
  padding: 24px 24px 20px;
  max-width: 420px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: winPop 0.32s cubic-bezier(.34,1.56,.64,1);
}

.sheet-header{
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.sheet-header h2{
  font-family: 'Space Grotesk', sans-serif;
  font-size: 20px;
  margin: 0;
}
.sheet-close{
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--tile-ink);
  padding: 4px 8px;
  border-radius: 6px;
}
.sheet-close:hover{ background: rgba(0,0,0,0.06); }

.sheet-stats{
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}
.stat-chip{
  background: #fff8ea;
  border: 1px solid var(--tile-shadow);
  border-radius: 8px;
  padding: 10px 12px;
}
.stat-chip .stat-value{
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 18px;
  display: block;
}
.stat-chip .stat-label{
  font-size: 11px;
  color: #5a4a30;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.sheet-list{
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.achievement-row{
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--tile-shadow);
  background: #fff8ea;
}
.achievement-row.locked{
  opacity: 0.55;
  background: transparent;
}
.row-icon{ font-size: 18px; line-height: 1.3; }
.row-text{ display: flex; flex-direction: column; text-align: left; }
.row-title{ font-weight: 700; font-size: 13.5px; }
.row-desc{ font-size: 12px; color: #5a4a30; }

.win-card .sheet-close{
  position: absolute;
  top: 10px;
  right: 10px;
}

.win-actions{
  display: flex;
  gap: 10px;
}
.win-actions .btn{
  flex: 1;
  padding: 12px 14px;
  font-size: 13px;
}

.btn-share{
  background: transparent;
  border: 1.5px solid var(--tile-shadow);
  color: var(--tile-ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}
.btn-share:hover{
  background: rgba(201, 162, 39, 0.12);
  border-color: var(--op-bg);
}
.btn-share .share-icon{
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  transition: transform 0.15s ease;
}
.btn-share:hover .share-icon{ transform: scale(1.08); }

.btn-share.copied{
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.solutions-counter{
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  text-align: center;
  color: var(--op-bg);
  margin-top: 4px;
}
.solutions-counter[hidden]{ display: none; }

.toast{
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: var(--paper);
  color: var(--tile-ink);
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 1100;
  animation: toastIn 0.3s ease-out;
}
.toast[hidden]{ display: none; }

@keyframes toastIn{
  from{ opacity: 0; transform: translateX(-50%) translateY(12px); }
  to{ opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Start gate (returning players) ===== */
.rig{
  position: relative;
}

.play-area{
  transition: filter 0.2s ease, opacity 0.2s ease;
}
.play-area.gated{
  filter: blur(6px);
  opacity: 0.5;
  pointer-events: none;
  user-select: none;
}

.start-gate{
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
}
.start-gate[hidden]{ display: none; }

.start-gate-inner{
  background: var(--tape-bg);
  border: 1px solid var(--felt-line);
  border-radius: 14px;
  padding: 32px 28px;
  text-align: center;
  max-width: 320px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.45);
}
.start-gate-emoji{ font-size: 38px; margin-bottom: 8px; }
.start-gate-inner h2{
  font-family: 'Space Grotesk', sans-serif;
  font-size: 22px;
  margin: 0 0 10px;
}
.start-gate-inner p{
  font-size: 13.5px;
  color: var(--muted);
  line-height: 1.6;
  margin: 0 0 20px;
}
.start-gate-inner p strong{ color: var(--paper); }
.start-gate-inner .btn{ width: 100%; }

/* ===== Guided tutorial ===== */
.tutorial-backdrop{
  position: fixed;
  inset: 0;
  background: rgba(6, 20, 15, 0.85);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1200;
  padding: 20px;
  animation: fadeIn 0.2s ease-out;
}
.tutorial-backdrop[hidden]{ display: none; }

.tutorial-card{
  background: var(--bg-deep);
  border: 1px solid var(--felt-line);
  border-radius: 16px;
  padding: 26px 26px 22px;
  max-width: 460px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.55);
  animation: winPop 0.3s cubic-bezier(.34,1.56,.64,1);
}

.tutorial-header{
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.tutorial-step-label{
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--op-bg);
}
.tutorial-skip{
  background: none;
  border: none;
  color: var(--muted);
  font-size: 12px;
  text-decoration: underline;
  cursor: pointer;
  padding: 2px 4px;
}
.tutorial-skip:hover{ color: var(--paper); }

.tutorial-card h2{
  font-family: 'Space Grotesk', sans-serif;
  font-size: 22px;
  margin: 0 0 8px;
  color: var(--paper);
}

.tutorial-instruction{
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--muted);
  margin: 0 0 18px;
}

.tutorial-card .tape-wrap{ margin-bottom: 20px; }
.tutorial-card .rack{ margin-bottom: 16px; }

/* ===== Victory overlay ===== */
.win-backdrop{
  position: fixed;
  inset: 0;
  background: rgba(6, 20, 15, 0.72);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}
.win-backdrop[hidden]{ display: none; }

@keyframes fadeIn{
  from{ opacity: 0; }
  to{ opacity: 1; }
}

#confettiCanvas{
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.win-card{
  position: relative;
  z-index: 1;
  background: var(--paper);
  color: var(--tile-ink);
  border-radius: 16px;
  padding: 40px 44px 32px;
  text-align: center;
  max-width: 340px;
  width: calc(100% - 48px);
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: winPop 0.42s cubic-bezier(.34,1.56,.64,1);
}

@keyframes winPop{
  0%{ transform: scale(0.6) translateY(10px); opacity: 0; }
  100%{ transform: scale(1) translateY(0); opacity: 1; }
}

.win-emoji{
  font-size: 48px;
  line-height: 1;
  margin-bottom: 6px;
}

.win-card h2{
  font-family: 'Space Grotesk', sans-serif;
  font-size: 28px;
  margin: 0 0 10px;
  color: var(--tile-ink);
}

.win-time{
  font-family: 'JetBrains Mono', monospace;
  font-size: 15px;
  color: #5a4a30;
  margin: 0 0 6px;
}
.win-time span{
  font-weight: 700;
  color: var(--tile-ink);
}

.win-streak{
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  color: #5a4a30;
  margin: 0 0 4px;
}

.win-rank{
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  font-weight: 700;
  color: var(--op-shadow);
  margin: 0 0 16px;
  min-height: 1em;
}
.win-rank:empty{ margin-bottom: 0; min-height: 0; }

#solutionsRankText{
  color: var(--muted);
  font-weight: 400;
}

.win-achievements{
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 20px;
}
.win-achievements:empty{ margin-bottom: 0; }

.achievement-badge{
  background: #fff8ea;
  border: 1px solid var(--tile-shadow);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
  text-align: left;
  animation: winPop 0.4s cubic-bezier(.34,1.56,.64,1);
}
.achievement-badge .badge-title{
  font-weight: 700;
  display: block;
  margin-bottom: 2px;
}
.achievement-badge .badge-desc{
  color: #5a4a30;
}

.win-card .btn-submit{
  width: 100%;
  margin-left: 0;
}

/* ===== About page ===== */
.back-link{
  align-self: flex-start;
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
  margin-bottom: 16px;
}
.back-link:hover{ color: var(--paper); text-decoration: underline; }

.about-wrap{
  width: 100%;
  max-width: 620px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.about-section{
  background: var(--tape-bg);
  border: 1px solid var(--felt-line);
  border-radius: 12px;
  padding: 26px 28px;
}

.about-section h2{
  font-family: 'Space Grotesk', sans-serif;
  font-size: 20px;
  margin: 0 0 14px;
  color: var(--paper);
}

.about-section p{
  font-size: 14px;
  line-height: 1.7;
  color: var(--muted);
  margin: 0 0 14px;
}
.about-section p:last-child{ margin-bottom: 0; }

.about-section a{
  color: var(--op-bg);
}
.about-section a:hover{ color: var(--op-bg-hover); }

.footer-link{
  color: inherit;
  text-decoration: underline;
}
.footer-link:hover{ color: var(--paper); }
