/* Loose Ends — a wooden frame on linen. The board is one full-screen canvas;
   the bars float over the cloth as frosted paper. Light and dark follow the
   system unless the player picks one (html[data-theme]). */

:root {
  color-scheme: light;
  --cloth: #ebe4d8;
  --ink: #2c1e12;
  --muted: #6c5644;
  --paper: #fbf7f0;
  --paper-2: #f2ebdf;
  --glass: rgba(251, 247, 240, 0.8);
  --line: rgba(70, 45, 20, 0.14);
  --accent: #a9482a;
  --accent-soft: rgba(169, 72, 42, 0.12);
  --ca-accent: #a9482a; /* the accounts SDK's sheets and chip (white on it 5.8:1) */
  --star: #e0a226;
  --star-off: rgba(70, 45, 20, 0.17);
  --shadow: 0 12px 34px -14px rgba(60, 35, 10, 0.42), 0 2px 6px -2px rgba(60, 35, 10, 0.16);
  --backdrop: rgba(30, 18, 6, 0.36);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --ease-out: cubic-bezier(0.2, 0.9, 0.25, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --cloth: #1d2128;
    --ink: #f2e8da;
    --muted: #c3b29c;
    --paper: #262a32;
    --paper-2: #2f343d;
    --glass: rgba(36, 40, 48, 0.78);
    --line: rgba(255, 255, 255, 0.11);
    --accent: #b5532f;
    --accent-soft: rgba(229, 140, 100, 0.16);
    --ca-accent: #b5532f;
    --star: #f0b640;
    --star-off: rgba(255, 255, 255, 0.16);
    --shadow: 0 14px 36px -14px rgba(0, 0, 0, 0.75), 0 2px 6px -2px rgba(0, 0, 0, 0.4);
    --backdrop: rgba(4, 6, 10, 0.55);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --cloth: #1d2128;
  --ink: #f2e8da;
  --muted: #c3b29c;
  --paper: #262a32;
  --paper-2: #2f343d;
  --glass: rgba(36, 40, 48, 0.78);
  --line: rgba(255, 255, 255, 0.11);
  --accent: #b5532f;
  --accent-soft: rgba(229, 140, 100, 0.16);
  --ca-accent: #b5532f;
  --star: #f0b640;
  --star-off: rgba(255, 255, 255, 0.16);
  --shadow: 0 14px 36px -14px rgba(0, 0, 0, 0.75), 0 2px 6px -2px rgba(0, 0, 0, 0.4);
  --backdrop: rgba(4, 6, 10, 0.55);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; height: 100%; }
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--cloth);
  color: var(--ink);
  font: 16px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }

/* ------------------------------------------------------------ the board */
.board {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  outline: none;
  transition: opacity 0.22s ease;
}
.board.swap { opacity: 0; }
.board:focus-visible { outline: 3px solid var(--accent); outline-offset: -8px; border-radius: 22px; }

/* ------------------------------------------------------------ the bars */
.bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) calc(env(safe-area-inset-right) + 12px) 8px calc(env(safe-area-inset-left) + 12px);
  pointer-events: none;
  z-index: 2;
}
.bar > * { pointer-events: auto; }
.bar-tools { display: flex; align-items: center; gap: 8px; }
.pill, .icon-btn, .dock, .coach, .toast {
  background: var(--glass);
  -webkit-backdrop-filter: blur(12px) saturate(1.15);
  backdrop-filter: blur(12px) saturate(1.15);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 44px;
  padding: 0 12px 0 16px;
  border-radius: 999px;
  color: var(--ink);
  transition: transform 0.18s var(--ease-out);
}
.pill:active { transform: scale(0.96); }
.pill-k { font-size: 13px; font-weight: 650; color: var(--muted); letter-spacing: 0.01em; }
.pill-n { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; min-width: 1.2ch; text-align: center; }
.pill-chev { width: 18px; height: 18px; color: var(--muted); }
.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--ink);
  transition: transform 0.18s var(--ease-out);
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 22px; height: 22px; }
.chip-slot { min-width: 36px; display: grid; place-items: center; }

