/* ==========================================================
   2D Vocabulary Adventure — Styles (v3 — Mobile + Topics)
   ========================================================== */

/* ---- CSS Variables ---- */
:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #4ecca3;
  --accent2: #ff6b6b;
  --text: #e8e8e8;
  --text-dim: #8892b0;
  --gold: #ffd700;
  --radius: 10px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

/* ---- Reset & Base ---- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  /* overflow: hidden is needed for the game screen but would block landing page scroll.
     The game screen sets overflow: hidden on itself; html/body stay scrollable so the
     start screen can scroll on mobile. */
  overflow-x: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 3px; }

/* Lock scroll on body while the game is running (toggled via JS .game-active class).
   This keeps the HUD bars visible on mobile and prevents inadvertent page scroll. */
body.game-active {
  overflow: hidden;
}

/* ---- Loading Overlay ---- */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 26, 46, 0.92);
  backdrop-filter: blur(4px);
}

.loading-content {
  text-align: center;
  color: var(--text);
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--surface2);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 1s linear infinite;
}

.spinner-small {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 28px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 44px;
  touch-action: manipulation;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #38b2ac);
  color: #1a1a2e;
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(78, 204, 163, 0.35);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ---- Project intro ---- */
.project-intro {
  text-align: center;
  margin-bottom: 8px;
}

.project-logo {
  height: 100px;
  margin-bottom: 10px;
  background: #fff;
  border-radius: 50%;
  padding: 14px;
  object-fit: contain;
}

.project-intro h1 {
  font-size: 1.6rem;
  margin-bottom: 2px;
}

.rainbow-text {
  background: linear-gradient(90deg, #ff6b6b, #feca57, #48dbfb, #ff9ff3, #54a0ff, #5f27cd, #ff6b6b);
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: rainbow-shift 3s linear infinite;
}

@keyframes rainbow-shift {
  to { background-position: 200% center; }
}

.project-tagline {
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 10px;
  letter-spacing: 0.03em;
}

.project-desc {
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.5;
  max-width: 520px;
  margin: 0 auto 14px;
}

.project-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 4px;
}

.section-divider {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  margin: 18px 0;
}

.demo-heading {
  font-size: 1.15rem;
  text-align: center;
  margin-bottom: 4px;
  color: var(--text);
}

.download-section {
  margin-top: 18px;
  text-align: center;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 18px;
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius);
  font-size: 0.85rem;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-download:hover {
  background: var(--surface);
  color: var(--text);
  border-color: var(--accent);
}

.download-icon {
  font-size: 1rem;
}

/* ---- Queue overlay ---- */
.queue-content {
  text-align: center;
  max-width: 360px;
}

.queue-content h2 {
  margin: 12px 0 8px;
  color: var(--accent2);
  font-size: 1.3rem;
}

.queue-content p {
  margin: 4px 0;
  color: var(--text);
  font-size: 0.95rem;
}

.queue-hint {
  color: var(--text-dim) !important;
  font-size: 0.85rem !important;
  margin-top: 12px !important;
}

.queue-icon {
  font-size: 2.5rem;
}

#queue-position {
  color: var(--gold);
  font-size: 1.1rem;
}

/* ---- Session timer ---- */
.session-timer {
  font-family: 'Courier New', monospace;
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text-dim);
  background: rgba(0, 0, 0, 0.3);
  padding: 2px 8px;
  border-radius: 4px;
  margin-left: 8px;
  flex-shrink: 0;
}

.session-timer.timer-warning {
  color: var(--accent2);
  animation: timer-pulse 1s ease-in-out infinite;
}

@keyframes timer-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ---- Demo offline ---- */
.demo-offline {
  text-align: center;
  padding: 30px 0 10px;
}

.demo-offline-emoji {
  font-size: 2.5rem;
  display: block;
  margin-bottom: 8px;
}

.demo-offline p {
  color: var(--text-dim);
  font-size: 0.9rem;
}

.btn-send {
  background: var(--accent);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  min-height: 44px;
}

.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---- Start Screen ---- */
.start-screen {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 100vh;
  height: 100vh;
  padding: 20px;
  overflow-y: auto;
}

