/* Unravel — "Loom". Natural-dye tones on undyed wool; Bitter for letters, Source Sans 3 for everything else. */
:root {
  --bg: #EFECE5;        /* wool */
  --fg: #2B2823;
  --muted: #7B7568;
  --line: #CFC9BC;
  --green: #5E8C5A;     /* moss */
  --yellow: #C9A227;    /* mustard */
  --grey: #A39A8C;      /* flax */
  --ash: #5A554D;
  --on-tile: #FBFAF7;
  --key: #DED9CE;
  --key-fg: #2B2823;
  --key-dead: #C8C2B4;
  --sheet: #F7F5F0;
  --shadow: rgba(43, 40, 35, .45);
  --display: "Bitter", Georgia, "Times New Roman", serif;
  --body: "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #221F1B;      /* peat */
    --fg: #EDE8DE;
    --muted: #9A9385;
    --line: #3D382F;
    --green: #6B9A66;
    --yellow: #D1AC33;
    --grey: #6E675C;
    --ash: #3A3733;
    --on-tile: #F6F2EA;
    --key: #3A352D;
    --key-fg: #EDE8DE;
    --key-dead: #2C2823;
    --sheet: #2A2621;
    --shadow: rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--body);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-user-select: none; user-select: none;
}

/* ---------- header ---------- */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; height: 54px; max-width: 500px; width: 100%; margin: 0 auto;
}
.top h1 {
  margin: 0; font-family: var(--display); font-weight: 500; font-size: 27px; letter-spacing: .08em;
}
.top h1 .tail {
  display: inline-block; font-weight: 300; opacity: .8;
  transform: translateY(3px) rotate(7deg); transform-origin: top left;
}
.icon {
  width: 36px; height: 36px; border-radius: 50%;
  border: 1.5px solid var(--line); background: none; color: var(--muted);
  font-size: 17px; font-weight: 700; cursor: pointer; font-family: var(--body);
}

.tabs { display: flex; border-bottom: 1px solid var(--line); max-width: 500px; width: 100%; margin: 0 auto; }
.tabs button {
  flex: 1; height: 40px; border: 0; background: none; color: var(--muted); font-family: var(--body);
  font-weight: 700; font-size: 13px; letter-spacing: .14em; text-transform: uppercase; cursor: pointer;
  position: relative;
}
.tabs button.on { color: var(--fg); }
.tabs button.on::after { content: ""; position: absolute; left: 24%; right: 24%; bottom: -1px; height: 3px; background: var(--green); border-radius: 2px 2px 0 0; }
.tabs button:focus-visible { outline: 2px solid var(--green); outline-offset: -2px; }

