/* กระเป๋าทวีธา ทนได้! — V3
   Palette taken from the original clip: cream stairwell walls, terracotta hex
   floor, dark wood treads, green school polo, gold crest. */

:root {
  --wall: #dfe3d2;
  --wall-dim: #b8c0ac;
  --ink: #12211a;
  --ink-soft: #1f3a2c;
  --green: #2f5c46;
  --green-deep: #16301f;
  --wood: #8c4e2a;
  --wood-hi: #b06a3c;
  --terracotta: #b06f4a;
  --gold: #d8a83a;
  --gold-hi: #ffe9a0;
  --cream: #f4efdd;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #0e1712;
  color: var(--cream);
  font-family: "Noto Sans Thai", "Sukhumvit Set", system-ui, -apple-system, sans-serif;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  display: grid;
  place-items: center;
  /* keeps the stage centred on desktop without a scrollbar */
  overflow: hidden;
}

#game-shell {
  position: relative;
  width: min(100vw, 480px);
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  background: #2a3a2e;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .55);
  /* stop the browser hijacking drags as scroll/zoom on phones */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  isolation: isolate;
}

@media (min-width: 520px) and (min-height: 800px) {
  #game-shell { height: min(100dvh, 880px); border-radius: 22px; }
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  /* the canvas is its own layer; nothing outside it needs to repaint */
  contain: strict;
  will-change: transform;
  touch-action: none;
}

/* ---------------------------------------------------------------- HUD --- */

.hud {
  position: absolute;
  inset: calc(var(--safe-top) + 10px) 12px auto 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: none;
  z-index: 5;
}

.school-badge {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: linear-gradient(160deg, var(--green) 0%, var(--green-deep) 100%);
  border: 2px solid rgba(216, 168, 58, .8);
  color: var(--gold-hi);
  font-weight: 900;
  font-size: 13px;
  letter-spacing: .5px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .35);
}

.score-wrap {
  display: flex;
  align-items: baseline;
  gap: 7px;
  padding: 5px 14px 6px;
  border-radius: 999px;
  background: rgba(18, 33, 26, .72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(244, 239, 221, .16);
}
.score-label { font-size: 11px; opacity: .72; letter-spacing: .4px; }
#score {
  font-size: 22px;
  font-weight: 900;
  color: var(--gold-hi);
  font-variant-numeric: tabular-nums;
  min-width: 1.6ch;
  text-align: right;
}

.hud-actions { margin-left: auto; display: flex; gap: 7px; pointer-events: auto; }

.icon-button {
  width: 38px; height: 38px;
  border-radius: 12px;
  border: 1px solid rgba(244, 239, 221, .2);
  background: rgba(18, 33, 26, .72);
  color: var(--cream);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: grid; place-items: center;
  transition: transform .12s ease, background .12s ease;
}
.icon-button:active { transform: scale(.92); background: rgba(47, 92, 70, .9); }

