/* Not Even Tired. Felt and thread at midnight: interface pieces are patches sewn on, with a running stitch and an
   uneven corner, never glass or cards with soft shadows. Fallback fonts cover scripts the bundled faces do not. */
@font-face { font-family: 'Londrina Solid'; font-weight: 900; src: url(fonts/londrina-solid-latin-900-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Mali'; font-weight: 500; src: url(fonts/mali-latin-500-normal.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Mali'; font-weight: 700; src: url(fonts/mali-latin-700-normal.woff2) format('woff2'); font-display: swap; }

:root {
  --night: #241a6b; --deep: #160e48; --quilt: #332a93; --plum: #5a3fb8; --moon: #ffd84d; --rose: #ff6f91; --teal: #3fd6c5; --wool: #fff4dc; --ink: #1a1147;
  --thread: rgba(255, 244, 220, .62);
  --display: 'Londrina Solid', 'Arial Black', 'Noto Sans', system-ui, sans-serif;
  --body: 'Mali', 'Trebuchet MS', 'Noto Sans', system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px); --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--deep); color: var(--wool); font-family: var(--body); font-weight: 500; overflow: hidden; overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--moon); outline-offset: 3px; }

/* A sewn-on patch. The stitch is the border; the corner radii are deliberately not all the same. */
.patch, .tab, .card, .sheet, .icon, .row-patch { position: relative; }
.patch { display: block; width: 100%; text-align: left; padding: 12px 18px 13px; background: var(--quilt); color: var(--wool); border-radius: 12px 16px 13px 18px; box-shadow: 0 3px 0 var(--deep); }
.patch::after, .card::after, .row-patch::after { content: ''; position: absolute; inset: 5px; border: 2px dashed var(--thread); border-radius: inherit; pointer-events: none; }
.patch .t { display: block; font-family: var(--display); font-weight: 900; font-size: 26px; line-height: 1.05; }
.patch .s { display: block; font-size: 14px; line-height: 1.25; margin-top: 3px; opacity: .92; }
.patch.big { background: var(--moon); color: var(--ink); padding: 16px 22px 18px; }
.patch.big::after { border-color: rgba(26, 17, 71, .5); }
.patch.big .t { font-size: 42px; }
.patch.moon { background: var(--rose); color: var(--ink); }
.patch.moon::after { border-color: rgba(26, 17, 71, .45); }
.patch.hot { background: var(--teal); color: var(--ink); }
.patch.hot::after { border-color: rgba(26, 17, 71, .45); }
.patch:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--deep); }
.tab { padding: 8px 13px; min-height: 42px; background: var(--deep); color: var(--wool); border-radius: 10px 13px 10px 12px; font-weight: 700; font-size: 15px; border: 2px dashed rgba(255, 244, 220, .36); line-height: 1.15; }
.tab[aria-pressed="true"] { background: var(--moon); color: var(--ink); border-color: rgba(26, 17, 71, .5); }
.tab:disabled { opacity: .5; cursor: default; }
.icon { position: fixed; width: 44px; height: 44px; padding: 9px; background: rgba(22, 14, 72, .82); color: var(--wool); border-radius: 12px 14px 11px 13px; border: 2px dashed rgba(255, 244, 220, .4); }
.icon svg { display: block; width: 100%; height: 100%; fill: currentColor; }
#btnMute { z-index: 40; right: calc(10px + var(--safe-r)); bottom: calc(10px + var(--safe-b)); }
#btnMute .off { display: none; } #btnMute.muted .off { display: inline; } #btnMute.muted .on { display: none; }
body.playing #btnMute { bottom: auto; top: calc(62px + var(--safe-t)); }
body.leveling #btnMute { display: none; }
body.sheetopen #btnMute { bottom: auto; top: calc(8px + var(--safe-t)); }

