/* ============ Longest Line — styles ============ */

:root {
  /* Okabe-Ito colorblind-safe palette */
  --p1: #0072B2;  /* blue    */
  --p2: #E69F00;  /* orange  */
  --p3: #009E73;  /* green   */
  --p4: #CC79A7;  /* pink    */
  --bg: #fdf6ec;
  --ink: #2d2a26;
  --card: #ffffff;
  --accent: #e8590c;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: ui-rounded, -apple-system, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  transition: background-color .5s ease;
  /* Touch-first: kill selection, callouts, tap highlights */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font-family: inherit;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
  border: none;
}

.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

/* ============ Setup screen ============ */

#setup {
  justify-content: center;
  gap: 14px;
  position: relative;
  overflow: hidden;
  touch-action: none; /* fingers draw rainbows instead of panning */
}

/* rainbow doodle canvas sits behind everything */
.doodle {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

/* ---- animated landing background: drifting player symbols ---- */
.floaters {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}
.floaters span {
  position: absolute;
  left: var(--x);
  bottom: -12%;
  font-size: var(--s);
  font-weight: 900;
  color: var(--c);
  opacity: .14;
  animation: floatUp var(--d) linear infinite;
  animation-delay: var(--dl);
  will-change: transform;
}
@keyframes floatUp {
  to { transform: translateY(-130vh) rotate(360deg); }
}
#setup > *:not(.floaters):not(.doodle) { position: relative; z-index: 1; }

/* ---- animated logo: grid draws itself, symbols pop in ---- */
.logo {
  width: min(150px, 32vmin);
  height: auto;
}
.logo line, .logo circle, .logo path {
  stroke-dasharray: 130;
  stroke-dashoffset: 130;
  animation: drawStroke .55s ease-out forwards;
}
.logo .g1 { animation-delay: .05s; }
.logo .g2 { animation-delay: .18s; }
.logo .g3 { animation-delay: .31s; }
.logo .g4 { animation-delay: .44s; }
.logo .sx line:first-child { animation-delay: .70s; }
.logo .sx line:last-child  { animation-delay: .85s; }
.logo .so { animation-delay: 1.00s; }
.logo .st { animation-delay: 1.15s; }
@keyframes drawStroke {
  to { stroke-dashoffset: 0; }
}

.title {
  font-size: clamp(2.2rem, 7vmin, 3.5rem);
  letter-spacing: 1px;
  animation: riseIn .6s .3s ease-out backwards;
}
.title .accent { color: var(--accent); }
.tagline { opacity: .7; margin-bottom: 6px; animation: riseIn .6s .55s ease-out backwards; }
@keyframes riseIn {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .floaters span, .logo line, .logo circle, .logo path, .title, .tagline { animation: none; }
  .logo line, .logo circle, .logo path { stroke-dashoffset: 0; }
}

/* ---- setup must always fit on screen (drawing disables scrolling) ---- */
.brand { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.setup-group .hint { font-weight: 400; opacity: .55; font-size: .78rem; }
.foot-links { display: flex; gap: 6px; }

/* Two-column settings grid by default — 9 groups won't stack on any tablet */
.settings {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 24px;
  width: min(720px, 94vw);
  align-items: start;
}
.setup-group.span2 { grid-column: 1 / -1; }

@media (max-height: 900px) {
  #setup { gap: 8px; }
  .settings { gap: 7px 20px; }
  .logo { width: min(96px, 16vmin); }
  .title { font-size: clamp(1.6rem, 5vmin, 2.4rem); }
  .tagline { display: none; }
  .setup-group h2 { margin-bottom: 4px; font-size: .9rem; }
  .seg-btn { min-height: 44px; padding: 8px 14px; font-size: .92rem; }
  .name-inputs input { min-height: 44px; }
  .big-btn { min-height: 52px; font-size: 1.2rem; padding: 10px 36px; }
}

/* Narrow phones: single column, maximum compression; the allow-scroll
   fallback below guarantees reachability if it still overflows */
@media (max-width: 559px) {
  .settings { grid-template-columns: 1fr; gap: 6px; }
  .seg-btn { min-height: 42px; padding: 6px 12px; font-size: .88rem; }
  .setup-group h2 { margin-bottom: 3px; }
  .logo { width: 64px; }
}

@media (max-height: 560px) {
  #setup { gap: 5px; }
  .brand { flex-direction: row; gap: 12px; }
  .brand-text { text-align: left; }
  .logo { width: 52px; }
  .settings { gap: 4px 20px; }
  .seg-btn { min-height: 40px; padding: 6px 10px; font-size: .85rem; }
  .name-inputs input { min-height: 38px; width: 104px; }
  .big-btn { min-height: 46px; font-size: 1.05rem; }
}

/* Safety net: if the setup screen still overflows (tiny/unusual viewport),
   restore vertical panning — vertical drags scroll, other drags still draw */
#setup.allow-scroll {
  touch-action: pan-y;
  overflow-y: auto;
  justify-content: flex-start;
}

/* ---- desktop & wide screens: settings become a tidy card ---- */
@media (min-width: 900px) {
  .settings {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: rgba(255, 255, 255, .78);
    border-radius: 20px;
    padding: 10px 26px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, .07);
    width: min(660px, 94vw);
  }
  .settings .setup-group {
    display: grid;
    grid-template-columns: 200px 1fr;
    align-items: center;
    width: 100%;
    text-align: left;
    padding: 7px 0;
    border-bottom: 1px solid rgba(0, 0, 0, .05);
  }
  .settings .setup-group:last-child { border-bottom: none; }
  .settings .setup-group h2 { margin: 0; text-align: left; padding-right: 12px; }
  .settings .setup-group .hint { display: block; margin-top: 2px; }
  .settings .setup-group .seg { justify-content: flex-start; }
  .settings .setup-group .seg-btn { min-height: 40px; padding: 6px 14px; font-size: .9rem; }
  .name-inputs { justify-content: flex-start; }
  /* the card has a solid background, so quiet the floaters right down */
  .floaters span { opacity: .08; }
}