.dock {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  transform: translateX(-50%);
  width: min(520px, calc(100vw - 20px - env(safe-area-inset-left) - env(safe-area-inset-right)));
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  align-items: center;
  gap: 2px;
  padding: 6px;
  border-radius: 24px;
  z-index: 2;
}
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 66px;
  height: 54px;
  padding: 0 8px;
  border: 0;
  border-radius: 18px;
  background: transparent;
  color: var(--ink);
  transition: background 0.18s ease, transform 0.18s var(--ease-out), opacity 0.2s ease;
}
.tool svg { width: 23px; height: 23px; }
.tool span { font-size: 11.5px; font-weight: 650; line-height: 1.1; white-space: nowrap; }
.tool:active:not(:disabled) { transform: scale(0.93); background: var(--accent-soft); }
.tool:disabled { opacity: 0.36; cursor: default; }
.tool.sure { color: #fff; background: var(--accent); }
.score {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-width: 0;
}
.score-n { display: flex; flex-direction: column; align-items: center; line-height: 1; min-width: 40px; }
.score-n b { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.score-n small { margin-top: 4px; font-size: 11px; font-weight: 650; color: var(--muted); }
.score-n b.bump { animation: bump 0.32s var(--spring); }
.hud-stars { display: flex; gap: 3px; }
.hud-stars i, .result-stars i, .lv-stars i {
  display: block;
  width: 15px;
  height: 15px;
  background: var(--star);
  clip-path: polygon(50% 2%, 62% 36%, 98% 37%, 69% 59%, 80% 94%, 50% 73%, 20% 94%, 31% 59%, 2% 37%, 38% 36%);
  transition: background 0.3s ease, transform 0.3s var(--spring);
}
.hud-stars i.off, .result-stars i.off, .lv-stars i.off { background: var(--star-off); }
.hud-stars i.lost { animation: lose 0.42s ease; }

/* landscape phones: the dock becomes a rail on the right */
@media (orientation: landscape) and (max-height: 540px) {
  .bar { padding-top: calc(env(safe-area-inset-top) + 6px); }
  .dock {
    left: auto;
    right: calc(env(safe-area-inset-right) + 10px);
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    width: auto;
    grid-template-columns: auto;
    grid-auto-flow: row;
    gap: 0;
    padding: 6px 4px;
  }
  .score { flex-direction: column; gap: 6px; padding: 6px 0; }
  .tool { min-width: 64px; height: 50px; }
}

/* ------------------------------------------------------------ coach and toast */
.coach {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 86px);
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 32px));
  margin: 0;
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  z-index: 2;
  animation: rise 0.38s var(--ease-out);
}
.coach p { margin: 0; }
.coach[hidden] { display: none; }
.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 64px);
  transform: translate(-50%, -8px);
  width: max-content;
  max-width: min(360px, calc(100vw - 32px));
  padding: 10px 18px;
  border-radius: 18px;
  text-align: center;
  font-size: 14px;
  font-weight: 650;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.3s var(--ease-out);
  z-index: 3;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (orientation: landscape) and (max-height: 540px) {
  .coach { bottom: calc(env(safe-area-inset-bottom) + 12px); width: min(420px, calc(100vw - 200px)); }
}

