:root {
  --night: #110f25;
  --deep-night: #090817;
  --room: #252346;
  --room-light: #302d57;
  --paper: #f5edf0;
  --muted: rgba(245, 237, 240, 0.58);
  --ward: #79ebdd;
  --ward-deep: #35aeb6;
  --ghost: #e5c8ff;
  --danger: #ff6f8f;
  --key: #f4c966;
  font-family: "Yu Gothic UI", "Hiragino Sans", Meiryo, system-ui, sans-serif;
  color: var(--paper);
  background: var(--deep-night);
}

* { box-sizing: border-box; }
html { min-width: 320px; min-height: 100%; background: var(--deep-night); }
body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 78% 2%, rgba(121, 235, 221, 0.08), transparent 26rem),
    radial-gradient(circle at 8% 26%, rgba(162, 109, 205, 0.13), transparent 29rem),
    linear-gradient(180deg, #15122d 0%, #090817 78%);
}

button, a, canvas { -webkit-tap-highlight-color: transparent; }
button { font: inherit; }
[hidden] { display: none !important; }

.page-shell {
  width: min(980px, calc(100% - 40px));
  margin: 0 auto;
  padding-bottom: 34px;
}

.topbar {
  min-height: 64px;
  border-bottom: 1px solid rgba(245, 237, 240, 0.12);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  color: rgba(245, 237, 240, 0.5);
  font: 750 0.62rem/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.14em;
}

.topbar p { margin: 0; }
.back-link {
  min-height: 44px;
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.back-link:hover, .back-link:focus-visible { color: var(--ward); }

.game-shell { padding-top: clamp(25px, 4vw, 44px); }
.game-heading {
  margin-bottom: 18px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 26px;
}

.eyebrow {
  margin: 0 0 8px;
  color: rgba(121, 235, 221, 0.72);
  font: 750 0.57rem/1.4 ui-monospace, Consolas, monospace;
  letter-spacing: 0.16em;
}

h1 {
  margin: 0;
  font-size: clamp(3.4rem, 7.5vw, 6.3rem);
  line-height: 0.9;
  letter-spacing: -0.085em;
  font-weight: 880;
}

.game-subtitle {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.76rem;
  letter-spacing: 0.1em;
}

.meters {
  width: min(420px, 46vw);
  display: grid;
  grid-template-columns: 1.08fr 1.08fr 0.82fr 0.82fr;
  gap: 7px;
}

.stat-card {
  min-width: 0;
  min-height: 70px;
  padding: 12px 13px;
  border: 1px solid rgba(245, 237, 240, 0.13);
  background: rgba(9, 8, 23, 0.62);
  display: grid;
  align-content: space-between;
  gap: 8px;
}

.stat-card span {
  color: rgba(245, 237, 240, 0.42);
  font: 700 0.54rem/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.13em;
}
.stat-card strong {
  white-space: nowrap;
  font: 820 1rem/1 ui-monospace, Consolas, monospace;
}
.time-card strong { color: var(--ward); }
.best-card strong { color: var(--key); }
.repel-card strong { color: var(--ghost); }
.ward-card strong { color: var(--ward); }

.game-board {
  position: relative;
  width: min(760px, 100%);
  margin-inline: auto;
  overflow: hidden;
  border: 1px solid rgba(245, 237, 240, 0.16);
  background: var(--deep-night);
  box-shadow: 0 28px 88px rgba(0, 0, 0, 0.42);
  isolation: isolate;
}
.game-board::after {
  content: "";
  position: absolute;
  inset: 11px;
  z-index: 2;
  border: 1px solid rgba(121, 235, 221, 0.1);
  pointer-events: none;
}

#game-canvas {
  width: 100%;
  aspect-ratio: 1;
  display: block;
  touch-action: none;
  cursor: crosshair;
  outline: none;
}
#game-canvas.is-aiming { cursor: grabbing; }
#game-canvas:focus-visible { box-shadow: inset 0 0 0 3px rgba(244, 201, 102, 0.82); }