/* Landscape phones: reclaim vertical space for the board */
@media (max-height: 500px) {
  .board { width: min(96vw, calc(100dvh - 150px)); }
  .turn-banner { min-height: 34px; font-size: 1rem; border-radius: 10px; }
  .end-bar { padding: 2px 0 0; }
  .end-hold { min-height: 40px; padding: 6px 16px; font-size: .85rem; }
  .hud .icon-btn, .hud .shot-clock { width: 40px; height: 40px; }
  .timer { font-size: 1.2rem; padding: 4px 10px; }
  .chip { min-height: 38px; padding: 4px 8px; }
  .game-toast { top: 84px; }
}

.setup-group { text-align: center; width: min(560px, 94vw); }
.setup-group h2 { font-size: 1rem; margin-bottom: 8px; opacity: .8; }
.setup-group .optional { font-weight: normal; opacity: .6; font-size: .85rem; }

.seg {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.seg-btn {
  min-width: 64px;
  min-height: 52px;
  padding: 10px 18px;
  font-size: 1.05rem;
  font-weight: 700;
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 2px 0 rgba(0,0,0,.12);
}
.seg-btn.selected {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 0 rgba(0,0,0,.25);
}

.name-inputs {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.player-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--card);
  border-radius: 14px;
  padding: 5px 8px;
}
.pick-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: #f2ead9;
  font-size: 1.25rem;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pick-btn.color-pick { border-radius: 50%; background: var(--pick, #ccc); }
.player-row input {
  border: none !important;
  background: transparent !important;
  width: 110px;
}
.name-inputs input {
  width: 130px;
  min-height: 48px;
  padding: 8px 12px;
  font-size: 1rem;
  font-family: inherit;
  border: 2px solid #ddd0be;
  border-radius: 12px;
  background: var(--card);
  text-align: center;
  -webkit-user-select: text;
  user-select: text;
}
.name-inputs input:focus { outline: 3px solid var(--accent); border-color: transparent; }

.big-btn {
  min-height: 60px;
  padding: 12px 44px;
  font-size: 1.4rem;
  font-weight: 800;
  border-radius: 18px;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 0 #a63e05;
  margin-top: 8px;
}
.big-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #a63e05; }
.big-btn.secondary {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(0,0,0,.15);
}

/* ============ Game screen ============ */

#game { padding: 8px; gap: 6px; }

.hud {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: max(4px, env(safe-area-inset-top)) 4px 0;
}

.score-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--chip-color, var(--card));
  color: #fff;
  border-radius: 14px;
  padding: 6px 12px;
  min-height: 48px;
  box-shadow: 0 2px 0 rgba(0,0,0,.18);
  border: 3px solid transparent;
  transition: border-color .2s, transform .2s, opacity .2s;
  opacity: .55;
}
.chip.current { opacity: 1; animation: chipPulse .9s infinite alternate; }
.chip.leader { border-color: gold; }
.chip.leader.current, .chip.leader { opacity: 1; }
@keyframes chipPulse {
  from { transform: scale(1); }
  to   { transform: scale(1.07); }
}
.chip .sym {
  font-size: 1.3rem;
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 1px 2px rgba(0,0,0,.3);
}
.chip .nm { font-weight: 700; font-size: .95rem; max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .ln {
  font-weight: 900;
  font-size: 1.15rem;
  background: rgba(255,255,255,.28);
  color: #fff;
  border-radius: 10px;
  padding: 2px 10px;
  min-width: 34px;
  text-align: center;
}

.hud-right { display: flex; align-items: center; gap: 10px; margin-left: auto; }

.icon-btn {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: var(--card);
  font-size: 1.3rem;
  box-shadow: 0 2px 0 rgba(0,0,0,.1);
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { transform: translateY(1px); }
.icon-btn[disabled] { opacity: .35; pointer-events: none; }

.shot-clock {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--card);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 2px 0 rgba(0,0,0,.1);
}
.shot-clock.urgent { color: #c92a2a; animation: pulse .4s infinite alternate; }

.turn-banner {
  width: 100%;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 1.35rem;
  color: #fff;
  background: var(--turn-color, var(--ink));
  border-radius: 14px;
  box-shadow: 0 2px 0 rgba(0,0,0,.18);
  transition: background-color .4s ease;
}

.end-bar {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 4px 0 2px;
}
.end-hold {
  min-height: 48px;
  padding: 10px 26px;
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  font-weight: 800;
  font-size: 1rem;
  box-shadow: 0 2px 0 rgba(0,0,0,.12);
}

.timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.6rem;
  font-weight: 900;
  background: var(--card);
  border-radius: 14px;
  padding: 6px 14px;
  box-shadow: 0 2px 0 rgba(0,0,0,.1);
}
.timer.urgent {
  color: #c92a2a;
  animation: pulse .5s infinite alternate;
}
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.12); } }