/* HUD: the thread being sewn across the top is your dream sand. */
#hud { position: fixed; inset: 0 0 auto 0; z-index: 10; pointer-events: none; padding: var(--safe-t) var(--safe-r) 0 var(--safe-l); }
#thread { height: 7px; background: rgba(10, 6, 40, .72); }
#threadFill { height: 100%; width: 0; background: repeating-linear-gradient(90deg, var(--moon) 0 10px, transparent 10px 15px); transition: width .12s linear; }
#hudRow { display: flex; align-items: flex-start; gap: 10px; padding: 6px 10px 0; }
#hudLeft { flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 7px; }
#eyes { display: flex; gap: 2px; flex: none; }
.eye { display: block; width: 22px; height: 22px; border-radius: 50%; background: #fffdf6; position: relative; overflow: hidden; box-shadow: 0 0 0 2px var(--ink); }
.eye::before { content: ''; position: absolute; left: 50%; top: 50%; width: 8px; height: 8px; margin: -4px; background: var(--ink); border-radius: 50%; }
.eye i { position: absolute; left: -2px; right: -2px; top: -2px; height: 110%; background: var(--plum); border-bottom: 2px solid var(--ink); transform: translateY(-100%); transition: transform .15s; }
#awakeWrap { flex: 1 1 auto; max-width: 180px; min-width: 40px; height: 12px; background: rgba(10, 6, 40, .72); border-radius: 6px 8px 6px 7px; overflow: hidden; box-shadow: 0 0 0 2px var(--ink); }
#awakeFill { height: 100%; width: 100%; background: var(--teal); transform-origin: left; }
#awakeFill.low { background: var(--rose); }
#hudMid { flex: none; text-align: center; font-family: var(--display); font-weight: 900; text-shadow: 0 2px 0 var(--ink); line-height: 1; }
#hudClock { font-size: 24px; white-space: nowrap; }
#hudQuiet { font-family: var(--body); font-weight: 700; font-size: 13px; color: var(--moon); margin-top: 3px; min-height: 16px; }
#hudRight { flex: 1 1 0; min-width: 0; text-align: right; text-shadow: 0 2px 0 var(--ink); line-height: 1; padding-right: 48px; }
#hudTucked { font-family: var(--display); font-weight: 900; font-size: 24px; }
#hudLevel, #hudSheep { font-size: 13px; font-weight: 700; margin-top: 3px; }
#btnPause { pointer-events: auto; top: calc(14px + var(--safe-t)); right: calc(10px + var(--safe-r)); }
#bossBar { margin: 6px auto 0; width: min(70%, 420px); text-align: center; }
#bossName { font-family: var(--display); font-weight: 900; font-size: 17px; text-shadow: 0 2px 0 var(--ink); }
#bossTrack { height: 9px; margin-top: 2px; border-radius: 5px; background: rgba(10, 6, 40, .72); box-shadow: 0 0 0 2px var(--ink); overflow: hidden; }
#bossFill { height: 100%; background: var(--rose); transform-origin: left; }

#hint { position: fixed; left: 50%; bottom: calc(14% + var(--safe-b)); transform: translateX(-50%) rotate(-1deg); z-index: 12; width: max-content; max-width: min(86vw, 460px); padding: 10px 16px; background: var(--wool); color: var(--ink); border-radius: 12px 15px 11px 14px; font-weight: 700; text-align: center; font-size: 16px; line-height: 1.25; pointer-events: none; }
#banner { position: fixed; left: 50%; top: 22%; transform: translateX(-50%); z-index: 12; width: max-content; max-width: min(92vw, 560px); text-align: center; pointer-events: none; text-shadow: 0 3px 0 var(--ink), 0 0 18px rgba(26, 17, 71, .85); animation: stamp .28s cubic-bezier(.2, 1.6, .4, 1) both; }
#bannerT { font-family: var(--display); font-weight: 900; font-size: clamp(30px, 8vw, 52px); line-height: 1.02; color: var(--moon); }
#bannerS { font-size: clamp(15px, 3.4vw, 19px); font-weight: 700; line-height: 1.22; margin-top: 4px; }
@keyframes stamp { from { transform: translateX(-50%) scale(1.5) rotate(3deg); opacity: 0; } to { transform: translateX(-50%) scale(1) rotate(-1deg); opacity: 1; } }

/* Menu: the title is cut out of felt, a letter at a time, over a live night. */
#menu { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; justify-content: center; gap: clamp(10px, 3vh, 26px); padding: calc(12px + var(--safe-t)) calc(16px + var(--safe-r)) calc(12px + var(--safe-b)) calc(16px + var(--safe-l)); overflow-y: auto; background: linear-gradient(180deg, rgba(22, 14, 72, .25), rgba(22, 14, 72, .55) 40%, rgba(22, 14, 72, .88)); }
.menu-head { max-width: 560px; width: 100%; margin: 0 auto; }
#logo { margin: 0; font-family: var(--display); font-weight: 900; font-size: clamp(54px, 17.5vw, 104px); line-height: .9; letter-spacing: .005em; }
#logo .w { display: inline-block; white-space: nowrap; margin-right: .22em; }
#logo .l { display: inline-block; text-shadow: 0 .05em 0 rgba(10, 6, 40, .85); }
#tagline { margin: 10px 0 0; font-size: clamp(16px, 4.2vw, 21px); font-weight: 700; line-height: 1.22; max-width: 26em; text-shadow: 0 2px 0 var(--ink); }
.menu-actions { display: flex; flex-direction: column; gap: 10px; max-width: 560px; width: 100%; margin: 0 auto; }
#menuLinks { display: flex; flex-wrap: wrap; gap: 8px; }
#menuLinks .tab { flex: 1 1 40%; }
#stats, #byline, #langRow { margin: 0; font-size: 13.5px; line-height: 1.3; text-shadow: 0 1px 0 var(--ink); }
#byline { opacity: .82; }
#langRow { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
#langRow .tab { min-height: 36px; padding: 5px 10px; font-size: 14px; }

/* Level-up: three quilt patches. */
#level { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) calc(10px + var(--safe-b)) calc(12px + var(--safe-l)); background: rgba(22, 14, 72, .62); overflow-y: auto; }
.level-box { width: min(100%, 880px); margin: auto; }
#lTitle { margin: 0 0 10px; text-align: center; font-family: var(--display); font-weight: 900; font-size: clamp(30px, 8.5vw, 46px); line-height: 1; color: var(--moon); text-shadow: 0 3px 0 var(--ink); }
#lCards { display: grid; grid-template-columns: 1fr; gap: 10px; }
.card { display: grid; grid-template-columns: 66px 1fr; gap: 2px 12px; align-items: center; text-align: left; width: 100%; padding: 13px 14px; background: var(--wool); color: var(--ink); border-radius: 14px 18px 13px 16px; box-shadow: 0 4px 0 var(--deep); min-height: 96px; }
.card::after { border-color: rgba(26, 17, 71, .38); }
.card canvas { grid-row: 1 / span 3; width: 66px; height: 66px; display: block; }
.card .n { font-family: var(--display); font-weight: 900; font-size: 24px; line-height: 1.02; }
.card .d { font-size: 14.5px; line-height: 1.22; }
.card .m { font-size: 13px; font-weight: 700; display: flex; flex-wrap: wrap; gap: 3px 10px; align-items: center; margin-top: 2px; }
.card .pips { display: inline-flex; gap: 3px; }
.card .pip { width: 11px; height: 11px; border-radius: 50%; border: 2px solid var(--ink); }
.card .pip.f { background: var(--ink); }
.card .pip.n { background: var(--rose); border-color: var(--rose); animation: pip 0.7s ease-in-out infinite alternate; }
@keyframes pip { to { transform: scale(1.25); } }
.card .mx { background: var(--rose); color: var(--ink); padding: 1px 8px 2px; border-radius: 7px 9px 7px 8px; }
.card .kk { opacity: .72; }
.card:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--deep); }
#level.armed .card { cursor: pointer; }
#level:not(.armed) .card { filter: saturate(.7); }
#lFoot { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; justify-content: center; margin-top: 10px; }
#lFair { margin: 0; flex: 1 1 260px; font-size: 13px; line-height: 1.25; text-shadow: 0 1px 0 var(--ink); max-width: 60ch; }