/* ---------- board ---------- */
main {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: safe center; padding: 8px 12px; position: relative; overflow-y: auto; min-height: 0;
}
.input-label { font-size: 10px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); margin-bottom: 5px; }
.input-toggle { display: flex; gap: 2px; padding: 3px; border: 1.5px solid var(--line); border-radius: 999px; margin-bottom: 10px; }
.input-toggle button {
  border: 0; background: none; color: var(--muted); font-family: var(--body); font-weight: 700; font-size: 12px;
  letter-spacing: .06em; text-transform: uppercase; padding: 5px 12px; border-radius: 999px; cursor: pointer;
}
.input-toggle button.on { background: var(--fg); color: var(--bg); }
.input-toggle .beta { font-size: 9px; letter-spacing: .1em; padding: 1px 5px; border-radius: 999px; background: var(--yellow); color: #2B2823; margin-left: 4px; vertical-align: 1px; }
.input-toggle button.on .beta { background: var(--yellow); }
.keyboard[hidden] { display: none; }

/* reels: every tile in the play row is a spinnable letter wheel */
.board.reel-mode { padding-right: 52px; }
.tile.reel { position: relative; overflow: hidden; cursor: ns-resize; touch-action: none; border-style: solid; border-color: var(--line); color: var(--muted); }
.tile.reel.changed { color: var(--fg); border-color: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 30%, transparent); }
.tile.reel .strip { position: absolute; left: 0; right: 0; top: -200%; height: 500%; will-change: transform; }
.tile.reel .strip span { position: relative; display: flex; align-items: center; justify-content: center; height: 20%; text-transform: uppercase; }
.tile.reel .strip span:not(.mid) { opacity: .22; font-size: .8em; }
.tile.reel .strip span.spent::after { content: ""; position: absolute; right: 5px; bottom: 5px; width: 6px; height: 6px; border-radius: 50%; background: var(--yellow); }
.tile.reel::before, .tile.reel::after {
  content: ""; position: absolute; left: 50%; width: 0; height: 0; margin-left: -4px; pointer-events: none; opacity: .45;
  border-left: 4px solid transparent; border-right: 4px solid transparent;
}
.tile.reel::before { top: 3px; border-bottom: 4px solid var(--muted); }
.tile.reel::after { bottom: 3px; border-top: 4px solid var(--muted); }
.tile.reel.tick { animation: tick .12s ease; }
@keyframes tick { 50% { transform: scale(1.04); } }
.row-actions { position: absolute; right: -52px; top: 0; display: flex; flex-direction: column; gap: 6px; }
.row-actions button {
  width: 44px; height: calc((var(--tile) - 6px) / 2 + 3px); border-radius: 8px; border: 1.5px solid var(--line); background: none;
  color: var(--muted); font-size: 18px; cursor: pointer; font-family: var(--body); line-height: 1;
}
.row-actions button:not(.go) { font-size: 22px; font-weight: 400; }
.row-actions button.go { background: var(--green); border-color: var(--green); color: var(--on-tile); }
.row-actions button:disabled { opacity: .3; cursor: default; }

.subtitle { margin: 0 0 12px; color: var(--muted); font-size: 14px; letter-spacing: .06em; font-weight: 600; }

.board { --cols: 5; display: grid; gap: 6px; position: relative; padding-left: 18px; }
.row { display: grid; grid-template-columns: repeat(var(--cols, 5), 1fr); gap: 6px; }
.board { --tile: clamp(46px, 12.5vw, 60px); }
.tile {
  width: var(--tile); height: var(--tile);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--display); font-size: clamp(23px, 6.2vw, 30px); font-weight: 700; text-transform: uppercase;
  border: 2px solid var(--line); border-radius: 9px; color: var(--fg);
  transition: transform .12s ease;
}
.tile.ghost { border-style: dashed; color: var(--muted); cursor: pointer; }
.tile.ghost:hover { border-color: var(--muted); }
.tile.dead { background: var(--ash); border-color: var(--ash); color: var(--on-tile); text-decoration: line-through; text-decoration-thickness: 3px; opacity: .85; }
.tile.selected { border-style: solid; border-color: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 30%, transparent); }
.tile.filled { color: var(--fg); border-color: var(--green); }
.tile.pop { animation: pop .12s ease; }
.tile.green, .tile.grey, .tile.yellow {
  color: var(--on-tile);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.18), inset 0 -2px 0 rgba(0,0,0,.14);
}
.tile.green { background: var(--green); border-color: var(--green); }
.tile.grey { background: var(--grey); border-color: var(--grey); }
.tile.yellow { background: var(--yellow); border-color: var(--yellow); }
.tile.flip { animation: flip .5s ease; }
.row.shake, .tile.shake { animation: shake .4s ease; }
.row.obliterate { animation: obliterate .5s ease-in forwards; }
.row.rewound .tile { animation: rewound .7s ease; }
.row.start { padding-bottom: 10px; margin-bottom: 6px; border-bottom: 2px solid color-mix(in srgb, var(--grey) 70%, transparent); }
.example .row.start, .mini .row.start { padding-bottom: 6px; margin-bottom: 3px; }

/* the thread: runs beside the surviving line, knotted at each row, frays over doomed rows */
.thread { position: absolute; left: 5px; width: 2px; background: var(--grey); opacity: .75; border-radius: 2px; pointer-events: none; }
.fray { position: absolute; left: 5px; width: 2px; pointer-events: none;
  background: repeating-linear-gradient(to bottom, var(--ash) 0 4px, transparent 4px 8px); }
.knot { position: absolute; left: 1px; width: 10px; height: 10px; border-radius: 50%; background: var(--green); border: 2px solid var(--bg); pointer-events: none; margin-top: -5px; }
.knot.cut { background: var(--ash); }

