:root {
  color-scheme: light;
  --ink: #152532;
  --muted: #65758a;
  --line: #d7e0ea;
  --panel: #ffffff;
  --page: #f2f7fb;
  --accent: #157f8f;
  --accent-dark: #0c6170;
  --gold: #e0a21a;
  --rose: #e65757;
  --leaf: #2fb879;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 12%, rgba(255, 207, 93, 0.26), transparent 24rem),
    radial-gradient(circle at 86% 10%, rgba(21, 127, 143, 0.18), transparent 22rem),
    linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.2)),
    var(--page);
}

button,
select {
  font: inherit;
}

input {
  font: inherit;
}

.loginScreen {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 18px;
  background:
    linear-gradient(180deg, rgba(10, 30, 46, 0.58), rgba(10, 30, 46, 0.74)),
    var(--page);
}

.loginScreen.hidden {
  display: none;
}

.loginCard {
  width: min(420px, 100%);
  display: grid;
  gap: 12px;
  padding: 18px;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 24px 70px rgba(6, 24, 35, 0.28);
  text-align: center;
}

.loginCard img {
  width: 128px;
  height: 128px;
  object-fit: cover;
  border-radius: 26px;
  justify-self: center;
  box-shadow: 0 14px 28px rgba(20, 48, 72, 0.18);
}

.loginCard h1 {
  font-size: 30px;
}

.loginCard input {
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  text-align: center;
  font-size: 20px;
  font-weight: 950;
  letter-spacing: 0;
  text-transform: uppercase;
}

.loginCard button,
.homeBtn,
.secondaryBtn {
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 950;
  cursor: pointer;
}

.secondaryBtn,
.homeBtn {
  background: #e8f1f6;
  color: #153246;
}

.loginError {
  min-height: 20px;
  color: #b8334b;
  font-weight: 900;
}

.app {
  width: min(100vw - 18px, 540px);
  margin: 0 auto;
  padding: 6px 0 8px;
}

.topbar,
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar {
  margin-bottom: 4px;
}

.topActions {
  display: grid;
  justify-items: end;
  gap: 4px;
  flex: 0 0 auto;
}

.studentPill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 3px 8px;
  background: #ffffff;
  color: #2b5068;
  border: 1px solid var(--line);
  font-size: 11px;
  font-weight: 950;
}

.homeBtn {
  padding: 6px 10px;
  font-size: 12px;
}

h1 {
  margin: 0 0 2px;
  font-size: clamp(21px, 6vw, 28px);
  line-height: 1;
  color: #102c3a;
  letter-spacing: 0;
}

p {
  margin: 0;
  color: #54677c;
  font-size: clamp(12px, 3.45vw, 15px);
  font-weight: 850;
}

.controls {
  display: none;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.selectLabel {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

select,
button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  padding: 0 14px;
  font-weight: 800;
}

button {
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

#startBtn {
  background: linear-gradient(180deg, #1d9aad, var(--accent));
  border-color: var(--accent);
  color: white;
  box-shadow: 0 8px 18px rgba(21, 127, 143, 0.22);
}

#startBtn:hover {
  background: var(--accent-dark);
}

.srOnlyControl {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin-bottom: 5px;
}

.hud > div {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  padding: 4px 6px;
  display: grid;
  align-content: center;
  gap: 3px;
  box-shadow: 0 8px 22px rgba(43, 63, 86, 0.06);
}

.hud span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
}

.hud strong {
  font-size: 13px;
}

.gameShell {
  position: relative;
  width: min(100%, calc((100vh - 98px) * 9 / 16));
  margin: 0 auto;
  border: 1px solid #b9cad8;
  border-radius: 8px;
  overflow: hidden;
  background: #102b36;
  box-shadow: 0 22px 52px rgba(26, 57, 76, 0.22);
}

.pauseBtn {
  position: absolute;
  right: 10px;
  top: 10px;
  z-index: 8;
  min-height: 34px;
  border: 2px solid rgba(255,255,255,.62);
  border-radius: 999px;
  padding: 0 12px;
  background: rgba(9, 34, 48, .58);
  color: #ffffff;
  font-size: 13px;
  font-weight: 950;
  box-shadow: 0 8px 16px rgba(0,0,0,.18);
}

.pauseBtn[hidden] {
  display: none;
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 9 / 16;
  touch-action: none;
}