.start-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 36px 32px;
  max-width: 640px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}

.start-card h1 {
  font-size: 1.8rem;
  margin-bottom: 8px;
  color: var(--accent);
}

.subtitle {
  color: var(--text-dim);
  margin-bottom: 24px;
  font-size: 0.95rem;
}

/* ---- Topic Cards ---- */
.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.topic-card {
  background: var(--surface2);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 18px 14px;
  cursor: pointer;
  transition: all 0.25s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  min-height: 44px;
}

.topic-card:hover {
  border-color: var(--accent);
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(78, 204, 163, 0.2);
}

.topic-card.selected {
  border-color: var(--accent);
  background: rgba(78, 204, 163, 0.12);
  box-shadow: 0 0 0 1px var(--accent), 0 4px 16px rgba(78, 204, 163, 0.25);
}

.topic-icon {
  font-size: 2rem;
}

.topic-title {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text);
}

.topic-desc {
  font-size: 0.78rem;
  color: var(--text-dim);
  line-height: 1.3;
}

/* ---- Word Preview ---- */
.word-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  padding: 14px;
  margin-bottom: 16px;
  background: rgba(15, 52, 96, 0.5);
  border-radius: var(--radius);
}

.preview-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: 20px;
  font-size: 0.82rem;
  color: var(--accent);
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

.preview-chip:hover {
  background: rgba(255, 107, 107, 0.15);
  border-color: var(--accent2);
  color: var(--accent2);
}

.preview-chip .chip-remove {
  font-size: 0.7rem;
  font-weight: 700;
  opacity: 0.6;
  transition: opacity 0.15s;
}

.preview-chip:hover .chip-remove {
  opacity: 1;
}

/* ---- Word Editor ---- */
.word-editor {
  margin-bottom: 16px;
}

.word-add-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  justify-content: center;
}

.add-word-input {
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: 8px;
  padding: 8px 14px;
  color: var(--text);
  font-size: 0.88rem;
  outline: none;
  width: 220px;
  min-height: 38px;
  transition: border-color 0.2s;
}

.add-word-input:focus {
  border-color: var(--accent);
}

.btn-add-word {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  min-height: 38px;
  line-height: 1;
  transition: all 0.2s;
}

.btn-add-word:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(78, 204, 163, 0.3);
}

.word-editor-hint {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 8px;
  text-align: center;
  opacity: 0.7;
}

/* Custom topic card dashed border */
.topic-card-custom {
  border-style: dashed;
  border-color: var(--text-dim);
}

.topic-card-custom:hover {
  border-style: dashed;
}

.topic-card-custom.selected {
  border-style: solid;
}

/* Saved scenario cards */
.topic-card-saved {
  position: relative;
}

.saved-delete-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255, 107, 107, 0.2);
  color: var(--accent2);
  border: none;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s, background 0.2s;
}

.topic-card-saved:hover .saved-delete-btn {
  opacity: 1;
}

.saved-delete-btn:hover {
  background: rgba(255, 107, 107, 0.5);
}

/* ---- Start Error ---- */
.start-error {
  color: var(--accent2);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

/* ---- Game Screen ---- */
/* position:fixed + inset:0 ensures the game always occupies the full visible viewport
   regardless of body scroll position (the landing page can scroll on mobile, and
   when the game starts the game-screen must cover from the very top). */
.game-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Vocab Bar ---- */
.vocab-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  overflow-x: auto;
  flex-shrink: 0;
}

.btn-exit {
  background: transparent;
  border: 1px solid var(--text-dim);
  color: var(--text-dim);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-exit:hover {
  background: #e74c3c;
  border-color: #e74c3c;
  color: #fff;
}

.vocab-label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.vocab-chips {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  flex: 1;
}

.vocab-chip {
  display: inline-block;
  padding: 3px 10px;
  background: var(--surface2);
  border: 1px solid var(--text-dim);
  border-radius: 16px;
  font-size: 0.78rem;
  color: var(--text-dim);
  white-space: nowrap;
  transition: all 0.3s ease;
}

.vocab-chip.used {
  background: rgba(78, 204, 163, 0.18);
  border-color: var(--accent);
  color: var(--accent);
}

.progress-counter {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
  padding: 3px 10px;
  background: var(--surface2);
  border-radius: 16px;
}

.progress-counter.complete {
  color: var(--accent);
  background: rgba(78, 204, 163, 0.18);
  border: 1px solid var(--accent);
}

/* ---- Inventory Bar ---- */
.inventory-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  overflow-x: auto;
  flex-shrink: 0;
}