/* ------------------------------------------------------------ sheets */
.sheet {
  border: 0;
  padding: 0;
  margin: auto auto 0;
  width: 100%;
  max-width: 560px;
  max-height: min(88dvh, 860px);
  border-radius: 26px 26px 0 0;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 -20px 60px -20px rgba(30, 18, 6, 0.5);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.sheet:not([open]) { display: none; }
.sheet[open] { animation: sheet-up 0.36s var(--ease-out); }
.sheet.closing { animation: sheet-down 0.24s ease forwards; }
.sheet::backdrop { background: var(--backdrop); -webkit-backdrop-filter: blur(5px); backdrop-filter: blur(5px); }
.sheet[open]::backdrop { animation: fade 0.3s ease; }
@media (min-width: 720px) and (min-height: 600px) {
  .sheet { margin: auto; border-radius: 26px; box-shadow: var(--shadow), 0 30px 80px -30px rgba(30, 18, 6, 0.5); }
  .sheet[open] { animation: sheet-pop 0.32s var(--ease-out); }
  .sheet.closing { animation: sheet-out 0.2s ease forwards; }
}
.sheet-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 20px 10px 22px;
}
.sheet-head h2 { margin: 0; font-size: 23px; font-weight: 800; letter-spacing: -0.015em; }
.sheet-head p { margin: 2px 0 0; font-size: 14px; font-weight: 600; }
.close {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0;
  display: grid;
  place-items: center;
  background: var(--paper-2);
  color: var(--muted);
}
.close svg { width: 20px; height: 20px; }
.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 4px 22px calc(22px + env(safe-area-inset-bottom));
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 56px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.row.last { border-bottom: 0; }
.row .two { display: flex; flex-direction: column; }
.note { font-size: 12.5px; font-weight: 500; color: var(--muted); }
.row select {
  min-height: 40px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--paper-2);
  font-weight: 600;
}
.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  position: relative;
  width: 50px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: var(--star-off);
  transition: background 0.2s ease;
  cursor: pointer;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.24s var(--spring);
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(20px); }
.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border-radius: 13px;
  background: var(--paper-2);
  border: 1px solid var(--line);
}
.seg button {
  border: 0;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  background: transparent;
  font-size: 14px;
  font-weight: 650;
  color: var(--muted);
  transition: background 0.18s ease, color 0.18s ease;
}
.seg button[aria-pressed="true"] { background: var(--paper); color: var(--ink); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }
.seg.small button { padding: 0 10px; font-size: 13px; }
.link-btn { border: 0; background: none; padding: 8px 0; color: var(--accent); font-weight: 700; }
.version { font-size: 13px; color: var(--muted); font-weight: 500; }

.btn {
  min-height: 48px;
  padding: 0 18px;
  border-radius: 15px;
  border: 0;
  font-weight: 750;
  font-size: 16px;
  transition: transform 0.16s var(--ease-out), background 0.2s ease;
}
.btn:active { transform: scale(0.96); }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 8px 20px -10px var(--accent); }
.btn.ghost { background: var(--paper-2); color: var(--ink); }