/* Sheets for everything else. */
#panel { position: fixed; inset: 0; z-index: 35; display: flex; align-items: center; justify-content: center; padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) calc(10px + var(--safe-b)) calc(12px + var(--safe-l)); background: rgba(22, 14, 72, .78); overflow-y: auto; }
.sheet { width: min(100%, 620px); margin: auto; padding: 18px 18px 16px; background: var(--night); border-radius: 18px 22px 16px 20px; box-shadow: 0 5px 0 var(--deep); border: 2px dashed var(--thread); }
#pTitle { margin: 0 0 10px; font-family: var(--display); font-weight: 900; font-size: clamp(30px, 8.5vw, 44px); line-height: 1; color: var(--moon); }
#pBody { font-size: 15.5px; line-height: 1.32; }
#pBody p { margin: 0 0 9px; max-width: 62ch; }
#pBtns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
#pBtns .patch { flex: 1 1 100%; }
#pBtns .patch .t { font-size: 24px; }
#pBtns .tab { flex: 1 1 28%; }
#pTitle { padding-right: 46px; }
#pToast { margin: 8px 0 0; font-size: 14px; font-weight: 700; color: var(--moon); }
#pToast:empty { margin: 0; }
#pAfter { font-size: 15px; line-height: 1.3; }
#pAfter p { margin: 0 0 7px; max-width: 62ch; }
.shareBox { width: 100%; min-height: 72px; font: inherit; font-size: 13px; color: var(--ink); background: var(--wool); border-radius: 8px; padding: 8px; border: 0; margin-top: 6px; resize: none; -webkit-user-select: text; user-select: text; }
.res-clock { font-size: 15px; font-weight: 700; margin-bottom: 2px; }
.res-big { font-family: var(--display); font-weight: 900; font-size: clamp(50px, 15vw, 76px); line-height: .95; color: var(--wool); }
.res-line { font-size: 16px; font-weight: 700; line-height: 1.25; margin: 2px 0 10px; }
.res-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 6px 12px; margin: 10px 0; }
.res-stats div { line-height: 1.1; }
.res-stats b { display: block; font-family: var(--display); font-weight: 900; font-size: 25px; color: var(--moon); }
.res-stats span { font-size: 13px; }
.res-note { margin: 3px 0; font-weight: 700; color: var(--teal); }
.res-note.best { color: var(--moon); font-family: var(--display); font-weight: 900; font-size: 25px; line-height: 1.05; }
.build { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.build canvas { width: 38px; height: 38px; display: block; }
.grid-title { font-family: var(--display); font-weight: 900; font-size: 22px; color: var(--wool); margin: 14px 0 6px; line-height: 1.1; }
.cat { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); gap: 7px; }
.row-patch { display: grid; grid-template-columns: 46px 1fr; gap: 1px 10px; align-items: center; text-align: left; padding: 8px 10px; background: var(--quilt); color: var(--wool); border-radius: 10px 13px 10px 12px; width: 100%; min-height: 62px; }
.row-patch::after { inset: 3px; border-width: 1.5px; border-color: rgba(255, 244, 220, .26); }
.row-patch canvas { grid-row: 1 / span 3; width: 46px; height: 46px; display: block; }
.row-patch .n { font-family: var(--display); font-weight: 900; font-size: 19px; line-height: 1.05; }
.row-patch .d { font-size: 13px; line-height: 1.2; }
.row-patch .g { font-size: 12.5px; font-weight: 700; color: var(--moon); line-height: 1.2; }
.row-patch.off { background: var(--deep); }
.row-patch.off canvas { filter: brightness(.25) saturate(.3); }
.row-patch.sel { background: var(--moon); color: var(--ink); }
.row-patch.sel::after { border-color: rgba(26, 17, 71, .4); }
.row-patch.sel .g { color: var(--ink); }
button.row-patch:disabled { cursor: default; }