.board-wrap {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.board {
  display: grid;
  gap: 4px;
  width: min(96vw, calc(100dvh - 220px));
  aspect-ratio: 1;
  background: #e5d9c5;
  border-radius: 16px;
  padding: 6px;
}

.cell {
  background: var(--card);
  border-radius: 22%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  line-height: 1;
  color: var(--cell-color, var(--ink));
  padding: 0;
  transition: background .15s;
}
.cell:not(.taken):active { background: #ffe9d6; }
.cell.taken { cursor: default; }

.board.s6  .cell { font-size: min(9vmin, 60px); }
.board.s9  .cell { font-size: min(6.2vmin, 44px); }
.board.s12 .cell { font-size: min(4.6vmin, 34px); }

.cell.pop { animation: pop .25s ease-out; }
@keyframes pop {
  0% { transform: scale(.3); }
  70% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.cell.glow {
  animation: glow .8s ease-out;
}
@keyframes glow {
  0%   { box-shadow: 0 0 0 0 var(--cell-color); }
  50%  { box-shadow: 0 0 14px 4px var(--cell-color); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.cell.winline {
  background: #fff3bf;
  box-shadow: inset 0 0 0 3px gold;
}

/* Special squares */
.cell.special-star, .cell.special-bomb {
  animation: specialPulse 1s infinite alternate;
}
@keyframes specialPulse {
  from { transform: scale(1); }
  to   { transform: scale(1.12); }
}
.cell.special-star { background: #fff3bf; }
.cell.special-bomb { background: #ffe3e3; }

/* In-game toast (special explains, badges, AI reactions) */
.game-toast {
  position: absolute;
  top: 110px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 6px 18px rgba(0,0,0,.3);
  animation: riseIn .3s ease-out;
  max-width: 92vw;
  text-align: center;
}
#game { position: relative; }

.turn-banner.flash {
  background: gold !important;
  color: var(--ink);
  animation: pulse .3s 3 alternate;
}

.end-quip { opacity: .8; font-weight: 700; margin-bottom: 6px; }
.match-line { font-weight: 800; margin-bottom: 10px; }
.trophy.champion { font-size: 4.6rem; }

/* Game options sheet */
.options-panel > h2 { margin-bottom: 14px; }
.options-panel .setup-group { width: 100%; margin-bottom: 14px; }
.options-panel .setup-group h2 { font-size: .95rem; margin-bottom: 6px; }
.options-panel .big-btn { display: block; margin: 6px auto 0; }

/* Records & badges */
.badge-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: 12px;
  padding: 10px 14px;
  margin-bottom: 8px;
}
.badge-row.locked { opacity: .55; }
.badge-row .ht-ico { font-size: 1.5rem; flex-shrink: 0; }
.badge-row p { font-size: .92rem; line-height: 1.3; }
.badge-row b { display: block; }

/* Blocking hint: the leader could extend their longest line here */
.cell.danger {
  box-shadow: inset 0 0 0 3px var(--danger-color, #c92a2a);
  animation: dangerFade 1.2s infinite alternate;
}
@keyframes dangerFade {
  from { opacity: 1; }
  to   { opacity: .55; }
}

.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 30;
}

/* ---- hold-to-confirm new match button ---- */
.hold-btn { position: relative; overflow: hidden; }
.hold-btn .hold-fill {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--hold, 0) * 100%);
  background: rgba(232, 89, 12, .45);
  pointer-events: none;
}
.hold-btn .hold-ico { position: relative; }

.confirm-sub { opacity: .7; margin-bottom: 8px; }

/* ---- how-to-play coaching ---- */
.howto-btn {
  background: none;
  font-weight: 700;
  font-size: .95rem;
  color: var(--ink);
  opacity: .65;
  min-height: 44px;
  padding: 6px 14px;
  border-radius: 12px;
}
.howto-btn:active { opacity: 1; }
.howto { text-align: left; }
.howto h2 { text-align: center; }
.howto-step {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: 12px;
  padding: 10px 14px;
  margin-bottom: 8px;
}
.howto-step .ht-ico { font-size: 1.5rem; flex-shrink: 0; }
.howto-step p { font-size: .95rem; line-height: 1.35; }
.howto .big-btn { display: block; margin: 14px auto 0; }

/* ---- placement particles ---- */
.board { position: relative; }
.particle {
  position: absolute;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--pc, #e8590c);
  pointer-events: none;
  z-index: 2;
  transform: translate(-50%, -50%);
  animation: particleFly .6s ease-out forwards;
}
@keyframes particleFly {
  to {
    transform: translate(calc(var(--px) - 50%), calc(var(--py) - 50%)) scale(.1);
    opacity: 0;
  }
}

/* ---- leader crown ---- */
.chip .crown { font-size: 1rem; text-shadow: 0 1px 2px rgba(0,0,0,.3); }

/* ---- win celebration ---- */
.trophy {
  font-size: 3.4rem;
  line-height: 1;
  margin-bottom: 6px;
  display: inline-block;
  animation:
    trophyIn .8s cubic-bezier(.2, 1.6, .4, 1),
    trophyBob 1.6s .9s ease-in-out infinite alternate;
}
@keyframes trophyIn {
  from { transform: translateY(-70px) scale(.2); opacity: 0; }
  to   { transform: none; opacity: 1; }
}
@keyframes trophyBob {
  to { transform: translateY(-6px) rotate(-4deg); }
}

@media (prefers-reduced-motion: reduce) {
  .particle, .trophy { animation: none; }
}

/* ============ End overlay ============ */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(45, 42, 38, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

.panel {
  background: var(--bg);
  border-radius: 22px;
  padding: 26px 30px;
  width: min(440px, 92vw);
  max-height: 88dvh;
  overflow-y: auto;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,.35);
}
.panel h2 { font-size: 1.8rem; margin-bottom: 14px; }
.panel h3 { font-size: 1rem; margin: 16px 0 8px; opacity: .75; }

.end-lines, .end-tally {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.end-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--card);
  border-radius: 12px;
  padding: 8px 14px;
  font-weight: 700;
}
.end-row .sym { color: var(--chip-color); font-weight: 900; margin-right: 8px; }
.end-row.winner { box-shadow: inset 0 0 0 3px gold; }

.end-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
  flex-wrap: wrap;
}

.version {
  position: absolute;
  bottom: calc(6px + env(safe-area-inset-bottom));
  right: 12px;
  font-size: .75rem;
  font-weight: 700;
  opacity: .4;
  z-index: 1;
}

.update-toast {
  position: fixed;
  bottom: calc(14px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  font-size: .95rem;
  padding: 12px 20px;
  min-height: 48px;
  border-radius: 999px;
  box-shadow: 0 6px 18px rgba(0,0,0,.35);
  animation: riseIn .4s ease-out;
}

[hidden] { display: none !important; }
