:root {
  --bg-deep: #180c36;
  --bg-light: #2a1657;
  --primary-cyan: #4deeea;
  --primary-dark: #2ba8a5;
  --accent-blue: #2f56e0;
  --title-blue: #63caff;
  --title-shadow: #2a7da3;
  --text-white: #ffffff;
  --fire-orange: #ff9e2c;
  --fire-red: #ff4f4f;
  --panel-bg: #2d1b5e;
  --font-main: 'Nunito', sans-serif;
  --font-display: 'Titan One', cursive;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-main);
  background: var(--bg-deep);
  color: var(--text-white);
  touch-action: manipulation;
}

/* ---------- #app: MOBILE = flex column fullscreen, DESKTOP = centered box ---------- */

#app {
  position: relative;
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(to bottom, #11052c, #2a1657);
}

/* ---------- CANVAS ---------- */

#scene {
  display: block;
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  background: transparent;
}

/* ---------- HUD BAR (top) ---------- */

#hud-bar {
  position: absolute;
  top: var(--safe-top);
  left: var(--safe-left);
  right: var(--safe-right);
  display: flex;
  justify-content: space-between;
  padding: 8px 16px;
  z-index: 10;
  pointer-events: none;
}

.hud-item {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  padding: 6px 14px;
  border-radius: 14px;
  border: 2px solid rgba(90, 58, 153, 0.6);
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-label {
  font-family: var(--font-display);
  font-size: 11px;
  color: var(--primary-cyan);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#score, #timer {
  font-family: var(--font-display);
  font-size: 22px;
  color: white;
  text-shadow: 1px 1px 0 #000;
  line-height: 1;
}

/* ---------- GAME CONTROLS (bottom area on mobile) ---------- */

#game-controls {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: max(8px, var(--safe-bottom));
  background: linear-gradient(to bottom, rgba(17, 5, 44, 0.0), rgba(17, 5, 44, 0.95) 20%);
  z-index: 10;
}

/* ---------- TASK CARD ---------- */

#taskCard {
  background: rgba(45, 27, 94, 0.92);
  border: 3px solid var(--primary-cyan);
  box-shadow: 0 6px 0 rgba(0,0,0,0.25), 0 0 16px rgba(77, 238, 234, 0.3);
  padding: 10px 28px;
  border-radius: 20px;
  text-align: center;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

#taskText {
  font-family: var(--font-display);
  font-size: 36px;
  color: white;
  -webkit-text-stroke: 1px var(--title-shadow);
  white-space: nowrap;
}

/* ---------- ANSWER ROW ---------- */

#answerRow {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 340px;
  align-items: center;
}

#answerInput {
  font-family: var(--font-display);
  font-size: 26px;
  padding: 10px 16px;
  border-radius: 14px;
  border: 3px solid #5a3a99;
  background: rgba(24, 12, 54, 0.9);
  color: var(--primary-cyan);
  flex: 1;
  min-width: 0;
  text-align: center;
  outline: none;
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.4);
  caret-color: var(--primary-cyan);
}

#answerInput:focus {
  border-color: var(--primary-cyan);
}

#okButton {
  font-family: var(--font-display);
  font-size: 20px;
  padding: 10px 20px;
  border-radius: 14px;
  border: none;
  background: var(--fire-orange);
  color: #fff;
  border-bottom: 5px solid #c96d00;
  cursor: pointer;
  text-transform: uppercase;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.2);
  transition: transform 0.1s;
  white-space: nowrap;
}

#okButton:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
}

/* ---------- ERROR ---------- */

#error {
  background: var(--fire-red);
  color: white;
  padding: 6px 16px;
  border-radius: 10px;
  font-weight: 800;
  font-size: 14px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.3);
}

#error.hidden { display: none; }

/* ---------- MOBILE NUMPAD ---------- */

#mobilePad {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 340px;
}

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

.pad-btn {
  flex: 1;
  background: rgba(45, 27, 94, 0.85);
  border: 2px solid rgba(90, 58, 153, 0.7);
  color: var(--primary-cyan);
  font-family: var(--font-display);
  font-size: 22px;
  padding: 14px 0;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 3px 0 rgba(0,0,0,0.3);
  transition: transform 0.08s;
  -webkit-user-select: none;
  user-select: none;
}

.pad-btn:active {
  transform: translateY(2px);
  box-shadow: none;
  background: rgba(77, 238, 234, 0.2);
}

.action-del {
  background: rgba(231, 76, 60, 0.35);
  border-color: rgba(192, 57, 43, 0.6);
  color: #ffdce0;
}