@keyframes pop { 0% { transform: scale(.9);} 50% { transform: scale(1.08);} 100% { transform: scale(1);} }
@keyframes flip { 0% { transform: rotateX(0);} 50% { transform: rotateX(90deg);} 100% { transform: rotateX(0);} }
@keyframes obliterate {
  0%   { transform: translateX(0) scale(1); opacity: 1; filter: blur(0); }
  15%  { transform: translateX(-6px) scale(1.03); }
  30%  { transform: translateX(6px) scale(1.03); }
  45%  { transform: translateX(-4px) scale(1); }
  100% { transform: translateX(0) scale(.6) rotateX(70deg); opacity: 0; filter: blur(3px); }
}
@keyframes rewound {
  0%   { box-shadow: 0 0 0 0 var(--green); }
  40%  { box-shadow: 0 0 0 4px var(--green); }
  100% { box-shadow: 0 0 0 0 transparent; }
}
@keyframes shake {
  10%, 90% { transform: translateX(-2px); } 20%, 80% { transform: translateX(3px); }
  30%, 50%, 70% { transform: translateX(-5px); } 40%, 60% { transform: translateX(5px); }
}
@media (prefers-reduced-motion: reduce) {
  .tile.flip, .tile.pop, .row.shake, .row.rewound .tile { animation: none; }
  .row.obliterate { animation-duration: .01s; }
}

.under { display: flex; align-items: center; gap: 14px; margin-top: 14px; min-height: 30px; padding-left: 18px; }
.moves { color: var(--muted); font-size: 14px; font-weight: 700; letter-spacing: .04em; }
.undo {
  border: 1.5px solid var(--line); background: none; color: var(--muted); font-family: var(--body);
  border-radius: 999px; padding: 5px 12px; font-weight: 700; font-size: 13px; cursor: pointer;
}
.undo:disabled { opacity: .35; cursor: default; }
.undo[hidden] { display: none; }
.undo.reshare { border-color: var(--green); color: var(--green); }

.toast {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 14px; border-radius: 8px;
  font-weight: 700; font-size: 14px; opacity: 0; pointer-events: none;
  transition: opacity .15s; white-space: nowrap; z-index: 30;   /* above the sheets, which are 10 */
}
.toast.show { opacity: 1; }

/* ---------- keyboard ---------- */
.keyboard { padding: 6px 6px 4px; max-width: 500px; width: 100%; margin: 0 auto; }
.krow { display: flex; justify-content: center; gap: 6px; margin-bottom: 8px; }
.key {
  flex: 1; max-width: 43px; height: 56px; border: 0; border-radius: 8px;
  background: var(--key); color: var(--key-fg); font-family: var(--body); font-weight: 700; font-size: 15px;
  text-transform: uppercase; cursor: pointer; padding: 0;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.08);
}
.key.wide { flex: 1.5; max-width: 66px; font-size: 12px; }
.key.spent { position: relative; }
.key.spent::after { content: ""; position: absolute; left: 50%; bottom: 6px; width: 6px; height: 6px; margin-left: -3px; border-radius: 50%; background: var(--yellow); }
.key:active { filter: brightness(.92); }

.foot { text-align: center; font-size: 11px; color: var(--muted); letter-spacing: .04em; padding: 0 12px calc(8px + env(safe-area-inset-bottom)); }
.foot a { color: inherit; text-decoration: none; }
.foot a:hover { text-decoration: underline; }

