:root {
  --font: "Be Vietnam Pro", Arial, sans-serif;
  --hand: "Itim", "Be Vietnam Pro", cursive;
  --paper: #fffdf6;
  --line: #d9e4f5;
  --margin: #f2a3a3;
  --ink: #23324f;
  --blue: #2f5fbf;
  --blue-soft: #e5eeff;
  --red: #e0503c;
  --green: #2f9e62;
  --gold: #f5b82e;
  --muted: #7a859b;
}
* {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}
html,
body {
  margin: 0;
  height: 100%;
}
body {
  background:
    radial-gradient(circle at 15% 10%, #ffe7b5 0, transparent 30%),
    radial-gradient(circle at 90% 90%, #cfe3ff 0, transparent 35%),
    #f4efe3;
  color: var(--ink);
  font-family: var(--font);
  overflow: hidden;
}
button,
input {
  font-family: var(--font);
}
button {
  cursor: pointer;
}

.app {
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
}
.top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
}
.back {
  color: var(--blue);
  font-weight: 700;
  font-size: 14px;
  text-decoration: none;
}
.top h1 {
  flex: 1;
  margin: 0;
  font-family: var(--hand);
  font-weight: 400;
  font-size: 28px;
  color: var(--blue);
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(35, 50, 79, 0.15);
  color: var(--ink);
  font-weight: 800;
}

/* ---------- trang vở ---------- */
.book {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  margin: 0 10px 10px;
  overflow: hidden;
  border-radius: 18px;
  background:
    linear-gradient(90deg, transparent 54px, var(--margin) 54px 56px, transparent 56px),
    repeating-linear-gradient(180deg, transparent 0 31px, var(--line) 31px 32px),
    var(--paper);
  box-shadow:
    0 1px 0 #e8dfc8,
    0 3px 0 #efe8d4,
    0 5px 0 #e3d9bf,
    0 16px 34px rgba(35, 50, 79, 0.18);
}

/* ---------- người chơi ---------- */
.players {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 14px 14px 10px 66px;
  background: linear-gradient(180deg, rgba(255, 253, 246, 0.97), rgba(255, 253, 246, 0.85));
  border-bottom: 2px dashed var(--line);
}
.pcard {
  position: relative;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 8px 8px;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 3px 10px rgba(35, 50, 79, 0.1);
  transition:
    box-shadow 0.25s,
    transform 0.25s;
}
.pcard.right {
  flex-direction: row-reverse;
  padding: 8px 8px 8px 12px;
  text-align: right;
}
.pcard.turn {
  box-shadow:
    0 0 0 3px var(--gold),
    0 6px 18px rgba(245, 184, 46, 0.35);
  transform: translateY(-2px);
}
.av-wrap {
  flex: none;
  width: 44px;
  height: 44px;
}
.av {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: var(--blue-soft);
  font-size: 24px;
}
.pinfo {
  display: grid;
  min-width: 0;
}
.nm {
  overflow: hidden;
  font-size: 15px;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.hearts {
  color: var(--red);
  font-size: 16px;
  letter-spacing: 2px;
}
.hearts i {
  font-style: normal;
  display: inline-block;
}
.hearts i.off {
  color: #d5d9e2;
}
.hearts i.pop {
  animation: heartLoss 0.7s;
}
@keyframes heartLoss {
  40% {
    transform: scale(1.8);
    color: var(--red);
  }
}
.cnt {
  margin-left: auto;
  padding: 2px 9px;
  border-radius: 99px;
  background: var(--blue-soft);
  color: var(--blue);
  font-weight: 800;
  font-size: 13px;
}
.pcard.right .cnt {
  margin-left: 0;
  margin-right: auto;
}
.vs {
  font-family: var(--hand);
  font-size: 22px;
  color: var(--margin);
}

/* ---------- chuỗi từ ---------- */
.chain {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 16px 16px 70px;
  scroll-behavior: smooth;
}
.chain:empty::before {
  content: "Chuỗi từ sẽ hiện ở đây";
  margin: auto;
  color: var(--muted);
  font-family: var(--hand);
  font-size: 20px;
}
.bubble {
  display: grid;
  max-width: 78%;
  animation: bubbleIn 0.35s cubic-bezier(0.3, 1.4, 0.5, 1);
}
.bubble.p0 {
  align-self: flex-start;
  justify-items: start;
}
.bubble.p1 {
  align-self: flex-end;
  justify-items: end;
}
.bubble .w {
  padding: 6px 14px 4px;
  border-radius: 16px;
  font-family: var(--hand);
  font-size: 24px;
  line-height: 1.2;
  box-shadow: 0 2px 6px rgba(35, 50, 79, 0.12);
}
.bubble.p0 .w {
  border-bottom-left-radius: 4px;
  background: var(--blue);
  color: #fff;
}
.bubble.p1 .w {
  border-bottom-right-radius: 4px;
  background: #fff;
  color: var(--ink);
}
.bubble .w b {
  font-weight: 400;
  padding: 0 3px;
  border-radius: 5px;
}
.bubble.p0 .w b {
  background: rgba(255, 255, 255, 0.22);
}
.bubble.p1 .w b {
  background: #fff2c4;
}
.bubble .w u {
  text-decoration: none;
  border-bottom: 3px solid var(--gold);
}
.bubble small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.bubble small em {
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 4px;
  background: #ece6ff;
  color: #6b4fc9;
  font-style: normal;
}
@keyframes bubbleIn {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.85);
  }
}
.sys {
  align-self: center;
  padding: 4px 12px;
  border-radius: 99px;
  background: #ffe6e1;
  color: #b2382a;
  font-weight: 700;
  font-size: 13px;
  animation: bubbleIn 0.35s;
}
.sys.new {
  background: #e3f6ea;
  color: var(--green);
}

