/* ============================================================
   Mad Chemist: Potion Panic — styles
   Responsive lab theme. Desktop-first, ไม่พังบน tablet/มือถือ.
   ============================================================ */

:root {
  --bg-0: #0b1020;
  --bg-1: #131a33;
  --bg-2: #1b2445;
  --ink: #eaf1ff;
  --ink-dim: #9fb0d4;
  --line: #2b3866;
  --accent: #38e8c6;      /* neon mint */
  --accent-2: #7c5cff;    /* neon purple */
  --danger: #ff5470;
  --good: #4fe08a;

  --red: #ff2f3a;
  --yellow: #ffe11f;
  --blue: #2f6bff;

  --radius: 18px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --font: "Kanit", system-ui, sans-serif;
  --font-display: "Baloo 2", "Kanit", system-ui, sans-serif;
}

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

html, body { height: 100%; }

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 800px at 80% -10%, rgba(124, 92, 255, 0.22), transparent 60%),
    radial-gradient(1000px 700px at -10% 110%, rgba(56, 232, 198, 0.18), transparent 55%),
    linear-gradient(160deg, var(--bg-0), var(--bg-1) 55%, var(--bg-2));
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#app { position: relative; width: 100vw; height: 100vh; height: 100dvh; overflow-x: hidden; }

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0;
  display: none;
  padding: clamp(12px, 2.5vw, 28px);
  overflow-y: auto;
}
.screen.active { display: flex; }
#screen-start.active, #screen-over.active {
  /* column + margin:auto บนการ์ด = จอสูงจัดกลาง, จอเตี้ยชิดบนแล้วเลื่อนได้จริง (ไม่โดนตัดหัว) */
  flex-direction: column;
  align-items: center;
}
.start-card, .over-card { margin-block: auto; }

/* ---------- corner sound button ---------- */
.corner-btn {
  position: fixed; top: 12px; right: 12px; z-index: 50;
  width: 46px; height: 46px; border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(20, 28, 56, 0.8);
  color: var(--ink); font-size: 20px; cursor: pointer;
  backdrop-filter: blur(6px);
  transition: transform .12s ease, background .2s;
}
.corner-btn:hover { transform: scale(1.08); background: rgba(40, 52, 100, 0.9); }
.corner-btn.muted { opacity: .5; }

/* ============================================================
   START SCREEN
   ============================================================ */
.lab-decor { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.float-flask {
  position: absolute; font-size: clamp(40px, 8vw, 90px); opacity: .12;
  animation: bob 6s ease-in-out infinite;
}
.float-flask.f1 { top: 10%; left: 8%; animation-delay: 0s; }
.float-flask.f2 { top: 65%; left: 12%; animation-delay: 1.4s; }
.float-flask.f3 { top: 18%; right: 10%; animation-delay: .8s; }
.float-flask.f4 { top: 70%; right: 8%; animation-delay: 2.2s; }
@keyframes bob { 0%,100%{ transform: translateY(0) rotate(-4deg);} 50%{ transform: translateY(-22px) rotate(4deg);} }

.start-card {
  position: relative; z-index: 2;
  width: min(680px, 100%);
  background: linear-gradient(180deg, rgba(27, 36, 69, 0.92), rgba(19, 26, 51, 0.92));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(20px, 4vw, 40px);
  text-align: center;
}

.game-title {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(38px, 9vw, 76px);
  line-height: .95;
  letter-spacing: 1px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2) 60%, #ff7ac6);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 4px 18px rgba(124, 92, 255, .4));
}
.game-subtitle { font-family: var(--font-display); font-weight: 700; font-size: clamp(16px, 3.5vw, 24px); margin-top: 6px; }
.tagline { color: var(--ink-dim); margin-top: 10px; font-size: clamp(14px, 2.6vw, 17px); }

.howto {
  text-align: left; margin: 22px auto 4px;
  background: rgba(11, 16, 32, .5);
  border: 1px solid var(--line);
  border-radius: 14px; padding: 16px 18px;
}
.howto h2 { font-size: 17px; margin-bottom: 8px; color: var(--accent); }
.howto ul { list-style: none; display: grid; gap: 6px; }
.howto li { font-size: clamp(13px, 2.5vw, 15.5px); line-height: 1.5; }