/* ---------- sheets ---------- */
.modal[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; background: var(--shadow);
  display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10;
}
.sheet {
  background: var(--sheet); color: var(--fg); border-radius: 14px; padding: 22px 22px 18px;
  width: 100%; max-width: 420px; position: relative; box-shadow: 0 10px 34px rgba(0,0,0,.28);
  max-height: calc(100dvh - 32px); overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.sheet h2 { margin: 0 0 10px; font-family: var(--display); font-weight: 500; font-size: 24px; letter-spacing: .01em; }
.sheet p { line-height: 1.45; margin: 8px 0; font-size: 16px; }
.sheet ul { padding-left: 20px; line-height: 1.5; margin: 8px 0; font-size: 16px; }
.sheet .fine { color: var(--muted); font-size: 13px; }
.sw { display: inline-block; width: 13px; height: 13px; border-radius: 3px; vertical-align: -1px; margin-right: 2px; }
.sw.green { background: var(--green); } .sw.yellow { background: var(--yellow); } .sw.grey { background: var(--grey); }
.close {
  position: sticky; top: -12px; float: right; margin: -10px -10px 0 0; border: 0; background: var(--sheet);
  font-size: 26px; color: var(--muted); cursor: pointer; line-height: 1; padding: 4px 8px; border-radius: 8px; z-index: 1;
}
.example, .mini { display: grid; gap: 4px; margin: 14px 0; justify-content: center; }
.example .row, .mini .row { gap: 4px; }
.example .tile, .mini .tile { width: 32px; height: 32px; font-size: 16px; border-width: 2px; border-radius: 6px; }
.example { margin: 10px 0; }
.tutorial { margin: 10px 0 14px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.tut-replay { display: block; margin: 6px auto 0; border: 1.5px solid var(--line); background: none; color: var(--muted); font-family: var(--body); font-weight: 700; font-size: 13px; border-radius: 999px; padding: 5px 12px; cursor: pointer; }
.tut-replay[hidden] { display: none; }
.shortest-label { text-align: center; color: var(--muted); font-size: 13px; font-weight: 700; letter-spacing: .04em; margin: 12px 0 4px; }
.tut-cap { text-align: center; font-size: 15px; min-height: 2.6em; margin: 8px 0 0; line-height: 1.3; }
.mini .tile.ghost { color: var(--muted); }

.share-text {
  width: 100%; height: 160px; margin-top: 10px; padding: 8px; box-sizing: border-box;
  font-family: ui-monospace, Menlo, monospace; font-size: 13px; line-height: 1.35;
  background: var(--bg); color: var(--fg); border: 1.5px solid var(--line); border-radius: 8px; resize: none;
}
.share-text[hidden] { display: none; }

.tip { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-top: 12px; }
.tip[hidden] { display: none; }
.tip-btn { display: inline-block; border: 1.5px solid var(--line); background: none; color: var(--fg); font-family: var(--body); font-weight: 700; font-size: 14px; border-radius: 999px; padding: 7px 16px; cursor: pointer; text-decoration: none; }
.tip-btn.done { color: var(--muted); cursor: default; pointer-events: none; }
.tip-note { font-size: 12px; color: var(--muted); }
.sheet .fine a { color: var(--green); font-weight: 700; text-decoration: none; }

.stats { display: flex; flex-direction: column; margin: 14px 0 4px; }
.stats > .stat-row { display: flex; justify-content: center; gap: 22px; }
.mode-h { font-family: var(--display); font-weight: 500; font-size: 18px; margin: 14px 0 2px; padding-top: 12px; border-top: 1px solid var(--line); }
.mode-h:first-child { border-top: 0; padding-top: 0; margin-top: 4px; }
.mode-h span { font-family: var(--body); font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-left: 6px; }
.dist { display: grid; gap: 4px; margin: 10px 0 4px; }
.dr { display: grid; grid-template-columns: 78px 1fr; align-items: center; gap: 8px; font-size: 13px; }
.dl { color: var(--muted); font-weight: 700; letter-spacing: .04em; text-align: right; }
.db { background: var(--grey); color: var(--on-tile); font-weight: 700; font-variant-numeric: tabular-nums; padding: 3px 7px; border-radius: 4px; text-align: right; box-sizing: border-box; min-width: 24px; }
.dr.hl .db { background: var(--green); }
.stat { text-align: center; }
.stat b { display: block; font-family: var(--display); font-weight: 500; font-size: 27px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }
.actions { display: flex; gap: 10px; margin-top: 14px; }
.actions button {
  flex: 1; height: 46px; border-radius: 10px; border: 2px solid var(--line); font-family: var(--body);
  background: none; color: var(--fg); font-weight: 700; font-size: 15px; cursor: pointer;
}
.actions button.primary { background: var(--green); border-color: var(--green); color: var(--on-tile); }