/* ------------------------------------------------------------ levels */
.chapter { margin: 6px 0 18px; }
.chapter h3 { display: flex; align-items: center; gap: 9px; margin: 0 0 10px; font-size: 15px; font-weight: 750; }
.chapter h3 small { margin-left: auto; font-size: 12.5px; font-weight: 650; color: var(--muted); }
.swatch { width: 18px; height: 18px; border-radius: 6px; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18); }
.w-oak { background: linear-gradient(135deg, #ddb780, #a0703f); }
.w-walnut { background: linear-gradient(135deg, #9a6c4b, #553420); }
.w-cherry { background: linear-gradient(135deg, #cf8a5c, #8a4527); }
.w-maple { background: linear-gradient(135deg, #f0dcb7, #c4a16d); }
.w-ash { background: linear-gradient(135deg, #e8d8ba, #b29a72); }
.w-teak { background: linear-gradient(135deg, #c28e56, #7f5428); }
.w-olive { background: linear-gradient(135deg, #d9c08c, #94784a); }
.w-elm { background: linear-gradient(135deg, #cfa57a, #8e6440); }
.lv-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; }
@media (min-width: 520px) { .lv-grid { grid-template-columns: repeat(10, minmax(0, 1fr)); } }
.lv {
  position: relative;
  aspect-ratio: 1;
  min-height: 48px;
  border-radius: 15px;
  border: 1px solid var(--line);
  background: var(--paper-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  font-weight: 800;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  transition: transform 0.16s var(--ease-out);
}
.lv:active:not(:disabled) { transform: scale(0.93); }
.lv.cur { box-shadow: 0 0 0 2.5px var(--accent); }
.lv.done { background: var(--paper); }
.lv:disabled { opacity: 0.38; cursor: default; }
.lv .lock { position: absolute; right: 6px; top: 6px; width: 11px; height: 11px; color: var(--muted); }
.lv-stars { display: flex; gap: 1px; }
.lv-stars i { width: 9px; height: 9px; }
.friends { margin: 8px 0 4px; padding-top: 16px; border-top: 1px solid var(--line); }
.friends h3 { margin: 0 0 10px; font-size: 15px; font-weight: 750; }
.friends-board { min-height: 120px; margin-top: 12px; }

/* ------------------------------------------------------------ help */
.help .rule { display: grid; grid-template-columns: 112px minmax(0, 1fr); gap: 14px; align-items: start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.help .rule.plain { grid-template-columns: minmax(0, 1fr); }
.help h3 { margin: 0 0 3px; font-size: 16px; font-weight: 750; }
.help p { margin: 0; font-size: 14.5px; color: var(--muted); font-weight: 500; }
.help .version { margin-top: 16px; }
.fig { width: 112px; height: auto; display: block; }
.fig-wood { fill: #c99a5f; stroke: #8c5f33; stroke-width: 1; }
.fig-cloth { fill: var(--cloth); }
.fig-rope { fill: none; stroke-width: 4.5; stroke-linecap: round; }
.fig-rope.r1 { stroke: #d8433a; }
.fig-rope.r2 { stroke: #2f72d8; }
.fig-rope.r3 { stroke: #ecb42a; }
.fig-rope.gone { opacity: 0.32; stroke-dasharray: 3 4; }
.fig-gap { stroke: none; }
.fig-peg { stroke: rgba(0, 0, 0, 0.35); stroke-width: 1; }
.fig-peg.r1 { fill: #d8433a; }
.fig-peg.r2 { fill: #2f72d8; }
.fig-peg.r3 { fill: #ecb42a; }
.fig-peg.gone { opacity: 0.3; }
.fig-peg.up { filter: drop-shadow(2px 4px 2px rgba(0, 0, 0, 0.35)); }
.fig-hole { fill: #2a170b; stroke: rgba(255, 255, 255, 0.7); stroke-width: 1.4; stroke-dasharray: 2 2; }
.fig-arrow { fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.fig-cross { fill: none; stroke: #c0392b; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------------------------------------ result */
.result { text-align: center; padding: 26px 22px calc(22px + env(safe-area-inset-bottom)); }
.result-stars { display: flex; justify-content: center; gap: 10px; margin: 4px 0 12px; }
.result-stars i { width: 46px; height: 46px; transform: scale(0.3); opacity: 0; }
.result[open] .result-stars i { animation: star-pop 0.5s var(--spring) forwards; }
.result[open] .result-stars i:nth-child(2) { animation-delay: 0.14s; }
.result[open] .result-stars i:nth-child(3) { animation-delay: 0.28s; }
.result h2 { margin: 0; font-size: 27px; font-weight: 850; letter-spacing: -0.02em; }
.result-line { margin: 6px 0 0; font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.result-sub { margin: 4px 0 18px; font-size: 14px; font-weight: 600; min-height: 1.4em; }
.result-sub .best { color: var(--accent); }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.result-actions .primary { order: -1; flex: 1 1 100%; }
.result-actions .ghost { flex: 1 1 0; }
@media (min-width: 520px) {
  .result-actions .primary { order: 0; flex: 1.4 1 0; }
}

/* ------------------------------------------------------------ motion */
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@keyframes sheet-pop { from { transform: translateY(14px) scale(0.97); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes sheet-out { to { transform: translateY(10px) scale(0.98); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 10px); } }
@keyframes bump { 40% { transform: scale(1.25); } }
@keyframes lose { 30% { transform: scale(1.3) rotate(-12deg); } 100% { transform: none; } }
@keyframes star-pop { 0% { transform: scale(0.3) rotate(-30deg); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-delay: 0ms !important; transition-duration: 0.01ms !important; }
  .result-stars i { transform: none; opacity: 1; }
}
:root.reduce *, :root.reduce *::before, :root.reduce *::after { animation-duration: 0.01ms !important; animation-delay: 0ms !important; transition-duration: 0.01ms !important; }
:root.reduce .result-stars i { transform: none; opacity: 1; }