.inventory-label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.inventory-items {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  flex: 1;
}

.inventory-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  background: rgba(254, 202, 87, 0.12);
  border: 1px solid rgba(254, 202, 87, 0.5);
  border-radius: 16px;
  font-size: 0.78rem;
  color: #feca57;
  white-space: nowrap;
  cursor: default;
  transition: all 0.3s ease;
  animation: itemAppear 0.4s ease-out;
}

.inventory-item:hover {
  background: rgba(254, 202, 87, 0.22);
  border-color: #feca57;
}

.inventory-item .item-emoji {
  font-size: 0.95rem;
}

.inventory-item .item-name {
  font-size: 0.78rem;
}

.inventory-counter {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: nowrap;
  padding: 3px 10px;
  background: var(--surface2);
  border-radius: 16px;
}

/* ---- NPC Quest Progress Bar ---- */
.npc-progress-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  flex-shrink: 0;
}

.npc-progress-label {
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.npc-progress-track {
  flex: 1;
  height: 8px;
  background: var(--surface2);
  border-radius: 4px;
  overflow: hidden;
  max-width: 200px;
}

.npc-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent2), #feca57);
  border-radius: 4px;
  transition: width 0.5s ease;
}

.npc-progress-fill.complete {
  background: linear-gradient(90deg, var(--accent), #48dbfb);
}

.npc-progress-text {
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--text-dim);
  white-space: nowrap;
}

@keyframes itemAppear {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

/* ---- Item Detail Modal ---- */
.item-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  animation: modalFadeIn 0.3s ease-out;
}
.item-modal.hidden { display: none; }

.item-modal-card {
  background: #1a1a2e;
  border: 4px solid #feca57;
  border-radius: 4px;
  padding: 32px 40px;
  text-align: center;
  min-width: 260px;
  max-width: 360px;
  box-shadow:
    0 0 0 4px #1a1a2e,
    0 0 0 8px #feca57,
    0 0 40px rgba(254, 202, 87, 0.3);
  image-rendering: pixelated;
  animation: modalCardPop 0.4s ease-out;
}

.item-modal-emoji {
  font-size: 80px;
  line-height: 1;
  margin-bottom: 12px;
  filter: contrast(1.2);
  animation: itemBounce 0.6s ease-out 0.2s both;
  image-rendering: pixelated;
  -webkit-font-smoothing: none;
}

.item-modal-name {
  font-family: 'Courier New', monospace;
  font-size: 1.4rem;
  font-weight: 700;
  color: #feca57;
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.5);
}

.item-modal-desc {
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
  color: #e0e0e0;
  margin: 0 0 6px;
  line-height: 1.4;
}

.item-modal-from {
  font-family: 'Courier New', monospace;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin: 0 0 20px;
  font-style: italic;
}

.item-modal-close {
  font-family: 'Courier New', monospace;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  padding: 8px 32px;
  border: 2px solid #feca57;
  box-shadow: 4px 4px 0 rgba(0,0,0,0.4);
}
.item-modal-close:hover {
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0 rgba(0,0,0,0.4);
}
.item-modal-close:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 rgba(0,0,0,0.4);
}