.mobilePad {
  display: none;
  justify-items: center;
  gap: 8px;
  margin-top: 14px;
}

.mobilePad div {
  display: flex;
  gap: 8px;
}

.padBtn {
  width: 58px;
  height: 48px;
  padding: 0;
  background: #ffffff;
}

.touchJoystick {
  position: absolute;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 5;
  display: none;
  width: 132px;
  height: 132px;
  place-items: center;
  touch-action: none;
  user-select: none;
}

.stickBase {
  position: relative;
  width: 118px;
  height: 118px;
  border-radius: 50%;
  opacity: 0.54;
  border: 2px solid rgba(255, 255, 255, 0.44);
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.18), rgba(21, 127, 143, 0.14)),
    rgba(12, 35, 48, 0.38);
  box-shadow: inset 0 0 24px rgba(255, 255, 255, 0.16), 0 12px 28px rgba(0, 0, 0, 0.22);
}

.stickBase::before,
.stickBase::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.22);
  border-radius: 999px;
}

.stickBase::before {
  left: 22px;
  right: 22px;
  top: 57px;
  height: 4px;
}

.stickBase::after {
  top: 22px;
  bottom: 22px;
  left: 57px;
  width: 4px;
}

.stickKnob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.82;
  border: 2px solid rgba(255, 255, 255, 0.72);
  background: linear-gradient(180deg, rgba(49, 199, 160, 0.9), rgba(21, 127, 143, 0.9));
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

.petPanel {
  text-align: center;
}

.petModalImg {
  display: block;
  width: min(180px, 48vw);
  height: min(180px, 48vw);
  object-fit: contain;
  margin: 0 auto 10px;
  filter: drop-shadow(0 13px 18px rgba(21, 47, 66, 0.18));
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(10, 25, 36, 0.58);
}

.modal[hidden] {
  display: none;
}

.modalPanel {
  width: min(560px, 100%);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.78);
  background: #ffffff;
  padding: 22px;
  box-shadow: 0 24px 70px rgba(6, 24, 35, 0.28);
}

.modalPanel h2 {
  margin: 0 0 14px;
  color: #102c3a;
  font-size: 26px;
}

.modalPanel p {
  margin: 14px 0 18px;
  font-size: 16px;
  line-height: 1.55;
  color: #46596c;
}