.ward-button {
  grid-column: 2;
  grid-row: 1 / span 2;
  width: 88px;
  height: 88px;
  padding: 8px;
  border: 1px solid rgba(244, 201, 102, 0.5);
  border-radius: 50%;
  background: rgba(15, 13, 32, 0.91);
  color: var(--key);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.32);
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.12s ease, background 0.12s ease, transform 0.12s ease;
}
.ward-button strong { font-size: 0.78rem; line-height: 1; }
.ward-button small {
  color: rgba(245, 237, 240, 0.46);
  font: 700 0.44rem/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.09em;
}
.ward-button:disabled { opacity: 0.28; cursor: default; }
.ward-button:not(:disabled):hover, .ward-button:not(:disabled):focus-visible {
  border-color: var(--key);
  outline: none;
}
.ward-button[data-ready="true"] {
  border-color: var(--ward);
  background: rgba(34, 69, 74, 0.92);
  color: var(--ward);
  box-shadow: 0 0 0 4px rgba(121, 235, 221, 0.08), 0 12px 30px rgba(0, 0, 0, 0.32);
}
.ward-button[data-full="true"] {
  border-color: rgba(244, 201, 102, 0.42);
  background: rgba(60, 48, 34, 0.86);
  color: rgba(244, 201, 102, 0.74);
}
.ward-button.is-denied { animation: key-denied 0.18s linear 2; }
.ward-button.is-cast { transform: scale(0.9); }

.key-mark {
  position: relative;
  width: 22px;
  height: 22px;
  display: block;
}
.key-mark::before {
  content: "";
  position: absolute;
  left: 1px;
  top: 2px;
  width: 8px;
  height: 8px;
  border: 3px solid currentColor;
  border-radius: 50%;
}
.key-mark::after {
  content: "";
  position: absolute;
  left: 10px;
  top: 10px;
  width: 13px;
  height: 4px;
  border-right: 3px solid currentColor;
  border-bottom: 3px solid currentColor;
  transform: rotate(-42deg);
  transform-origin: left center;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 6;
  padding: 20px;
  background: rgba(8, 7, 20, 0.65);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
}
.result-overlay { cursor: pointer; }
.overlay-card {
  width: min(500px, 100%);
  padding: clamp(24px, 5vw, 42px);
  border: 1px solid rgba(245, 237, 240, 0.2);
  background:
    linear-gradient(135deg, rgba(121, 235, 221, 0.07), transparent 45%),
    rgba(18, 16, 39, 0.96);
  box-shadow: 0 24px 72px rgba(0, 0, 0, 0.46);
  text-align: center;
}
.overlay-kicker {
  margin: 0 0 11px;
  color: var(--ward);
  font: 750 0.56rem/1.4 ui-monospace, Consolas, monospace;
  letter-spacing: 0.18em;
}
.overlay-card h2 {
  margin: 0;
  font-size: clamp(1.9rem, 5vw, 3rem);
  line-height: 1.14;
  letter-spacing: -0.052em;
}
.overlay-card > p:not(.overlay-kicker):not(.result-time) {
  margin: 15px 0 0;
  color: rgba(245, 237, 240, 0.66);
  font-size: 0.81rem;
  line-height: 1.72;
}

.how-to-steps {
  margin: 17px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
  list-style: none;
  text-align: left;
}
.how-to-steps li {
  min-height: 43px;
  padding: 7px 10px;
  border: 1px solid rgba(245, 237, 240, 0.1);
  background: rgba(245, 237, 240, 0.03);
  display: grid;
  grid-template-columns: 27px 1fr;
  align-items: center;
  gap: 9px;
  color: rgba(245, 237, 240, 0.54);
  font-size: 0.64rem;
  line-height: 1.35;
}
.how-to-steps b {
  width: 26px;
  height: 26px;
  border: 1px solid rgba(121, 235, 221, 0.46);
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--ward);
  font: 800 0.67rem/1 ui-monospace, Consolas, monospace;
}
.how-to-steps span, .how-to-steps strong { display: block; }
.how-to-steps strong {
  margin-bottom: 1px;
  color: rgba(245, 237, 240, 0.92);
  font-size: 0.76rem;
}

.primary-button {
  min-width: 205px;
  min-height: 49px;
  margin-top: 22px;
  padding: 0 23px;
  border: 0;
  background: var(--ward);
  color: #101126;
  font-weight: 850;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.14s ease, box-shadow 0.14s ease;
}
.primary-button:hover, .primary-button:focus-visible {
  transform: translate(-3px, -3px);
  box-shadow: 7px 7px 0 rgba(244, 201, 102, 0.7);
  outline: none;
}
.overlay-card > small {
  display: block;
  margin-top: 12px;
  color: rgba(245, 237, 240, 0.4);
  font: 650 0.54rem/1.4 ui-monospace, Consolas, monospace;
  letter-spacing: 0.07em;
}

