:root {
  --bg: #050d1f;
  --fg: #f4f8ff;
  --muted: #8fa6d6;
  --line: #2a3a5a;
  --cyan: #1fb6ff;
  --correct: #2fb34f;
  --present: #f0b92a;
  --absent: #1b2538;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;

  /* Board geometry: the tile size is the smaller of what fits the width and what fits the height. */
  --gutter: 8px;
  --apad: 28px; /* wide enough for the ball to travel up the sides and across the top */
  --gap: 7px;
  --chrome: 360px; /* header, sub-header, keyboard, footer: everything that is not the arena */
  --tw: calc((min(100vw, 480px) - 2 * var(--gutter) - 2 * var(--apad) - 4 * var(--gap) - 4px) / 5);
  --th: calc((100svh - var(--chrome) - 2 * var(--apad) - 5 * var(--gap) - 4px) / 7.35);
  --t: max(36px, min(var(--tw), var(--th)));
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100svh;
  background: radial-gradient(ellipse at 50% 0%, #0d2147 0, #07122a 45%, #050d1f 100%);
  color: var(--fg);
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 10px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Header ---------- */
.topbar {
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 4px;
}
.icon-btn {
  width: 44px; height: 44px;
  border: 2px solid var(--cyan);
  border-radius: 10px;
  background: rgba(10, 24, 52, .6);
  box-shadow: 0 0 12px rgba(31, 182, 255, .18);
  color: var(--fg);
  font: 900 1.5rem var(--font);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  padding: 0;
}
.icon-btn.stats-btn { font-size: 1.3rem; }
.icon-btn:hover { background: rgba(31, 182, 255, .18); }
.logo {
  margin: 0;
  font-size: clamp(1.35rem, 6.2vw, 2.2rem);
  font-weight: 900;
  letter-spacing: .01em;
  line-height: 1;
  position: relative;
  white-space: nowrap;
}
.logo .w1 {
  background: linear-gradient(#fff, #a9c4f4);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 10px rgba(80, 150, 255, .45));
}
.logo .w2 {
  font-style: italic;
  background: linear-gradient(#6fe0ff, #1698ff);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 10px rgba(31, 182, 255, .6));
}
.logo::after { /* the little ball flying off the K */
  content: "";
  position: absolute; right: -14px; top: -6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 8px 2px var(--cyan), -10px 8px 6px -2px rgba(31, 182, 255, .6), -18px 14px 5px -3px rgba(31, 182, 255, .35);
}

.mode-btn {
  margin: 8px 0 10px;
  padding: 4px 12px;
  background: none;
  border: 0;
  border-radius: 999px;
  color: var(--muted);
  font: 700 .85rem var(--font);
  letter-spacing: .22em;
  text-transform: uppercase;
  cursor: default;
}
.mode-btn:not(:disabled) { cursor: pointer; color: var(--cyan); border: 1px solid var(--cyan); letter-spacing: .12em; }

/* ---------- Arena ---------- */
main { flex: none; display: flex; justify-content: center; }
.arena {
  position: relative;
  width: calc(5 * var(--t) + 4 * var(--gap) + 2 * var(--apad) + 4px);
  height: calc(6 * var(--t) + 5 * var(--gap) + 1.35 * var(--t) + 2 * var(--apad) + 4px);
  border: 2px solid var(--cyan);
  border-radius: 18px;
  background: rgba(3, 10, 26, .85);
  box-shadow: 0 0 14px rgba(31, 182, 255, .55), inset 0 0 16px rgba(31, 182, 255, .12);
}
.ball-canvas { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 3; pointer-events: none; border-radius: 16px; }
.board {
  position: absolute;
  left: var(--apad); top: var(--apad);
  z-index: 2;
  display: grid;
  grid-template-columns: repeat(5, var(--t));
  grid-template-rows: repeat(6, var(--t));
  gap: var(--gap);
  pointer-events: none;
}
.row { display: contents; }

.tile {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid #1b2b47;
  border-radius: 10px;
  background: rgba(5, 14, 32, .55);
  font-size: calc(var(--t) * .55);
  font-weight: 800;
  text-transform: uppercase;
  color: #fff;
  isolation: isolate;
}
.tile.filled { border-color: var(--cyan); background: rgba(6, 22, 46, .75); box-shadow: 0 0 8px rgba(31, 182, 255, .35); }
.tile.correct, .tile.present, .tile.absent { background: transparent; border-color: transparent; box-shadow: none; text-shadow: 0 1px 2px rgba(0, 0, 0, .55); }
.tile.present { color: #1c1708; text-shadow: none; }
/* Graded tiles are sprites; data-hp 3/2/1 picks the crack stage and .eaten the shattered remnant.
   The sprite is cropped 10/252 px (≈4%) beyond the tile box on every side. */
.tile.correct::before, .tile.present::before, .tile.absent::before {
  content: ""; position: absolute; z-index: -1;
  left: -3.97%; top: -3.97%; width: 107.94%; height: 107.94%;
  background: var(--tile-art) 0 0 / 100% 100% no-repeat;
  pointer-events: none;
}
.tile.absent  { --k: absent; }
.tile.present { --k: present; }
.tile.correct { --k: correct; }
.tile.absent[data-hp="3"]  { --tile-art: url("assets/tiles/absent-0.webp"); }
.tile.absent[data-hp="2"]  { --tile-art: url("assets/tiles/absent-1.webp"); }
.tile.absent[data-hp="1"]  { --tile-art: url("assets/tiles/absent-2.webp"); }
.tile.absent.eaten         { --tile-art: url("assets/tiles/absent-3.webp"); }
.tile.present[data-hp="3"] { --tile-art: url("assets/tiles/present-0.webp"); }
.tile.present[data-hp="2"] { --tile-art: url("assets/tiles/present-1.webp"); }
.tile.present[data-hp="1"] { --tile-art: url("assets/tiles/present-2.webp"); }
.tile.present.eaten        { --tile-art: url("assets/tiles/present-3.webp"); }
.tile.correct[data-hp="3"] { --tile-art: url("assets/tiles/correct-0.webp"); }
.tile.correct[data-hp="2"] { --tile-art: url("assets/tiles/correct-1.webp"); }
.tile.correct[data-hp="1"] { --tile-art: url("assets/tiles/correct-2.webp"); }
.tile.correct.eaten        { --tile-art: url("assets/tiles/correct-3.webp"); }
.tile.eaten { color: transparent; text-shadow: none; }

.tile.hit::before { animation: impact .3s ease; }
.tile.eaten.shatter::before { animation: crumble .4s ease-out; }
@keyframes impact { 40% { filter: brightness(2.2); transform: scale(.93); } }
@keyframes crumble { from { opacity: .4; transform: scale(1.12); } to { opacity: 1; transform: scale(1); } }
.tile.pop { animation: pop .12s ease-in-out; }
.tile.flip { animation: flip .5s ease; }
.tile.shake { animation: tile-shake .4s ease; }
@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes flip { 50% { transform: scaleY(0); } }
@keyframes tile-shake {
  0%, 100% { transform: translateX(0); } 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); } 80% { transform: translateX(4px); }
}

.grid-toast {
  position: absolute; left: 50%; top: 22%; z-index: 12;
  transform: translate(-50%, -50%) scale(.9);
  margin: 0; padding: 10px 18px; border-radius: 8px;
  background: rgba(0, 0, 0, .9); border: 1px solid var(--cyan);
  font-size: .95rem; font-weight: 700; white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity .15s ease, transform .15s ease;
}
.grid-toast.visible { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ---------- Keyboard ---------- */
.keyboard { width: 100%; max-width: 480px; padding: 0 6px; margin-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.key-row { display: flex; justify-content: center; gap: 6px; }
.key-spacer { flex: .5; }
.key {
  flex: 1; min-width: 0; padding: 0;
  height: clamp(42px, 6.4svh, 56px);
  background: linear-gradient(#35435f, #28344d);
  color: #fff;
  border: 1px solid #1a2438;
  border-radius: 7px;
  box-shadow: inset 0 1px rgba(255, 255, 255, .12), 0 2px 0 rgba(0, 0, 0, .35);
  font: 700 1.2rem var(--font);
  text-transform: uppercase;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.key.wide { flex: 1.5; font-size: .8rem; }
.key.correct { background: linear-gradient(#3ec35e, #279443); }
.key.present { background: linear-gradient(#f6c845, #d79f17); color: #1c1708; }
.key.absent { background: linear-gradient(#1f2a3f, #19223a); color: #6c7b9c; }

.message { min-height: 0; margin: 4px 0 0; font-size: .9rem; font-weight: 600; text-align: center; color: var(--muted); }
.site-footer { color: #5d6f96; font-size: .72rem; text-align: center; padding: 8px 16px 6px; }
.site-footer a { color: #8fa6d6; text-decoration: underline; }

/* ---------- Modals ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(2, 6, 16, .75); display: flex; align-items: center; justify-content: center; z-index: 20; }
.modal-backdrop.hidden { display: none; }
.modal { position: relative; background: #0a1b38; border: 1px solid var(--line); border-radius: 12px; padding: 24px 26px; max-width: 360px; width: 90%; text-align: center; overflow: hidden; box-shadow: 0 0 24px rgba(31, 182, 255, .2); }
.modal h2 { margin: 0 0 8px; font-size: 1.4rem; }
.modal p { color: #b9c8ec; margin: 0 0 12px; font-size: .92rem; line-height: 1.45; }
.help-modal { text-align: left; }
.help-modal h2 { text-align: center; }
.c-green { color: #46d368; } .c-yellow { color: var(--present); }
.share-grid { background: #050d1f; border-radius: 6px; padding: 12px; font-size: 1.1rem; line-height: 1.3; white-space: pre-wrap; margin: 0 0 16px; }
.modal-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 8px; }
.primary-btn, .secondary-btn { padding: 10px 16px; border-radius: 8px; border: none; font: 700 .9rem var(--font); cursor: pointer; }
.primary-btn { background: var(--cyan); color: #03203a; }
.secondary-btn { background: transparent; color: var(--fg); border: 1px solid #4a6190; }
.wide-btn { display: block; width: 100%; margin: 10px 0; }
.link-btn { display: inline-block; margin-top: 12px; background: none; border: 0; padding: 0; color: var(--cyan); font: 700 .85rem var(--font); text-decoration: none; cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.setting-row { display: flex; justify-content: space-between; align-items: center; padding: 10px 2px; font-weight: 700; }
.setting-row input { width: 22px; height: 22px; accent-color: var(--cyan); }
.share-toast { position: absolute; left: 50%; bottom: 12px; transform: translate(-50%, 6px); background: rgba(0, 0, 0, .9); border: 1px solid var(--cyan); font-size: .82rem; font-weight: 600; padding: 6px 12px; border-radius: 6px; white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease; }
.share-toast.visible { opacity: 1; transform: translate(-50%, 0); }

.stats-tabs { display: flex; justify-content: center; gap: 6px; margin: 0 0 10px; }
.stats-tab { background: none; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font: 700 .8rem var(--font); padding: 3px 14px; cursor: pointer; }
.stats-tab.active { color: var(--fg); border-color: var(--cyan); }
.stats-versus { margin: 0 0 14px; color: var(--cyan) !important; font-weight: 700; }
.stats-summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 6px; margin-bottom: 16px; }
.stat-num { font-size: 1.5rem; font-weight: 700; }
.stat-label { font-size: .7rem; color: var(--muted); line-height: 1.2; }
.stats-modal h3 { margin: 0 0 8px; font-size: .9rem; text-align: left; }
.stats-dist { margin-bottom: 14px; }
.dist-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; font-size: .8rem; }
.dist-row .dist-n { width: 10px; text-align: right; }
.dist-bar { background: #2a3a5a; color: #fff; min-width: 18px; padding: 1px 6px; text-align: right; font-weight: 700; border-radius: 3px; }
.dist-bar.empty { min-width: 0; width: 4px; padding: 1px 0; background: #17233c; }
.dist-bar.last { background: var(--correct); }

/* Short windows: the board shrinks (see --th) but the chrome can too. */
@media (max-height: 700px) { :root { --chrome: 330px; } .topbar { padding-top: 8px; } .site-footer { display: none; } }

/* Phones turned sideways: no room for the board and keyboard, so ask for portrait. */
.rotate-hint { display: none; }
@media (orientation: landscape) and (max-height: 520px) and (pointer: coarse) {
  .rotate-hint { position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; background: rgba(5, 13, 31, .97); color: #b9d0ff; font-weight: 700; text-align: center; padding: 20px; }
  .rotate-hint .icon { font-size: 2.6rem; display: inline-block; animation: rotate-nudge 2s ease-in-out infinite; }
  .rotate-hint p { margin: 0; }
}
@keyframes rotate-nudge { 0%, 30% { transform: rotate(-90deg); } 70%, 100% { transform: rotate(0deg); } }

.icon-btn.world-btn { text-decoration: none; font-size: 1.3rem; }
.footer-link { background: none; border: 0; padding: 0; font: inherit; color: #8fa6d6; text-decoration: underline; cursor: pointer; }