@keyframes modalFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modalCardPop {
  0% { transform: scale(0.6); opacity: 0; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes itemBounce {
  0% { transform: translateY(-30px) scale(0.5); opacity: 0; }
  60% { transform: translateY(5px) scale(1.1); }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

/* ---- Time-Up Modal ---- */
.timeout-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 9500;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.82);
  animation: modalFadeIn 0.4s ease-out;
}
.timeout-overlay.hidden { display: none; }

.timeout-card {
  background: var(--surface);
  border: 3px solid var(--accent2);
  border-radius: 12px;
  padding: 40px 48px;
  text-align: center;
  min-width: 280px;
  max-width: 380px;
  box-shadow: 0 0 40px rgba(255, 107, 107, 0.35), var(--shadow);
  animation: modalCardPop 0.4s ease-out;
}

.timeout-emoji {
  font-size: 72px;
  line-height: 1;
  margin-bottom: 16px;
}

.timeout-card h2 {
  font-size: 1.6rem;
  color: var(--accent2);
  margin: 0 0 12px;
}

.timeout-card p {
  color: var(--text-dim);
  font-size: 0.95rem;
  margin: 0 0 24px;
  line-height: 1.5;
}

/* ---- Game Main Layout ---- */
.game-main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ---- Canvas Panel ---- */
.canvas-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px;
  position: relative;
  min-width: 0;
}

#game-canvas {
  background: #000;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  border-radius: 6px;
  max-width: 100%;
  height: auto;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.5);
}

.game-hint {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 6px;
  text-align: center;
}

/* ---- Chat Panel ---- */
.chat-panel {
  width: 340px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--surface2);
  flex-shrink: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  font-weight: 700;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--surface2);
  color: var(--text);
}

.chat-toggle-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 90%;
}

.chat-msg-player {
  align-self: flex-end;
}

.chat-msg-npc {
  align-self: flex-start;
}

.chat-npc-name {
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 3px;
  padding-left: 4px;
}

.chat-bubble {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.45;
  word-break: break-word;
}

.chat-msg-player .chat-bubble {
  background: var(--accent);
  color: var(--bg);
  border-bottom-right-radius: 4px;
}

.chat-msg-npc .chat-bubble {
  background: var(--surface2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* Typing indicator */
.dot-typing span {
  animation: dotBlink 1.4s infinite;
  font-size: 1.4rem;
  line-height: 1;
}
.dot-typing span:nth-child(2) { animation-delay: 0.2s; }
.dot-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotBlink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}

/* ---- Player Choice Buttons ---- */
.chat-choices {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 12px;
  border-top: 1px solid var(--surface2);
  background: var(--surface);
}

.chat-choices.hidden {
  display: none;
}

.choice-btn {
  display: block;
  width: 100%;
  padding: 10px 14px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid rgba(78, 204, 163, 0.3);
  border-radius: 10px;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
  min-height: 38px;
  line-height: 1.35;
}

.choice-btn:hover {
  background: rgba(78, 204, 163, 0.15);
  border-color: var(--accent);
  color: var(--accent);
}

.choice-btn:active {
  transform: scale(0.98);
}

.chat-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--surface2);
}

.chat-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 0.88rem;
  outline: none;
  min-height: 44px;
}

.chat-input:focus {
  border-color: var(--accent);
}

.chat-input:disabled {
  opacity: 0.5;
}

/* ---- Mobile Controls ---- */
.mobile-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 12px 20px 20px;
  pointer-events: none;
  z-index: 50;
}

.mobile-controls > * {
  pointer-events: auto;
}

.dpad {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 52px 52px 52px;
  gap: 4px;
  user-select: none;
}

.dpad-btn {
  width: 52px;
  height: 52px;
  border: none;
  border-radius: 10px;
  background: rgba(78, 204, 163, 0.25);
  color: var(--accent);
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  backdrop-filter: blur(4px);
}

.dpad-btn:active {
  background: rgba(78, 204, 163, 0.5);
  transform: scale(0.92);
}

.dpad-up    { grid-column: 2; grid-row: 1; }
.dpad-left  { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down  { grid-column: 2; grid-row: 3; }

.action-buttons {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 20px;
}

.action-btn {
  width: 60px;
  height: 60px;
  border: none;
  border-radius: 50%;
  font-size: 1.3rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  backdrop-filter: blur(4px);
}

.btn-a {
  background: rgba(78, 204, 163, 0.35);
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(78, 204, 163, 0.3);
}

.btn-a:active {
  background: rgba(78, 204, 163, 0.6);
  transform: scale(0.92);
}

.btn-b {
  background: rgba(255, 107, 107, 0.3);
  color: var(--accent2);
  width: 48px;
  height: 48px;
  font-size: 1.1rem;
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.25);
}

