:root {
  --bg1: #1b1347;
  --bg2: #2b1d6b;
  --ink: #fff8ec;
  --muted: #b9b0e6;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-line: rgba(255, 255, 255, 0.14);
  --cell: rgba(255, 255, 255, 0.055);
  --cell-line: rgba(255, 255, 255, 0.09);
  --t1: #ffe29a; --t1d: #e5b54a;   /* 1 pt  */
  --t2: #a8f0c6; --t2d: #4fc589;   /* 2-3   */
  --t3: #9fd8ff; --t3d: #4aa3e0;   /* 4-5   */
  --t4: #ffb3d1; --t4d: #e5609a;   /* 8-10  */
  --stone: #9a95ad; --stoned: #6b6680;
  --sel: #ff8a3d; --seld: #d4561a;
  --good: #5ef0a0;
  --bad: #ff6b81;
  --accent: #ffd23f;
  --font: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Nunito', system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg1);
  -webkit-tap-highlight-color: transparent;
}
body::before {
  content: ''; position: fixed; inset: 0; z-index: -1;
  background: radial-gradient(1200px 800px at 20% -10%, #4b2fa8 0%, transparent 60%),
              radial-gradient(900px 700px at 110% 110%, #7a2f8f 0%, transparent 55%),
              linear-gradient(160deg, var(--bg1), var(--bg2));
}
a { color: var(--accent); }
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- buttons ---------- */
.btn {
  border: 0; border-radius: 14px; padding: 0.8em 1.2em; font-size: 1.05rem; font-weight: 600;
  color: #2a1a00; background: var(--accent); box-shadow: 0 5px 0 #c99a00;
  transition: transform 0.08s, box-shadow 0.08s; letter-spacing: 0.01em;
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #c99a00; }
.btn.big { font-size: 1.35rem; padding: 0.8em 1.4em; width: 100%; }
.btn.green { background: var(--good); box-shadow: 0 5px 0 #23a862; color: #052a16; }
.btn.green:active { box-shadow: 0 1px 0 #23a862; }
.btn.pink { background: var(--t4); box-shadow: 0 5px 0 var(--t4d); color: #3a0018; }
.btn.pink:active { box-shadow: 0 1px 0 var(--t4d); }
.btn.blue { background: var(--t3); box-shadow: 0 5px 0 var(--t3d); color: #002238; }
.btn.blue:active { box-shadow: 0 1px 0 var(--t3d); }
.btn.ghost { background: var(--panel); color: var(--ink); box-shadow: 0 4px 0 rgba(0,0,0,0.25); border: 1px solid var(--panel-line); }
.btn.ghost:active { box-shadow: 0 1px 0 rgba(0,0,0,0.25); }
.btn:disabled { opacity: 0.45; pointer-events: none; }

/* ---------- logo tiles ---------- */
.logo { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.35em; margin: 0.2em 0 0.1em; }
.logo .word { display: flex; gap: 0.18em; }
.ltile {
  width: 1.7em; height: 1.7em; display: grid; place-items: center; border-radius: 0.3em;
  font-weight: 700; color: #2a1a00; background: var(--t1); box-shadow: 0 0.16em 0 var(--t1d);
  animation: bounceIn 0.6s cubic-bezier(.3,1.6,.5,1) both;
}
.ltile:nth-child(3n+2) { background: var(--t2); box-shadow: 0 0.16em 0 var(--t2d); }
.ltile:nth-child(3n+3) { background: var(--t3); box-shadow: 0 0.16em 0 var(--t3d); }
.word + .word .ltile:nth-child(2n) { background: var(--t4); box-shadow: 0 0.16em 0 var(--t4d); }
@keyframes bounceIn { from { transform: translateY(-120%) scale(0.6); opacity: 0; } }

/* ---------- landing ---------- */
.landing { max-width: 980px; margin: 0 auto; padding: 1.2rem 1rem 3rem; }
.landing header { text-align: center; font-size: clamp(1.4rem, 5vw, 2.4rem); }
.tagline { text-align: center; color: var(--muted); font-size: 1.1rem; margin: 0.6rem 0 1.6rem; }
.cols { display: grid; gap: 1.2rem; grid-template-columns: 1fr; }
@media (min-width: 820px) { .cols { grid-template-columns: 1.05fr 1fr; } }
.card { background: var(--panel); border: 1px solid var(--panel-line); border-radius: 22px; padding: 1.2rem; backdrop-filter: blur(6px); }
.card h2 { margin: 0 0 0.8rem; font-size: 1.25rem; font-weight: 600; }
.field { display: block; margin-bottom: 1rem; }
.field span { display: block; font-size: 0.9rem; color: var(--muted); margin-bottom: 0.35rem; }
.input {
  width: 100%; font-size: 1.2rem; padding: 0.65em 0.8em; border-radius: 12px; border: 2px solid var(--panel-line);
  background: rgba(0,0,0,0.25); color: var(--ink); outline: none;
}
.input:focus { border-color: var(--accent); }
.stack { display: grid; gap: 0.9rem; }
.row { display: flex; gap: 0.6rem; }
.row > * { flex: 1; }
.divider { display: flex; align-items: center; gap: 0.6rem; color: var(--muted); font-size: 0.9rem; margin: 0.4rem 0; }
.divider::before, .divider::after { content: ''; height: 1px; flex: 1; background: var(--panel-line); }
.join-row { display: flex; gap: 0.6rem; }
.join-row .input { text-transform: uppercase; letter-spacing: 0.3em; text-align: center; flex: 1; min-width: 0; }
.how { list-style: none; padding: 0; margin: 0; display: grid; gap: 0.55rem; color: var(--muted); }
.how li { display: flex; gap: 0.6rem; align-items: flex-start; }
.how b { color: var(--ink); font-weight: 600; }
.how .ic { flex: none; width: 1.8em; text-align: center; font-size: 1.1rem; }

/* ---------- leaderboard ---------- */
.tabs { display: inline-flex; background: rgba(0,0,0,0.25); border-radius: 12px; padding: 4px; margin-bottom: 0.8rem; }
.tabs button { border: 0; background: none; color: var(--muted); padding: 0.45em 1em; border-radius: 9px; font-size: 1rem; font-weight: 600; }
.tabs button.on { background: var(--accent); color: #2a1a00; }
.lb { width: 100%; border-collapse: collapse; font-size: 1rem; }
.lb th { text-align: left; font-weight: 500; color: var(--muted); font-size: 0.85rem; padding: 0.3rem 0.5rem; }
.lb td { padding: 0.5rem; border-top: 1px solid var(--panel-line); }
.lb td.num, .lb th.num { text-align: right; font-variant-numeric: tabular-nums; }
.lb .rank { width: 2.4em; color: var(--muted); }
.lb tbody tr:nth-child(1) .rank::before { content: '🥇'; }
.lb tbody tr:nth-child(2) .rank::before { content: '🥈'; }
.lb tbody tr:nth-child(3) .rank::before { content: '🥉'; }
.lb.full tr .rank::before { content: none; }
.lb .best { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.lb .me td { background: rgba(255, 210, 63, 0.14); }
.lb-empty { color: var(--muted); padding: 1rem 0.5rem; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 1rem; color: var(--muted); }
.hide-sm { }
@media (max-width: 560px) { .hide-sm { display: none; } }
.foot { text-align: center; color: var(--muted); font-size: 0.85rem; margin-top: 2rem; }

/* ---------- game layout ---------- */
body.game { overflow: hidden; overscroll-behavior: none; position: fixed; inset: 0; }
#app { position: fixed; inset: 0; display: flex; flex-direction: column; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.hud { flex: none; display: flex; align-items: center; gap: 0.5rem; padding: 0.5rem 0.7rem; }
.hud .btn { padding: 0.45em 0.8em; font-size: 0.95rem; }
.hud .spacer { flex: 1; }
.stat { background: var(--panel); border: 1px solid var(--panel-line); border-radius: 12px; padding: 0.25em 0.75em; text-align: center; min-width: 4.5em; }
.stat small { display: block; font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.stat b { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.stat.warn b { color: var(--bad); }
.icon-btn { background: var(--panel); border: 1px solid var(--panel-line); color: var(--ink); border-radius: 12px; width: 2.6rem; height: 2.6rem; font-size: 1.15rem; }

#arena { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; gap: 1rem; padding: 0 0.6rem; }
#arena.portrait { flex-direction: column-reverse; gap: 0.4rem; }
.side { display: flex; flex-direction: column; align-items: center; gap: 0.3rem; }
.side .label { font-size: 0.95rem; color: var(--muted); display: flex; gap: 0.5rem; align-items: baseline; max-width: 100%; }
.side .label .nm { color: var(--ink); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 9em; }
.side .label .sc { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.side .fill { width: 100%; height: 5px; border-radius: 3px; background: rgba(255,255,255,0.1); overflow: hidden; }
.side .fill i { display: block; height: 100%; width: 0; background: var(--good); transition: width 0.3s, background 0.3s; }
.side .fill i.mid { background: var(--accent); }
.side .fill i.hot { background: var(--bad); }

.grid {
  --n: 10; position: relative; display: grid; grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr);
  gap: 3px; padding: 5px; border-radius: 14px; background: rgba(0,0,0,0.28); border: 2px solid var(--panel-line);
  touch-action: none; user-select: none; -webkit-user-select: none; transition: border-color 0.3s, box-shadow 0.3s;
}
.grid.danger { border-color: var(--bad); animation: danger 0.9s ease-in-out infinite; }
@keyframes danger { 50% { box-shadow: 0 0 0 4px rgba(255,107,129,0.25), 0 0 30px rgba(255,107,129,0.35); } }
.grid.ko { filter: grayscale(0.8) brightness(0.7); }
.cell { position: relative; border-radius: 18%; background: var(--cell); outline: 1px solid var(--cell-line); outline-offset: -1px; }
.cell.target { background: rgba(255, 210, 63, 0.25); }

.tile {
  position: absolute; inset: 0; border-radius: 18%; display: grid; place-items: center;
  font-weight: 700; color: #2a1a00; background: var(--t1); box-shadow: 0 var(--d, 3px) 0 var(--t1d);
  font-size: calc(var(--cs, 40px) * 0.5); line-height: 1; cursor: pointer;
  transition: transform 0.08s, background 0.12s;
}
.tile.qu { font-size: calc(var(--cs, 40px) * 0.36); letter-spacing: -0.02em; }
.tile .pt { position: absolute; right: 9%; bottom: 6%; font-size: calc(var(--cs, 40px) * 0.2); font-weight: 600; opacity: 0.6; }
.tile.v2 { background: var(--t2); box-shadow: 0 var(--d,3px) 0 var(--t2d); }
.tile.v3 { background: var(--t3); box-shadow: 0 var(--d,3px) 0 var(--t3d); }
.tile.v4 { background: var(--t4); box-shadow: 0 var(--d,3px) 0 var(--t4d); }
.tile.stone {
  background: repeating-linear-gradient(135deg, var(--stone) 0 6px, #8d88a2 6px 12px); box-shadow: 0 var(--d,3px) 0 var(--stoned); color: #2a2838;
}
.tile.sel { background: var(--sel); box-shadow: 0 var(--d,3px) 0 var(--seld); color: #fff; transform: translateY(-2px) scale(1.04); z-index: 2; }
.tile .ord {
  position: absolute; left: -12%; top: -14%; min-width: 46%; height: 46%; padding: 0 3px; border-radius: 99px; background: #fff; color: var(--seld);
  font-size: calc(var(--cs, 40px) * 0.24); display: grid; place-items: center; font-weight: 700; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.tile.drop { animation: drop 0.42s cubic-bezier(.3,1.5,.5,1) both; }
@keyframes drop { from { transform: translateY(-260%) scale(0.7); opacity: 0; } 60% { opacity: 1; } }
.tile.junk-in { animation: thud 0.5s ease-out both; }
@keyframes thud { from { transform: scale(1.8); opacity: 0; } 60% { transform: scale(0.92); opacity: 1; } }
.tile.dragging { opacity: 0.25; }
.drag-ghost { position: fixed; z-index: 50; pointer-events: none; transform: translate(-50%, -60%) scale(1.15) rotate(-4deg); box-shadow: 0 10px 25px rgba(0,0,0,0.45) !important; }

.opp .grid { gap: 2px; padding: 3px; border-radius: 10px; }
.opp .tile { cursor: default; }
.opp .tile .pt { display: none; }

/* chain line overlay */
.chain-svg { position: absolute; inset: 0; pointer-events: none; z-index: 1; overflow: visible; }
.chain-svg polyline { fill: none; stroke: rgba(255, 138, 61, 0.55); stroke-linecap: round; stroke-linejoin: round; }

/* ---------- tray ---------- */
.tray { flex: none; display: flex; align-items: center; gap: 0.6rem; padding: 0.6rem 0.8rem 0.8rem; max-width: 760px; width: 100%; margin: 0 auto; }
.tray .btn { padding: 0.75em 1em; font-size: 1.1rem; flex: none; }
.wordbox {
  flex: 1; min-width: 0; min-height: 3.3rem; border-radius: 14px; background: rgba(0,0,0,0.3); border: 2px dashed var(--panel-line);
  display: flex; align-items: center; justify-content: center; gap: 0.6rem; padding: 0.3rem 0.7rem; position: relative;
}
.wordbox .w { font-size: clamp(1.2rem, 4.5vw, 1.8rem); font-weight: 700; letter-spacing: 0.12em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wordbox .hint { color: var(--muted); font-size: 0.95rem; text-align: center; }
.wordbox .pv { font-size: 0.9rem; color: var(--accent); white-space: nowrap; font-weight: 600; }
.wordbox .chainy { font-size: 0.75rem; background: var(--sel); color: #fff; border-radius: 99px; padding: 0.1em 0.5em; white-space: nowrap; }
.wordbox.shake { animation: shake 0.4s; border-color: var(--bad); }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }

/* ---------- floaters / toasts ---------- */
.floater { position: fixed; z-index: 60; pointer-events: none; font-weight: 700; text-align: center; transform: translate(-50%, -50%);
  animation: floatUp 1.3s ease-out forwards; text-shadow: 0 3px 0 rgba(0,0,0,0.35); white-space: nowrap; }
.floater .pts { font-size: 2.2rem; color: var(--accent); display: block; }
.floater .lbl { font-size: 1rem; color: #fff; display: block; }
@keyframes floatUp { from { opacity: 0; transform: translate(-50%, -20%) scale(0.6); } 15% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); } to { opacity: 0; transform: translate(-50%, -160%) scale(1); } }
.poof { position: fixed; z-index: 55; pointer-events: none; border-radius: 50%; background: var(--accent); animation: poof 0.55s ease-out forwards; }
@keyframes poof { from { transform: translate(-50%,-50%) scale(0.3); opacity: 0.9; } to { transform: translate(-50%,-50%) scale(1.7); opacity: 0; } }
.toast { position: fixed; left: 50%; top: 4.2rem; transform: translateX(-50%); z-index: 70; background: rgba(20,12,50,0.92); border: 1px solid var(--panel-line);
  padding: 0.5em 1em; border-radius: 12px; font-size: 1rem; animation: toast 2.2s ease forwards; pointer-events: none; white-space: nowrap; }
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, -10px); } 10%, 80% { opacity: 1; transform: translate(-50%, 0); } 100% { opacity: 0; } }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; background: rgba(12, 6, 35, 0.72); backdrop-filter: blur(4px); padding: 1rem; overflow-y: auto; }
.overlay[hidden] { display: none; }
.modal { background: linear-gradient(160deg, #2e2075, #22175a); border: 1px solid var(--panel-line); border-radius: 24px; padding: 1.4rem; width: min(460px, 100%); text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,0.5); }
.modal h2 { margin: 0 0 0.4rem; font-size: 1.7rem; }
.modal p { color: var(--muted); margin: 0.4rem 0 1rem; }
.code { font-size: 2.8rem; letter-spacing: 0.3em; font-weight: 700; color: var(--accent); margin: 0.3rem 0; padding-left: 0.3em; }
.countdown { font-size: 8rem; font-weight: 700; color: var(--accent); text-shadow: 0 6px 0 rgba(0,0,0,0.35); animation: pulse 1s ease-out infinite; }
@keyframes pulse { from { transform: scale(1.4); opacity: 0; } 30% { opacity: 1; transform: scale(1); } }
.spinner { width: 46px; height: 46px; margin: 0.6rem auto 1rem; border-radius: 50%; border: 5px solid var(--panel-line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.big-score { font-size: 3.2rem; font-weight: 700; color: var(--accent); line-height: 1.1; }
.versus { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 0.6rem; margin: 0.8rem 0; }
.versus .who { background: var(--panel); border-radius: 14px; padding: 0.6rem; }
.versus .who.win { outline: 2px solid var(--accent); }
.versus .who .n { font-size: 0.95rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.versus .who .s { font-size: 1.7rem; font-weight: 700; }
.chips { display: flex; flex-wrap: wrap; gap: 0.35rem; justify-content: center; max-height: 7.5rem; overflow-y: auto; margin: 0.6rem 0 1rem; }
.chip { background: var(--panel); border-radius: 8px; padding: 0.15em 0.5em; font-size: 0.85rem; letter-spacing: 0.05em; }
.chip.best { background: var(--accent); color: #2a1a00; font-weight: 600; }
.modal .stack { margin-top: 0.6rem; }
.small { font-size: 0.9rem; color: var(--muted); }
.copy-link { word-break: break-all; font-size: 0.85rem; color: var(--muted); background: rgba(0,0,0,0.25); padding: 0.5em; border-radius: 8px; margin-bottom: 0.8rem; }

.icon-btn.off { opacity: 0.45; }