.guideSteps {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.guideStep {
  min-width: 0;
  border: 2px solid #d9e7f0;
  border-radius: 12px;
  padding: 10px 8px;
  display: grid;
  gap: 8px;
  justify-items: center;
  background: linear-gradient(180deg, #f8fcff, #eef7fb);
  color: #102c3a;
  text-align: center;
}

.guideStep strong {
  font-size: 13px;
  line-height: 1.2;
}

.guideIcon {
  width: 70px;
  height: 70px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  color: #ffffff;
  font-size: 18px;
  font-weight: 950;
  box-shadow: inset 0 2px 0 rgba(255,255,255,.28), 0 10px 18px rgba(31,64,88,.18);
}

.guideCalc { background: linear-gradient(135deg, #2fb879, #157f8f); }
.guideDoor {
  border-radius: 50%;
  border: 5px solid #baf3ff;
  background: radial-gradient(circle, #2ba9c9, #105a72);
}
.guideBonus {
  transform: rotate(45deg);
  background: linear-gradient(135deg, #ffd166, #e0a21a);
}
.guideBonus::first-letter {
  transform: rotate(-45deg);
}

.guideWarn { background: linear-gradient(135deg, #ef6f5e, #b8334b); }
.guideBlock { background: linear-gradient(135deg, #8a6b43, #d7b16a); }
.guideShadow { background: linear-gradient(135deg, #467eaa, #7de0ff); font-size: 16px; }

.smallPanel {
  width: min(420px, 100%);
  text-align: center;
}

.modalActions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.modalActions button {
  flex: 1;
}

.resultPanel {
  width: min(520px, 100%);
}

.resultStats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin: 12px 0;
}

.resultStats div {
  border: 1px solid #d7e0ea;
  border-radius: 10px;
  background: #f8fcff;
  padding: 10px;
  display: grid;
  gap: 3px;
}

.resultStats span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
}

.resultStats strong {
  color: #102c3a;
  font-size: 20px;
  font-weight: 950;
}

.bossDiagram {
  position: relative;
  height: 230px;
  overflow: hidden;
  border-radius: 8px;
  background: #102b38;
  color: #ffffff;
}

.diagramMonster,
.diagramBlock,
.diagramPlayer,
.diagramShadow {
  position: absolute;
  display: grid;
  place-items: center;
  font-weight: 900;
}

.diagramMonster {
  left: 34px;
  top: 82px;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: #e65757;
}

.diagramMonster span {
  font-size: 24px;
}

.diagramWave {
  position: absolute;
  left: 70px;
  top: 115px;
  width: 360px;
  height: 360px;
  border: 9px solid rgba(255, 91, 91, 0.66);
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

.diagramBlock {
  left: 230px;
  top: 82px;
  width: 74px;
  height: 74px;
  border-radius: 8px;
  background: #8a6b43;
  border: 3px solid #d7b16a;
}

.diagramShadow {
  left: 282px;
  top: 50px;
  width: 190px;
  height: 134px;
  clip-path: polygon(0 25%, 100% 0, 100% 100%, 0 75%);
  background: rgba(70, 126, 170, 0.55);
}

.diagramPlayer {
  right: 46px;
  top: 92px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: #2fb879;
}

@media (max-width: 760px) {
  .app {
    width: min(100vw - 12px, 540px);
    padding-top: 5px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .controls {
    justify-content: stretch;
  }

  .controls > * {
    flex: 1 1 120px;
  }

  .hud {
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
  }

  .hud > div {
    padding: 4px 5px;
  }

  .hud strong {
    font-size: 14px;
  }

  .mobilePad {
    display: grid;
  }
}

body.touchControls .touchJoystick {
  display: grid;
}

@media (max-width: 900px), (pointer: coarse) {
  body {
    overscroll-behavior: none;
  }

  .app {
    width: min(100vw - 12px, 540px);
    padding-top: max(5px, env(safe-area-inset-top));
    padding-bottom: max(5px, env(safe-area-inset-bottom));
  }

  .topbar {
    align-items: end;
    gap: 4px;
    margin-bottom: 4px;
  }

  h1 {
    font-size: 23px;
  }

  p {
    font-size: 13px;
  }

  .controls {
    gap: 6px;
  }

  select,
  button {
    min-height: 38px;
    padding: 0 10px;
  }

  .hud {
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
    margin-bottom: 5px;
  }

  .hud > div {
    min-height: 38px;
    padding: 4px 5px;
  }

  .hud strong {
    font-size: 14px;
  }

  .gameShell {
    box-shadow: 0 12px 28px rgba(26, 57, 76, 0.18);
  }

  .mobilePad {
    display: none;
  }
}

body.touchControls {
  overscroll-behavior: none;
}

body.touchControls .app {
  width: min(100vw - 12px, 540px);
}

body.mobilePreview .app {
  width: min(430px, 100vw - 12px);
}

body.touchControls .topbar {
  align-items: end;
  gap: 4px;
  margin-bottom: 4px;
}

body.touchControls h1 {
  font-size: 23px;
}

body.touchControls p {
  font-size: 13px;
}

body.touchControls .controls {
  gap: 6px;
}

body.touchControls select,
body.touchControls button {
  min-height: 38px;
  padding: 0 10px;
}

body.touchControls .hud {
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin-bottom: 5px;
}

body.touchControls .hud > div {
  min-height: 38px;
  padding: 4px 5px;
}

body.touchControls .hud strong {
  font-size: 14px;
}

body.touchControls .gameShell {
  box-shadow: 0 12px 28px rgba(26, 57, 76, 0.18);
}

@media (max-width: 520px) {
  .topbar {
    display: grid;
    grid-template-columns: 1fr;
  }

  .controls {
    justify-content: stretch;
  }

  .controls > * {
    flex: 1 1 92px;
  }

  .hud {
    grid-template-columns: repeat(4, 1fr);
  }

  .gameShell {
    width: min(100%, calc((100vh - 124px) * 9 / 16));
  }
}

@media (max-width: 900px) and (orientation: landscape) {
  .app {
    width: min(420px, calc(100vw - max(12px, env(safe-area-inset-left)) - max(12px, env(safe-area-inset-right))));
  }

  .topbar {
    display: none;
  }

  canvas {
    max-height: calc(100vh - 66px);
    aspect-ratio: 9 / 16;
  }
}