/* สูตรสี: เรียงลงมา บรรทัดละสูตร */
.formulas { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.formulas-label { font-size: clamp(12px, 2.4vw, 14px); color: var(--ink-dim); }
.formula { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.formula i { font-style: normal; color: var(--ink-dim); font-weight: 700; }

.chip {
  display: inline-block; padding: 1px 9px; border-radius: 999px;
  font-weight: 600; color: #10131f; font-size: 12.5px;
}
.chip.red { background: #ff5b63; color:#fff; }
.chip.yellow { background: #ffe11f; }
.chip.blue { background: #4f82ff; color:#fff; }
.chip.orange { background: #ff9b2f; }
.chip.green { background: #37c76a; color:#fff; }
.chip.purple { background: #a066e0; color:#fff; }

.difficulty { display: flex; gap: 10px; justify-content: center; margin: 18px 0 16px; flex-wrap: wrap; }
.diff-btn {
  font-family: var(--font); font-size: 15px; font-weight: 600;
  padding: 10px 18px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: rgba(11,16,32,.5); color: var(--ink);
  transition: all .15s;
}
.diff-btn.active { background: linear-gradient(90deg, var(--accent), var(--accent-2)); color: #08111f; border-color: transparent; }

.btn-primary {
  font-family: var(--font-display); font-weight: 700;
  font-size: clamp(18px, 4vw, 22px);
  padding: 14px 40px; border-radius: 999px; cursor: pointer; border: none;
  color: #08111f;
  background: linear-gradient(90deg, var(--accent), #6ee7ff);
  box-shadow: 0 8px 24px rgba(56, 232, 198, .45);
  transition: transform .12s ease, box-shadow .2s;
}
.btn-primary:hover { transform: translateY(-2px) scale(1.02); box-shadow: 0 12px 30px rgba(56,232,198,.6); }
.btn-primary:active { transform: translateY(0) scale(.99); }

/* ============================================================
   GAME SCREEN
   ============================================================ */
#screen-game { flex-direction: column; gap: clamp(8px, 1.6vh, 14px); }

#hud {
  display: flex; gap: 10px; justify-content: center; flex-wrap: wrap;
  max-width: 1100px; width: 100%; margin: 0 auto;
}
.hud-item {
  flex: 1 1 0; min-width: 90px;
  display: flex; flex-direction: column; align-items: center;
  background: rgba(27, 36, 69, .7); border: 1px solid var(--line);
  border-radius: 14px; padding: 8px 6px;
}
.hud-label { font-size: 12px; color: var(--ink-dim); }
.hud-value { font-family: var(--font-display); font-weight: 700; font-size: clamp(18px, 4vw, 26px); }
.hud-lives .hud-value { letter-spacing: 2px; }

/* timer */
#timer-wrap {
  position: relative; max-width: 1100px; width: 100%; margin: 0 auto;
  height: 22px; border-radius: 999px;
  background: rgba(11,16,32,.7); border: 1px solid var(--line); overflow: hidden;
}
#timer-bar {
  position: absolute; inset: 0; width: 100%; transform-origin: left;
  background: linear-gradient(90deg, var(--good), var(--accent));
  transition: width .25s linear, background .3s;
}
#timer-bar.warn { background: linear-gradient(90deg, #ffb64f, #ff8a3d); }
#timer-bar.danger { background: linear-gradient(90deg, #ff6b6b, var(--danger)); }
#timer-text {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-weight: 700; font-size: 13px; color: #08111f; text-shadow: 0 1px 2px rgba(255,255,255,.4);
}

/* playfield: target card | mixing zone */
#playfield {
  flex: 1; display: grid;
  grid-template-columns: minmax(200px, 300px) minmax(0, 1fr);
  gap: clamp(10px, 2vw, 22px);
  max-width: 1100px; width: 100%; margin: 0 auto; min-height: 0;
}
/* กัน grid track ดันจนล้นจอ (min-width:auto ของ grid item) */
#playfield > *, #mix-zone, #beaker-stage, #target-card { min-width: 0; }

/* target card */
#target-card {
  background: rgba(27, 36, 69, .7); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
#target-card h3 { color: var(--accent); font-size: 15px; letter-spacing: 1px; }
.target-swatch-wrap {
  width: 100%; aspect-ratio: 1.4 / 1; border-radius: 14px; padding: 8px;
  background:
    repeating-conic-gradient(#2a3560 0% 25%, #222c52 0% 50%) 50% / 22px 22px;
  border: 1px solid var(--line);
}
#target-swatch {
  width: 100%; height: 100%; border-radius: 10px;
  background: #ff9b2f;
  box-shadow: inset 0 0 22px rgba(0,0,0,.35), 0 6px 18px rgba(0,0,0,.35);
}
.target-info { text-align: center; }
.target-name { font-family: var(--font-display); font-weight: 700; font-size: clamp(20px, 3.4vw, 26px); }
.target-vol { color: var(--ink-dim); font-size: 15px; margin-top: 2px; }
.target-vol b { color: var(--ink); font-size: 18px; }

.btn-ghost {
  font-family: var(--font); font-size: 14px; font-weight: 600;
  padding: 8px 16px; border-radius: 999px; cursor: pointer;
  border: 1px dashed var(--line); background: transparent; color: var(--ink-dim);
  transition: all .15s;
}
.btn-ghost:hover { color: var(--ink); border-color: var(--accent); }
#hint-box {
  font-size: 13.5px; color: var(--ink); background: rgba(11,16,32,.6);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; text-align: center; line-height: 1.5;
}

/* mixing zone */
#mix-zone {
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  gap: clamp(8px, 1.8vh, 16px); min-height: 0;
}
#beaker-stage {
  display: flex; align-items: center; gap: clamp(10px, 2vw, 26px);
  flex: 1; min-height: 0;
}
#beaker { height: min(42vh, 320px); width: auto; max-width: 100%; filter: drop-shadow(0 12px 20px rgba(0,0,0,.4)); }
#beaker.shake { animation: shake .4s ease; }
@keyframes shake {
  0%,100%{ transform: translateX(0) rotate(0);}
  20%{ transform: translateX(-8px) rotate(-3deg);}
  40%{ transform: translateX(8px) rotate(3deg);}
  60%{ transform: translateX(-6px) rotate(-2deg);}
  80%{ transform: translateX(6px) rotate(2deg);}
}
#liquid { transition: y .35s cubic-bezier(.34,1.4,.5,1), height .35s cubic-bezier(.34,1.4,.5,1), fill .3s; }
#liquid-top { transition: cy .35s cubic-bezier(.34,1.4,.5,1); }

#readout {
  background: rgba(11,16,32,.6); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 16px; text-align: center; min-width: 120px;
}
.readout-vol { font-family: var(--font-display); font-weight: 800; font-size: clamp(24px, 5vw, 34px); }
.readout-vol span { color: var(--accent); }
.readout-mix { margin-top: 6px; font-size: 15px; color: var(--ink-dim); display: flex; gap: 8px; justify-content: center; }

/* dispensers */
#dispensers { display: flex; gap: clamp(10px, 3vw, 26px); justify-content: center; }
.flask {
  position: relative; border: none; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px; transition: transform .1s;
}
.flask svg { width: clamp(52px, 12vw, 78px); height: auto; filter: drop-shadow(0 6px 10px rgba(0,0,0,.4)); }
.flask .flask-neck { fill: #cfe0ff; }
.flask[data-color="red"] .flask-body { fill: var(--red); }
.flask[data-color="yellow"] .flask-body { fill: var(--yellow); }
.flask[data-color="blue"] .flask-body { fill: var(--blue); }
.flask .flask-name { font-size: 13px; font-weight: 600; color: var(--ink-dim); }
.flask:hover { transform: translateY(-4px); }
.flask:active, .flask.pour { transform: translateY(2px) rotate(-8deg); }
.flask::after {
  content: ""; position: absolute; top: -4px; left: 50%; width: 60%; height: 60%;
  border-radius: 50%; background: radial-gradient(circle, rgba(255,255,255,.5), transparent 60%);
  transform: translate(-50%, -100%); opacity: 0; pointer-events: none;
}

/* controls */
#controls { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.btn-ctrl {
  font-family: var(--font); font-weight: 600; font-size: clamp(14px, 2.8vw, 16px);
  padding: 11px 20px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: rgba(27,36,69,.85); color: var(--ink);
  transition: all .13s;
}
.btn-ctrl:hover { transform: translateY(-2px); border-color: var(--accent); }
.btn-ctrl:active { transform: translateY(0); }
.btn-check {
  background: linear-gradient(90deg, var(--good), var(--accent));
  color: #08111f; border-color: transparent; font-weight: 700;
}

/* flash message */
#flash {
  position: absolute; inset: 0; display: grid; place-items: center; z-index: 40;
  pointer-events: none;
}
#flash .flash-inner {
  font-family: var(--font-display); font-weight: 800;
  font-size: clamp(40px, 12vw, 100px); text-align: center;
  animation: pop .6s cubic-bezier(.2,1.6,.4,1);
  text-shadow: 0 6px 30px rgba(0,0,0,.6);
}
#flash.good .flash-inner { color: var(--good); }
#flash.bad .flash-inner { color: var(--danger); }
@keyframes pop { 0%{ transform: scale(.3); opacity: 0;} 60%{ transform: scale(1.1); opacity: 1;} 100%{ transform: scale(1); opacity: 1;} }

