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

:root {
  --bg: #0f1221;
  --panel: #1a1f36;
  --panel2: #232a4d;
  --accent: #6c7bff;
  --accent2: #ff6c8a;
  --text: #e8ebff;
  --muted: #9aa2c7;
  --danger: #ff5566;
  --ok: #4ad991;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: "Segoe UI", "Sarabun", system-ui, -apple-system, sans-serif;
  background: radial-gradient(1200px 600px at 50% -10%, #1c2350, var(--bg));
  color: var(--text);
  min-height: 100vh;
  /* กันโดน notch iPhone บัง */
  padding: calc(24px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
    calc(60px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  -webkit-tap-highlight-color: transparent;
}

header {
  text-align: center;
  margin-bottom: 24px;
}

h1 {
  font-size: clamp(1.5rem, 6vw, 2rem);
}

.tagline {
  color: var(--muted);
  margin-top: 6px;
}

main {
  max-width: 900px;
  margin: 0 auto;
}

.view {
  background: var(--panel);
  border: 1px solid #2b3260;
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 20px;
}

.hidden {
  display: none !important;
}

h2 {
  margin-bottom: 16px;
}

h3 {
  margin: 20px 0 10px;
  color: var(--muted);
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

input[type="text"] {
  width: 100%;
  padding: 14px;
  font-size: 1.1rem; /* ≥16px กัน iOS auto-zoom ตอน focus */
  border-radius: 10px;
  border: 1px solid #3a4280;
  background: var(--panel2);
  color: var(--text);
  margin-bottom: 12px;
}

button {
  cursor: pointer;
  font-size: 1rem;
  padding: 12px 20px;
  min-height: 44px; /* touch target */
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  transition: transform 0.05s, opacity 0.2s;
  touch-action: manipulation; /* กัน double-tap zoom */
}

button:hover {
  opacity: 0.9;
}

button:active {
  transform: scale(0.97);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.hint {
  color: var(--muted);
  font-size: 0.9rem;
  margin-top: 10px;
}

.badge {
  background: var(--accent);
  border-radius: 999px;
  padding: 2px 12px;
  font-size: 0.9rem;
  vertical-align: middle;
}

/* ===== player list ===== */
.player-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-list li {
  background: var(--panel2);
  padding: 12px 16px;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.crown {
  color: #ffd369;
}

/* ===== host controls ===== */
.host-box {
  margin-top: 20px;
  border-top: 1px solid #2b3260;
  padding-top: 20px;
}

.time-options {
  display: flex;
  gap: 10px;
  margin: 10px 0 16px;
}

.time-btn {
  background: var(--panel2);
  flex: 1;
}

.time-btn.selected {
  background: var(--accent2);
}

/* ===== timer ===== */
.timer-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  justify-content: space-between;
  align-items: center;
  background: var(--panel2);
  padding: 14px 18px;
  border-radius: 12px;
  margin-bottom: 16px;
}

.timer {
  font-size: clamp(1.5rem, 7vw, 1.8rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.timer.low {
  color: var(--danger);
  animation: pulse 1s infinite;
}

@keyframes pulse {
  50% { opacity: 0.4; }
}

/* ===== cards ===== */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
}

.card {
  background: linear-gradient(160deg, var(--panel2), #2d366b);
  border: 1px solid #3a4280;
  border-radius: 14px;
  padding: 18px 14px;
  text-align: center;
  position: relative;
}

.card .pname {
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.card .pword {
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 12px;
  word-break: break-word;
}

.card.me {
  border-color: var(--accent);
}

.card.me .pword {
  color: var(--accent);
}

.card.caught {
  filter: grayscale(1);
  opacity: 0.5;
}

.card .catch-btn {
  width: 100%;
  background: var(--accent2);
  font-size: 0.95rem;
  padding: 10px;
}

.card .uncatch-btn {
  width: 100%;
  background: #555f8f;
  font-size: 0.85rem;
  padding: 8px;
  margin-top: 6px;
}

.card .caught-tag {
  color: var(--danger);
  font-weight: 700;
}

.card .you-tag {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 0.7rem;
  background: var(--accent);
  padding: 2px 8px;
  border-radius: 999px;
}

/* ===== catch log ===== */
.catch-log {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.catch-log li {
  background: var(--panel2);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
}

.catch-log .hl {
  color: var(--accent2);
  font-weight: 700;
}

/* ===== winner ===== */
.winner-box {
  background: linear-gradient(135deg, #2a3270, #46307a);
  border-radius: 14px;
  padding: 24px;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 10px;
}

/* ===== toast ===== */
.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: white;
  padding: 12px 24px;
  border-radius: 10px;
  font-weight: 600;
  z-index: 100;
  max-width: calc(100% - 32px);
  text-align: center;
}

/* ===== Tablet / iPad ===== */
@media (max-width: 820px) {
  .view {
    padding: 20px;
  }
  .cards {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 12px;
  }
}

/* ===== Mobile ===== */
@media (max-width: 520px) {
  body {
    padding: calc(16px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right))
      calc(40px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
  }
  .view {
    padding: 16px;
    border-radius: 14px;
  }
  /* ปุ่มหลักเต็มความกว้าง กดง่าย */
  #joinBtn,
  #startBtn,
  #backToLobbyBtn {
    width: 100%;
  }
  .cards {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 10px;
  }
  .card {
    padding: 14px 10px;
  }
  .card .pword {
    font-size: 1.25rem;
  }
  .timer-bar {
    justify-content: center;
    text-align: center;
  }
}

/* จอเล็กมาก (เครื่องแคบ ~360px) ให้การ์ด 2 คอลัมน์แน่ ๆ */
@media (max-width: 360px) {
  .cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ลดอนิเมชันถ้าผู้ใช้ตั้งค่าไว้ */
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