.btn-b:active {
  background: rgba(255, 107, 107, 0.55);
  transform: scale(0.92);
}

/* ---- Completion Overlay ---- */
.completion-overlay {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 26, 46, 0.88);
  backdrop-filter: blur(6px);
}

.completion-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 40px 32px;
  text-align: center;
  max-width: 420px;
  width: 90%;
  box-shadow: var(--shadow);
}

.completion-card h2 {
  font-size: 1.6rem;
  margin-bottom: 12px;
  color: var(--gold);
}

.completion-card p {
  color: var(--text-dim);
  margin-bottom: 24px;
}

/* ---- Confetti ---- */
.confetti-piece {
  position: absolute;
  width: 10px;
  height: 10px;
  top: -20px;
  border-radius: 2px;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(100vh) rotate(720deg);
    opacity: 0;
  }
}

/* ---- Utilities ---- */
.hidden { display: none !important; }

/* ==========================================================
   Touch & Mobile Base Enhancements
   ========================================================== */

/* Prevent overscroll bounce / pull-to-refresh during gameplay */
html {
  overscroll-behavior: none;
}

/* Smooth touch on all interactive elements — prevent 300ms tap delay */
button, a, input, .topic-card, .choice-btn,
.vocab-chip, .inventory-item, .preview-chip {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Prevent text selection on game UI (preserve on text inputs) */
.game-screen {
  -webkit-user-select: none;
  user-select: none;
}
.chat-input, .add-word-input {
  -webkit-user-select: auto;
  user-select: auto;
}

/* ==========================================================
   Responsive — Tablet (max-width: 900px)
   ========================================================== */
@media (max-width: 900px) {
  .chat-panel {
    width: 260px;
  }

  .start-card {
    padding: 24px 20px;
  }

  .topic-grid {
    grid-template-columns: 1fr 1fr;
  }
}

/* ==========================================================
   Responsive — Mobile (max-width: 640px)
   Portrait-first layout: vocab → inventory → canvas → chat
   ========================================================== */
@media (max-width: 640px) {

  /* ---- Start Screen ---- */
  .start-screen {
    padding: 12px;
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-bottom: env(safe-area-inset-bottom);
    align-items: flex-start;
  }

  .start-card {
    padding: 20px 16px;
    border-radius: 12px;
  }

  .start-card h1 {
    font-size: 1.4rem;
  }

  .project-logo {
    height: 72px;
    padding: 10px;
  }

  .project-desc {
    font-size: 0.82rem;
  }

  .subtitle {
    font-size: 0.88rem;
    margin-bottom: 16px;
  }

  /* Topic cards — vertical stack, larger touch targets */
  .topic-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .topic-card {
    flex-direction: row;
    text-align: left;
    padding: 16px 14px;
    gap: 12px;
    min-height: 56px; /* Comfortable touch target */
  }

  .topic-icon {
    font-size: 1.6rem;
    flex-shrink: 0;
  }

  .topic-title {
    font-size: 0.92rem;
  }

  .topic-desc {
    font-size: 0.75rem;
    display: block;
    color: var(--text-dim);
  }

  /* Word editor — full width */
  .word-add-row {
    width: 100%;
  }

  .add-word-input {
    flex: 1;
    width: auto;
    min-width: 0;
  }

  /* Start button — prominent full-width */
  #start-btn {
    width: 100%;
    padding: 14px 24px;
    font-size: 1.05rem;
  }

  /* ---- Game Screen — Full Viewport ---- */
  /* position:fixed already handles the viewport; safe-area-inset keeps content
     away from notches and home indicators on iOS/Android. */
  .game-screen {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  /* ---- Vocab Bar — Compact & Scrollable with Fade ---- */
  .vocab-bar {
    padding: 4px 10px;
    gap: 6px;
    flex-shrink: 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .vocab-bar::-webkit-scrollbar {
    display: none;
  }

  .vocab-label {
    font-size: 0.75rem;
  }

  .vocab-chips {
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    /* Gradient fade on edges to hint at scrollability */
    -webkit-mask-image: linear-gradient(
      to right,
      transparent 0,
      black 8px,
      black calc(100% - 8px),
      transparent 100%
    );
    mask-image: linear-gradient(
      to right,
      transparent 0,
      black 8px,
      black calc(100% - 8px),
      transparent 100%
    );
    padding: 0 4px;
  }

  .vocab-chips::-webkit-scrollbar {
    display: none;
  }

  .vocab-chip {
    font-size: 0.72rem;
    padding: 2px 8px;
  }

  .progress-counter {
    font-size: 0.72rem;
    padding: 2px 6px;
  }

  .session-timer {
    font-size: 0.78rem;
    padding: 2px 6px;
  }

  /* ---- Inventory Bar — Compact & Scrollable ---- */
  .inventory-bar {
    padding: 3px 10px;
    gap: 6px;
    flex-shrink: 0;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .inventory-bar::-webkit-scrollbar {
    display: none;
  }

  .inventory-label {
    font-size: 0.75rem;
  }

  .inventory-items {
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    -webkit-mask-image: linear-gradient(
      to right,
      transparent 0,
      black 8px,
      black calc(100% - 8px),
      transparent 100%
    );
    mask-image: linear-gradient(
      to right,
      transparent 0,
      black 8px,
      black calc(100% - 8px),
      transparent 100%
    );
    padding: 0 4px;
  }

  .inventory-items::-webkit-scrollbar {
    display: none;
  }

  .inventory-item {
    font-size: 0.72rem;
    padding: 2px 8px;
  }

  .inventory-counter {
    font-size: 0.72rem;
    padding: 2px 6px;
  }

  /* ---- NPC Progress — Compact ---- */
  .npc-progress-bar {
    padding: 3px 10px;
    gap: 6px;
  }
  .npc-progress-label {
    font-size: 0.75rem;
  }
  .npc-progress-text {
    font-size: 0.72rem;
  }
  .npc-progress-track {
    max-width: 120px;
    height: 6px;
  }

  /* ---- Game Main — Stack Vertically ---- */
  .game-main {
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    min-height: 0;
  }

  /* ---- Canvas Panel — Fills Available Space ---- */
  .canvas-panel {
    flex: 1;
    padding: 2px;
    position: relative;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  #game-canvas {
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 4px;
  }

  .game-hint {
    display: none;
  }

  /* ---- Chat Panel — Collapsible Bottom Panel ---- */
  .chat-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--surface2);
    flex-shrink: 0;
    max-height: 50vh;
    transition: max-height 0.3s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .chat-header {
    padding: 10px 14px;
    cursor: pointer;
    flex-shrink: 0;
    position: relative;
  }

  /* Visual drag handle on chat header */
  .chat-header::before {
    content: '';
    position: absolute;
    top: 3px;
    left: 50%;
    transform: translateX(-50%);
    width: 32px;
    height: 3px;
    background: var(--text-dim);
    border-radius: 2px;
    opacity: 0.5;
  }

  /* Chat toggle button — always visible on mobile */
  .chat-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    font-size: 1rem;
    border-radius: 6px;
    background: var(--surface2);
  }

  .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 10px;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
  }

  .chat-form {
    padding: 8px 10px;
    gap: 6px;
  }

  .chat-input {
    padding: 8px 12px;
    font-size: 0.85rem;
    min-height: 40px;
  }

  .btn-send {
    padding: 8px 14px;
    min-height: 40px;
    font-size: 0.85rem;
  }

  /* Collapsed state — show only header */
  .chat-panel.collapsed {
    max-height: 44px;
  }

  .chat-panel.collapsed .chat-messages,
  .chat-panel.collapsed .chat-form,
  .chat-panel.collapsed .chat-choices {
    display: none;
  }

  /* ---- Player Choice Buttons — Mobile ---- */
  .choice-btn {
    padding: 10px 12px;
    font-size: 0.82rem;
    min-height: 44px;
  }

  /* ---- Mobile Controls — Positioned Inside Canvas Panel ---- */
  .mobile-controls {
    position: absolute;
    bottom: 8px;
    left: 0;
    right: 0;
    padding: 0 12px 4px;
    z-index: 50;
    pointer-events: none;
  }

  .mobile-controls > * {
    pointer-events: auto;
  }

  .dpad {
    grid-template-columns: 48px 48px 48px;
    grid-template-rows: 48px 48px 48px;
    gap: 3px;
  }

  .dpad-btn {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    background: rgba(78, 204, 163, 0.3);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    font-size: 1.1rem;
    border: 1px solid rgba(78, 204, 163, 0.15);
  }

  .action-buttons {
    margin-bottom: 8px;
  }

  .action-btn {
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
  }

  .btn-a {
    width: 56px;
    height: 56px;
    font-size: 1.2rem;
    border: 1px solid rgba(78, 204, 163, 0.15);
  }

  .btn-b {
    width: 46px;
    height: 46px;
    font-size: 1rem;
    border: 1px solid rgba(255, 107, 107, 0.15);
  }

  /* ---- Completion & Modals — Mobile ---- */
  .completion-card {
    padding: 28px 20px;
    width: 92%;
  }

  .item-modal-card {
    padding: 24px 20px;
    max-width: 300px;
    min-width: 220px;
  }

  .item-modal-emoji {
    font-size: 60px;
  }
}

