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

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #3b6b2a;
  font-family: "Noto Sans Thai", "Sarabun", system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-touch-callout: none;
}

canvas {
  display: block;
  touch-action: none;
}

#hud {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: calc(env(safe-area-inset-right, 0px) + 10px);
  display: flex;
  gap: 8px;
  z-index: 10;
}

.hudbtn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.6);
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  font-size: 26px;
  line-height: 1;
}

.hudbtn.off { opacity: 0.55; }
#hud[hidden], #editbar[hidden] { display: none; }

#editbar {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 14px;
  background: rgba(15, 25, 15, 0.88);
  color: #fff;
  font-size: 14px;
  z-index: 20;
}
#editbar button {
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 16px;
}
#editbar button.ok { background: #2f9e44; }
#editbar span { min-width: 84px; text-align: center; }

/* ----- โลโก้ (ใช้ทั้งล็อบบี้และหน้าโหลด) ----- */
.logo {
  font-weight: 900;
  line-height: 1;
  color: #fff;
  letter-spacing: 1px;
  -webkit-text-stroke: 0.16em #0e2a12;
  paint-order: stroke fill;
  text-shadow: 0 0.08em 0 #0e2a12, 0 0.16em 0 rgba(0, 0, 0, 0.25);
}
.logo span { color: #ffd23f; }

/* ----- หน้าล็อบบี้ (แบบเกม .io) ----- */
#menu {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) env(safe-area-inset-right, 0px)
           calc(env(safe-area-inset-bottom, 0px) + 36px) calc(env(safe-area-inset-left, 0px) + clamp(16px, 5vw, 64px));
  background:
    radial-gradient(ellipse at 78% 50%, rgba(0, 0, 0, 0) 30%, rgba(4, 18, 6, 0.5) 100%),
    linear-gradient(90deg, rgba(8, 28, 10, 0.88) 0%, rgba(8, 28, 10, 0.62) 38%, rgba(8, 28, 10, 0) 68%);
}
#menu[hidden] { display: none; }

.lobby {
  width: min(400px, 94vw);
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.6vh, 12px);
  color: #fff;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.lobby::-webkit-scrollbar { display: none; }

.lobby .logo {
  font-size: clamp(40px, 15vh, 76px);
  transform: rotate(-3deg);
  transform-origin: left center;
  animation: lb-pop 0.7s cubic-bezier(0.2, 1.5, 0.4, 1) both;
}
.lb-tag {
  font-size: clamp(13px, 2.6vh, 16px);
  opacity: 0.92;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.4);
}

.lb-card {
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 2vh, 14px);
  padding: clamp(10px, 2.4vh, 16px);
  border-radius: 20px;
  background: rgba(10, 30, 14, 0.62);
  border: 2px solid rgba(255, 255, 255, 0.22);
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.join { display: flex; gap: 8px; }
#pname {
  flex: 1;
  min-width: 0;
  height: clamp(46px, 11vh, 56px);
  padding: 0 14px;
  border-radius: 14px;
  border: 3px solid rgba(255, 255, 255, 0.7);
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  font-size: 19px;
  font-family: inherit;
  outline: none;
  user-select: text;
  -webkit-user-select: text;
}
#pname:focus { border-color: #ffd23f; box-shadow: 0 0 0 3px rgba(255, 210, 63, 0.35); }
#pname::placeholder { color: rgba(255, 255, 255, 0.5); }

.play {
  position: relative;
  overflow: hidden;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  height: clamp(46px, 11vh, 56px);
  padding: 0 20px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(#5be06c, #2fa944);
  color: #fff;
  font-size: 24px;
  font-weight: 900;
  font-family: inherit;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  box-shadow: 0 5px 0 #1d6e2d, inset 0 2px 0 rgba(255, 255, 255, 0.35);
}
.play svg { width: 20px; height: 20px; filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.3)); }
.play::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: -60%;
  width: 40%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
  transform: skewX(-20deg);
  animation: lb-shine 2.8s ease-in-out infinite;
}
.play:active { transform: translateY(3px); box-shadow: 0 2px 0 #1d6e2d, inset 0 2px 0 rgba(255, 255, 255, 0.35); }

.colors-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; font-size: 14px; }
.colors-head span { opacity: 0.85; }
.colors-head b {
  padding: 2px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  font-weight: 700;
}
.swatches { display: flex; justify-content: space-between; gap: 6px; }
.swatch {
  position: relative;
  flex: 1;
  max-width: 52px;
  aspect-ratio: 1;
  min-width: 30px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.35);
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.22), 0 3px 0 rgba(0, 0, 0, 0.25);
  transition: transform 0.15s ease;
}
.swatch.sel {
  border-color: #fff;
  transform: translateY(-3px) scale(1.1);
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.35), inset 0 -5px 0 rgba(0, 0, 0, 0.22), 0 6px 0 rgba(0, 0, 0, 0.25);
}
.swatch.sel::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 46%;
  width: 28%;
  height: 50%;
  border: solid #fff;
  border-width: 0 3px 3px 0;
  transform: translate(-50%, -50%) rotate(45deg);
  filter: drop-shadow(0 1px 0 rgba(0, 0, 0, 0.45));
}

