/* Threadgrid - styles. Cozy woven-cloth look. Colours: ink #2B1D14, paper #F6EFE0, terracotta #C2542D, gold #E3A412, indigo #1C2C78, green #1E7A3E. */
:root { --ink: #2B1D14; --paper: #F6EFE0; --paper2: #EDE1C8; --terra: #C2542D; --gold: #E3A412; --indigo: #1C2C78; --green: #1E7A3E; --red: #C0392B; --muted: #6d5a49; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--paper); color: var(--ink); overflow: hidden; overscroll-behavior: none; }
body { font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--indigo); outline-offset: 2px; }
[hidden] { display: none !important; }
#app { height: 100%; display: flex; flex-direction: column; background:
  repeating-linear-gradient(0deg, rgba(43,29,20,.035) 0 1px, transparent 1px 4px), repeating-linear-gradient(90deg, rgba(43,29,20,.035) 0 1px, transparent 1px 4px), var(--paper); }

/* header: dark band with a woven stripe */
#top { flex: none; display: flex; align-items: center; gap: 6px; min-height: 52px; padding: 4px 8px; background: var(--ink); color: var(--paper); position: relative; }
#top::after { content: ""; position: absolute; left: 0; right: 0; bottom: -6px; height: 6px; background: repeating-linear-gradient(90deg, var(--gold) 0 14px, var(--terra) 14px 28px, var(--green) 28px 42px, var(--indigo) 42px 56px); z-index: 3; }
#topTitle { flex: 1; font-weight: 800; letter-spacing: .02em; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
.ibtn { width: 44px; height: 44px; border-radius: 12px; border: 2px solid transparent; background: transparent; color: var(--paper); display: inline-flex; align-items: center; justify-content: center; padding: 0; }
.ibtn.txt { font-weight: 800; font-size: 15px; border-color: rgba(246,239,224,.45); }
.ibtn:hover { background: rgba(246,239,224,.12); }
.ibtn.off { opacity: .7; }
#btn-sound.off { color: #e9b8a8; }

#screens { flex: 1; min-height: 0; position: relative; margin-top: 6px; }
.screen { position: absolute; inset: 0; overflow: auto; }

