:root {
  --ink: #3b2414; --ink2: #6b4a2e; --paper: #fff7e6; --paper2: #f6e7c8; --wood: #c47a35; --wood-d: #7a4a26;
  --green: #2f6b45; --green-l: #3f8a5a; --orange: #f07a2a; --orange-d: #c95a14; --bad: #d6453d; --ok: #2b7d49;
  --muted: #a8977f; --font: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: #e9d3a6 url(img/bg.webp) center 30% / cover no-repeat; color: var(--ink); font-family: var(--font);
  position: fixed; inset: 0; overscroll-behavior: none;
}
body::before { content: ""; position: fixed; inset: 0; background: rgba(255, 244, 222, .38); pointer-events: none; }
button { font-family: inherit; cursor: pointer; color: inherit; }
.hidden { display: none !important; }
#app { position: relative; height: 100%; display: flex; flex-direction: column; max-width: 1040px; margin: 0 auto; padding: 10px 16px 12px; }

/* header */
#top { display: flex; align-items: center; gap: 12px; flex: 0 0 auto; }
#top h1 { margin: 0; font-size: 28px; font-weight: 900; color: #fff; letter-spacing: .5px; white-space: nowrap;
  background: var(--green); border: 3px solid var(--ink); border-radius: 14px; padding: 2px 14px 4px; box-shadow: 0 4px 0 var(--ink); }
#top h1 span { color: #ffd36b; }
#tabs { display: flex; gap: 6px; margin-left: auto; background: rgba(59, 36, 20, .85); padding: 4px; border-radius: 14px; }
#tabs button { border: 0; background: transparent; color: #f4e3c4; font-weight: 800; font-size: 15px; padding: 8px 14px; border-radius: 10px; }
#tabs button.on { background: var(--orange); color: #fff; box-shadow: inset 0 -3px 0 var(--orange-d); }
.snd { display: flex; gap: 6px; }
.snd button { width: 42px; height: 42px; border-radius: 12px; border: 3px solid var(--ink); background: var(--paper); padding: 6px; box-shadow: 0 3px 0 var(--ink); }
.snd button svg { width: 100%; height: 100%; display: block; }

#main { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; margin-top: 10px; }
section { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }

/* options */
#opts { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.seg { display: flex; background: rgba(59, 36, 20, .85); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; color: #f4e3c4; font-weight: 800; font-size: 15px; padding: 7px 14px; border-radius: 9px; min-width: 54px; }
.seg button.on { background: var(--paper); color: var(--ink); }

/* stats row */
#statsRow { display: flex; align-items: flex-end; gap: 12px; margin-top: 8px; }
#coach { display: flex; align-items: flex-end; gap: 4px; flex: 1 1 auto; min-width: 0; }
#utan { width: 120px; height: 120px; object-fit: contain; flex: 0 0 auto; margin-bottom: -16px; position: relative; z-index: 2; transition: transform .15s; }
#utan.bump { transform: translateY(-6px) rotate(-3deg); }
#bubble { position: relative; background: var(--paper); border: 3px solid var(--ink); border-radius: 16px; padding: 8px 12px; font-weight: 700; font-size: 15px;
  margin-bottom: 26px; max-width: 300px; box-shadow: 0 3px 0 rgba(59, 36, 20, .5); }
#bubble::before { content: ""; position: absolute; left: -11px; bottom: 10px; border: 8px solid transparent; border-right-color: var(--ink); border-left: 0; }
#stats, #rainHud { display: flex; gap: 8px; flex: 0 0 auto; }
.stat { background: var(--paper); border: 3px solid var(--ink); border-radius: 14px; padding: 4px 12px 6px; text-align: center; min-width: 76px; box-shadow: 0 3px 0 var(--ink); margin-bottom: 8px; }
.stat .lbl { display: block; font-size: 12px; font-weight: 800; color: var(--ink2); text-transform: uppercase; letter-spacing: .5px; }
.stat b { font-size: 26px; font-weight: 900; font-variant-numeric: tabular-nums; }
.stat.big { background: #ffe39a; }
.stat.best b { color: var(--green); }
.hearts { letter-spacing: 2px; color: var(--bad); }

/* typing card */
#card { position: relative; flex: 0 0 auto; background: var(--paper); border: 4px solid var(--ink); border-radius: 22px; box-shadow: 0 6px 0 var(--ink), 0 14px 30px rgba(59, 36, 20, .25);
  padding: 22px 26px; background-image: repeating-linear-gradient(transparent 0 calc(var(--lh) - 2px), rgba(120, 160, 200, .22) calc(var(--lh) - 2px) var(--lh));
  background-position: 0 22px; --fs: 28px; --lh: 48px; cursor: text; }