.lb-links { display: flex; gap: 8px; }
.lb-links .ghost { flex: 1; }
.ghost {
  height: 42px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  font-size: 16px;
  font-family: inherit;
}
.ghost:active { background: rgba(255, 255, 255, 0.18); }
.ghost[hidden] { display: none; }

.lb-foot {
  position: absolute;
  left: calc(env(safe-area-inset-left, 0px) + 16px);
  right: calc(env(safe-area-inset-right, 0px) + 16px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
  pointer-events: none;
}
.lb-tip { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-ver { flex: 0 0 auto; opacity: 0.75; }

@media (max-height: 340px) {
  .lb-tag, .lb-tip { display: none; }
  .lobby .logo { font-size: 38px; }
  .ghost { height: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  .lobby .logo, .play::after, .ld-icon, .rot-phone { animation: none; }
}
@keyframes lb-pop { from { transform: rotate(-3deg) scale(0.6); opacity: 0; } to { transform: rotate(-3deg) scale(1); opacity: 1; } }
@keyframes lb-shine { 0%, 60% { left: -60%; } 100% { left: 130%; } }

/* ----- หน้ากำลังโหลด ----- */
#loader {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  color: #fff;
  background: radial-gradient(circle at 50% 40%, #a8f060 0%, #4fb43c 45%, #166a34 100%);
  transition: opacity 0.5s ease, visibility 0.5s;
}
#loader.done { opacity: 0; visibility: hidden; pointer-events: none; }
.ld-icon {
  width: clamp(72px, 22vh, 112px);
  height: auto;
  border-radius: 24px;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.25);
  animation: ld-bob 1.6s ease-in-out infinite;
}
.ld-logo { font-size: clamp(36px, 11vh, 52px); }
.ld-bar {
  width: min(260px, 70vw);
  height: 14px;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
}
.ld-bar i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 10px;
  background: linear-gradient(#ffe27a, #ffb020);
  transition: width 0.15s linear;
}
.ld-tip {
  max-width: min(420px, 88vw);
  min-height: 2.6em;
  text-align: center;
  font-size: 15px;
  opacity: 0.92;
}
@keyframes ld-bob { 50% { transform: translateY(-6px); } }

/* ----- เตือนให้หมุนจอ (เฉพาะอุปกรณ์สัมผัสที่ถืออยู่แนวตั้ง) ----- */
#rotate {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
  line-height: 1.5;
  font-size: 20px;
  color: #fff;
  background: radial-gradient(circle at 50% 40%, #2e6a35, #0f2a14);
}
@media (orientation: portrait) and (pointer: coarse) { #rotate { display: flex; } }
.rot-phone { transform-origin: 32px 28px; animation: rot-turn 2.4s ease-in-out infinite; }
@keyframes rot-turn { 0%, 15% { transform: rotate(0); } 55%, 100% { transform: rotate(90deg); } }

.noscript { position: fixed; inset: 0; z-index: 90; padding-top: 30vh; text-align: center; color: #fff; background: #1e2024; }

#toast {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  padding: 10px 16px;
  border-radius: 12px;
  background: #2f9e44;
  color: #fff;
  font-size: 15px;
  box-shadow: 0 3px 0 #1f6e2f;
}
#toast[hidden] { display: none; }

/* ----- หน้าวิธีเล่น/คอนโทรล ----- */
#controls {
  position: fixed;
  inset: 0;
  z-index: 32;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 12px calc(env(safe-area-inset-bottom, 0px) + 12px);
  background: radial-gradient(ellipse at center, rgba(20, 50, 20, 0.92), rgba(6, 16, 6, 0.97));
}
#controls[hidden] { display: none; }
.ctl-card {
  width: min(420px, 100%);
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
  border-radius: 18px;
  color: #fff;
  background: rgba(16, 36, 16, 0.9);
  border: 2px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35);
  -webkit-overflow-scrolling: touch;
}
.ctl-head { display: flex; align-items: center; gap: 12px; margin-bottom: 2px; }
.ctl-head h2 { font-size: 28px; color: #ffd23f; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35); }
.ctl-back {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.6);
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  font-size: 20px;
  line-height: 1;
}
.ctl-card h3 { font-size: 14px; font-weight: 700; letter-spacing: 0.02em; color: #ffd23f; opacity: 0.9; margin-top: 6px; }
.ctl-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.ctl-list li { display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 12px; background: rgba(0, 0, 0, 0.28); }
.ctl-list li > div { flex: 1; min-width: 0; }
.ctl-list b { display: block; font-size: 16px; }
.ctl-list p { font-size: 14px; line-height: 1.4; opacity: 0.85; margin-top: 2px; }
.ctl-list svg { flex: 0 0 56px; width: 56px; height: 56px; }
.ctl-list svg .ring { fill: rgba(255, 255, 255, 0.1); stroke: rgba(255, 255, 255, 0.55); stroke-width: 2.5; }
.ctl-list svg .glyph { fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; }
.keys-wrap { flex: 0 0 72px; display: flex; justify-content: center; gap: 4px; flex-wrap: wrap; }
.key {
  display: inline-block;
  min-width: 28px;
  height: 30px;
  padding: 0 5px;
  line-height: 30px;
  text-align: center;
  border-radius: 7px;
  background: #f4f1e6;
  color: #1f2a1f;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 3px 0 #9a9584;
}
.ctl-note { font-size: 13px; opacity: 0.75; text-align: center; margin-top: 4px; }
