html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #fff7ec;
  color: #8b6550;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
#gameCanvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100svh;
  display: block;
  background: #fff8ed;
}
.panel {
  position: fixed;
  background: rgba(255, 249, 239, 0.96);
  border: 2px solid rgba(226, 173, 156, 0.85);
  box-shadow: 0 12px 45px rgba(118, 76, 59, 0.18);
  border-radius: 18px;
  box-sizing: border-box;
  color: #7a5645;
}
.loading {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(88vw, 480px);
  padding: 26px;
  text-align: center;
}
.loading .title {
  font-size: 24px;
  font-weight: 800;
  margin-bottom: 14px;
}
.bar {
  height: 12px;
  border: 1px solid #e7b9aa;
  border-radius: 999px;
  margin: 18px 0 8px;
  overflow: hidden;
  background: #fff;
}
.bar span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #f3a7b8, #e7c45c);
  border-radius: inherit;
}
.small { font-size: 12px; opacity: .76; }
.top-buttons {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  z-index: 12;
}
.top-buttons button,
.touch-controls button,
.close {
  border: 1px solid rgba(184, 123, 104, 0.65);
  background: rgba(255, 248, 236, 0.82);
  color: #7b5a49;
  border-radius: 12px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(78, 40, 25, .12);
  -webkit-tap-highlight-color: transparent;
}
.top-buttons button {
  padding: 9px 12px;
}
.touch-controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  padding: 0 16px;
  box-sizing: border-box;
  z-index: 10;
}
.dpad, .action-buttons { pointer-events: auto; }
.dpad {
  width: 148px;
  height: 148px;
  display: grid;
  grid-template-areas:
    ". up ."
    "left . right"
    ". down .";
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 6px;
  opacity: .92;
}
.dpad button {
  font-size: 22px;
  min-width: 46px;
  min-height: 46px;
  background: rgba(255, 245, 231, .76);
}
.dpad .up { grid-area: up; }
.dpad .left { grid-area: left; }
.dpad .right { grid-area: right; }
.dpad .down { grid-area: down; }
.action-buttons {
  display: grid;
  grid-template-columns: repeat(4, minmax(56px, 1fr));
  gap: 8px;
  width: min(52vw, 360px);
}
.action-buttons button {
  min-height: 48px;
  padding: 7px 6px;
  font-size: 13px;
  background: rgba(255, 244, 232, .82);
}
.touch-controls button:active,
.touch-controls button.active {
  transform: translateY(1px) scale(.98);
  background: rgba(247, 205, 210, .92);
}
.help {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(92vw, 780px);
  max-height: min(86svh, 680px);
  overflow: auto;
  padding: 24px 28px;
  z-index: 20;
}
.help h1 { margin: 0 0 14px; font-size: 28px; }
.help h2 { margin: 18px 0 8px; font-size: 20px; }
.help p { line-height: 1.65; margin: 8px 0; }
.close {
  position: absolute;
  right: 12px;
  top: 10px;
  width: 36px;
  height: 36px;
  font-size: 22px;
}
.hidden { display: none; }
@media (pointer: fine) and (min-width: 900px) {
  .touch-controls { opacity: .42; }
  .touch-controls:hover { opacity: 1; }
}
@media (max-width: 640px) {
  .dpad { width: 132px; height: 132px; }
  .dpad button { min-height: 42px; min-width: 42px; font-size: 20px; }
  .action-buttons {
    grid-template-columns: repeat(3, minmax(52px, 1fr));
    width: 50vw;
    gap: 6px;
  }
  .action-buttons button { min-height: 43px; font-size: 12px; }
  .help { padding: 20px 18px; }
  .help h1 { font-size: 23px; }
}


/* mobile layout fixes */
@media (pointer: coarse), (max-width: 820px) {
  .top-buttons {
    top: calc(env(safe-area-inset-top) + 154px);
    right: max(10px, env(safe-area-inset-right));
    gap: 6px;
  }
  .top-buttons button {
    padding: 8px 11px;
    font-size: 14px;
    background: rgba(255, 248, 236, 0.92);
  }
  .touch-controls {
    align-items: flex-end;
    padding: 0 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
  }
  .dpad {
    width: 128px;
    height: 128px;
    gap: 5px;
  }
  .action-buttons {
    width: min(54vw, 330px);
    grid-template-columns: repeat(3, minmax(54px, 1fr));
    gap: 6px;
  }
  .action-buttons button {
    min-height: 46px;
    font-size: 12px;
  }
}

