*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  color: var(--color-text);
  font-family: var(--font-body);
  line-height: var(--line-height-body);
  position: relative;
  overflow-x: hidden;
  /* Chợ đêm đèn lồng (§6.1 YEU-CAU-UPDATE-2.md): trời tím có sao, mờ dần sang
     sắc ấm phía dưới nơi sạp gỗ đứng. */
  background:
    radial-gradient(1.5px 1.5px at 10% 12%, rgba(255,255,255,0.9), transparent),
    radial-gradient(1.5px 1.5px at 30% 28%, rgba(255,255,255,0.7), transparent),
    radial-gradient(1.5px 1.5px at 52% 8%, rgba(255,255,255,0.85), transparent),
    radial-gradient(1.5px 1.5px at 72% 22%, rgba(255,255,255,0.65), transparent),
    radial-gradient(1.5px 1.5px at 88% 14%, rgba(255,255,255,0.8), transparent),
    radial-gradient(1.5px 1.5px at 20% 45%, rgba(255,255,255,0.5), transparent),
    radial-gradient(1.5px 1.5px at 65% 40%, rgba(255,255,255,0.55), transparent),
    linear-gradient(180deg, #221539 0%, #3a2655 35%, #4a3420 75%, #2b1b10 100%);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

/* Dây đèn lồng nhiều màu vắt ngang phía trên */
body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 10px;
  background: repeating-linear-gradient(
    90deg,
    #ffd166 0 8px,
    transparent 8px 26px,
    #ef476f 26px 34px,
    transparent 34px 52px,
    #ffd166 52px 60px,
    transparent 60px 78px
  );
  opacity: 0.9;
  pointer-events: none;
  z-index: 0;
}

.lantern {
  position: fixed;
  top: 18px;
  width: 46px;
  height: 64px;
  pointer-events: none;
  z-index: 0;
  display: none;
}
.lantern-left { left: 24px; }
.lantern-right { right: 24px; }
.lantern::before {
  content: "";
  position: absolute;
  inset: 8px 2px 10px 2px;
  border-radius: 50% 50% 45% 45%;
  background: radial-gradient(circle at 35% 30%, #ff8a65, #d94343 70%);
  box-shadow: 0 0 14px 2px rgba(217, 67, 67, 0.55);
}
.lantern::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 2px;
  height: 10px;
  background: #caa43d;
  transform: translateX(-50%);
}
@media (min-width: 560px) {
  .lantern { display: block; }
}

.game {
  width: 100%;
  max-width: 400px;
  position: relative;
  z-index: 1;
}

/* Hiện đúng 1 màn hình theo trạng thái game (registering/idle/playing/finished) */
.screen { display: none; }
.game[data-state="registering"] .screen-register { display: block; }
.game[data-state="idle"] .screen-start { display: block; }
.game[data-state="playing"] .screen-play { display: block; }
.game[data-state="finished"] .screen-end { display: block; }

.panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-6) var(--space-5);
  text-align: center;
  box-shadow: var(--shadow-card);
}

.title {
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-2xl);
  letter-spacing: var(--letter-spacing-display);
  margin: 0 0 var(--space-3);
}

.tagline {
  color: var(--color-text-muted);
  font-size: var(--font-size-md);
  margin: 0 0 var(--space-4);
}

.high-score,
.end-high {
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-md);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-5);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: var(--control-height-md);
  padding: 0 var(--space-6);
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-md);
  cursor: pointer;
  transition: background var(--transition-fast), transform var(--transition-fast);
}
/* Nút đèn lồng đỏ (§6.1 YEU-CAU-UPDATE-2.md), ghi đè cục bộ trong phạm vi .game */
.game { --color-accent: #d94343; --color-accent-hover: #b93636; }
.btn-primary { background: var(--color-accent); color: var(--color-accent-text); }
.btn-primary:hover { background: var(--color-accent-hover); }
.btn-primary:active { transform: scale(0.98); }
.btn:focus-visible { outline: 3px solid var(--color-focus); outline-offset: 2px; }
.btn-lg { height: var(--control-height-lg); font-size: var(--font-size-lg); }

.hud {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-lg);
  padding: 0 var(--space-1);
  margin-bottom: var(--space-4);
}

.hud-time {
  transition: color var(--transition-fast);
}
.hud-time.low-time {
  color: var(--color-danger);
}