@media (min-width: 760px) {
  #lCards { grid-template-columns: repeat(3, 1fr); }
  .card { grid-template-columns: 1fr; text-align: center; justify-items: center; align-content: start; padding: 18px 14px; gap: 4px; }
  .card canvas { grid-row: auto; width: 88px; height: 88px; }
  .card .m { justify-content: center; }
}
/* Short screens, phones on their side. */
@media (max-height: 520px) and (min-width: 520px) {
  #lCards { grid-template-columns: repeat(3, 1fr); gap: 7px; }
  .card { grid-template-columns: 1fr; padding: 8px 10px; text-align: center; justify-items: center; align-content: start; gap: 1px; min-height: 0; }
  .card canvas { grid-row: auto; width: 42px; height: 42px; }
  .card .n { font-size: 18px; } .card .d { font-size: 12.5px; } .card .m { font-size: 11.5px; justify-content: center; }
  #lTitle { font-size: 24px; margin-bottom: 5px; }
  #lFoot { margin-top: 5px; } #lFair { font-size: 11.5px; }
  #lFoot .tab { min-height: 36px; padding: 4px 10px; font-size: 13px; }
  #menu { flex-direction: row; align-items: center; gap: 18px; }
  .menu-head, .menu-actions { flex: 1 1 50%; margin: 0; max-height: 100%; }
  #logo { font-size: clamp(36px, 8.4vw, 76px); }
  #tagline { font-size: 14px; margin-top: 6px; }
  .menu-actions { gap: 6px; overflow-y: auto; padding: 2px; }
  .patch { padding: 8px 14px 9px; } .patch .t { font-size: 20px; } .patch.big { padding: 8px 14px 10px; } .patch.big .t { font-size: 28px; } .patch .s { font-size: 12px; margin-top: 1px; }
  #menuLinks .tab { flex: 1 1 22%; min-height: 34px; padding: 3px 6px; font-size: 12.5px; }
  #stats, #byline, #langRow { font-size: 11.5px; }
  #langRow .tab { min-height: 30px; padding: 2px 8px; font-size: 12px; }
  .sheet { padding: 12px 14px; }
  #pTitle { font-size: 26px; margin-bottom: 5px; }
  .res-big { font-size: 40px; } .res-line { margin-bottom: 4px; font-size: 14px; }
  .res-stats { margin: 4px 0; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); } .res-stats b { font-size: 19px; }
  .res-note.best { font-size: 18px; }
  #banner { top: 16%; }
  #hudClock, #hudTucked { font-size: 20px; }
}
@media (max-width: 379px) {
  #awakeWrap { max-width: 64px; }
  #hudClock, #hudTucked { font-size: 20px; }
  .patch.big .t { font-size: 34px; }
  .card { padding: 10px 11px; min-height: 80px; grid-template-columns: 52px 1fr; }
  .card canvas { width: 52px; height: 52px; }
  .card .n { font-size: 20px; } .card .d { font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) { #banner { animation: none; } .card .pip.n { animation: none; } }
body.reduced #banner { animation: none; } body.reduced .card .pip.n { animation: none; }