@media (max-width: 430px) {
  .top-buttons {
    top: calc(env(safe-area-inset-top) + 145px);
  }
  .touch-controls {
    padding: 0 8px;
  }
  .dpad {
    width: 120px;
    height: 120px;
  }
  .action-buttons {
    width: min(52vw, 300px);
  }
}


/* clear screen / ranking */
.clear-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  box-sizing: border-box;
  background: rgba(72, 46, 34, .34);
  backdrop-filter: blur(1px);
}
.clear-overlay.hidden { display: none; }
.clear-card {
  width: min(94vw, 920px);
  max-height: min(94svh, 850px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: rgba(255, 249, 239, .98);
  border: 2px solid rgba(226, 173, 156, .95);
  border-radius: 22px;
  box-shadow: 0 20px 60px rgba(72, 46, 34, .26);
  padding: 14px;
  color: #7a5645;
  box-sizing: border-box;
}
.clear-image {
  display: block;
  width: 100%;
  max-height: min(58svh, 560px);
  object-fit: contain;
  border-radius: 14px;
  background: #fff8ed;
}
.clear-info-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid rgba(226, 173, 156, .62);
  border-radius: 16px;
  background: rgba(255, 246, 235, .78);
}
.clear-medal {
  width: 58px;
  height: 50px;
  object-fit: contain;
  flex: 0 0 auto;
}
.clear-medal-text {
  font-size: 20px;
  font-weight: 800;
}
.clear-time-text {
  margin-top: 2px;
  font-size: 16px;
  font-weight: 700;
  color: #936d59;
}
.score-form {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid rgba(226, 173, 156, .62);
  border-radius: 16px;
  background: rgba(255, 251, 244, .86);
}
.score-form label {
  display: block;
  font-weight: 800;
  margin-bottom: 8px;
}
.score-form-row {
  display: flex;
  gap: 8px;
}
.score-form input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid rgba(184, 123, 104, .62);
  background: rgba(255, 255, 255, .92);
  color: #7b5a49;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 16px;
  font-weight: 700;
  box-sizing: border-box;
}
.score-form button {
  flex: 0 0 auto;
  border: 1px solid rgba(184, 123, 104, .68);
  background: rgba(255, 238, 224, .96);
  color: #7b5a49;
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 800;
}
.score-form button:disabled {
  opacity: .55;
}
.score-saved {
  margin-top: 8px;
  font-weight: 800;
  color: #4f9c65;
}
.ranking-box {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255, 245, 235, .72);
  border: 1px solid rgba(226, 173, 156, .52);
}
.ranking-box h2 {
  margin: 0 0 8px;
  font-size: 18px;
}
.ranking-box ol {
  margin: 0;
  padding-left: 26px;
}
.ranking-box li {
  margin: 4px 0;
  font-weight: 700;
}
.ranking-note {
  margin: 8px 0 0;
  font-size: 12px;
  opacity: .72;
}

@media (pointer: coarse), (max-width: 820px) {
  .clear-overlay {
    align-items: flex-start;
    padding-top: max(8px, env(safe-area-inset-top));
  }
  .clear-card {
    width: calc(100vw - 12px);
    max-height: calc(100svh - 16px);
    padding: 10px;
    border-radius: 18px;
  }
  .clear-image {
    max-height: 45svh;
  }
  .clear-info-row {
    gap: 8px;
    margin-top: 8px;
    padding: 8px;
  }
  .clear-medal {
    width: 48px;
    height: 42px;
  }
  .clear-medal-text {
    font-size: 16px;
  }
  .clear-time-text {
    font-size: 14px;
  }
  .score-form,
  .ranking-box {
    margin-top: 8px;
    padding: 8px;
  }
  .score-form label {
    font-size: 13px;
    margin-bottom: 6px;
  }
  .score-form input {
    font-size: 16px;
    padding: 9px 10px;
  }
  .score-form button {
    padding: 9px 12px;
    font-size: 14px;
  }
  .ranking-box h2 {
    font-size: 16px;
  }
  .ranking-box li {
    font-size: 13px;
    margin: 3px 0;
  }
}

@media (max-width: 430px) {
  .clear-image {
    max-height: 42svh;
  }
  .ranking-box ol {
    padding-left: 22px;
  }
}


