/* Storybook Crayon UI — Optimized for iPad, Android Tablets, and Desktop */

:root {
  --ink: #222933;
  --cream: #fffdf5;
  --sky-top: #5ec5ff;
  --sky-bot: #d4f3ff;
  --green: #4ed964;
  --green-dark: #2ba841;
  --orange: #ff9233;
  --orange-dark: #d96b0f;
  --blue: #3ea8ff;
  --blue-dark: #1d75cc;
  --pink: #ff6b9d;
  --pink-dark: #d63d73;
  --yellow: #ffd93d;
  --yellow-dark: #d6a20b;
  --shadow: 0 8px 0 rgba(34, 41, 51, 0.22);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Fredoka', 'Nunito', 'Rounded Mplus 1c', system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky-top) 0%, var(--sky-bot) 100%);
  touch-action: manipulation;
}

input[type="text"] {
  user-select: text;
  -webkit-user-select: text;
}

/* ================= SCREENS ================= */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top, 12px) env(safe-area-inset-right, 16px) env(safe-area-inset-bottom, 12px) env(safe-area-inset-left, 16px);
}

.screen.active {
  display: flex;
}

/* ================= TITLE SCREEN ================= */
#screen-title, #screen-levels, #screen-customize {
  background:
    linear-gradient(180deg, rgba(16, 42, 78, 0.22) 0%, rgba(16, 42, 78, 0.32) 100%),
    url('./assets/bg_meadow.jpg') center center / cover no-repeat;
}

.top-bar-float {
  position: fixed;
  top: max(12px, env(safe-area-inset-top, 12px));
  right: max(16px, env(safe-area-inset-right, 16px));
  display: flex;
  gap: 8px;
  z-index: 20;
}

.icon-pill {
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 7px 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
  transition: transform 0.08s;
}
.icon-pill:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--ink);
}
.icon-pill.off {
  opacity: 0.55;
  text-decoration: line-through;
}

.title-card {
  width: min(760px, 94vw);
  max-height: 92vh;
  background: var(--cream);
  border: 5px solid var(--ink);
  border-radius: 32px;
  padding: 20px 26px 24px;
  box-shadow: 0 12px 0 rgba(34, 41, 51, 0.22);
  text-align: center;
  overflow-y: auto;
}

.storybook-badge {
  display: inline-block;
  background: #fff1b8;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  padding: 4px 16px;
  font-size: 14px;
  font-weight: 700;
  color: #b45309;
  margin-bottom: 6px;
}

.main-title {
  margin: 2px 0 16px;
  font-size: clamp(30px, 5vw, 50px);
  line-height: 1.05;
  color: #ff595e;
  text-shadow:
    -3px -3px 0 #fff,
    3px -3px 0 #fff,
    -3px 3px 0 #fff,
    3px 3px 0 #fff,
    0 5px 0 var(--ink);
  letter-spacing: 0.5px;
}

.title-showcase {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
}

.preview-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#title-preview-canvas {
  width: clamp(160px, 24vw, 215px);
  height: clamp(160px, 24vw, 215px);
  border-radius: 50%;
}

.photo-badge-btn {
  margin-top: -22px;
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 6px 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}
.photo-badge-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--ink);
}

.title-right {
  flex: 1;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dino-picker-box {
  background: #f1f9ff;
  border: 3px dashed #7cc4fa;
  border-radius: 20px;
  padding: 12px 14px;
}

.picker-label {
  display: block;
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 10px;
}

.swatch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.color-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  cursor: pointer;
  transition: transform 0.12s;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}
.color-swatch.selected {
  transform: scale(1.18);
  box-shadow: 0 0 0 4px #ffd93d, 0 4px 0 var(--ink);
}

.title-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ================= BUTTONS ================= */
.big-btn {
  font-family: inherit;
  font-size: clamp(18px, 2.5vw, 22px);
  font-weight: 700;
  color: #ffffff;
  border: 4px solid var(--ink);
  border-radius: 20px;
  padding: 13px 22px;
  cursor: pointer;
  box-shadow: 0 6px 0 var(--ink);
  transition: transform 0.08s;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.28);
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--ink);
}
.full-w { width: 100%; }

.small-btn {
  font-family: inherit;
  font-size: 15px;
  font-weight: 700;
  color: #ffffff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 8px 14px;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}
.small-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--ink);
}

