:root {
  color-scheme: dark;
  font-family: Arial, "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans CJK KR", sans-serif;
  background: #0d1421;
  color: #ecedf1;
  --gold-light: #f2e3bc;
  --gold-mid: #c3a678;
  --gold-shade: #7b6449;
  --player: #89deed;
  --cpu: #f2b777;
}
:root:lang(ko) {
  font-family: "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans CJK KR", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  --arena-art: url("./battle-fortress.webp");
  background:
    linear-gradient(#070a1426, #070a1400 30%, #070a1400 72%, #070a1460),
    var(--arena-art) center / cover no-repeat #0d1421;
}
button {
  border: 1px solid;
  border-color: var(--gold-light) var(--gold-mid) var(--gold-shade) var(--gold-mid);
  border-radius: 6px;
  color: inherit;
  background: linear-gradient(#30384c, #192234);
  box-shadow: inset 0 1px 0 #ffedc24d, inset 0 -1px 0 #0b0c16, 0 2px 3px #0006;
  font: inherit;
  font-weight: 700;
  text-shadow: 0 1px 2px #000c;
  min-height: 44px;
  padding: 0 .85em;
  cursor: pointer;
}
button:hover { border-color: #fff1cc #dbc199 #9e805c #dbc199; }
button:focus-visible { outline: 2px solid var(--player); outline-offset: 2px; }
.cabinet {
  width: min(100%, 980px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: auto;
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
}
.top { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #776342; padding: 0 2px 5px; }
.brand { font-size: 3.65rem; line-height: 52px; font-weight: 850; letter-spacing: -.05em; color: inherit; text-decoration: none; }
.brand span { color: var(--player); }
@media (max-width: 26rem) {
  .brand { font-size: 2.35rem; line-height: 1; }
}
.entry { display: grid; flex: 1; place-items: center; padding: 18px 0; text-align: center; }
.plaque {
  width: min(100%, 620px);
  padding: clamp(18px, 5vw, 42px);
}
.plaque, .dialog {
  border: 3px solid transparent;
  border-radius: 10px;
  background:
    linear-gradient(#323b4a, #121925 62%, #0d131e) padding-box,
    linear-gradient(145deg, #fff0ce, var(--gold-mid) 52%, var(--gold-shade) 85%, #dbc298) border-box;
  box-shadow: inset 0 0 0 1px #0a0e17, 0 0 0 1px #2a2018, 0 6px 18px #000a;
}
.eyebrow { color: var(--cpu); font-size: .76rem; letter-spacing: .23em; font-weight: 800; margin: 0 0 12px; }
h1 { margin: 0; line-height: 1.12; font-weight: 800; }
.entry h1 {
  padding-bottom: clamp(12px, 3vw, 20px);
  border-bottom: 1px solid #e9d7af2e;
  font-size: clamp(26px, 8vw, 65px);
  color: #fff7e6;
  text-shadow: 0 3px 4px #000a;
}
.intro { font-weight: 700; margin: 14px 0 8px; white-space: pre-line; }
.rules { color: #d0d8e4; font-size: .875rem; margin: 0 0 20px; line-height: 1.5; }
.rules span { display: block; }
.hint, .keyboard-hint { color: #bbc7d8; font-size: .81rem; margin: 12px 0 0; line-height: 1.4; }
.lobby-link {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  color: var(--gold-light);
  font-size: .85rem;
  text-underline-offset: 3px;
}
.menu-button {
  min-width: 116px;
  padding-inline: 1.6em;
  background: linear-gradient(#3b4556, #1a2230);
  color: #fff5df;
  text-shadow: 0 1px 2px #000c;
}
.menu-button:focus-visible { outline-color: #fff0cb; }
.difficulty-options {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  width: min(100%, 480px);
  gap: clamp(4px, 1.4vw, 8px);
  margin-inline: auto;
}
.difficulty-options button {
  min-width: 0;
  min-height: 52px;
  padding: 4px 2px;
  font-size: clamp(.72rem, 2.9vw, .94rem);
}
.difficulty-options .online-option {
  display: grid;
  place-content: center;
  gap: 2px;
  border-color: #fff3cb #e5cd8b #b8995d;
  background: linear-gradient(#fff2be, #f7e2a6);
  color: #382812;
  text-shadow: none;
  cursor: default;
}
.online-option small { font-size: .78em; }
.match {
  height: calc(100vh - 74px);
  height: calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)) - 58px);
  min-height: 298px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
}
.match-bar { display: flex; gap: 6px; align-items: center; justify-content: space-between; }
.match-bar h1 { font-size: clamp(14px, 4vw, 24px); color: #ffe0a5; text-shadow: 0 2px 3px #000b; }
.actions { display: flex; gap: 5px; }
.actions button { font-size: .8rem; }
#fullscreen { display: none; width: 44px; padding: 0; font-size: 24px; line-height: 1; }
#fullscreen[aria-pressed="true"] { color: var(--player); }
.arena { position: relative; flex: 1; min-height: 180px; container-type: size; }
.battle {
  --tower-width: 116px;
  --battle-gap: 10px;
  --board-width: min(310px, calc((100cqw - var(--tower-width) - 2 * var(--battle-gap) - 8px) / 2), calc((100cqh - 54px) / 2));
  display: grid;
  grid-template-columns: var(--board-width) var(--tower-width) var(--board-width);
  justify-content: center;
  align-items: center;
  gap: var(--battle-gap);
  height: 100%;
}
.station { min-width: 0; align-self: start; }
.station-head {
  height: 48px;
  padding: 0 2px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2px;
  font-size: clamp(10px, 2.6vw, 13px);
  color: #c7d0dc;
}
.station-head h2 { font-size: 1.22em; margin: 0; }
.station-head canvas { display: block; width: min(54px, 38%); aspect-ratio: 2; height: auto; }
.player h2, .player-color { color: var(--player); }
.cpu h2, .cpu-color { color: var(--cpu); }
.board-frame { position: relative; isolation: isolate; }
.board-frame::before {
  content: "";
  position: absolute;
  inset: -4px;
  z-index: -1;
  border: 1px solid;
  border-color: var(--gold-light) #b19369 var(--gold-shade) #d7bd92;
  border-radius: 3px;
  background: linear-gradient(145deg, #fff0ce, #d3b788 42%, #a98d65 72%, #dbc298);
  box-shadow: inset 0 1px 0 #fff6dfb3, inset 0 -1px 0 #5e4c36, 0 0 0 1px #2a2018, 0 4px 9px #0009;
  pointer-events: none;
}
.board-frame::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 1px 1px 0 #3c3024, inset -1px -1px 0 #d5b78970;
  pointer-events: none;
}
.board {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 2;
  background: #080b13;
}
.tower { align-self: center; position: relative; width: 100%; text-align: center; color: #e8cc9a; }
.castle { display: block; width: 100%; height: auto; filter: drop-shadow(0 5px 4px #0007); }
.tower-readout { position: absolute; inset: 75% 6% auto 0; transform: translateY(-50%); font-weight: 800; font-size: 12px; line-height: 1.25; white-space: nowrap; text-shadow: 1px 1px #000, 0 0 4px #000; }
.pending { display: flex; justify-content: center; gap: 3px; font-variant-numeric: tabular-nums; }
.pending strong { font-size: 1.16em; }
.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: #070c14b8; z-index: 1; }
.dialog {
  width: min(100% - 20px, 266px);
  padding: 0 14px 14px;
  text-align: center;
}
.dialog h2 {
  margin: 0 -14px 12px;
  padding: 14px 10px 12px;
  border-bottom: 1px solid #e9d7af2e;
  color: #fff7e6;
  font-size: clamp(20px, 5vw, 26px);
  font-weight: 800;
  text-shadow: 0 2px 3px #000b;
}
.desktop-controls {
  display: grid;
  grid-template-columns: repeat(4, minmax(36px, 1fr)) minmax(67px, 1.5fr);
  gap: 5px;
  width: min(100%, 410px);
  margin-inline: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.desktop-controls button { padding: 0 2px; white-space: nowrap; -webkit-tap-highlight-color: transparent; }
.desktop-controls button:active { background: #415375; }
.drop { color: #ffe0a5; }
.mobile-controls {
  display: none;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  width: min(100%, 560px);
  margin-inline: auto;
  padding: 8px 0 clamp(24px, 8dvh, 68px);
  flex: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.mobile-controls button { -webkit-tap-highlight-color: transparent; }
.joystick, .mobile-actions button {
  --metal-rim: linear-gradient(145deg, #fff2d3, #d3b788 39%, #8d7150 69%, #cdb28a);
  border: 3px solid transparent;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px #111723, 0 3px 8px #0008;
}
.joystick {
  --stick-size: clamp(96px, 29vw, 124px);
  --arrow-inset: 8px;
  position: relative;
  display: flex;
  width: var(--stick-size);
  height: var(--stick-size);
  background:
    radial-gradient(circle, #26334a 0 41%, #131b2b 43% 67%, #394359 71%, #131927 76%) padding-box,
    var(--metal-rim) border-box;
}
.joystick::before {
  content: "";
  position: absolute;
  inset: -9px;
}
.joystick button {
  position: relative;
  width: 50%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}
.joystick button span {
  position: absolute;
  width: 7px;
  height: 9px;
  background: var(--gold-light);
  clip-path: polygon(0 0, 100% 50%, 0 100%);
  pointer-events: none;
}
.joystick [data-stick-direction="left"] { border-radius: 100% 0 0 100% / 50% 0 0 50%; }
.joystick [data-stick-direction="right"] { border-radius: 0 100% 100% 0 / 0 50% 50% 0; }
.joystick [data-stick-direction="left"] span { inset: 50% auto auto var(--arrow-inset); transform: translate(-50%, -50%) rotate(180deg); }
.joystick [data-stick-direction="right"] span { inset: 50% var(--arrow-inset) auto auto; transform: translate(50%, -50%); }
.joystick [data-stick-direction="down"] {
  position: absolute;
  inset: auto auto 0 50%;
  width: 44px;
  height: 32%;
  min-height: 0;
  transform: translateX(-50%);
  border-radius: 0 0 50% 50%;
}
.joystick [data-stick-direction="down"] span { inset: auto auto var(--arrow-inset) 50%; transform: translate(-50%, 50%) rotate(90deg); }
.joystick-knob {
  position: absolute;
  inset: 50% auto auto 50%;
  width: 44%;
  aspect-ratio: 1;
  transform: translate(calc(-50% + var(--stick-x, 0px)), calc(-50% + var(--stick-y, 0px)));
  border: 1px solid var(--gold-light);
  border-radius: 50%;
  background: radial-gradient(circle at 34% 25%, #68788c, #37465b 46%, #1c2534 80%);
  box-shadow: 0 2px 4px #0009;
  pointer-events: none;
}
.mobile-actions { display: flex; align-items: end; gap: 10px; }
.mobile-actions button {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 2px;
  padding: 0;
  background:
    radial-gradient(circle at 35% 24%, #425168, #1b2334 67%) padding-box,
    var(--metal-rim) border-box;
  color: #fff0ce;
  font-size: 10px;
  line-height: 1.15;
  white-space: nowrap;
}
.mobile-actions button:hover { border-color: transparent; }
.mobile-actions button:active {
  background: linear-gradient(#4a5971, #2b364b) padding-box, var(--metal-rim) border-box;
}
.mobile-actions .mobile-rotate { width: clamp(64px, 18vw, 80px); height: auto; aspect-ratio: 1; }
.mobile-drop { width: clamp(78px, 22vw, 96px); height: auto; aspect-ratio: 1; }
.action-icon { font-size: 19px; line-height: 1; }
.mobile-drop .action-icon { font-size: 25px; }
.keyboard-hint { margin: 0; text-align: center; }
@media (orientation: portrait) and (max-width: 760px) {
  body { --arena-art: url("./battle-fortress-portrait.webp"); }
}
@media (max-width: 600px) {
  .battle {
    --tower-width: clamp(48px, 14cqw, 74px);
    --battle-gap: 6px;
    --board-width: min(260px, calc((100cqw - var(--tower-width) - 2 * var(--battle-gap) - 4px) / 1.58), calc((100cqh - 52px) / 2));
    grid-template-columns: var(--board-width) var(--tower-width) calc(var(--board-width) * .58);
    align-content: center;
  }
  .tower { align-self: start; margin-top: 62px; }
  .tower-readout { right: 8%; font-size: 9px; }
  :lang(en) .tower-readout > span { font-size: 7px; }
  .pending { gap: 1px; }
}
@media (pointer: coarse), (max-width: 600px) {
  .in-match .top, .match-bar h1 { display: none; }
  .in-match .cabinet { width: 100%; }
  .match {
    height: calc(100vh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)));
    height: calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)));
    padding-top: 0;
  }
  .match-bar { justify-content: end; }
  #fullscreen { display: block; }
  .desktop-controls { display: none; }
  .mobile-controls { display: flex; }
  .keyboard-hint { display: none; }
}
@media (orientation: portrait) and (min-width: 360px) and (min-height: 640px) {
  .mobile-controls { padding-bottom: calc(clamp(24px, 8dvh, 68px) + 25px); }
  .mobile-controls .joystick { margin-inline-start: 20px; }
}
@media (max-height: 420px) and (pointer: coarse), (max-height: 420px) and (max-width: 600px) {
  .match { min-height: 0; }
  .arena { min-height: 105px; }
  .battle { --board-width: min(130px, calc((100cqh - 38px) / 2)); }
  .station-head { height: 32px; }
  .tower { width: min(100%, calc((100cqh - 8px) * .4)); justify-self: center; align-self: center; margin-top: 0; }
  .joystick { --stick-size: 80px; }
  .mobile-drop { width: 66px; height: 66px; }
  .mobile-actions .mobile-rotate { width: 56px; height: 56px; }
}
@media (orientation: landscape) and (pointer: coarse),
       (orientation: landscape) and (max-width: 600px) {
  .match {
    --landscape-drop-size: clamp(64px, 11vw, 96px);
    --landscape-rotate-size: clamp(52px, 9vw, 76px);
    --landscape-board-head: 28px;
    --landscape-control-lift: clamp(16px, 9dvh, 42px);
    --landscape-stick-lift: 10px;
    --landscape-stick-inset: clamp(15px, calc((100vw - 420px) * .11), 46px);
    --landscape-stick-size: min(clamp(80px, 15vw, 126px),
      calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)) - var(--landscape-control-lift) - var(--landscape-stick-lift) - 150px));
    position: relative;
    min-height: 0;
  }
  .match-bar {
    position: absolute;
    inset: 0 auto auto 0;
    z-index: 2;
  }
  .match-bar .actions { flex-direction: column; width: 68px; }
  #fullscreen { width: 100%; }
  .match-bar .actions button { padding-inline: 4px; font-size: .75rem; }
  .match-bar .actions #fullscreen { font-size: 24px; }
  .arena {
    margin-inline: calc(var(--landscape-stick-size) + var(--landscape-stick-inset) + 12px)
      calc(var(--landscape-rotate-size) + var(--landscape-drop-size) + 16px);
  }
  .battle {
    --tower-width: min(clamp(30px, 15cqw, 90px), calc((100cqh - 8px) * .4));
    --battle-gap: 8px;
    --board-width: min(calc((100cqh - var(--landscape-board-head) - 8px) / 2), calc((100cqw - var(--tower-width) - 2 * var(--battle-gap)) / 1.74));
    grid-template-columns: var(--board-width) var(--tower-width) calc(var(--board-width) * .74);
    align-content: center;
  }
  .tower { width: 100%; margin-top: 0; align-self: center; }
  .station-head { height: var(--landscape-board-head); padding-bottom: 5px; font-size: 10px; white-space: nowrap; }
  .station-head h2 { line-height: 1.05; }
  .tower-readout { right: 8%; font-size: 8px; }
  .mobile-controls {
    position: absolute;
    inset: auto 0 var(--landscape-control-lift);
    width: 100%;
    padding: 0;
    z-index: 1;
    pointer-events: none;
  }
  .joystick, .mobile-actions button { pointer-events: auto; }
  .joystick {
    --stick-size: var(--landscape-stick-size);
    top: calc(-1 * var(--landscape-stick-lift));
    margin-inline-start: var(--landscape-stick-inset);
  }
  .mobile-actions { gap: 8px; }
  .mobile-actions button { gap: 1px; font-size: 9px; }
  .mobile-actions .mobile-rotate { width: var(--landscape-rotate-size); height: var(--landscape-rotate-size); font-size: 10px; }
  .mobile-drop { width: var(--landscape-drop-size); height: var(--landscape-drop-size); }
  .mobile-rotate .action-icon { font-size: 20px; }
  .mobile-drop .action-icon { font-size: 28px; }
}
@media (orientation: landscape) and (max-height: 285px) and (pointer: coarse),
       (orientation: landscape) and (max-height: 285px) and (max-width: 600px) {
  .match {
    --landscape-stick-size: clamp(64px, calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)) - var(--landscape-control-lift) - var(--landscape-stick-lift) - 144px), 80px);
    --landscape-control-lift: clamp(0px, calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)) - 224px), 20px);
  }
  .match-bar .actions { gap: 2px; }
}