/* buttons */
.btn { min-height: 48px; padding: 0 22px; border-radius: 14px; border: 3px solid var(--ink); background: var(--paper); font-weight: 800; font-size: 17px; box-shadow: 0 3px 0 var(--ink); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.btn.primary { background: var(--gold); }
.btn.ghost { background: transparent; box-shadow: none; border-color: rgba(43,29,20,.5); }
.btn.big { min-height: 60px; font-size: 22px; padding: 0 44px; border-radius: 18px; }
.btn.small { min-height: 44px; font-size: 15px; }
.btn.danger { background: #f3c7bd; border-color: var(--red); color: #6d150d; }
.btn[data-done="1"]::after { content: " \2713"; }
.muted { color: var(--muted); }
.link { background: none; border: 0; padding: 6px 8px; min-height: 44px; font-weight: 700; color: var(--muted); text-decoration: underline; }

/* title */
#s-title { display: flex; align-items: center; justify-content: center; }
.title-wrap { width: min(520px, 100%); padding: 14px 18px 22px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 10px; margin: auto; }
#titleName { margin: 0; font-size: clamp(36px, 9vw, 54px); letter-spacing: .04em; font-weight: 900; line-height: 1.05; }
#titleName::after { content: ""; display: block; height: 8px; margin: 6px auto 0; width: 150px; border-radius: 4px; background: repeating-linear-gradient(90deg, var(--gold) 0 10px, var(--terra) 10px 20px, var(--green) 20px 30px, var(--indigo) 30px 40px); }
#titleTag { margin: 0; font-size: 18px; color: var(--muted); font-weight: 600; }
.demo-card { width: min(300px, 100%); background: var(--paper2); border: 3px solid var(--ink); border-radius: 18px; padding: 10px 10px 8px; box-shadow: 0 4px 0 var(--ink); }
#demo { height: 238px; display: flex; align-items: center; justify-content: center; }
.mini-gauge { margin-top: 6px; }
.mg-track { height: 12px; border: 2px solid var(--ink); border-radius: 8px; background: var(--paper); overflow: hidden; }
#demoFill { height: 100%; width: 0; background: repeating-linear-gradient(90deg, var(--gold) 0 8px, #d09115 8px 10px); transition: width .2s; }
#demoTxt { font-size: 13px; font-weight: 800; margin-top: 3px; }
.title-row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
#titleStreak { margin: 0; min-height: 1.2em; font-weight: 700; }

/* cloth list */
#clothList { width: min(760px, 100%); margin: 0 auto; padding: 10px 14px 28px; }
.cloth { background: rgba(255,255,255,.45); border: 3px solid var(--ink); border-radius: 18px; padding: 12px 12px 14px; margin-bottom: 16px; box-shadow: 0 4px 0 rgba(43,29,20,.25); }
.chead { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.chead h2 { margin: 0; font-size: 22px; font-weight: 900; }
.swatches { display: inline-flex; gap: 4px; }
.swatches .sw { width: 22px; height: 22px; border-radius: 6px; border: 2px solid var(--ink); display: inline-block; }
.cdesc { margin: 4px 0 10px; color: var(--muted); font-size: 14px; }
.lvls { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 10px; }
.lvl { position: relative; min-height: 112px; border-radius: 14px; border: 3px solid var(--ink); background: var(--paper); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 8px 4px 6px; box-shadow: 0 3px 0 var(--ink); }
.lvl .th { width: 60px; height: 60px; border-radius: 6px; border: 2px solid var(--ink); background: var(--paper2); }
.lvl .lsz { font-weight: 800; font-size: 14px; }
.lvl.isdone { background: #fff8e6; }
.lvl.lockd { opacity: .55; cursor: not-allowed; box-shadow: none; }
.lvl .lk { color: var(--muted); }
.lvl .lnew { height: 14px; }
.stars { display: inline-flex; gap: 1px; height: 14px; }
.stars .ic.on, #winStars .ic.on { color: var(--gold); stroke: #8a5d00; }
.stars .ic.off, #winStars .ic.off { color: #d8cdb5; }

/* play layout: portrait = board then controls; wide = board left, controls right */
#s-play { display: grid; grid-template-rows: minmax(0, 1fr) auto; overflow: hidden; }
#stage { position: relative; min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; padding: 6px 4px 2px; overflow: hidden; }
#boardHost { display: flex; align-items: center; justify-content: center; }
#side { padding: 4px 12px 10px; display: flex; flex-direction: column; gap: 6px; max-width: 560px; width: 100%; margin: 0 auto; }
@media (min-aspect-ratio: 5/4) {
  #s-play { grid-template-rows: 1fr; grid-template-columns: minmax(0, 1fr) clamp(250px, 32vw, 340px); }
  #side { align-self: center; padding: 8px 14px; max-width: none; margin: 0; gap: 10px; }
}
#hud { display: flex; align-items: center; gap: 12px; }
#miniWrap { flex: none; width: 56px; height: 56px; border: 3px solid var(--ink); border-radius: 8px; overflow: hidden; background: var(--paper2); display: flex; }
#mini { display: block; width: 100%; height: 100%; image-rendering: pixelated; }
@media (min-aspect-ratio: 5/4) and (min-height: 560px) { #miniWrap { width: 84px; height: 84px; } }
#gauge { flex: 1; min-width: 0; }
.g-label { display: flex; justify-content: space-between; font-weight: 800; font-size: 15px; margin-bottom: 3px; }
.g-track { position: relative; height: 18px; border: 3px solid var(--ink); border-radius: 12px; background: var(--paper2); }
#gFill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; border-radius: 8px; background: repeating-linear-gradient(90deg, var(--gold) 0 8px, #d09115 8px 10px); transition: width .16s ease-out; }
#gShuttle { position: absolute; top: -8px; left: 0; width: 12px; height: 28px; margin-left: -6px; border-radius: 6px; background: var(--ink); transition: left .16s ease-out; box-shadow: 0 0 0 2px var(--paper); }
#gShuttle::after { content: ""; position: absolute; left: 50%; top: 4px; bottom: 4px; width: 2px; margin-left: -1px; background: var(--gold); border-radius: 1px; }
#gTicks i { position: absolute; top: 3px; width: 6px; height: 6px; border-radius: 50%; background: rgba(43,29,20,.25); margin-left: -3px; }
#gTicks i:nth-child(1) { left: 25%; } #gTicks i:nth-child(2) { left: 50%; } #gTicks i:nth-child(3) { left: 75%; } #gTicks i:nth-child(4) { left: 96%; }
#gTicks i.lit { background: var(--paper); }
.bump { animation: bump .28s ease-out; }
@keyframes bump { 0% { transform: scale(1); filter: brightness(1.0); } 40% { transform: scale(1.06, 1.18); filter: brightness(1.35); } 100% { transform: scale(1); filter: brightness(1); } }
.g-sub { min-height: 22px; margin-top: 4px; }
.chip { display: inline-block; font-size: 13px; font-weight: 800; padding: 1px 10px; border-radius: 10px; background: var(--paper2); border: 2px solid rgba(43,29,20,.35); }
.chip.some { background: #f6d7d0; border-color: var(--red); color: #7a1a10; }
.chip.flash { animation: flashr .5s; }
@keyframes flashr { 0%, 60% { background: var(--red); color: #fff; transform: scale(1.12); } 100% { transform: scale(1); } }

#toast { position: absolute; top: 6px; left: 50%; transform: translateX(-50%); z-index: 10; max-width: 94%; text-align: center; background: var(--ink); color: var(--paper); padding: 7px 14px; border-radius: 14px; font-weight: 800; pointer-events: none; box-shadow: 0 3px 10px rgba(0,0,0,.35); }
#toast.flash { animation: capin .3s ease-out; }
#s-play[data-tut="0"] #caption, #s-play[data-tut="0"] #skipTut { display: none; }
#caption { min-height: 46px; padding: 8px 12px; border-radius: 12px; background: var(--paper2); border-left: 6px solid var(--terra); font-weight: 800; font-size: 16px; display: flex; align-items: center; visibility: hidden; }
#caption.on { visibility: visible; }
#caption.flash { animation: capin .35s ease-out; }
@keyframes capin { 0% { transform: translateY(6px); opacity: .2; } 100% { transform: none; opacity: 1; } }

#palette { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.tool { position: relative; width: 66px; height: 70px; border-radius: 14px; border: 3px solid rgba(43,29,20,.45); background: var(--paper); padding: 0; display: flex; align-items: center; justify-content: center; }
.tool.sel { border-color: var(--ink); box-shadow: 0 3px 0 var(--ink); transform: translateY(-3px); background: #fff; }
.tool .tsw { width: 36px; height: 36px; border-radius: 10px; border: 2px solid var(--ink); display: flex; align-items: center; justify-content: center; font-size: 18px; line-height: 1; background: var(--paper2); }
.tool .tsw .shp { font-style: normal; }
html[data-shapes="0"] .tool .tsw .shp { display: none; }
.tkey { position: absolute; left: 4px; top: 1px; font-size: 11px; font-weight: 800; color: var(--muted); }
.tleft { position: absolute; left: 0; right: 0; bottom: 1px; font-size: 14px; font-weight: 900; text-align: center; }
.tool:not(.xtool) .tsw { margin-top: -10px; }
.tool.fin { opacity: .6; } .tool.fin .tleft { color: var(--green); }
.tlab { position: absolute; left: 0; right: 0; bottom: -1px; text-align: center; font-size: 11px; font-weight: 800; color: var(--muted); }
.xtool .tsw { margin-top: -10px; }
.pulse { animation: tpulse .9s ease-in-out infinite; border-color: #ff7a00 !important; }
@keyframes tpulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(255,122,0,.65); } 50% { box-shadow: 0 0 0 9px rgba(255,122,0,0); transform: translateY(-3px) scale(1.07); } }
#actions { display: flex; gap: 10px; justify-content: center; }
.act { min-height: 44px; padding: 0 16px; border-radius: 12px; border: 3px solid var(--ink); background: var(--paper); font-weight: 800; display: inline-flex; align-items: center; gap: 8px; box-shadow: 0 3px 0 var(--ink); }
.act:disabled { opacity: .4; box-shadow: none; cursor: default; }
.act:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

/* board */
.bd { position: relative; margin: auto; font-size: 30px; }
.rows { position: absolute; left: 0; }
.cols { position: absolute; top: 0; display: flex; }
.rc { display: flex; flex-direction: row; justify-content: flex-end; align-items: center; gap: .04em; padding-right: 4px; border-radius: .2em; transition: background .15s; }
.cc { display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: .04em; padding-bottom: 3px; border-radius: .2em; height: 100%; transition: background .15s; }
.cb { position: relative; display: inline-flex; align-items: center; justify-content: center; border-radius: 26%; font-weight: 900; line-height: 1; flex: none; box-shadow: inset 0 0 0 1.5px rgba(0,0,0,.28); }
.cb.zero { background: transparent; color: #8a7a66; box-shadow: none; font-weight: 700; }
.cb .num { font-weight: 900; font-style: normal; }
.cb .shp { display: none; position: absolute; right: -.08em; top: -.1em; font-size: .42em; font-style: normal; line-height: 1; padding: 0 .1em; background: rgba(255,255,255,.88); color: var(--ink); border-radius: 50%; }
.bd.shapes .cb .shp { display: block; }
.rc.done .cb, .cc.done .cb { opacity: .28; filter: grayscale(1); }
.rc.hot, .cc.hot { background: rgba(227,164,18,.32); }
.rc.hint, .cc.hint { background: rgba(255,122,0,.3); animation: hintl 1s ease-in-out infinite; }
@keyframes hintl { 50% { background: rgba(255,122,0,.55); } }
.rc.cflash, .cc.cflash { animation: cflash .9s; }
@keyframes cflash { 0% { background: rgba(30,122,62,.7); } 100% { background: transparent; } }
.cells { position: absolute; display: grid; border: 3px solid var(--ink); border-radius: 6px; overflow: hidden; background: var(--paper2); box-sizing: content-box; margin: -3px 0 0 -3px; }
.cell { position: relative; border-right: 1px solid rgba(43,29,20,.22); border-bottom: 1px solid rgba(43,29,20,.22); display: flex; align-items: center; justify-content: center; font-size: inherit; box-sizing: border-box; }
.cell.gv { border-right: 2px solid rgba(43,29,20,.55); } .cell.gh { border-bottom: 2px solid rgba(43,29,20,.55); }
.cell.f { border-color: rgba(0,0,0,.3); background-image: repeating-linear-gradient(90deg, rgba(0,0,0,.13) 0 1px, transparent 1px .22em), repeating-linear-gradient(0deg, rgba(255,255,255,.16) 0 1px, transparent 1px .3em); }
.cell .shp { font-style: normal; font-size: .52em; line-height: 1; }
.cell.x::before, .cell.x::after, .cell.ax::before, .cell.ax::after { content: ""; position: absolute; left: 24%; right: 24%; top: 50%; height: .09em; margin-top: -.045em; background: var(--ink); border-radius: 2px; transform: rotate(45deg); opacity: .8; }
.cell.x::after, .cell.ax::after { transform: rotate(-45deg); }
.cell.ax::before, .cell.ax::after { opacity: .28; }
.cell.autoin::before, .cell.autoin::after { animation: autoin .35s ease-out; }
@keyframes autoin { from { opacity: 0; transform: rotate(45deg) scaleX(.2); } }
.cell.autoin::after { animation-name: autoin2; } @keyframes autoin2 { from { opacity: 0; transform: rotate(-45deg) scaleX(.2); } }
.cell.pop { animation: pop .24s ease-out; z-index: 1; }
@keyframes pop { 0% { transform: scale(.45); filter: brightness(1.5); } 60% { transform: scale(1.18); } 100% { transform: scale(1); filter: none; } }
.cell.snag { background-color: rgba(192,57,43,.28); }
.cell.snag::after { content: ""; position: absolute; width: .34em; height: .34em; border-radius: 50%; background: var(--red); border: .04em solid #fff; left: 50%; top: 50%; margin: -.17em 0 0 -.17em; }
.cell.shake { animation: shake .42s; z-index: 1; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-.14em); } 40% { transform: translateX(.14em); } 60% { transform: translateX(-.09em); } 80% { transform: translateX(.07em); } }
.cell.glow { animation: glow .62s ease-out both; }
@keyframes glow { 0% { filter: none; transform: scale(1); } 40% { filter: brightness(1.7) saturate(1.3); transform: scale(1.1); z-index: 2; } 100% { filter: none; transform: scale(1); } }
.cell.hl { outline: 3px solid #ff7a00; outline-offset: -3px; animation: hlr 1s ease-in-out infinite; z-index: 3; background-color: rgba(255,122,0,.25); }
@keyframes hlr { 50% { outline-color: #fff; background-color: rgba(255,122,0,.5); } }
.cell.cur { outline: 3px dashed var(--indigo); outline-offset: -3px; z-index: 3; }
.bd.won .rows, .bd.won .cols { opacity: 0; transition: opacity .6s; }
.bd.won .cell { border-color: transparent; transition: border-color .6s; }
.bd.won .cell.ax::before, .bd.won .cell.ax::after, .bd.won .cell.x::before, .bd.won .cell.x::after { opacity: 0; transition: opacity .5s; }
.bd.won .cells { animation: wonb 1.1s ease-out; }
@keyframes wonb { 0% { box-shadow: 0 0 0 0 rgba(227,164,18,.9); } 100% { box-shadow: 0 0 0 28px rgba(227,164,18,0); } }

.hand { position: absolute; left: 0; top: 0; display: none; pointer-events: none; z-index: 6; opacity: 1; }
.hand .dot { position: absolute; inset: 24%; border-radius: 50%; background: #fff; border: 3px solid var(--ink); box-shadow: 0 3px 8px rgba(0,0,0,.45); transition: transform .12s, background .12s; }
.hand.press .dot { transform: scale(.78); background: #ffd66b; }
.hand .ring { position: absolute; inset: 6%; border-radius: 50%; border: 3px solid var(--ink); opacity: .0; }
.hand.press .ring { animation: ripple .9s ease-out infinite; }
@keyframes ripple { 0% { transform: scale(.6); opacity: .8; } 100% { transform: scale(1.25); opacity: 0; } }

/* win overlay */
#win { position: absolute; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; background: rgba(43,29,20,.74); padding: 12px; overflow: auto; }
#winBox { background: var(--paper); border: 4px solid var(--ink); border-radius: 22px; padding: 18px 20px 16px; text-align: center; width: min(380px, 100%); box-shadow: 0 6px 0 rgba(0,0,0,.4); animation: winin .45s cubic-bezier(.2, 1.3, .4, 1); }
@keyframes winin { 0% { transform: scale(.7); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
#winCloth { display: block; margin: 0 auto 8px; border: 4px solid var(--ink); border-radius: 8px; box-shadow: 0 4px 0 var(--ink); image-rendering: pixelated; }
#winTitle { margin: 4px 0 0; font-size: 28px; font-weight: 900; }
#winName { margin: 2px 0 6px; color: var(--muted); font-weight: 700; min-height: 1.3em; }
#winStars { display: flex; justify-content: center; gap: 6px; min-height: 36px; }
#winStars .ic { animation: starin .5s cubic-bezier(.2, 1.6, .4, 1) both; }
@keyframes starin { 0% { transform: scale(0) rotate(-40deg); } 100% { transform: scale(1) rotate(0); } }
#winStats { margin: 6px 0 10px; font-weight: 700; color: var(--muted); min-height: 1.3em; }
#winBtns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* settings sheet */
#sheet { position: fixed; inset: 0; z-index: 50; background: rgba(43,29,20,.72); display: flex; align-items: center; justify-content: center; padding: 14px; overflow: auto; }
#sheetBox { background: var(--paper); border: 4px solid var(--ink); border-radius: 20px; padding: 18px; width: min(420px, 100%); max-height: 100%; overflow: auto; display: flex; flex-direction: column; gap: 10px; }
#sheetTitle { margin: 0; font-size: 24px; font-weight: 900; }
.srow { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-weight: 700; }
.tog { min-width: 96px; min-height: 44px; border-radius: 12px; border: 3px solid var(--ink); background: var(--paper2); font-weight: 800; }
.tog.on { background: var(--gold); }
#sheetBody { display: flex; flex-direction: column; gap: 10px; }
#sheetBody .logo { align-self: center; margin-top: 6px; }
.credits { margin: 0; font-size: 12px; color: var(--muted); text-align: center; line-height: 1.4; }

@media (max-height: 640px) and (max-aspect-ratio: 5/4) { .tool { width: 60px; height: 62px; } #side { gap: 4px; padding-bottom: 6px; } }
/* phone landscape / short screens */
@media (max-height: 480px) {
  #top { min-height: 48px; }
  #stage { padding: 2px; } #side { gap: 5px; padding: 4px 10px; }
  .tool { width: 58px; height: 58px; } .tool .tsw { width: 30px; height: 30px; }
  #caption { min-height: 38px; padding: 4px 10px; font-size: 14px; }
  #miniWrap { width: 48px; height: 48px; } .btn.big { min-height: 52px; }
  #demo { height: 190px; }
}
@media (min-aspect-ratio: 5/4) and (max-height: 520px) {
  .title-wrap { display: grid; grid-template-columns: 1fr 1fr; width: min(820px, 100%); column-gap: 20px; align-items: center; text-align: center; }
  .title-wrap > * { grid-column: 1; justify-self: center; } .title-wrap .demo-card { grid-column: 2; grid-row: 1 / span 5; }
}
html[data-rm="1"] *, html[data-rm="1"] *::before, html[data-rm="1"] *::after { animation-duration: .001s !important; animation-iteration-count: 1 !important; transition-duration: .001s !important; }
