*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
}

::selection {
  background: transparent;
  color: inherit;
}

:root {
  --ink: #1a1208;
  --parchment: #e8d5b0;
  --parchment-dark: #cbb892;
  --wood: #3b2414;
  --wood-light: #6b4226;
  --gold: #c9a227;
  --gold-bright: #f0d060;
  --blood: #7a1f1f;
  --stone: #2a2420;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: fixed;
  overscroll-behavior: none;
  background: var(--ink);
  font-family: "Shippori Mincho", "Cinzel", serif;
  color: var(--parchment);
}

#screen {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background:
    radial-gradient(ellipse at 50% 20%, rgba(201, 162, 39, 0.12), transparent 55%),
    linear-gradient(180deg, #1c140c 0%, #0d0906 100%);
  padding:
    calc(12px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(12px + var(--safe-left));
  display: flex;
  flex-direction: column;
  align-items: center;
}

.hidden { display: none !important; }

.icon-btn {
  position: absolute;
  top: calc(10px + var(--safe-top));
  right: calc(10px + var(--safe-right));
  z-index: 40;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid var(--gold);
  background: radial-gradient(circle at 30% 30%, var(--wood-light), var(--wood));
  color: var(--parchment);
  font-size: 18px;
  cursor: pointer;
  transition: transform 0.08s ease, opacity 0.08s ease;
}

.icon-btn:active {
  transform: scale(0.92);
  opacity: 0.85;
}

#hud {
  position: absolute;
  top: calc(10px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  gap: 10px;
  padding: 6px 14px;
  background: linear-gradient(180deg, #4a2f1a, #2a180c);
  border: 2px solid var(--gold);
  box-shadow: 0 0 0 1px #1a1008, inset 0 0 12px rgba(0,0,0,0.45);
  border-radius: 4px;
  min-width: min(92vw, 340px);
  justify-content: space-around;
}

.hud-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: Cinzel, "Shippori Mincho", serif;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.04em;
  color: var(--gold-bright);
}

.hud-ico { color: var(--parchment); opacity: 0.85; }

.panel {
  margin: auto;
  width: min(92vw, 380px);
  padding: 28px 22px 24px;
  text-align: center;
  background:
    linear-gradient(180deg, rgba(232, 213, 176, 0.96), rgba(203, 184, 146, 0.96));
  color: var(--ink);
  border: 3px solid var(--gold);
  box-shadow:
    0 0 0 2px #2a180c,
    0 18px 40px rgba(0,0,0,0.55),
    inset 0 0 40px rgba(90, 60, 20, 0.18);
  border-radius: 6px;
  position: relative;
}

.panel::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px solid rgba(90, 60, 20, 0.35);
  pointer-events: none;
  border-radius: 3px;
}

.panel.overlay {
  position: absolute;
  z-index: 50;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.crest {
  font-size: 28px;
  color: var(--blood);
  margin-bottom: 6px;
}

h1, h2 {
  margin: 0 0 8px;
  font-family: Cinzel, "Shippori Mincho", serif;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--wood);
}

h1 { font-size: clamp(1.6rem, 6vw, 2.1rem); }
h2 { font-size: 1.25rem; }

.tagline {
  margin: 0 0 22px;
  font-size: 0.95rem;
  letter-spacing: 0.2em;
  color: #5a3d22;
}

.medieval-btn {
  appearance: none;
  border: 2px solid var(--gold);
  background: linear-gradient(180deg, #5a3420, #2f1a0e);
  color: var(--parchment);
  font-family: Cinzel, "Shippori Mincho", serif;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 12px 22px;
  border-radius: 4px;
  cursor: pointer;
  box-shadow: 0 4px 0 #1a1008, inset 0 1px 0 rgba(255,255,255,0.12);
  transition: transform 0.08s ease, opacity 0.08s ease;
  min-height: 48px;
}

.medieval-btn.primary {
  background: linear-gradient(180deg, #8a2a2a, #4a1212);
}

.medieval-btn:active {
  transform: scale(0.94) translateY(2px);
  opacity: 0.9;
}

.btn-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 8px;
}

#char-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 14px;
}

.char-card {
  appearance: none;
  border: 2px solid #7a5a2a;
  background: linear-gradient(180deg, #2a1a10, #120c08);
  border-radius: 6px;
  padding: 10px 6px 8px;
  cursor: pointer;
  color: var(--parchment);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transition: transform 0.08s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  min-height: 120px;
}

.char-card img {
  width: 100%;
  max-width: 96px;
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.6));
  pointer-events: none;
}

.char-card span {
  font-family: Cinzel, "Shippori Mincho", serif;
  font-size: 0.95rem;
  letter-spacing: 0.2em;
  color: var(--gold-bright);
}

.char-card:active { transform: scale(0.94); }

.char-card.selected,
.char-card:hover {
  border-color: var(--gold-bright);
  box-shadow: 0 0 0 1px var(--gold), 0 0 16px rgba(201, 162, 39, 0.45);
}

#play-screen {
  flex: 1 1 auto;
  align-self: stretch;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  padding-top: 52px;
}

#canvas-frame {
  flex: 1 1 auto;
  min-height: 200px;
  border: 3px solid var(--gold);
  border-radius: 6px;
  box-shadow:
    0 0 0 2px #1a1008,
    inset 0 0 30px rgba(0,0,0,0.5),
    0 12px 28px rgba(0,0,0,0.45);
  background: #120c08;
  overflow: hidden;
  position: relative;
  width: 100%;
}

#game-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#game-canvas.grabbing {
  cursor: grabbing;
}

#scroll-hint {
  position: absolute;
  right: 10px;
  bottom: 12px;
  z-index: 5;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1.5px solid var(--gold);
  background: rgba(26, 16, 8, 0.72);
  color: var(--gold-bright);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  pointer-events: none;
  animation: scrollPulse 1.4s ease-in-out infinite;
}

@keyframes scrollPulse {
  0%, 100% { transform: translateY(0); opacity: 0.55; }
  50% { transform: translateY(4px); opacity: 1; }
}

#start-gates {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 10px;
  padding-bottom: 4px;
}

.gate-btn {
  appearance: none;
  min-height: 52px;
  border: 2px solid var(--gold);
  background:
    linear-gradient(180deg, #4a3424 0%, #24160c 100%);
  color: var(--gold-bright);
  font-family: Cinzel, serif;
  font-size: 1.35rem;
  font-weight: 700;
  border-radius: 4px;
  cursor: pointer;
  box-shadow: 0 3px 0 #120c08, inset 0 0 10px rgba(0,0,0,0.4);
  transition: transform 0.08s ease, opacity 0.08s ease, background 0.15s;
}

.gate-btn:active {
  transform: scale(0.94);
  opacity: 0.9;
}

.gate-btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

#result-img {
  width: 110px;
  height: auto;
  margin: 0 auto 10px;
  display: block;
  filter: drop-shadow(0 6px 10px rgba(0,0,0,0.45));
}

#result-delta,
#end-score {
  margin: 0 0 16px;
  font-family: Cinzel, "Shippori Mincho", serif;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}

#result-delta.positive { color: #2f6b2f; }
#result-delta.negative { color: var(--blood); }

@media (max-width: 360px) {
  #char-grid { gap: 6px; }
  .char-card { min-height: 108px; padding: 8px 4px; }
  .gate-btn { min-height: 48px; font-size: 1.15rem; }
}