.btn-green  { background: linear-gradient(180deg, #62e877 0%, var(--green) 100%); }
.btn-orange { background: linear-gradient(180deg, #ffab4a 0%, var(--orange) 100%); }
.btn-blue   { background: linear-gradient(180deg, #5ec0ff 0%, var(--blue) 100%); }
.btn-yellow { background: linear-gradient(180deg, #ffe766 0%, var(--yellow) 100%); color: var(--ink); text-shadow: none; }
.btn-gray   { background: #94a3b8; }

.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}
.file-btn input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* ================= MODALS (CUSTOMIZE & LEVEL SELECT) ================= */
.modal-card {
  width: min(860px, 95vw);
  max-height: 92vh;
  background: var(--cream);
  border: 5px solid var(--ink);
  border-radius: 28px;
  padding: 18px 22px 22px;
  box-shadow: 0 12px 0 rgba(34, 41, 51, 0.25);
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.modal-header h2 {
  margin: 0;
  font-size: clamp(22px, 3vw, 28px);
}

.close-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: #ff6b6b;
  color: #fff;
  font-size: 18px;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--ink);
}

.customize-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

@media (max-width: 720px) {
  .customize-grid {
    grid-template-columns: 1fr;
  }
}

.custom-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field-label {
  font-size: 15px;
  font-weight: 700;
  display: block;
  margin-bottom: 4px;
}

#input-hero-name {
  width: 100%;
  padding: 10px 14px;
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: #ffffff;
  color: var(--ink);
  outline: none;
}
#input-hero-name:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(62, 168, 255, 0.25);
}

.photo-studio {
  background: #f0f9ff;
  border: 3px solid #bae6fd;
  border-radius: 20px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#photo-crop-canvas {
  width: 200px;
  height: 200px;
  border-radius: 18px;
  border: 3px solid var(--ink);
  cursor: grab;
  touch-action: none;
}
#photo-crop-canvas:active {
  cursor: grabbing;
}

.photo-controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.zoom-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 240px;
  font-size: 14px;
  font-weight: 700;
}
.zoom-row input {
  flex: 1;
}

.tiny-hint {
  margin: 0;
  font-size: 12px;
  color: #475569;
  text-align: center;
}

.live-preview-box {
  display: flex;
  justify-content: center;
  background: linear-gradient(180deg, #dcf5ff 0%, #fff9db 100%);
  border: 3px solid var(--ink);
  border-radius: 20px;
  padding: 6px;
}

#customize-preview-canvas {
  width: 185px;
  height: 165px;
}

.option-group {
  background: #f8fafc;
  border: 2.5px solid #e2e8f0;
  border-radius: 16px;
  padding: 10px 12px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip-btn {
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 6px 11px;
  border-radius: 999px;
  border: 2.5px solid var(--ink);
  background: #ffffff;
  color: var(--ink);
  cursor: pointer;
}
.chip-btn.selected {
  background: var(--yellow);
  box-shadow: 0 3px 0 var(--ink);
}

/* ================= LEVEL SELECT MAP ================= */
.levels-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

@media (max-width: 640px) {
  .levels-grid {
    grid-template-columns: 1fr;
  }
}

.level-card {
  position: relative;
  border: 4px solid var(--ink);
  border-radius: 22px;
  padding: 16px;
  cursor: pointer;
  box-shadow: 0 6px 0 var(--ink);
  text-align: left;
  transition: transform 0.1s;
  color: var(--ink);
  font-family: inherit;
}
.level-card:active:not(.locked) {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--ink);
}
.level-card.theme-meadow { background: linear-gradient(135deg, #c7f9cc 0%, #80ed99 100%); }
.level-card.theme-sky    { background: linear-gradient(135deg, #dbeafe 0%, #93c5fd 100%); }
.level-card.theme-cave   { background: linear-gradient(135deg, #e0e7ff 0%, #c4b5fd 100%); }
.level-card.theme-castle { background: linear-gradient(135deg, #fecdd3 0%, #fda4af 100%); }
.level-card.locked {
  opacity: 0.62;
  filter: grayscale(0.4);
  cursor: not-allowed;
}

.level-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.level-card-num {
  font-size: 13px;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.85);
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 2px 10px;
}
.level-card-score {
  font-size: 14px;
  font-weight: 800;
  background: #ffd93d;
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 2px 10px;
}
.level-card h3 {
  margin: 4px 0 4px;
  font-size: 20px;
}
.level-card-stats {
  display: flex;
  gap: 12px;
  font-size: 14px;
  font-weight: 700;
  margin-top: 8px;
}

/* ================= GAME SCREEN & HUD ================= */
#screen-game {
  padding: 0;
  background: #1e293b;
  touch-action: none;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.hud-top {
  position: fixed;
  top: max(10px, env(safe-area-inset-top, 10px));
  left: max(12px, env(safe-area-inset-left, 12px));
  right: max(12px, env(safe-area-inset-right, 12px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: none;
  z-index: 15;
}

.hud-pills {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-pill {
  background: rgba(255, 253, 245, 0.92);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 5px 13px;
  font-size: clamp(14px, 2vw, 18px);
  font-weight: 700;
  box-shadow: 0 4px 0 rgba(34, 41, 51, 0.35);
}

.pill-star.danger {
  background: #ff4d6d;
  color: #ffffff;
  animation: pulseDanger 0.45s infinite alternate;
}

@keyframes pulseDanger {
  from { transform: scale(1); }
  to   { transform: scale(1.1); }
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}

.hud-level-tag {
  background: rgba(34, 41, 51, 0.72);
  color: #ffffff;
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 700;
}
@media (max-width: 700px) {
  .hud-level-tag { display: none; }
}

.hud-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: var(--cream);
  font-size: 19px;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}
.hud-btn:active {
  transform: translateY(2px);
}

.keyboard-hint {
  position: fixed;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 25, 36, 0.6);
  color: rgba(255, 255, 255, 0.9);
  padding: 4px 14px;
  border-radius: 999px;
  font-size: 12px;
  pointer-events: none;
  z-index: 12;
}
@media (hover: none) and (pointer: coarse) {
  .keyboard-hint { display: none; }
}

/* ================= TABLET TOUCH CONTROLS ================= */
.touch-controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(12px, env(safe-area-inset-bottom, 12px));
  padding: 0 max(18px, env(safe-area-inset-left, 18px));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 20;
}

.touch-left, .touch-right {
  pointer-events: auto;
  touch-action: none;
}

.touch-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.dpad-row {
  display: flex;
  gap: 14px;
}

.touch-btn {
  font-family: inherit;
  border: 4px solid rgba(34, 41, 51, 0.85);
  color: #ffffff;
  font-weight: 800;
  cursor: pointer;
  touch-action: none;
  box-shadow: 0 6px 0 rgba(34, 41, 51, 0.55);
  transition: transform 0.05s, filter 0.05s;
}

.touch-btn.pressed {
  transform: translateY(4px) scale(0.96);
  box-shadow: 0 2px 0 rgba(34, 41, 51, 0.55);
  filter: brightness(1.12);
}

.dpad-btn {
  width: clamp(68px, 10vw, 88px);
  height: clamp(68px, 10vw, 88px);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.82);
  color: var(--ink);
  font-size: clamp(28px, 4vw, 36px);
}

.down-btn {
  width: clamp(132px, 19vw, 170px);
  height: 44px;
  border-radius: 16px;
  background: rgba(255, 232, 130, 0.86);
  color: var(--ink);
  font-size: 14px;
}

.action-cluster {
  display: flex;
  align-items: flex-end;
  gap: clamp(10px, 1.8vw, 18px);
}

.act-btn {
  width: clamp(72px, 10.5vw, 94px);
  height: clamp(72px, 10.5vw, 94px);
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.05;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
}

.act-egg    { background: rgba(62, 168, 255, 0.9); margin-bottom: 18px; }
.act-tongue { background: rgba(255, 107, 157, 0.9); margin-bottom: 36px; }
.act-jump   {
  width: clamp(84px, 12vw, 108px);
  height: clamp(84px, 12vw, 108px);
  background: rgba(78, 217, 100, 0.92);
}

.act-icon { font-size: clamp(22px, 3vw, 28px); }
.act-txt  { font-size: clamp(11px, 1.5vw, 14px); }
.act-sub  { font-size: 10px; opacity: 0.95; font-weight: 600; }

/* ================= OVERLAYS ================= */
.overlay, .rotate-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.62);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 40;
}

.hidden {
  display: none !important;
}

.popup-card {
  width: min(460px, 92vw);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--cream);
  border: 5px solid var(--ink);
  border-radius: 28px;
  padding: 22px 24px;
  text-align: center;
  box-shadow: 0 12px 0 rgba(0, 0, 0, 0.28);
}

.popup-card h2 {
  margin: 0 0 12px;
  font-size: 28px;
}

.popup-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
}

.victory-sub {
  margin: 0 0 14px;
  font-size: 16px;
  font-weight: 600;
  color: #475569;
}

.score-tally {
  background: #f1f9ff;
  border: 3px solid #93c5fd;
  border-radius: 18px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tally-row, .tally-total {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
}
.tally-total {
  border-top: 2.5px dashed #93c5fd;
  padding-top: 8px;
  margin-top: 4px;
  font-size: 20px;
  color: #d97706;
}

.help-card {
  width: min(560px, 94vw);
  text-align: left;
}
.help-card h2 { text-align: center; }
.help-list {
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 15px;
  line-height: 1.4;
  margin-bottom: 18px;
}

.rotate-icon {
  font-size: 52px;
  margin-bottom: 8px;
}