.result-card { border-color: rgba(255, 111, 143, 0.34); }
.result-card .overlay-kicker { color: var(--danger); }
.result-time {
  margin: 0 0 8px;
  color: var(--key);
  font: 850 clamp(2.8rem, 8vw, 5.2rem)/1 ui-monospace, Consolas, monospace;
  letter-spacing: -0.08em;
}
.result-stats {
  width: min(280px, 100%);
  margin: 18px auto 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}
.result-stats span {
  min-height: 55px;
  padding: 9px;
  border: 1px solid rgba(245, 237, 240, 0.1);
  display: grid;
  align-content: space-between;
  gap: 5px;
}
.result-stats small {
  color: rgba(245, 237, 240, 0.42);
  font: 700 0.48rem/1 ui-monospace, Consolas, monospace;
  letter-spacing: 0.12em;
}
.result-stats strong { color: var(--ward); font: 800 0.92rem/1 ui-monospace, Consolas, monospace; }

.game-footer {
  width: min(760px, 100%);
  min-height: 102px;
  margin-inline: auto;
  padding: 15px 4px 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 22px;
}
.game-status, .control-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.65;
}
.game-status {
  grid-column: 1;
  grid-row: 1;
  max-width: 500px;
  color: rgba(245, 237, 240, 0.8);
  font-weight: 650;
}
.control-hint { grid-column: 1; grid-row: 2; text-align: left; font-family: ui-monospace, Consolas, monospace; }
.control-hint span { color: var(--key); }

@keyframes key-denied {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(-4px); }
}

@media (max-width: 800px) {
  .game-heading { align-items: stretch; flex-direction: column; gap: 18px; }
  .meters { width: 100%; }
}

@media (min-width: 801px) and (max-height: 850px) {
  .game-board { width: min(660px, calc(100vh - 190px)); }
  .game-footer { width: min(660px, calc(100vh - 190px)); }
}

@media (max-width: 620px) {
  .page-shell { width: min(100% - 24px, 980px); padding-bottom: 22px; }
  .topbar { min-height: 54px; }
  .game-shell { padding-top: 21px; }
  h1 { font-size: clamp(3.45rem, 17vw, 5.2rem); }
  .game-heading { margin-bottom: 14px; gap: 14px; }
  .game-subtitle { margin-top: 8px; }
  .stat-card { min-height: 56px; padding: 8px 10px; }
  .stat-card strong { font-size: 0.82rem; }
  .game-board::after { inset: 6px; }
  .ward-button { width: 72px; height: 72px; }
  .key-mark { transform: scale(0.82); }
  .overlay { padding: 7px; }
  .overlay-card { padding: 15px 14px; }
  .game-footer { min-height: 86px; gap: 6px 10px; }
}

@media (max-width: 430px) {
  .topbar p { font-size: 0.51rem; }
  .eyebrow { margin-bottom: 4px; font-size: 0.48rem; }
  .game-subtitle { font-size: 0.64rem; }
  .meters { gap: 4px; }
  .stat-card { min-height: 49px; padding: 7px 8px; gap: 5px; }
  .stat-card span { font-size: 0.45rem; }
  .stat-card strong { font-size: 0.72rem; }
  .overlay { padding: 4px; }
  .overlay-card { padding: 8px 9px; }
  .overlay-kicker { margin-bottom: 3px; font-size: 0.4rem; }
  .overlay-card h2 { font-size: 1.25rem; line-height: 1.08; }
  .overlay-card > p:not(.overlay-kicker):not(.result-time) { margin-top: 5px; font-size: 0.58rem; line-height: 1.32; }
  .how-to-steps { margin-top: 6px; gap: 2px; }
  .how-to-steps li {
    min-height: 28px;
    padding: 3px 5px;
    grid-template-columns: 19px 1fr;
    gap: 5px;
    font-size: 0.47rem;
    line-height: 1.12;
  }
  .how-to-steps b { width: 18px; height: 18px; font-size: 0.5rem; }
  .how-to-steps strong { font-size: 0.55rem; }
  .primary-button { min-width: 178px; min-height: 42px; margin-top: 7px; }
  .overlay-card > small { margin-top: 4px; font-size: 0.4rem; }
  .result-time { margin-bottom: 3px; font-size: 2.6rem; }
  .result-stats { margin-top: 7px; gap: 4px; }
  .result-stats span { min-height: 42px; padding: 6px; }
  .result-stats strong { font-size: 0.72rem; }
  .ward-button { width: 64px; height: 64px; }
  .ward-button strong { font-size: 0.66rem; }
  .ward-button small { font-size: 0.38rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
