:root {
  --ink: #4a2e1a; --cream: #fff8e8; --paper: #fffdf6; --grass: #7cc35a; --grass-d: #4f9a3a;
  --sky: #9fd8f0; --gold: #f2b51d; --red: #e8524a; --muted: #8b7560;
  --vvh: 100dvh; --vvtop: 0px;
  --sat: env(safe-area-inset-top); --sab: env(safe-area-inset-bottom);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; background: var(--sky); }
body {
  background: url(art/bg-meadow.webp) center bottom / cover no-repeat,
              linear-gradient(#9fd8f0 0 55%, #b9e39a 55% 70%, #7cc35a 70%);
}
button, input { font: inherit; color: inherit; }
img { -webkit-user-drag: none; user-select: none; }

/* The app box tracks the *visual* viewport so the on-screen keyboard never covers the footer. */
#app { position: fixed; left: 0; right: 0; top: 0; height: var(--vvh); transform: translateY(var(--vvtop));
  display: flex; flex-direction: column; max-width: 560px; margin: 0 auto; }
.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.on { display: flex; }
.scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 16px 16px;
  overscroll-behavior: contain; }
.center { display: flex; flex-direction: column; align-items: stretch; justify-content: flex-start;
  padding-top: calc(28px + var(--sat)); }

/* Top bar and footer are floating cards: same width, border, radius and shadow as the castle cards. */
.bar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; margin: calc(10px + var(--sat)) 16px 0;
  background: var(--paper); border: 3px solid var(--ink); border-radius: 18px; box-shadow: 0 4px 0 var(--ink); }
.bar .title { font-weight: 800; font-size: 18px; }
.spacer { flex: 1; }
.foot { padding: 10px 12px; margin: 2px 16px calc(12px + var(--sab));
  background: var(--paper); border: 3px solid var(--ink); border-radius: 18px; box-shadow: 0 4px 0 var(--ink); }
body.kb .foot { margin-bottom: 8px; padding: 8px 10px; }
body.kb .bar { margin-top: 8px; }
.row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.card { background: var(--paper); border: 3px solid var(--ink); border-radius: 18px; box-shadow: 0 4px 0 var(--ink);
  padding: 14px; margin-bottom: 14px; }
.card .h { font-weight: 800; font-size: 17px; margin-bottom: 8px; }