/* ---------- ô nhập ---------- */
.composer {
  position: relative;
  display: grid;
  gap: 6px;
  padding: 10px 14px 14px 66px;
  background: linear-gradient(180deg, rgba(255, 253, 246, 0.85), #fffdf6 30%);
  border-top: 2px dashed var(--line);
}
.timer {
  position: relative;
  height: 8px;
  overflow: hidden;
  border-radius: 99px;
  background: #edf1f8;
}
.timer i {
  position: absolute;
  inset: 0;
  transform-origin: left;
  background: linear-gradient(90deg, var(--green), #7fd49b);
}
.timer.warn i {
  background: linear-gradient(90deg, var(--red), #ff9a7a);
}
.timer span {
  position: absolute;
  top: -20px;
  right: 0;
  color: var(--muted);
  font-weight: 800;
  font-size: 12px;
}
.timer.warn span {
  color: var(--red);
}
.prompt {
  min-height: 20px;
  color: var(--muted);
  font-weight: 700;
  font-size: 14px;
}
.prompt b {
  color: var(--blue);
}
.entry {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 14px;
  border-radius: 16px;
  background: #fff;
  box-shadow:
    inset 0 0 0 2px var(--line),
    0 3px 10px rgba(35, 50, 79, 0.08);
  transition: box-shadow 0.2s;
}
.entry:focus-within {
  box-shadow:
    inset 0 0 0 2px var(--blue),
    0 3px 14px rgba(47, 95, 191, 0.2);
}
.entry.shake {
  animation: shake 0.4s;
}
.entry.good {
  box-shadow:
    inset 0 0 0 2px var(--green),
    0 3px 14px rgba(47, 158, 98, 0.25);
}
@keyframes shake {
  20%,
  60% {
    transform: translateX(-7px);
  }
  40%,
  80% {
    transform: translateX(7px);
  }
}
.first {
  font-family: var(--hand);
  font-size: 26px;
  color: var(--blue);
  white-space: nowrap;
}
.first:empty {
  display: none;
}
.entry input {
  flex: 1;
  min-width: 0;
  height: 42px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-family: var(--hand);
  font-size: 26px;
}
.entry input::placeholder {
  color: #b8c0cf;
  font-size: 18px;
}
.choices {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.choices:empty {
  display: none;
}
.choices button {
  padding: 4px 12px;
  border: 0;
  border-radius: 99px;
  background: var(--blue-soft);
  color: var(--blue);
  font-family: var(--hand);
  font-size: 20px;
  animation: bubbleIn 0.25s;
}
.choices button:hover {
  background: var(--blue);
  color: #fff;
}
.msg {
  min-height: 20px;
  font-weight: 700;
  font-size: 13px;
}
.msg.err {
  color: var(--red);
}
.msg.ok {
  color: var(--green);
}
.msg.info {
  color: #8a5a00;
}
.tools {
  display: flex;
  gap: 8px;
}

/* ---------- nút ---------- */
.btn {
  height: 42px;
  padding: 0 16px;
  border: 0;
  border-radius: 13px;
  background: #fff;
  color: var(--ink);
  font-weight: 800;
  font-size: 14px;
  white-space: nowrap;
  box-shadow:
    inset 0 0 0 2px var(--line),
    0 3px 0 #cdd8ea;
  transition:
    transform 0.1s,
    opacity 0.2s;
}
.btn.primary {
  background: linear-gradient(180deg, #4b7ce0, var(--blue));
  color: #fff;
  box-shadow: 0 3px 0 #1f428a;
}
.btn.ghost {
  flex: 1;
}
.btn.big {
  height: 52px;
  font-size: 18px;
}
.btn:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: none;
}
.btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.reopen {
  position: absolute;
  bottom: 18px;
  left: 50%;
  z-index: 9;
  transform: translateX(-50%);
}

/* ---------- lớp phủ ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(35, 50, 79, 0.35);
  backdrop-filter: blur(3px);
  animation: fade 0.25s;
}
@keyframes fade {
  from {
    opacity: 0;
  }
}
.sheet {
  position: relative;
  display: grid;
  gap: 12px;
  width: min(100%, 420px);
  max-height: 100%;
  overflow: auto;
  padding: 22px;
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 20px 40px rgba(35, 50, 79, 0.3);
  animation: sheetIn 0.32s cubic-bezier(0.3, 1.4, 0.5, 1);
}
@keyframes sheetIn {
  from {
    transform: scale(0.9) translateY(12px);
    opacity: 0;
  }
}
.sheet h2 {
  margin: 0;
  font-family: var(--hand);
  font-weight: 400;
  font-size: 34px;
  color: var(--blue);
  text-align: center;
}
.sub {
  margin: -8px 0 0;
  color: var(--muted);
  font-weight: 600;
  font-size: 13px;
  text-align: center;
}
.logo {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  font-family: var(--hand);
  font-size: 22px;
}
.logo span {
  padding: 2px 10px;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  animation: bubbleIn 0.5s backwards;
}
.logo span:nth-of-type(2) {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(35, 50, 79, 0.15);
  animation-delay: 0.15s;
}
.logo span:nth-of-type(3) {
  animation-delay: 0.3s;
}
.logo i {
  font-style: normal;
  color: var(--margin);
}
.group {
  display: grid;
  gap: 6px;
}
.group label {
  color: var(--muted);
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.seg {
  display: flex;
  padding: 3px;
  border-radius: 12px;
  background: #e9eef8;
}
.seg button {
  flex: 1;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: #55607a;
  font-weight: 700;
  font-size: 14px;
}
.seg button.on {
  background: #fff;
  color: var(--blue);
  box-shadow: 0 1px 4px rgba(35, 50, 79, 0.18);
}
.names {
  display: flex;
  gap: 8px;
}
.names input {
  flex: 1;
  min-width: 0;
  height: 38px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-size: 15px;
}
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.stats div {
  display: grid;
  padding: 6px 2px;
  border-radius: 10px;
  background: #fff;
  box-shadow: inset 0 0 0 1px var(--line);
  text-align: center;
}
.stats b {
  font-size: 16px;
  color: var(--blue);
}
.stats small {
  color: var(--muted);
  font-weight: 600;
  font-size: 10px;
}
.stats p {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}
.res-ico {
  font-size: 52px;
  line-height: 1;
  text-align: center;
  animation: bounce 0.9s cubic-bezier(0.3, 1.6, 0.5, 1);
}
@keyframes bounce {
  from {
    transform: scale(0.2) rotate(-20deg);
  }
}
.res-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.res-grid div {
  display: grid;
  padding: 10px 4px;
  border-radius: 12px;
  background: #fff;
  box-shadow: inset 0 0 0 1px var(--line);
  text-align: center;
}
.res-grid b {
  font-family: var(--hand);
  font-weight: 400;
  font-size: 26px;
  color: var(--blue);
}
.res-grid small {
  color: var(--muted);
  font-weight: 600;
  font-size: 11px;
}
.row {
  display: grid;
  grid-template-columns: 1fr 1fr 1.2fr;
  gap: 8px;
}
.rules {
  width: min(100%, 520px);
  gap: 8px;
  font-size: 14px;
  line-height: 1.55;
}
.rules h3 {
  margin: 6px 0 0;
  color: var(--blue);
  font-size: 15px;
}
.rules p,
.rules ul {
  margin: 0;
}
.rules ul {
  padding-left: 20px;
}
.rules .small {
  color: var(--muted);
  font-size: 13px;
}
.close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: #e9eef8;
  font-size: 20px;
  line-height: 1;
}
.confetti {
  position: absolute;
  inset: 0;
  z-index: 11;
  overflow: hidden;
  pointer-events: none;
}
.confetti i {
  position: absolute;
  top: -20px;
  width: 9px;
  height: 14px;
  border-radius: 2px;
  animation: confetti var(--dur, 2.6s) var(--delay, 0s) cubic-bezier(0.25, 0.6, 0.5, 1) forwards;
}
@keyframes confetti {
  to {
    transform: translate(var(--dx, 0px), calc(100vh + 40px)) rotate(var(--spin, 720deg));
  }
}

/* ---------- điện thoại ---------- */
@media (max-width: 560px) {
  .top {
    padding: 8px 10px;
  }
  .top h1 {
    font-size: 24px;
  }
  .book {
    margin: 0 4px 4px;
    border-radius: 14px;
    background:
      linear-gradient(90deg, transparent 22px, var(--margin) 22px 23px, transparent 23px),
      repeating-linear-gradient(180deg, transparent 0 31px, var(--line) 31px 32px),
      var(--paper);
  }
  .players {
    gap: 6px;
    padding: 10px 8px 8px 30px;
  }
  .pcard {
    gap: 6px;
    padding: 6px 8px 6px 6px;
  }
  .pcard.right {
    padding: 6px 6px 6px 8px;
  }
  .av-wrap {
    width: 34px;
    height: 34px;
  }
  .av {
    font-size: 19px;
  }
  .nm {
    font-size: 13px;
  }
  .hearts {
    font-size: 13px;
    letter-spacing: 1px;
  }
  .cnt {
    position: absolute;
    top: -8px;
    right: -4px;
    padding: 1px 7px;
    font-size: 11px;
  }
  .pcard.right .cnt {
    right: auto;
    left: -4px;
  }
  .vs {
    font-size: 16px;
  }
  .chain {
    padding: 10px 10px 12px 32px;
  }
  .bubble {
    max-width: 88%;
  }
  .bubble .w {
    font-size: 21px;
  }
  .composer {
    padding: 8px 8px 10px 30px;
  }
  .first,
  .entry input {
    font-size: 22px;
  }
  .row {
    grid-template-columns: 1fr 1fr;
  }
  .row .primary {
    grid-column: 1 / -1;
  }
}
@media (max-height: 520px) and (orientation: landscape) {
  .top {
    padding: 4px 12px;
  }
  .top h1 {
    font-size: 20px;
  }
  .players {
    padding-top: 6px;
    padding-bottom: 6px;
  }
  .av-wrap {
    width: 30px;
    height: 30px;
  }
  .composer {
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: 8px;
    row-gap: 3px;
    padding-top: 6px;
    padding-bottom: 6px;
  }
  .timer,
  .prompt,
  .choices,
  .msg {
    grid-column: 1 / -1;
  }
  .timer {
    order: 1;
  }
  .prompt {
    order: 2;
    min-height: 0;
    font-size: 12px;
  }
  .entry {
    order: 3;
    padding-top: 3px;
    padding-bottom: 3px;
  }
  .entry input {
    height: 34px;
  }
  .tools {
    order: 4;
  }
  .tools .btn {
    height: 40px;
    padding: 0 10px;
  }
  .choices {
    order: 5;
  }
  .msg {
    order: 6;
    min-height: 16px;
  }
  .pcard {
    padding-top: 4px;
    padding-bottom: 4px;
  }
  .chain {
    padding-top: 6px;
  }
}
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