/* confetti + boom particles */
.particle { position: absolute; z-index: 45; pointer-events: none; will-change: transform, opacity; }

/* ============================================================
   GAME OVER
   ============================================================ */
.over-card {
  width: min(520px, 100%); text-align: center;
  background: linear-gradient(180deg, rgba(27,36,69,.92), rgba(19,26,51,.92));
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: clamp(24px, 5vw, 44px);
}
#over-emoji { font-size: clamp(60px, 16vw, 110px); animation: bob 3s ease-in-out infinite; }
#over-title { font-family: var(--font-display); font-weight: 800; font-size: clamp(28px, 7vw, 44px); margin-top: 4px; }
#over-msg { color: var(--ink-dim); margin-top: 6px; font-size: 16px; }
.over-stats { display: flex; gap: 16px; justify-content: center; margin: 22px 0; }
.over-stats div {
  background: rgba(11,16,32,.5); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 22px; display: flex; flex-direction: column;
}
.over-stats .big { font-family: var(--font-display); font-weight: 800; font-size: clamp(28px, 7vw, 40px); color: var(--accent); }
.over-stats small { color: var(--ink-dim); font-size: 13px; }
.over-fact {
  background: rgba(56,232,198,.08); border: 1px solid rgba(56,232,198,.3);
  border-radius: 12px; padding: 12px 14px; margin-bottom: 20px;
  font-size: 14px; line-height: 1.6; color: var(--ink);
}
.over-fact b { color: var(--accent); }