/* opening menu */
.opening-menu,
.ranking-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(14px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  box-sizing: border-box;
  background: radial-gradient(circle at 50% 20%, rgba(255, 244, 232, .72), rgba(102, 66, 49, .42));
  backdrop-filter: blur(2px);
}
.opening-menu.hidden,
.ranking-overlay.hidden { display: none; }
.opening-card {
  position: relative;
  width: min(94vw, 680px);
  max-height: min(94svh, 860px);
  aspect-ratio: 1122 / 1402;
  overflow: hidden;
  border-radius: 26px;
  border: 3px solid rgba(244, 191, 166, .95);
  box-shadow: 0 24px 72px rgba(77, 47, 34, .28);
  background: #fff8ed;
}
.opening-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.opening-vignette {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255,255,255,.04), rgba(255,246,235,.18) 62%, rgba(255,242,230,.90) 100%);
  pointer-events: none;
}
.opening-actions {
  position: absolute;
  left: 7%;
  right: 7%;
  bottom: 5.6%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.opening-actions button,
.clear-actions button {
  border: 2px solid rgba(184, 123, 104, .72);
  border-radius: 999px;
  padding: 14px 12px;
  color: #7b5a49;
  background: rgba(255, 248, 236, .94);
  font-size: 20px;
  font-weight: 900;
  box-shadow: 0 8px 20px rgba(78, 40, 25, .16);
  -webkit-tap-highlight-color: transparent;
}
.opening-main-button,
.clear-action-primary {
  background: linear-gradient(180deg, rgba(255, 237, 221, .98), rgba(255, 215, 207, .96)) !important;
}
.opening-actions button:active,
.clear-actions button:active,
.ranking-close:active {
  transform: translateY(1px) scale(.985);
}
.clear-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}
.clear-actions button {
  font-size: 18px;
  padding: 12px 10px;
}
.ranking-modal {
  position: relative;
  width: min(90vw, 560px);
  max-height: min(86svh, 680px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box;
  padding: 24px 24px 18px;
  background: rgba(255, 249, 239, .98);
  border: 2px solid rgba(226, 173, 156, .95);
  border-radius: 24px;
  color: #7a5645;
  box-shadow: 0 24px 72px rgba(77, 47, 34, .28);
}
.ranking-modal h1 {
  margin: 0 44px 14px 0;
  font-size: 26px;
}
.ranking-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 42px;
  height: 42px;
  border-radius: 999px;
  border: 1px solid rgba(184, 123, 104, .65);
  background: rgba(255, 248, 236, .92);
  color: #7b5a49;
  font-size: 24px;
  font-weight: 900;
}
.menu-ranking-list {
  margin: 0;
  padding-left: 28px;
}
.menu-ranking-list li {
  margin: 8px 0;
  font-size: 17px;
  font-weight: 800;
}
.ui-hidden { display: none !important; }

@media (pointer: coarse), (max-width: 820px) {
  .opening-card {
    width: min(96vw, 520px);
    max-height: calc(100svh - 24px);
    border-radius: 20px;
  }
  .opening-actions {
    left: 6%;
    right: 6%;
    bottom: 4.5%;
    gap: 10px;
  }
  .opening-actions button {
    font-size: 16px;
    padding: 12px 8px;
  }
  .clear-actions button {
    font-size: 15px;
    padding: 10px 8px;
  }
  .ranking-modal {
    width: calc(100vw - 24px);
    padding: 20px 18px 16px;
  }
  .ranking-modal h1 { font-size: 22px; }
  .menu-ranking-list li { font-size: 15px; }
}

.language-selector {
  position: absolute;
  z-index: 4;
  left: 5%;
  right: 5%;
  bottom: 18.5%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
}
.language-selector button {
  border: 1px solid rgba(184, 123, 104, .65);
  background: rgba(255, 248, 236, .86);
  color: #7b5a49;
  border-radius: 999px;
  padding: 7px 10px;
  font-size: 13px;
  font-weight: 800;
  box-shadow: 0 4px 12px rgba(78, 40, 25, .12);
  -webkit-tap-highlight-color: transparent;
}
.language-selector button.active-lang {
  background: rgba(236, 142, 133, .92);
  color: white;
  border-color: rgba(202, 91, 82, .9);
}
@media (pointer: coarse), (max-width: 820px) {
  .language-selector {
    bottom: 20%;
    gap: 5px;
  }
  .language-selector button {
    font-size: 11px;
    padding: 6px 8px;
  }
}
@media (max-width: 430px) {
  .language-selector {
    bottom: 19%;
  }
  .language-selector button {
    font-size: 10px;
    padding: 5px 6px;
  }
}