/* ==========================================================
   Mobile Landscape (short viewport, wide screen)
   Canvas left + chat right, compact controls
   ========================================================== */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  .game-screen {
    /* position:fixed; inset:0 already gives full viewport — just add safe-area padding */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  /* Landscape: canvas left, chat right (row layout) */
  .game-main {
    flex-direction: row;
  }

  .canvas-panel {
    flex: 1;
    padding: 2px;
    min-width: 0;
  }

  #game-canvas {
    width: auto;
    height: 100%;
    max-height: 100%;
    max-width: 100%;
  }

  .chat-panel {
    width: 220px;
    max-height: none;
    border-left: 1px solid var(--surface2);
    border-top: none;
  }

  .chat-panel.collapsed {
    width: 40px;
    max-height: none;
    overflow: hidden;
  }

  .chat-panel.collapsed .chat-header {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    padding: 14px 6px;
    white-space: nowrap;
  }

  .chat-panel.collapsed .chat-header::before {
    display: none;
  }

  /* Compact bars in landscape */
  .vocab-bar {
    padding: 2px 8px;
  }

  .inventory-bar {
    padding: 2px 8px;
  }

  /* Smaller controls in landscape */
  .mobile-controls {
    bottom: 4px;
    padding: 0 8px;
  }

  .dpad {
    grid-template-columns: 40px 40px 40px;
    grid-template-rows: 40px 40px 40px;
    gap: 2px;
  }

  .dpad-btn {
    width: 40px;
    height: 40px;
    font-size: 0.95rem;
  }

  .btn-a {
    width: 48px;
    height: 48px;
  }

  .btn-b {
    width: 38px;
    height: 38px;
  }

  .action-buttons {
    margin-bottom: 4px;
  }
}