.combo-tag {
  position: absolute;
  top: calc(var(--safe-top) + 58px);
  left: 50%;
  transform: translateX(-50%) scale(.85);
  padding: 4px 14px;
  border-radius: 999px;
  background: linear-gradient(140deg, #f0913a, #d8531f);
  color: #fff8e6;
  font-weight: 900;
  font-size: 14px;
  letter-spacing: .5px;
  box-shadow: 0 4px 14px rgba(216, 83, 31, .45);
  opacity: 0;
  pointer-events: none;
  z-index: 5;
  transition: opacity .18s ease, transform .18s ease;
}
.combo-tag.visible { opacity: 1; transform: translateX(-50%) scale(1); }

/* --------------------------------------------------------- power meter --- */

.power-meter {
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: calc(var(--safe-bottom) + 14px);
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 9px;
  border-radius: 16px;
  background: rgba(11, 22, 17, .7);
  border: 1px solid rgba(244, 239, 221, .16);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  transform: translateY(14px);
  transition: opacity .22s ease, transform .22s ease;
  pointer-events: none;
  z-index: 5;
}
.power-meter.visible { opacity: 1; transform: none; pointer-events: auto; }
.power-meter.visible { cursor: pointer; }

.power-body { flex: 1; min-width: 0; }
.power-hint {
  margin: 5px 2px 0;
  font-size: 10.5px;
  line-height: 1.3;
  letter-spacing: .1px;
  color: rgba(244, 239, 221, .8);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* light the hint up once the meter is charged (no-op on browsers without :has) */
.power-meter:has(.power-fill.ready) .power-hint { color: var(--gold-hi); }

@media (max-width: 340px) {
  .power-hint { font-size: 9.5px; }
}

.power-track {
  flex: 1;
  height: 12px;
  border-radius: 999px;
  background: rgba(11, 22, 17, .75);
  border: 1px solid rgba(244, 239, 221, .18);
  overflow: hidden;
}
.power-fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, #6f9c76, #d8a83a);
  transition: width .16s linear;
}
.power-fill.ready { background: linear-gradient(90deg, #ffd873, #fff3c4); }
.power-fill.active { background: linear-gradient(90deg, #ff9f43, #ffe08a); }

.power-button {
  pointer-events: auto;
  flex: 0 0 auto;
  min-width: 86px;
  padding: 11px 14px;
  border-radius: 14px;
  border: 1px solid rgba(216, 168, 58, .35);
  background: rgba(18, 33, 26, .82);
  color: rgba(244, 239, 221, .55);
  font-family: inherit;
  font-weight: 900;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center;
  line-height: 1.15;
  transition: transform .12s ease, color .12s ease, box-shadow .2s ease;
}
.power-button small { font-size: 9px; opacity: .6; font-weight: 700; }
.power-button.ready {
  color: #1a1205;
  background: linear-gradient(150deg, #ffe08a, #d8a83a);
  box-shadow: 0 0 0 0 rgba(216, 168, 58, .6);
  animation: pulse 1.3s ease-out infinite;
}
.power-button:active { transform: scale(.94); }

@keyframes pulse {
  to { box-shadow: 0 0 0 16px rgba(216, 168, 58, 0); }
}

/* --------------------------------------------------------------- mixer --- */

.mixer-panel {
  position: absolute;
  top: calc(var(--safe-top) + 56px);
  right: 12px;
  width: min(268px, calc(100% - 24px));
  padding: 14px 15px 12px;
  border-radius: var(--radius);
  background: rgba(15, 27, 21, .95);
  border: 1px solid rgba(244, 239, 221, .16);
  box-shadow: 0 18px 40px rgba(0, 0, 0, .5);
  opacity: 0;
  transform: translateY(-8px) scale(.97);
  pointer-events: none;
  transition: opacity .16s ease, transform .16s ease;
  z-index: 20;
}
.mixer-panel.visible { opacity: 1; transform: none; pointer-events: auto; }

.mixer-header {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 10px;
}
.mixer-header strong { font-size: 14px; }
.mixer-header span { font-size: 9px; letter-spacing: 1.6px; opacity: .5; }

.mix-row {
  display: block;
  margin-bottom: 11px;
  font-size: 12px;
}
.mix-row > span { display: flex; justify-content: space-between; margin-bottom: 5px; opacity: .88; }
.mix-row small { font-size: 9px; letter-spacing: 1px; opacity: .48; }
.mix-row.toggle > span { align-items: center; }
.mix-row.toggle { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.mix-row.toggle > span { flex: 1; margin-bottom: 0; }

.mix-row input[type="range"] {
  width: 100%;
  height: 22px;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
}
.mix-row input[type="range"]::-webkit-slider-runnable-track {
  height: 5px; border-radius: 999px;
  background: rgba(244, 239, 221, .18);
}
.mix-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 17px; height: 17px; margin-top: -6px;
  border-radius: 50%;
  background: var(--gold-hi);
  border: 2px solid var(--gold);
}
.mix-row input[type="range"]::-moz-range-track {
  height: 5px; border-radius: 999px; background: rgba(244, 239, 221, .18);
}
.mix-row input[type="range"]::-moz-range-thumb {
  width: 15px; height: 15px; border-radius: 50%;
  background: var(--gold-hi); border: 2px solid var(--gold);
}
.mix-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--gold); }

.mix-divider { height: 1px; background: rgba(244, 239, 221, .13); margin: 12px 0; }

.mix-record strong { display: block; font-size: 12px; margin-bottom: 7px; }
.record-button {
  width: 100%;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid rgba(220, 108, 53, .5);
  background: rgba(220, 108, 53, .16);
  color: #ffcda8;
  font-family: inherit; font-weight: 700; font-size: 12px;
  cursor: pointer;
}
.record-button.recording { background: #c8391f; color: #fff; border-color: #c8391f; }
.record-status { font-size: 10px; opacity: .6; margin: 6px 0 0; line-height: 1.4; }
.mix-hint { font-size: 10px; opacity: .45; margin: 10px 0 0; text-align: center; }

/* --------------------------------------------------------------- cards --- */

.screen-card {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: calc(var(--safe-top) + 22px) 26px calc(var(--safe-bottom) + 22px);
  text-align: center;
  background:
    radial-gradient(120% 80% at 50% 20%, rgba(28, 48, 36, .82), rgba(9, 17, 13, .95));
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  overflow-y: auto;
}
.screen-card.visible { display: flex; animation: cardIn .3s ease both; }

@keyframes cardIn {
  from { opacity: 0; transform: scale(.97); }
  to { opacity: 1; transform: none; }
}

.year-tag {
  font-size: 9px;
  letter-spacing: 2.4px;
  opacity: .6;
  border: 1px solid rgba(244, 239, 221, .22);
  padding: 4px 12px;
  border-radius: 999px;
}

.title-graphic, .endcard-graphic {
  width: min(200px, 52%);
  border-radius: 10px;
  filter: saturate(.85) contrast(1.06);
  box-shadow: 0 10px 28px rgba(0, 0, 0, .5);
}

.screen-card h1 {
  font-family: "Chonburi", "Noto Sans Thai", serif;
  font-size: clamp(34px, 11vw, 46px);
  line-height: 1.06;
  margin: 4px 0 0;
  color: var(--cream);
  text-shadow: 0 3px 0 rgba(0, 0, 0, .3);
}
.screen-card h1 em { font-style: normal; color: var(--gold-hi); }
.screen-card h2 {
  font-family: "Chonburi", "Noto Sans Thai", serif;
  font-size: 27px; margin: 2px 0 0;
}
.tagline { font-size: 13px; opacity: .78; margin: 0; line-height: 1.5; }

.lore-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  width: 100%;
  max-width: 300px;
  margin-top: 4px;
}
.lore-strip img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 6px;
  filter: saturate(.75) contrast(1.05);
  border: 1px solid rgba(244, 239, 221, .16);
}
.lore-caption { font-size: 10px; opacity: .5; margin: 0; }

.primary-button {
  margin-top: 8px;
  padding: 13px 34px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(150deg, var(--gold-hi) 0%, var(--gold) 62%, #a97c1c 100%);
  color: #22190a;
  font-family: inherit;
  font-weight: 900;
  font-size: 17px;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  box-shadow: 0 8px 22px rgba(216, 168, 58, .32), inset 0 -3px 0 rgba(0, 0, 0, .18);
  transition: transform .12s ease;
}
.primary-button small { font-size: 9px; letter-spacing: 1.4px; opacity: .6; font-weight: 700; }
.primary-button:active { transform: translateY(2px) scale(.98); }

.ghost-button {
  background: none;
  border: 1px solid rgba(244, 239, 221, .28);
  color: rgba(244, 239, 221, .8);
  padding: 8px 18px;
  border-radius: 999px;
  font-family: inherit; font-size: 12px;
  cursor: pointer;
}
.how-to { font-size: 11px; opacity: .55; margin: 2px 0 0; line-height: 1.5; }

/* --- game-over: the guy from the last shot, saying his line --------------- */

.speaker-block {
  position: relative;
  width: min(300px, 82%);
  margin: 2px auto 0;
  /* room for the bubble above and the stamp to the side, so neither clips */
  padding: 58px 0 0;
}
.speaker-block .speaker {
  display: block;
  width: 74%;
  margin: 0 0 0 auto;
  filter: drop-shadow(0 10px 20px rgba(0, 0, 0, .5));
}

.speech-bubble {
  position: absolute;
  top: 0;
  left: 0;
  max-width: 74%;
  padding: 9px 15px 10px;
  border-radius: 15px 15px 4px 15px;
  background: linear-gradient(160deg, #fffdf3, #efe7cd);
  color: #1c2a1f;
  font-weight: 900;
  font-size: clamp(15px, 4.6vw, 19px);
  line-height: 1.35;
  /* Thai tone marks sit high; give them somewhere to go */
  padding-top: 11px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .4);
  animation: bubbleIn .34s cubic-bezier(.2, 1.3, .4, 1) .35s both;
  z-index: 2;
}
.speech-bubble::after {
  content: "";
  position: absolute;
  right: -6px;
  bottom: 8px;
  width: 20px;
  height: 16px;
  background: #efe7cd;
  clip-path: polygon(0 0, 100% 55%, 6% 100%);
}
.speech-bubble span { display: block; }

@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(8px) scale(.86); }
  to   { opacity: 1; transform: none; }
}

.result-card .stamp {
  position: absolute;
  left: -4px;
  bottom: 6px;
  transform: rotate(-11deg);
  border: 3px double #d8531f;
  color: #ff9a6a;
  background: rgba(12, 22, 16, .55);
  padding: 6px 11px;
  border-radius: 8px;
  font-weight: 900;
  font-size: 14px;
  line-height: 1.15;
  opacity: .95;
  z-index: 2;
}
.result-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: min(280px, 100%);
  margin-top: 6px;
}
.result-grid > div {
  background: rgba(18, 33, 26, .72);
  border: 1px solid rgba(244, 239, 221, .14);
  border-radius: 14px;
  padding: 11px 8px;
}
.result-grid span { display: block; font-size: 10px; opacity: .6; letter-spacing: .6px; }
.result-grid strong { font-size: 27px; color: var(--gold-hi); font-variant-numeric: tabular-nums; }
#result-message { font-size: 13px; opacity: .8; margin: 4px 0 0; line-height: 1.5; max-width: 300px; }

/* ----------------------------------------------------------- lore modal --- */

.lore-modal {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(6, 12, 9, .88);
}
.lore-modal.visible { display: flex; }
.lore-content {
  position: relative;
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  padding: 22px 20px;
  border-radius: var(--radius);
  background: linear-gradient(170deg, #1a2b21, #101a14);
  border: 1px solid rgba(244, 239, 221, .16);
}
.lore-close {
  position: absolute; top: 10px; right: 12px;
  background: none; border: none; color: var(--cream);
  font-size: 26px; line-height: 1; cursor: pointer; opacity: .6;
}
.lore-content h3 { font-family: "Chonburi", serif; margin: 0 0 2px; font-size: 21px; }
.lore-subtitle { font-size: 10px; opacity: .5; margin: 0 0 14px; }
.lore-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.lore-grid img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 8px; }
.lore-grid figure { margin: 0; }
.lore-grid figcaption { font-size: 9.5px; opacity: .6; margin-top: 4px; line-height: 1.35; }
.lore-text { font-size: 12.5px; line-height: 1.75; opacity: .85; margin: 14px 0 0; }
.lore-actions { display: flex; justify-content: center; margin-top: 6px; }

/* --------------------------------------------------------------- shout --- */

.shout {
  position: absolute;
  left: 50%;
  top: 26%;
  transform: translate(-50%, -50%) scale(.6);
  font-family: "Chonburi", "Noto Sans Thai", serif;
  font-size: clamp(21px, 6.4vw, 29px);
  color: var(--gold-hi);
  text-shadow: 0 3px 0 #7a4a12, 0 6px 18px rgba(0, 0, 0, .55);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
  z-index: 6;
}
.shout.pop { animation: shoutPop 1.25s cubic-bezier(.2, .9, .25, 1) both; }

@keyframes shoutPop {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(.5) rotate(-5deg); }
  18%  { opacity: 1; transform: translate(-50%, -50%) scale(1.12) rotate(2deg); }
  30%  { transform: translate(-50%, -50%) scale(1) rotate(0deg); }
  78%  { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -62%) scale(.96); }
}

/* --------------------------------------------------------------- extra --- */

.scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  background:
    repeating-linear-gradient(to bottom,
      rgba(0, 0, 0, .055) 0 1px,
      rgba(255, 255, 255, 0) 1px 3px);
  mix-blend-mode: multiply;
  opacity: .8;
}
.scanlines::after {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(125% 95% at 50% 45%, transparent 52%, rgba(0, 0, 0, .42) 100%);
}
body.perf-lite .scanlines { display: none; }
body.perf-lite .power-meter,
body.perf-lite .score-wrap,
body.perf-lite .screen-card { backdrop-filter: none; -webkit-backdrop-filter: none; }

.fps-badge {
  position: absolute;
  left: 12px;
  top: calc(var(--safe-top) + 56px);
  z-index: 40;
  font: 700 10px/1.4 ui-monospace, monospace;
  background: rgba(0, 0, 0, .6);
  padding: 4px 8px;
  border-radius: 7px;
  color: #9fe6b0;
  white-space: pre;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  .power-button.ready { animation: none; }
  .shout.pop { animation-duration: .6s; }
}