.hidden { display: none !important; }

/* ============================================================
   RESPONSIVE — tablet / มือถือ (แนวตั้ง)
   ============================================================ */
@media (max-width: 820px) {
  #playfield { grid-template-columns: 1fr; }
  /* HUD กันล้นขอบบนจอแคบ: ให้ card ย่อได้ ไม่ดันจนหลุดจอ */
  #hud { gap: 6px; }
  .hud-item { min-width: 0; padding: 6px 4px; }
  .hud-value { font-size: clamp(16px, 4.5vw, 22px); }
  .hud-lives .hud-value { letter-spacing: 0; }
  #target-card {
    flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 12px;
    padding: 12px;
  }
  #target-card h3 { width: 100%; }
  .target-swatch-wrap { width: 96px; aspect-ratio: 1; flex: 0 0 auto; }
  .target-info { flex: 0 0 auto; text-align: left; }
  #btn-hint { flex: 0 0 auto; }
  #hint-box { width: 100%; }
  #beaker { height: min(34vh, 260px); }
}

@media (max-width: 820px) and (orientation: portrait) {
  #beaker-stage { flex-direction: column; gap: 8px; }
  #readout { display: flex; gap: 18px; align-items: center; padding: 8px 16px; }
  .readout-vol { font-size: 26px; }
}

/* จอเตี้ยมาก (มือถือแนวนอน) */
@media (max-height: 560px) {
  .howto { display: none; }
  #beaker { height: min(60vh, 240px); }
}

/* จอใหญ่ */
@media (min-width: 1100px) {
  #beaker { height: min(46vh, 360px); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .05ms !important; }
}