.action-del:active {
  background: rgba(231, 76, 60, 0.5);
}

/* ---------- OVERLAYS ---------- */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(17, 5, 44, 0.88);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 100;
  overflow-y: auto;
  padding: 20px 12px;
}

.overlay.active {
  display: flex;
}

.panel {
  background: linear-gradient(135deg, #4b2a85, #2d1b5e);
  border: 3px solid #6c46ba;
  box-shadow: 0 12px 0 rgba(0,0,0,0.25);
  border-radius: 24px;
  padding: 28px 24px;
  text-align: center;
  width: 100%;
  max-width: 440px;
  max-height: 90dvh;
  overflow-y: auto;
}

.game-title {
  font-family: var(--font-display);
  font-size: 52px;
  line-height: 0.9;
  margin: 0 0 10px 0;
  color: var(--title-blue);
  text-shadow: 
    0px 5px 0px var(--title-shadow),
    0px 10px 4px rgba(0,0,0,0.4);
  transform: rotate(-3deg);
}

.white-text {
  color: white;
  text-shadow: 
    0px 5px 0px #999,
    0px 10px 4px rgba(0,0,0,0.4);
}

.subtitle {
  font-size: 16px;
  margin-bottom: 16px;
  color: #bfa3ff;
  font-weight: 700;
}

.intro-text {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.75);
  margin: 0 0 12px 0;
  line-height: 1.4;
}

#welcomeMessage {
  color: #f1c40f;
  font-weight: bold;
  margin: 0 0 16px 0;
  font-size: 15px;
}

#welcomeMessage:empty {
  display: none;
}

/* Buttons */
.primary-btn, .secondary-btn {
  font-family: var(--font-display);
  border: none;
  cursor: pointer;
  text-transform: uppercase;
  transition: transform 0.1s;
  display: inline-block;
}

.primary-btn {
  background: var(--fire-orange);
  color: #fff;
  border-bottom: 5px solid #c96d00;
  border-radius: 14px;
  padding: 12px 24px;
  font-size: 20px;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.2);
}

.primary-btn:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
}

.secondary-btn {
  background: #6c46ba;
  color: white;
  border-bottom: 5px solid #4a2c85;
  border-radius: 14px;
  padding: 10px 20px;
  font-size: 16px;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.2);
}

.secondary-btn:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
}

.big-btn {
  font-size: 28px;
  padding: 14px 40px;
  border-bottom-width: 7px;
  width: 100%;
  margin-bottom: 16px;
}

.danger-btn {
  background: #e74c3c;
  color: white;
  border: none;
  font-family: var(--font-display);
  border-bottom: 5px solid #c0392b;
  border-radius: 14px;
  padding: 10px 20px;
  font-size: 16px;
  text-transform: uppercase;
  cursor: pointer;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.2);
}

.danger-btn:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
}

.row {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
}

/* Highscores */
#startHighscores {
  margin-top: 16px;
  background: rgba(0,0,0,0.2);
  border-radius: 14px;
  padding: 12px;
}

#startHighscores h3 {
  margin: 0 0 8px 0;
  color: var(--primary-cyan);
  font-family: var(--font-display);
  font-size: 15px;
  text-transform: uppercase;
}

#miniHighscoreList div {
  font-size: 14px;
  padding: 4px 0;
  border-bottom: 1px dashed rgba(255,255,255,0.1);
  display: flex;
  justify-content: space-between;
}

.links {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 12px;
}

.hidden { display: none !important; }

/* ---------- Settings / Highscore panels ---------- */

textarea {
  width: 100%;
  background: rgba(24, 12, 54, 0.9);
  border: 2px solid #5a3a99;
  color: white;
  padding: 10px;
  border-radius: 12px;
  font-family: monospace;
  font-size: 15px;
  resize: vertical;
  min-height: 80px;
  outline: none;
}

textarea:focus { border-color: var(--primary-cyan); }

h2 {
  font-family: var(--font-display);
  color: var(--primary-cyan);
  margin: 0 0 16px 0;
  font-size: 26px;
}

#highscoreList {
  background: rgba(0,0,0,0.3);
  border-radius: 12px;
  padding: 8px;
  max-height: 160px;
  overflow-y: auto;
  text-align: left;
  margin: 16px 0 8px;
}

#highscoreList div {
  padding: 6px 8px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  font-size: 14px;
}

input[type="text"], input[type="number"] {
  background: rgba(24, 12, 54, 0.9);
  border: 2px solid #5a3a99;
  color: white;
  padding: 10px;
  border-radius: 10px;
  font-size: 15px;
  outline: none;
}