/* ==========================================================
   Very Small Screens (max-width: 380px)
   e.g. iPhone SE (375px), small Android (360px)
   ========================================================== */
@media (max-width: 380px) {
  .start-card {
    padding: 16px 12px;
  }

  .start-card h1 {
    font-size: 1.2rem;
  }

  .project-logo {
    height: 56px;
    padding: 8px;
  }

  .project-desc {
    font-size: 0.78rem;
  }

  .topic-card {
    padding: 12px 10px;
    gap: 10px;
    min-height: 48px;
  }

  .topic-title {
    font-size: 0.85rem;
  }

  /* Smaller D-pad for small screens */
  .dpad {
    grid-template-columns: 42px 42px 42px;
    grid-template-rows: 42px 42px 42px;
    gap: 2px;
  }

  .dpad-btn {
    width: 42px;
    height: 42px;
    font-size: 1rem;
  }

  .btn-a {
    width: 50px;
    height: 50px;
  }

  .btn-b {
    width: 42px;
    height: 42px;
  }

  .chat-header {
    padding: 8px 10px;
    font-size: 0.82rem;
  }

  .chat-input {
    font-size: 0.82rem;
    padding: 6px 10px;
  }

  .chat-bubble {
    font-size: 0.82rem;
    padding: 6px 10px;
  }
}