.btn { appearance: none; border: 3px solid var(--ink); border-radius: 14px; background: var(--gold); font-weight: 800;
  padding: 10px 14px; box-shadow: 0 4px 0 var(--ink); cursor: pointer; touch-action: manipulation; font-size: 16px;
  min-height: 46px; user-select: none; -webkit-user-select: none; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.big { font-size: 19px; min-height: 54px; width: 100%; }
.btn.grow { width: auto; }
.btn.ghost { background: var(--paper); }
.btn.sm { min-height: 42px; padding: 6px 12px; font-size: 15px; }
.btn:disabled { opacity: .45; transform: none; box-shadow: 0 4px 0 var(--ink); }
.icon { border: 0; background: none; font-size: 22px; padding: 4px 8px; font-weight: 800; }

input { font-size: 16px; } /* ≥16px stops iOS zoom-on-focus */
.form label { display: block; font-weight: 700; margin-bottom: 12px; }
.form input, .code-in { width: 100%; display: block; margin-top: 6px; border: 3px solid var(--ink); border-radius: 12px;
  padding: 12px; background: #fff; font-size: 18px; }
.code-in { margin: 0; text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 800; flex: 1; }
.or { text-align: center; margin: 14px 0 8px; color: var(--muted); font-weight: 700; }

.logo { display: flex; flex-direction: column; align-items: center; }
.logo img.mark { width: 112px; height: 112px; border-radius: 26px; border: 3px solid var(--ink); box-shadow: 0 4px 0 var(--ink); background: var(--grass); }
.logo .fallback { font-size: 72px; }
h1 { margin: 10px 0 0; font-size: 40px; letter-spacing: -1px; text-shadow: 0 3px 0 var(--cream); }
h1 span { color: var(--red); }
.tag { text-align: center; font-weight: 700; margin: 4px 0 18px; }
.rules summary { font-weight: 800; cursor: pointer; }
.rules ol { padding-left: 20px; margin: 10px 0 0; line-height: 1.45; }

/* lobby */
.codecard { display: flex; align-items: center; gap: 12px; padding: 12px 14px; }
.codeleft { flex: 1; min-width: 0; }
.bigcode { font-size: 44px; font-weight: 900; letter-spacing: 6px; line-height: 1.05; margin: 0 0 8px; }
.codecard .btn { width: auto; }
.qr { flex: none; display: flex; }
.qr img, .qr svg { width: 104px; height: 104px; image-rendering: pixelated; border-radius: 8px; }
.scroll.top-safe { padding-top: calc(12px + var(--sat)); }
.players { list-style: none; padding: 0; margin: 0 0 10px; }
.players li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 2px dashed #e7d9bf; font-weight: 700; }
.players li:last-child { border: 0; }
.dot { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid var(--ink); flex: none; background: var(--c); }
.players .tagx { font-size: 12px; font-weight: 800; padding: 2px 7px; border-radius: 8px; border: 2px solid var(--ink); background: var(--cream); }
.players .off { opacity: .45; }
.players .kick { margin-left: auto; border: 0; background: none; font-size: 18px; color: var(--muted); padding: 4px 8px; }
.set { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; font-weight: 700; gap: 10px; }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button { width: 44px; height: 44px; border: 3px solid var(--ink); border-radius: 12px; background: var(--paper); font-weight: 900; font-size: 20px; box-shadow: 0 3px 0 var(--ink); }
.stepper b { min-width: 52px; text-align: center; font-size: 20px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.chips button { border: 2.5px solid var(--ink); border-radius: 10px; background: var(--paper); padding: 6px 9px; font-weight: 800; min-height: 38px; }
.chips button.on { background: var(--gold); }
body:not(.host) .host-only { display: none !important; }
body.host .guest-only { display: none !important; }
body:not(.host) .stepper button, body:not(.host) .chips button:not(.on) { display: none; }
.waiting { text-align: center; font-weight: 700; padding: 14px 0; }

/* deploy */
.dbar { justify-content: space-between; }
.rnd { display: flex; flex-direction: column; line-height: 1.1; }
.rnd small, .left-pill small { font-size: 11px; font-weight: 700; color: var(--muted); }
.rnd b { font-size: 20px; }
.left-pill { display: flex; flex-direction: column; align-items: center; background: var(--gold); border: 3px solid var(--ink);
  border-radius: 16px; padding: 4px 16px; box-shadow: 0 3px 0 var(--ink); line-height: 1.05; transition: background .2s; }
.left-pill b { font-size: 24px; }
.left-pill.zero { background: var(--grass); }
.left-pill.over { background: var(--red); }
.timer { position: relative; width: 50px; height: 50px; }
.timer svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.timer circle { fill: var(--paper); stroke-width: 5; }
.timer .track { stroke: #e7d9bf; }
.timer .prog { fill: none; stroke: var(--grass-d); stroke-linecap: round; stroke-dasharray: 119.4; transition: stroke .3s; }
.timer.warn .prog { stroke: var(--gold); }
.timer.hot .prog { stroke: var(--red); }
.timer.hot { animation: pulse .5s infinite alternate; }
.timer span { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 900; font-size: 17px; }
@keyframes pulse { to { transform: scale(1.08); } }
.status-strip { display: flex; gap: 6px; padding: 10px 16px 0; overflow-x: auto; min-height: 36px; scrollbar-width: none; }
.status-strip .p { display: flex; align-items: center; gap: 4px; font-size: 13px; font-weight: 800; white-space: nowrap;
  padding: 2px 8px 2px 3px; border-radius: 12px; background: var(--paper); border: 2px solid var(--ink); }
.status-strip .p.lock { background: #d9f2cc; }
.status-strip .p .dot { width: 14px; height: 14px; border-width: 2px; }
body.kb .status-strip { display: none; }

.castle { display: grid; grid-template-columns: 64px 1fr auto; grid-template-areas: "art name ctl" "slide slide slide";
  align-items: center; gap: 4px 10px; padding: 10px 12px; margin-bottom: 10px; }
.castle .art { grid-area: art; width: 64px; height: 64px; object-fit: contain; }
.castle .art.fallback { font-size: 44px; text-align: center; line-height: 64px; }
.castle .name { grid-area: name; line-height: 1.15; min-width: 0; }
.castle .name b { font-size: 17px; display: block; }
.castle .pts { display: inline-block; margin-top: 3px; font-weight: 900; font-size: 13px; background: var(--gold); border: 2px solid var(--ink); border-radius: 9px; padding: 0 7px; }
.castle .ctl { grid-area: ctl; display: flex; align-items: center; gap: 6px; }
.castle .ctl button { width: 46px; height: 46px; border: 3px solid var(--ink); border-radius: 12px; background: var(--paper);
  font-size: 24px; font-weight: 900; box-shadow: 0 3px 0 var(--ink); touch-action: manipulation; user-select: none; -webkit-user-select: none; padding: 0; }
.castle .ctl button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.castle .ctl input { width: 66px; height: 46px; text-align: center; font-size: 22px; font-weight: 900; border: 3px solid var(--ink);
  border-radius: 12px; background: #fff; padding: 0; -moz-appearance: textfield; }
.castle .ctl input:focus { outline: 4px solid var(--gold); outline-offset: 0; }
.castle .ctl input.flash { animation: flash .35s; }
@keyframes flash { 50% { background: #ffd6d2; } }
.castle input[type=range] { grid-area: slide; width: 100%; margin: 6px 0 0; accent-color: var(--grass-d); height: 28px; }
.castle.has { background: #f3fbe9; }
body.kb .castle { grid-template-columns: 1fr auto; grid-template-areas: "name ctl"; padding: 6px 12px; margin-bottom: 6px; box-shadow: 0 2px 0 var(--ink); }
body.kb .castle .art, body.kb .castle input[type=range] { display: none; }
body.kb .castle .pts { margin-top: 0; }
.kb-only { display: none !important; }
body.kb .kb-only { display: flex !important; }
body.kb .nokb-only { display: none !important; }
.kbnav { align-items: center; gap: 8px; }
.kbnav .kbleft { flex: 1; font-weight: 900; font-size: 18px; }
.lockcover { display: none; position: absolute; left: 0; right: 0; bottom: 0; top: 0; background: rgba(255,248,232,.7);
  align-items: center; justify-content: center; padding: 24px; z-index: 5; }
.lockcover .card { text-align: center; width: 100%; max-width: 340px; }
.lockcover .big { font-size: 26px; font-weight: 900; margin-bottom: 6px; }
.lockcover .btn { margin-top: 14px; width: 100%; }
#s-deploy { position: relative; }
#s-deploy.locked #d-lockcover, #s-deploy.spectate #d-spectate { display: flex; }
#s-deploy.locked .dfoot, #s-deploy.spectate .dfoot { visibility: hidden; }

/* reveal */
.rcastle { opacity: 0; transform: translateY(14px) scale(.97); transition: opacity .35s, transform .35s; }
.rcastle.on { opacity: 1; transform: none; }
.rcastle .top { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.rcastle .top img { width: 56px; height: 56px; object-fit: contain; }
.rcastle .top .fallback { font-size: 40px; }
.rcastle .top b { font-size: 18px; }
.rcastle .verdict { margin-left: auto; text-align: right; font-weight: 900; font-size: 14px; }
.rrow { display: grid; grid-template-columns: 1fr 44px; align-items: center; gap: 8px; margin: 4px 0; font-weight: 700; font-size: 14px; }
.rbar { position: relative; height: 26px; border: 2px solid var(--ink); border-radius: 9px; background: #f2ead8; overflow: hidden; }
.rbar i { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--c); transition: width .7s cubic-bezier(.2,.9,.3,1.2); }
.rbar span { position: absolute; left: 8px; top: 0; bottom: 0; display: flex; align-items: center; gap: 4px; white-space: nowrap;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 2px #fff; }
.rrow .n { text-align: right; font-weight: 900; }
.rrow.win .rbar { border-width: 3px; }
.rrow.me span { text-decoration: underline; text-decoration-thickness: 2px; }
.flag { position: relative; width: 22px; height: 22px; display: inline-block; flex: none; }
.flag::before { content: ""; position: absolute; inset: 0; background: var(--c); -webkit-mask: url(art/flag.webp) center/contain no-repeat; mask: url(art/flag.webp) center/contain no-repeat; }
.flag img { position: absolute; inset: 0; width: 100%; height: 100%; mix-blend-mode: multiply; }
.empty { font-weight: 700; color: var(--muted); }
.stomp { animation: stomp .45s; }
@keyframes stomp { 30% { transform: scale(1.04) rotate(-.6deg); } 60% { transform: scale(.99) rotate(.4deg); } }

/* score */
.podium { display: flex; align-items: flex-end; justify-content: center; gap: 8px; margin: 6px 0 16px; min-height: 10px; }
.podium .pl { flex: 1; max-width: 120px; text-align: center; font-weight: 800; }
.podium .pl .blk { border: 3px solid var(--ink); border-radius: 12px 12px 4px 4px; background: var(--c); box-shadow: 0 4px 0 var(--ink);
  display: grid; place-items: center; font-size: 26px; color: #fff; text-shadow: 0 2px 0 var(--ink); }
.podium .nm { font-size: 14px; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table { list-style: none; margin: 0; padding: 0; }
.table li { display: grid; grid-template-columns: 26px 18px 1fr auto auto; gap: 8px; align-items: center; padding: 9px 0; border-bottom: 2px dashed #e7d9bf; font-weight: 700; }
.table li:last-child { border: 0; }
.table .rk { font-weight: 900; }
.table .plus { color: var(--grass-d); font-weight: 900; font-size: 14px; }
.table .tot { font-weight: 900; font-size: 18px; min-width: 44px; text-align: right; }
.table .auto { font-size: 11px; color: var(--muted); }
.hist { overflow-x: auto; margin-top: 10px; }
.hist table { border-collapse: collapse; font-size: 13px; font-weight: 700; }
.hist td, .hist th { padding: 4px 6px; text-align: center; border-bottom: 1px solid #e7d9bf; }
.hist td.w { background: #d9f2cc; border-radius: 6px; }
.hist td:first-child, .hist th:first-child { text-align: left; white-space: nowrap; }
body:not(.final) .final-only, body:not(.scorephase) .score-only { display: none !important; }

.net { position: fixed; top: calc(8px + var(--sat)); left: 50%; transform: translateX(-50%) translateY(-80px); background: var(--ink); color: var(--cream);
  padding: 6px 14px; border-radius: 12px; font-weight: 800; font-size: 14px; transition: transform .3s; z-index: 20; }
.net.on { transform: translateX(-50%); }
.toast { position: fixed; left: 16px; right: 16px; bottom: calc(90px + var(--sab)); text-align: center; z-index: 30; pointer-events: none; opacity: 0; transition: opacity .25s; }
.toast.on { opacity: 1; }
.toast span { display: inline-block; background: var(--ink); color: var(--cream); padding: 10px 16px; border-radius: 14px; font-weight: 800; }
@media (min-height: 760px) { .castle .art { width: 72px; height: 72px; } .castle { grid-template-columns: 72px 1fr auto; } }

.herd { display: flex; justify-content: center; gap: 2px; margin: -6px 0 10px; height: 64px; }
.herd img { height: 64px; animation: march 0.9s ease-in-out infinite alternate; }
.herd img:nth-child(2) { animation-delay: .3s; } .herd img:nth-child(3) { animation-delay: .6s; }
@keyframes march { from { transform: translateY(0) rotate(-3deg); } to { transform: translateY(-5px) rotate(3deg); } }
.lockcover .cheer { width: 110px; height: 110px; object-fit: contain; margin: -8px auto 2px; display: block; animation: march .7s ease-in-out infinite alternate; }
body.kb .herd { display: none; }
@media (prefers-reduced-motion: reduce) { .herd img, .lockcover .cheer, .timer.hot { animation: none; } }

/* Wide screens: the meadow is portrait art. Show it whole behind the column, over a blurred fill. */
@media (min-aspect-ratio: 4/5) {
  body { background: none; }
  body::before, body::after { content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
    background: url(art/bg-meadow.webp) center / cover no-repeat; }
  body::before { filter: blur(28px) saturate(1.05) brightness(1.03); transform: scale(1.15); }
  body::after { background-size: auto 100%; background-position: center bottom;
    -webkit-mask: linear-gradient(90deg, transparent calc(50% - 28.1vh), #000 calc(50% - 20vh), #000 calc(50% + 20vh), transparent calc(50% + 28.1vh));
            mask: linear-gradient(90deg, transparent calc(50% - 28.1vh), #000 calc(50% - 20vh), #000 calc(50% + 20vh), transparent calc(50% + 28.1vh)); }
}

/* Scroll areas never slice a card with a hard edge: content fades out at an edge only when there is
   more to scroll that way (--ft/--fb set by app.js). */
.scroll { --ft: 0px; --fb: 0px;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 var(--ft), #000 calc(100% - var(--fb)), transparent 100%);
          mask-image: linear-gradient(to bottom, transparent 0, #000 var(--ft), #000 calc(100% - var(--fb)), transparent 100%); }
.scroll.more-top { --ft: 34px; }
.scroll.more-bottom { --fb: 34px; }
/* Timer chips get their own full-width row so they never wrap awkwardly. */
.set.col { flex-direction: column; align-items: stretch; gap: 8px; }
.set.col .chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.set.col .chips button { padding: 6px 0; min-width: 0; }
body:not(.host) .set.col .chips { display: flex; justify-content: flex-start; }

.logo h1 { margin: 6px 0 0; line-height: 0; }
.logo .wordmark { width: min(78vw, 300px); height: auto; filter: drop-shadow(0 3px 0 rgba(74,46,26,.25)); }
/* Lobby density: fit players + all rules on one phone screen. */
#s-lobby .card { margin-bottom: 12px; }
#s-lobby .players { margin-bottom: 0; }
#s-lobby .set { padding: 5px 0; }
#s-lobby .set.col { padding-top: 8px; }
#s-lobby .stepper button { width: 42px; height: 42px; }

/* Invite links: joining is the big obvious action; creating is a small link. */
body:not(.invite) .invite-only, body.invite .noinvite-only { display: none !important; }
.invite-head { text-align: center; margin: -2px 0 12px; }
.invite-head small { display: block; font-weight: 700; color: var(--muted); font-size: 14px; }
.invite-head b { display: block; font-size: 46px; font-weight: 900; letter-spacing: 8px; line-height: 1.1; }
.linkbtn { display: block; margin: 16px auto 2px; border: 0; background: none; color: var(--muted); font-weight: 700;
  font-size: 15px; text-decoration: underline; text-underline-offset: 3px; padding: 6px 10px; }

/* Keyboard / programmatic scrolling keeps cards clear of the edge fades. */
.scroll { position: relative; }
.scroll .card { scroll-margin: 44px 0; }

/* Temporary text wordmark until the Cowstle logo art lands. */
.logo h1.textmark { line-height: 1; margin: 10px 0 0; font-size: 58px; font-weight: 900; letter-spacing: -1px; color: #ffe39a;
  -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill; text-shadow: 0 4px 0 var(--ink); }
.logo h1.textmark span { color: var(--red); }