input[type="text"]:focus, input[type="number"]:focus {
  border-color: var(--primary-cyan);
}

/* ---------- Settings Panel ---------- */

.settings-panel {
  display: flex;
  flex-direction: column;
  max-height: 88dvh;
  width: 100%;
  max-width: 550px;
}

.settings-scroll-area {
  flex: 1;
  overflow-y: auto;
  text-align: left;
  padding-right: 4px;
  margin-bottom: 16px;
}

.settings-scroll-area::-webkit-scrollbar { width: 6px; }
.settings-scroll-area::-webkit-scrollbar-track { background: rgba(0,0,0,0.1); border-radius: 3px; }
.settings-scroll-area::-webkit-scrollbar-thumb { background: var(--primary-dark); border-radius: 3px; }

.settings-group {
  margin-bottom: 14px;
}

.settings-group label {
  display: block;
  color: var(--primary-cyan);
  font-weight: bold;
  margin-bottom: 5px;
  font-size: 14px;
}

.settings-group input[type="text"], 
.settings-group input[type="number"],
.settings-group textarea {
  width: 100%;
}

.row-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.row-group label { margin-bottom: 0; }
.row-group input { width: 90px !important; }

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.settings-actions button {
  flex: 1;
  min-width: 120px;
  font-size: 16px;
  padding: 10px 14px;
}

/* ========== DESKTOP (min-width: 700px) ========== */

@media (min-width: 700px) {
  #app {
    width: calc(100vw - 40px);
    height: calc(100vh - 40px);
    max-width: 1000px;
    max-height: 700px;
    border-radius: 24px;
    border: 4px solid #4a2c85;
    box-shadow: 0 20px 50px rgba(0,0,0,0.6);
    margin: auto;
    position: absolute;
    inset: 0;
  }

  body {
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at center, var(--bg-light), var(--bg-deep) 80%);
  }

  /* Desktop: Canvas fills full area, controls overlay bottom */
  #scene {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    flex: none;
  }

  #hud-bar {
    padding: 14px 20px;
    top: 0;
    left: 0;
    right: 0;
  }

  .hud-item { padding: 8px 18px; }
  .hud-label { font-size: 13px; }
  #score, #timer { font-size: 28px; }

  /* Desktop: Controls overlay on canvas */
  #game-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px 20px 20px;
    background: linear-gradient(to bottom, rgba(17, 5, 44, 0.0), rgba(17, 5, 44, 0.8) 30%);
  }

  #taskCard {
    padding: 14px 40px;
    border-radius: 24px;
  }

  #taskText {
    font-size: 48px;
    -webkit-text-stroke: 1.5px var(--title-shadow);
  }

  #answerRow { max-width: 380px; }

  #answerInput {
    font-size: 30px;
    padding: 12px 20px;
    width: 260px;
  }

  #okButton {
    font-size: 22px;
    padding: 12px 24px;
  }

  /* Hide numpad on desktop */
  #mobilePad {
    display: none !important;
  }

  .game-title { font-size: 72px; }
  .subtitle { font-size: 18px; margin-bottom: 24px; }
  .big-btn { font-size: 30px; padding: 16px 48px; }
  .panel { padding: 36px 32px; }
}

/* ========== SMALL PHONES (max-height: 650px) ========== */

@media (max-height: 650px) {
  #taskCard { padding: 6px 20px; }
  #taskText { font-size: 28px; }
  
  .pad-btn { padding: 10px 0; font-size: 20px; }
  #game-controls { gap: 5px; padding: 5px 10px; }

  #answerInput { font-size: 22px; padding: 8px 12px; }
  #okButton { font-size: 18px; padding: 8px 16px; }

  .game-title { font-size: 42px; }
  .big-btn { font-size: 22px; padding: 10px 24px; }
  .panel { padding: 20px 16px; }
  .subtitle { font-size: 14px; margin-bottom: 12px; }
}

/* ========== LANDSCAPE MOBILE ========== */

@media (max-height: 500px) and (orientation: landscape) {
  #game-controls {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 16px;
    padding: 4px 16px;
  }

  #taskCard { order: 1; }
  #answerRow { order: 2; max-width: 260px; }
  #mobilePad { 
    order: 3; 
    max-width: 240px; 
    gap: 3px;
  }
  .pad-row { gap: 3px; }
  .pad-btn { padding: 8px 0; font-size: 18px; }
  #error { order: 4; }
}
