:root {
  --bs: 360px; --side: 300px; --cs: calc((var(--bs) - 12px) / 9);
  --ink: #1f2a44; --user: #2f6fec; --bad: #e5404c; --thick: #34426a; --thin: #d3dbec;
  --sel: #b7cdff; --peer: #e9effc; --same: #cfdcff; --gold: #ffd36b;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; color: #eef1ff;
  background: radial-gradient(1000px 640px at 50% -10%, #2c3d86 0%, #16214f 50%, #0c1230 100%) fixed, #0f1738;
  font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  user-select: none; -webkit-user-select: none; overscroll-behavior: none; touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; color: inherit; }
.hidden { display: none !important; }
#app {
  height: 100%; 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);
}

/* ---------- đầu trang ---------- */
header { flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 10px 4px; }
.brand { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; font-weight: 900; font-size: 20px; letter-spacing: .3px; white-space: nowrap; }
.brand .logo { width: 28px; height: 28px; flex: none; filter: drop-shadow(0 2px 6px rgba(91, 116, 240, .6)); }
.brand span { background: linear-gradient(90deg, #b6c6ff, #8fe3ff); -webkit-background-clip: text; background-clip: text; color: transparent; }
.hbtn {
  flex: none; background: rgba(255, 255, 255, .08); border: 1px solid rgba(255, 255, 255, .16); border-radius: 10px;
  padding: 7px 10px; font-size: 14px; line-height: 1; font-weight: 700; white-space: nowrap;
}
.hbtn:hover { background: rgba(255, 255, 255, .15); }

/* ---------- bố cục ---------- */
#main { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 6px 10px 12px; touch-action: none; }
#left { width: var(--bs); flex: none; }
#right { width: var(--bs); flex: none; display: flex; flex-direction: column; gap: 10px; }
#app.wide #main { flex-direction: row; align-items: center; gap: 22px; }
#app.wide #right { width: var(--side); height: calc(var(--bs) + 40px); padding-top: 40px; justify-content: center; }

.info { display: flex; align-items: center; justify-content: space-between; gap: 8px; height: 40px; padding: 0 2px; font-size: 15px; }
.lvl { font-weight: 800; padding: 4px 12px; border-radius: 999px; background: rgba(255, 255, 255, .1); border: 1px solid rgba(255, 255, 255, .14); white-space: nowrap; }
.lvl.daily { background: linear-gradient(135deg, #ffcf5c, #ff9f43); color: #2a1800; border-color: transparent; }
.mist { opacity: .85; font-weight: 700; white-space: nowrap; }
.mist.bad { color: #ff9a9a; opacity: 1; animation: bump .3s ease; }
@keyframes bump { 50% { transform: scale(1.15); } }
.timer { display: flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; font-weight: 800; font-size: 17px; }
#btn-pause { width: 32px; height: 32px; border-radius: 50%; border: 1px solid rgba(255, 255, 255, .2); background: rgba(255, 255, 255, .1); padding: 0; display: grid; place-items: center; }
#btn-pause svg { width: 16px; height: 16px; fill: #fff; }

/* ---------- bảng ---------- */
#board-wrap { position: relative; width: var(--bs); height: var(--bs); }
#board {
  width: 100%; height: 100%; padding: 3px; border-radius: 14px; background: var(--thick);
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 3px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .45), 0 0 0 1px rgba(255, 255, 255, .08);
}
.box { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 1px; background: var(--thin); overflow: hidden; }
.box:nth-child(1) { border-top-left-radius: 11px; }
.box:nth-child(3) { border-top-right-radius: 11px; }
.box:nth-child(7) { border-bottom-left-radius: 11px; }
.box:nth-child(9) { border-bottom-right-radius: 11px; }
.cell {
  position: relative; background: #fff; display: grid; place-items: center; cursor: pointer; min-width: 0; min-height: 0;
  color: var(--ink); font-size: calc(var(--cs) * .6); font-weight: 600; line-height: 1; transition: background-color .12s;
}
.cell .v { position: relative; z-index: 1; }
.cell.given { font-weight: 800; }
.cell.user { color: var(--user); }
.cell.hinted { color: #0f9a6c; }
.cell.wrong { color: var(--bad); }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.clash { background: #ffe0e2; }
.cell.sel { background: var(--sel); }
.cell.wrong.sel { background: #ffc9cf; }
.cell .nt { position: absolute; inset: 2px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); pointer-events: none; }
.cell .nt i { font-style: normal; font-size: calc(var(--cs) * .27); font-weight: 700; color: #6a7593; display: grid; place-items: center; line-height: 1; }
.cell .nt i.hl { color: #fff; background: var(--user); border-radius: 4px; }
.cell.pop .v { animation: pop .28s cubic-bezier(.2, 1.6, .4, 1); }
@keyframes pop { from { transform: scale(.4); opacity: .3; } }
.cell.shake .v { animation: shake .35s ease; }
@keyframes shake { 20%, 60% { transform: translateX(-12%); } 40%, 80% { transform: translateX(12%); } }
.cell.hint-fx::after {
  content: ''; position: absolute; inset: 0; background: radial-gradient(circle, rgba(60, 220, 160, .55), rgba(60, 220, 160, 0) 70%);
  animation: glow .9s ease forwards; pointer-events: none;
}
@keyframes glow { from { transform: scale(.3); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }
.cell.flash::before {
  content: ''; position: absolute; inset: 0; background: linear-gradient(135deg, #8fb3ff, #b6f0ff); opacity: 0;
  animation: flash .7s ease forwards; animation-delay: inherit; pointer-events: none;
}
@keyframes flash { 25% { opacity: .85; } 100% { opacity: 0; } }
.cell.winfx { animation: winc .9s ease both; }
@keyframes winc { 0% { background: #fff; } 35% { background: #ffe39a; transform: scale(1.12); } 100% { background: #fff8e1; transform: none; } }
#board.done .cell { color: #8a5a00; background: #fff8e1; }

#pause-cover {
  position: absolute; inset: 0; border-radius: 14px; z-index: 3; display: grid; place-items: center; text-align: center;
  background: rgba(20, 30, 70, .82); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
#pause-cover p { margin: 0 0 12px; font-size: 20px; font-weight: 800; }

/* ---------- công cụ + bàn phím số ---------- */
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tools button {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 2px 7px; border-radius: 14px;
  background: rgba(255, 255, 255, .08); border: 1px solid rgba(255, 255, 255, .13); font-size: 12px; font-weight: 700;
  transition: background .15s, transform .08s;
}
.tools button:hover { background: rgba(255, 255, 255, .14); }
.tools button:active { transform: scale(.95); }
.tools svg { width: 24px; height: 24px; }
.tools em {
  position: absolute; top: -6px; right: 4px; font-style: normal; font-size: 10px; font-weight: 900; padding: 2px 6px; border-radius: 999px;
  background: #56607e; color: #fff; letter-spacing: .3px;
}
#t-notes.on { background: linear-gradient(135deg, #8aa6ff, #5b74f0); border-color: transparent; box-shadow: 0 4px 14px rgba(80, 110, 255, .45); }
#t-notes.on em { background: #ffd36b; color: #2a1800; }
#t-hint em { background: #0f9a6c; }
#t-hint.empty { opacity: .5; }
#pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
#pad button {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 62px; border-radius: 12px; padding: 0;
  background: linear-gradient(180deg, #ffffff, #e6ecfb); color: #2a4bd6; border: 0; box-shadow: 0 3px 0 #9fb0dd, 0 6px 14px rgba(0, 0, 0, .3);
  transition: transform .08s, box-shadow .08s, opacity .2s;
}
#pad button b { font-size: 28px; line-height: 1; font-weight: 800; }
#pad button small { font-size: 11px; color: #8590ad; font-weight: 700; margin-top: 2px; }
#pad button:active { transform: translateY(3px); box-shadow: 0 0 0 #9fb0dd, 0 3px 8px rgba(0, 0, 0, .3); }
#pad.notes button { color: #6a7593; }
#pad.notes button b { font-size: 22px; }
#pad button.done { opacity: .28; }
#pad button.done small { visibility: hidden; }
#pad button.pop { animation: padpop .45s ease; }
@keyframes padpop { 40% { transform: scale(1.18); background: #ffe39a; } }
#app.wide #pad { grid-template-columns: repeat(3, 1fr); grid-auto-rows: var(--padrow, 84px); gap: 10px; }
#app.wide #pad button { height: auto; }
#app.wide #pad button b { font-size: clamp(26px, calc(var(--side) / 9), 40px); }
#app.wide .tools button { padding: 10px 2px 9px; font-size: clamp(11px, calc(var(--side) / 26), 13px); }
.tools span { white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }

/* ---------- lớp phủ ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: rgba(6, 10, 34, .6); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0; } }
.panel {
  position: relative; width: 100%; max-width: 410px; max-height: 100%; overflow: auto; text-align: center; padding: 22px 22px 18px; border-radius: 20px;
  background: linear-gradient(165deg, rgba(42, 56, 130, .97), rgba(16, 22, 60, .97));
  border: 1px solid rgba(170, 185, 255, .25); box-shadow: 0 24px 60px rgba(0, 0, 0, .6), 0 0 40px rgba(110, 140, 255, .2);
  animation: rise .35s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes rise { from { transform: scale(.88) translateY(12px); opacity: 0; } }
.panel h1 { margin: 0 0 4px; font-size: 32px; background: linear-gradient(90deg, #c4d0ff, #8fe3ff); -webkit-background-clip: text; background-clip: text; color: transparent; }
.panel .sub { margin: 0 0 14px; font-size: 14px; opacity: .8; line-height: 1.45; }
.x { position: absolute; top: 8px; right: 10px; width: 34px; height: 34px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .08); font-size: 22px; line-height: 1; }
.big {
  display: flex; flex-direction: column; align-items: center; width: 100%; gap: 2px; padding: 12px; margin: 0 0 10px; border-radius: 14px;
  background: rgba(255, 255, 255, .08); border: 1px solid rgba(255, 255, 255, .16); transition: transform .1s, filter .15s;
}
.big b { font-size: 17px; }
.big small { font-size: 13px; opacity: .8; }
.big:hover { filter: brightness(1.1); }
.big:active { transform: scale(.98); }
.big.primary { background: linear-gradient(135deg, #8aa6ff, #5b74f0); border: 0; box-shadow: 0 6px 18px rgba(80, 110, 255, .45); }
.big.daily { background: linear-gradient(135deg, rgba(255, 207, 92, .2), rgba(255, 159, 67, .14)); border-color: rgba(255, 200, 100, .45); }
.big.daily.done small { color: #8ff0b8; opacity: 1; }
.m-label { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; opacity: .6; margin: 6px 0 8px; font-weight: 800; }
.levels { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.levels button {
  padding: 10px 6px; border-radius: 12px; background: rgba(255, 255, 255, .07); border: 1px solid rgba(255, 255, 255, .14);
  display: flex; flex-direction: column; align-items: center; gap: 2px; transition: background .15s, transform .1s;
}
.levels button:hover { background: rgba(255, 255, 255, .14); }
.levels button:active { transform: scale(.97); }
.levels b { font-size: 16px; }
.levels .dots { letter-spacing: 2px; font-size: 10px; color: var(--gold); }
.levels small { font-size: 11px; opacity: .65; }
.switch { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 14px 0 6px; font-size: 14px; cursor: pointer; }
.switch input { display: none; }
.switch .sl { width: 42px; height: 24px; border-radius: 999px; background: rgba(255, 255, 255, .2); position: relative; transition: background .2s; flex: none; }
.switch .sl::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked + .sl { background: #5b74f0; }
.switch input:checked + .sl::after { transform: translateX(18px); }
.m-stats { font-size: 13px; opacity: .8; margin: 4px 0 0; line-height: 1.5; }
.gnx-btn {
  display: inline-block; margin-top: 10px; background: rgba(255, 255, 255, .1); color: #fff; border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 10px; padding: 8px 14px; font-size: 14px; font-weight: 700;
}
.r-icon { font-size: 46px; line-height: 1; margin-bottom: 4px; }
.panel h2 { margin: 0 0 10px; font-size: 26px; }
#r-panel.win h2 { background: linear-gradient(90deg, #ffe08a, #ff9ccb); -webkit-background-clip: text; background-clip: text; color: transparent; }
#r-panel.lose h2 { color: #ff9a9a; }
.r-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 10px; }
.r-stats div { background: rgba(255, 255, 255, .07); border-radius: 12px; padding: 8px 4px; }
.r-stats span { display: block; font-size: 12px; opacity: .7; }
.r-stats b { display: block; font-size: 19px; font-variant-numeric: tabular-nums; margin-top: 2px; }
.r-stats b.gold { color: var(--gold); }
.r-more { font-size: 13px; opacity: .9; line-height: 1.5; }
.r-more p { margin: 4px 0; }
.badge { display: inline-block; margin: 0 0 8px; padding: 4px 12px; border-radius: 999px; font-size: 13px; font-weight: 800; color: #2a1800; background: linear-gradient(90deg, #ffe08a, #ffb347); animation: pulse 1.2s ease infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }
.p-btns { display: flex; gap: 10px; justify-content: center; margin-top: 12px; flex-wrap: wrap; }
.p-btns button, #pause-cover button { font-size: 16px; font-weight: 800; padding: 11px 18px; border-radius: 13px; border: 1px solid rgba(255, 255, 255, .2); background: rgba(255, 255, 255, .1); }
button.primary { border: 0 !important; color: #fff; background: linear-gradient(135deg, #8aa6ff, #5b74f0) !important; box-shadow: 0 5px 16px rgba(80, 110, 255, .45); }
.p-btns button:hover { filter: brightness(1.1); }
#loading { flex-direction: column; gap: 14px; font-weight: 700; }
#loading p { margin: 0; }
.spin { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255, 255, 255, .2); border-top-color: #8aa6ff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

@media (max-width: 420px) {
  #t-new { display: none; }
  .brand { font-size: 18px; }
  #pad { gap: 4px; }
  #pad button b { font-size: 25px; }
}
@media (max-height: 560px) {
  header { padding: 4px 10px 2px; }
  #main { padding: 2px 10px 6px; }
  .tools button { padding: 5px 2px 4px; font-size: 11px; }
  .tools svg { width: 20px; height: 20px; }
}