#card.focus { box-shadow: 0 6px 0 var(--ink), 0 0 0 4px rgba(240, 122, 42, .55), 0 14px 30px rgba(59, 36, 20, .25); }
#words { position: relative; height: calc(var(--lh) * 3); overflow: hidden; font-size: var(--fs); line-height: var(--lh); user-select: none; }
#wordsInner { position: relative; display: flex; flex-wrap: wrap; column-gap: .42em; transition: transform .18s ease-out; }
.word { position: relative; white-space: nowrap; color: var(--muted); font-weight: 700; }
.word .l.ok { color: var(--ink); }
.word .l.bad { color: var(--bad); background: rgba(214, 69, 61, .12); border-radius: 3px; }
.word .l.x { color: #a8322b; opacity: .7; }
.word.err { text-decoration: underline wavy var(--bad); text-underline-offset: 7px; text-decoration-thickness: 2px; }
.word.cur { color: #8a7861; }
#caret { position: absolute; width: 3px; border-radius: 2px; background: var(--orange); height: calc(var(--fs) * 1.15); top: 0; left: 0; transition: left .08s linear, top .12s; z-index: 3; }
#caret.blink { animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
#card.shake { animation: shake .22s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
#hint { text-align: center; margin-top: 10px; font-weight: 800; color: var(--orange-d); font-size: 15px; }

#result { position: relative; display: flex; align-items: center; justify-content: center; gap: 18px; padding: 10px 18px; animation: pop .3s; cursor: default; }
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
#resUtan { width: 150px; height: 150px; object-fit: contain; flex: 0 0 auto; }
.resMain { text-align: center; min-width: 0; }
.resKpm { display: flex; align-items: baseline; justify-content: center; gap: 8px; line-height: 1; }
.resKpm b { font-size: 64px; font-weight: 900; color: var(--orange-d); }
.resKpm span { font-size: 22px; font-weight: 900; }
#rRank { margin: 4px 0 8px; font-weight: 700; font-size: 15px; color: var(--ink2); }
.resGrid { display: grid; grid-template-columns: repeat(4, auto); gap: 6px 14px; justify-content: center; margin-bottom: 10px; }
.resGrid div { display: flex; flex-direction: column; }
.resGrid span { font-size: 12px; font-weight: 800; color: var(--ink2); text-transform: uppercase; }
.resGrid b { font-size: 20px; font-weight: 900; }
#resBadge, #rainBadge { display: inline-block; background: var(--green); color: #fff; font-weight: 900; padding: 3px 12px; border-radius: 999px; margin-bottom: 6px; animation: wob .8s ease-in-out infinite alternate; }
@keyframes wob { from { transform: rotate(-3deg) scale(1); } to { transform: rotate(3deg) scale(1.06); } }

.btn { border: 3px solid var(--ink); background: var(--orange); color: #fff; font-weight: 900; font-size: 18px; padding: 10px 26px; border-radius: 14px;
  box-shadow: 0 4px 0 var(--ink); display: inline-flex; align-items: center; gap: 8px; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.ghost { background: var(--paper); color: var(--ink); }
.btn svg { width: 20px; height: 20px; }
#actions { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 16px; flex: 0 0 auto; }
.keys { font-size: 13px; font-weight: 700; color: var(--ink2); background: rgba(255, 247, 230, .8); padding: 3px 8px; border-radius: 8px; }
kbd { font-family: inherit; background: var(--paper); border: 2px solid var(--ink2); border-bottom-width: 3px; border-radius: 6px; padding: 0 5px; font-size: 12px; }
#about { text-align: center; font-size: 13px; color: var(--ink2); margin: 10px auto 0; max-width: 620px; background: rgba(255, 247, 230, .75); padding: 4px 10px; border-radius: 10px; }

/* hidden input: kept inside the active area so phones open the keyboard without jumping */
#typer { position: absolute; left: 50%; top: 40%; width: 2px; height: 2px; opacity: 0; border: 0; padding: 0; font-size: 16px; z-index: -1; caret-color: transparent; color: transparent; background: transparent; }

/* rain */
#rainHud { justify-content: center; flex-wrap: wrap; }
#rainHud .stat { margin-bottom: 0; }
#stage { position: relative; flex: 1 1 auto; min-height: 0; margin-top: 8px; border: 4px solid var(--ink); border-radius: 22px; overflow: hidden;
  background: linear-gradient(#2f6b45, #285c3b); box-shadow: 0 6px 0 var(--ink); cursor: text; }
#rain { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(30, 50, 35, .45); padding: 12px; }
.panel { background: var(--paper); border: 4px solid var(--ink); border-radius: 22px; box-shadow: 0 6px 0 var(--ink); padding: 16px 22px; text-align: center; max-width: 440px; animation: pop .3s; }
.panel h2 { margin: 0 0 6px; font-size: 28px; color: var(--green); }
.panel p { margin: 0 0 12px; font-weight: 600; line-height: 1.4; }
.panel .keys { display: block; margin-top: 8px; background: none; }
.endUtan { width: 110px; height: 110px; object-fit: contain; display: block; margin: -6px auto 0; }

#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 50; }

/* phones and small screens */
@media (max-width: 720px) {
  #app { padding: 6px 10px 8px; }
  #top { gap: 6px; }
  #top h1 { font-size: 17px; padding: 2px 8px 3px; border-width: 2px; border-radius: 10px; box-shadow: 0 3px 0 var(--ink); }
  #tabs { padding: 3px; gap: 3px; }
  #tabs button { font-size: 12.5px; padding: 6px 7px; }
  .snd button { width: 34px; height: 34px; border-width: 2px; padding: 4px; border-radius: 10px; }
  #main { margin-top: 6px; }
  .seg button { font-size: 13px; padding: 5px 9px; min-width: 0; }
  #opts { gap: 6px; }
  #statsRow { margin-top: 4px; gap: 6px; }
  #utan { width: 64px; height: 64px; margin-bottom: -10px; }
  #bubble { font-size: 12.5px; padding: 5px 8px; margin-bottom: 14px; border-width: 2px; border-radius: 12px; }
  #bubble::before { bottom: 6px; }
  .stat { min-width: 0; padding: 2px 7px 4px; border-width: 2px; border-radius: 10px; box-shadow: 0 2px 0 var(--ink); margin-bottom: 4px; }
  .stat .lbl { font-size: 10px; }
  .stat b { font-size: 19px; }
  #stats .stat.best { display: none; }
  #statsRow { flex-wrap: wrap; }
  #card { padding: 12px 14px; --fs: 21px; --lh: 36px; border-width: 3px; border-radius: 16px; background-position: 0 12px; }
  #hint { font-size: 13px; margin-top: 4px; }
  #actions { margin-top: 10px; }
  .btn { font-size: 16px; padding: 8px 18px; }
  .keys.desk, #actions .keys { display: none; }
  #result { flex-direction: column; gap: 2px; padding: 0; }
  #hLives { font-size: 15px; letter-spacing: 0; }
  #resUtan { width: 64px; height: 64px; }
  .resKpm b { font-size: 40px; }
  .resGrid { grid-template-columns: repeat(4, auto); gap: 2px 10px; margin-bottom: 6px; }
  .resGrid b { font-size: 16px; }
  #rRank { font-size: 12.5px; margin: 2px 0 4px; }
  #rainHud { gap: 4px; flex-wrap: nowrap; }
  #rainHud .stat { padding: 2px 6px 3px; min-width: 0; flex: 1 1 0; }
  #stage { border-width: 3px; border-radius: 16px; margin-top: 6px; }
  .panel { padding: 12px 14px; }
  .panel h2 { font-size: 22px; }
  .panel p { font-size: 14px; }
}
@media (max-width: 720px) and (max-height: 560px) {
  #about { display: none; }
  #utan { width: 48px; height: 48px; }
  #bubble { display: none; }
  #result { flex-direction: row; }
}
@media (max-width: 380px) { #tabs button { font-size: 11.5px; padding: 6px 5px; } #top h1 { font-size: 15px; } }
@media (max-height: 640px) and (min-width: 721px) { #about { display: none; } #utan { width: 90px; height: 90px; } }