/* ---------- Sạp chợ (mục 6.1 / §4.9) ---------- */
.market-stall { position: relative; }
.market-banner {
  height: 14px;
  border-radius: var(--radius-pill) var(--radius-pill) 0 0;
  background: repeating-linear-gradient(
    45deg,
    var(--c-red-500) 0 14px,
    var(--c-cream-50) 14px 28px
  );
  margin-bottom: var(--space-2);
}
.grid-wrap {
  position: relative;
  background: linear-gradient(180deg, #caa056, #a1783c);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  box-shadow: var(--shadow-card);
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-3);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.cell {
  position: relative;
  aspect-ratio: 1;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  overflow: visible;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.cell:hover { border-color: var(--color-border-strong); }
.cell:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

/* Sọt tre: vệt đan xen ở đáy ô */
.cell::before {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  bottom: 6%;
  height: 42%;
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  background: repeating-linear-gradient(100deg, #c69a5a 0 6px, #ad8148 6px 12px);
  box-shadow: inset 0 4px 6px rgba(43, 27, 16, 0.3);
}

.fruit-slot {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 1;
}

/* ---------- Quả nguyên (nhô lên) ---------- */
.fruit-whole {
  position: relative;
  width: 58%;
  height: 58%;
  opacity: 0;
  transform: translateY(40%) scale(0.5);
  transition: transform 150ms ease, opacity 150ms ease;
}
.cell.has-fruit .fruit-whole { opacity: 1; transform: translateY(0) scale(1); }

.fruit-body {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  box-shadow: inset -6px -6px 10px rgba(43, 27, 16, 0.2);
}

/* Kiểu dễ thương: mắt, miệng cười, má hồng, điểm sáng (§6.1 YEU-CAU-UPDATE-2.md) */
.fruit-eye {
  position: absolute;
  top: 36%;
  width: 13%;
  height: 13%;
  background: #2b1b10;
  border-radius: 50%;
}
.eye-l { left: 27%; }
.eye-r { right: 27%; }
.fruit-eye::after {
  content: "";
  position: absolute;
  top: 18%;
  left: 18%;
  width: 35%;
  height: 35%;
  background: #fff;
  border-radius: 50%;
}
.fruit-mouth {
  position: absolute;
  left: 50%;
  top: 56%;
  width: 24%;
  height: 12%;
  border-bottom: 3px solid #2b1b10;
  border-radius: 0 0 50% 50%;
  transform: translateX(-50%);
}
.fruit-blush {
  position: absolute;
  top: 50%;
  width: 15%;
  height: 9%;
  background: rgba(255, 110, 110, 0.55);
  border-radius: 50%;
}
.blush-l { left: 10%; }
.blush-r { right: 10%; }
.fruit-shine {
  position: absolute;
  top: 12%;
  left: 16%;
  width: 20%;
  height: 20%;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 50%;
}

/* Hình dạng/màu riêng từng loại quả (§4.9) */
.fruit-whole[data-fruit="watermelon"] .fruit-body { background: #2f7a3d; }
.fruit-whole[data-fruit="orange"] .fruit-body { background: #f2994a; }
.fruit-whole[data-fruit="apple"] .fruit-body { background: #e5484d; }
.fruit-whole[data-fruit="banana"] .fruit-body {
  background: #f4d35e;
  border-radius: 60% 60% 15% 15% / 90% 90% 10% 10%;
  transform: rotate(-18deg);
}
.fruit-whole[data-fruit="pineapple"] .fruit-body {
  background: #caa43d;
  border-radius: 40% 40% 45% 45% / 30% 30% 70% 70%;
}
.fruit-whole[data-fruit="pineapple"]::before {
  content: "";
  position: absolute;
  top: -22%;
  left: 50%;
  width: 0;
  height: 0;
  transform: translateX(-50%);
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 16px solid #3f7a3a;
}
.fruit-whole[data-fruit="mango"] .fruit-body {
  background: linear-gradient(135deg, #f2a541, #e4572e);
  border-radius: 55% 45% 55% 45% / 50% 55% 45% 50%;
}

/* ---------- Quả bị chém (hai nửa + hiệu ứng) ---------- */
.slice-wrap { position: relative; width: 58%; height: 58%; }

.fruit-half {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: #ccc;
}
.half-a { clip-path: polygon(0 0, 100% 0, 0 100%); animation: fly-a 500ms ease forwards; }
.half-b { clip-path: polygon(100% 0, 100% 100%, 0 100%); animation: fly-b 500ms ease forwards; }

.slice-wrap[data-fruit="watermelon"] .fruit-half { background: #e5484d; }
.slice-wrap[data-fruit="orange"] .fruit-half { background: #ffcf99; }
.slice-wrap[data-fruit="apple"] .fruit-half { background: #fff3e0; }
.slice-wrap[data-fruit="banana"] .fruit-half { background: #fff3e0; }
.slice-wrap[data-fruit="pineapple"] .fruit-half { background: #ffe28a; }
.slice-wrap[data-fruit="mango"] .fruit-half { background: #ffb703; }

.seed {
  position: absolute;
  width: 6%;
  height: 10%;
  border-radius: 50%;
  background: #241a10;
}
.segment-line {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 46%;
  height: 1px;
  background: rgba(224, 123, 29, 0.55);
  transform-origin: left center;
}

.blade {
  position: absolute;
  inset: -12%;
  border-top: 4px solid rgba(255, 255, 255, 0.95);
  transform: rotate(-45deg) scaleX(0);
  transform-origin: center;
  animation: blade-streak 200ms ease forwards;
}

.juice {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-danger);
  opacity: 1;
}
.slice-wrap[data-fruit="watermelon"] .juice { background: #e5484d; }
.slice-wrap[data-fruit="orange"] .juice { background: #ffb366; }
.slice-wrap[data-fruit="apple"] .juice { background: #fff3e0; }
.slice-wrap[data-fruit="banana"] .juice { background: #fff3e0; }
.slice-wrap[data-fruit="pineapple"] .juice { background: #ffe28a; }
.slice-wrap[data-fruit="mango"] .juice { background: #ffb703; }
.juice.j1 { animation: juice-fly-1 450ms ease forwards; }
.juice.j2 { animation: juice-fly-2 450ms ease forwards; }
.juice.j3 { animation: juice-fly-3 450ms ease forwards; }
.juice.j4 { animation: juice-fly-4 450ms ease forwards; }

.plus-one {
  position: absolute;
  top: 6%;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  color: var(--color-success);
  animation: plus-one-float 600ms ease forwards;
  white-space: nowrap;
}

.hit-bubble,
.drop-bubble {
  position: absolute;
  bottom: 104%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-inverse-bg);
  color: var(--color-inverse-text);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: var(--font-size-xs);
  white-space: nowrap;
  z-index: 5;
  animation: bubble-fade 800ms ease forwards;
}
.drop-bubble { background: var(--color-surface-strong); color: var(--color-text); }

@keyframes fly-a { to { transform: translate(-70%, -50%) rotate(-35deg); opacity: 0; } }
@keyframes fly-b { to { transform: translate(70%, 50%) rotate(35deg); opacity: 0; } }
@keyframes blade-streak {
  0% { transform: rotate(-45deg) scaleX(0); opacity: 1; }
  40% { transform: rotate(-45deg) scaleX(1); opacity: 1; }
  100% { transform: rotate(-45deg) scaleX(1); opacity: 0; }
}
@keyframes juice-fly-1 { to { transform: translate(-28px, -24px); opacity: 0; } }
@keyframes juice-fly-2 { to { transform: translate(26px, -22px); opacity: 0; } }
@keyframes juice-fly-3 { to { transform: translate(-24px, 26px); opacity: 0; } }
@keyframes juice-fly-4 { to { transform: translate(28px, 24px); opacity: 0; } }
@keyframes plus-one-float {
  0% { opacity: 0; transform: translateX(-50%) translateY(0) scale(0.6); }
  25% { opacity: 1; transform: translateX(-50%) translateY(-8px) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-34px) scale(1); }
}
@keyframes bubble-fade {
  0% { opacity: 0; transform: translateX(-50%) translateY(6px); }
  15% { opacity: 1; transform: translateX(-50%) translateY(0); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- Combo &amp; cảnh báo hết giờ ---------- */
.combo-banner {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-xl);
  color: #fff3e0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  text-align: center;
  pointer-events: none;
  z-index: 10;
  animation: combo-pop 1200ms ease forwards;
}
@keyframes combo-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  25% { transform: translate(-50%, -50%) scale(1); }
  85% { opacity: 1; }
  100% { opacity: 0; }
}
.low-time-line {
  text-align: center;
  color: var(--color-danger);
  font-weight: var(--font-weight-medium);
  font-size: var(--font-size-sm);
  margin: var(--space-2) 0 0;
}

.hud-score strong.bump { animation: score-bump 200ms ease; }
@keyframes score-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.3); }
  100% { transform: scale(1); }
}

.greeting {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin: 0 0 var(--space-3);
  font-style: italic;
}

.end-score {
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-3xl);
  margin: 0 0 var(--space-2);
}
.end-title {
  font-family: var(--font-display);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-lg);
  margin: 0 0 var(--space-1);
  color: var(--color-accent);
}
.end-comment {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin: 0 0 var(--space-4);
}

.end-record {
  color: var(--color-success);
  font-weight: var(--font-weight-bold);
  margin: 0 0 var(--space-5);
}

/* Giảm chuyển động: rút gọn mọi animation/transition gần như tức thời (§4.9) */
@media (prefers-reduced-motion: reduce) {
  .fruit-whole,
  .half-a,
  .half-b,
  .blade,
  .juice,
  .plus-one,
  .hit-bubble,
  .drop-bubble,
  .combo-banner,
  .hud-score strong.bump {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
  }
}

.field {
  display: block;
  text-align: left;
  margin-bottom: var(--space-4);
}
.field-label {
  display: block;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  margin-bottom: var(--space-2);
}
.field input {
  display: block;
  width: 100%;
  height: var(--control-height-md);
  padding: 0 var(--space-3);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--font-size-md);
}
.field input:focus { outline: none; border-color: var(--color-accent); }
.field input.input-error { border-color: var(--color-danger); }
.field-error {
  display: block;
  color: var(--color-danger);
  font-size: var(--font-size-xs);
  margin-top: var(--space-2);
  min-height: 1em;
}
#registerForm .btn { width: 100%; margin-top: var(--space-2); }

@media (max-width: 400px) {
  body { padding: var(--space-4); }
  .panel { padding: var(--space-5) var(--space-4); }
}
