:root {
  --bg: #111827;
  --panel: #1f2937;
  --panel-alt: #0f172a;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #22d3ee;
  --border: #334155;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  min-height: 100dvh;
  font-family: "Consolas", "Menlo", "Courier New", monospace;
  background: radial-gradient(circle at top right, #0b2a3d, var(--bg) 55%);
  color: var(--text);
  overscroll-behavior: none;
  padding:
    max(0px, env(safe-area-inset-top))
    max(0px, env(safe-area-inset-right))
    max(0px, env(safe-area-inset-bottom))
    max(0px, env(safe-area-inset-left));
}

button {
  touch-action: manipulation;
}

button,
.touch-controls,
.canvas-wrap {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.layout {
  max-width: 1240px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  gap: 16px;
}

.card {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(180deg, var(--panel), var(--panel-alt));
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
  min-height: 0;
}

.game-area {
  position: relative;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.7), rgba(2, 6, 23, 0.92));
}

.canvas-wrap.shake {
  animation: impact-shake 280ms ease-in-out;
}

#gameCanvas {
  display: block;
  width: min(100%, 820px);
  height: auto;
  max-height: calc(100dvh - 240px);
  image-rendering: pixelated;
  border: 2px solid #0ea5e9;
  border-radius: 8px;
  background: #111827;
}

.combat-banner {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  font-size: 0.8rem;
  color: #fee2e2;
  background: rgba(153, 27, 27, 0.86);
  border: 1px solid #dc2626;
  border-radius: 6px;
  padding: 4px 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.screen-flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at center, rgba(255, 255, 255, 0.72), rgba(255, 255, 255, 0) 65%);
}

.screen-flash.active {
  animation: flash-hit 260ms ease-out;
}

.scene-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at top, rgba(34, 211, 238, 0.2), rgba(15, 23, 42, 0.82) 46%),
    rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(2px);
  padding: 20px;
  z-index: 3;
}

.scene-overlay-card {
  width: min(88%, 420px);
  max-height: min(82dvh, 520px);
  overflow: auto;
  border: 1px solid rgba(103, 232, 249, 0.35);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.96), rgba(8, 47, 73, 0.88));
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  padding: 22px 20px;
  text-align: center;
}

.overlay-eyebrow {
  margin: 0 0 10px;
  color: #67e8f9;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.78rem;
}

.overlay-title {
  margin: 0;
  font-size: 1.8rem;
  color: #f8fafc;
}

.overlay-text {
  margin: 12px 0 18px;
  color: #cbd5e1;
  line-height: 1.5;
}

.overlay-button {
  border: 1px solid #22d3ee;
  border-radius: 999px;
  background: linear-gradient(180deg, #164e63, #0f172a);
  color: #ecfeff;
  padding: 10px 18px;
  min-height: 44px;
  font-family: inherit;
  cursor: pointer;
}

.class-pick-list {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.is-hidden {
  display: none;
}

.panel-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.panel-title.small {
  font-size: 0.82rem;
}

.hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.side-panel {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}

.class-summary {
  color: #cbd5e1;
  font-size: 0.84rem;
  line-height: 1.5;
}

.class-meta {
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
  color: #94a3b8;
  font-size: 0.76rem;
}

.status-list {
  display: grid;
  gap: 8px;
}

.status-item {
  display: grid;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: rgba(15, 23, 42, 0.6);
  font-size: 0.9rem;
}

.status-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.meter {
  height: 9px;
  border-radius: 999px;
  background: #0b1220;
  border: 1px solid #243244;
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  transition: width 0.18s ease-out;
}

.meter-fill.hp {
  background: linear-gradient(90deg, #ef4444, #f97316);
}

.meter-fill.mp {
  background: linear-gradient(90deg, #0ea5e9, #22d3ee);
}

.meter-fill.exp {
  background: linear-gradient(90deg, #84cc16, #22c55e);
}

.meter-fill.enemy {
  background: linear-gradient(90deg, #fb7185, #ef4444);
}

.panel-block {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  background: rgba(15, 23, 42, 0.45);
}

.resource-strip {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 8px;
}

.resource-pill {
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.62);
  padding: 8px;
  display: grid;
  gap: 4px;
  text-align: center;
  font-size: 0.78rem;
}

.resource-pill span {
  color: #94a3b8;
}

.detail-button {
  width: 100%;
  min-height: 42px;
  border: 1px solid rgba(34, 211, 238, 0.45);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(8, 47, 73, 0.95), rgba(15, 23, 42, 0.95));
  color: #ecfeff;
  font-family: inherit;
  cursor: pointer;
}

.detail-stats {
  text-align: left;
  display: grid;
  gap: 8px;
}

.detail-stats p {
  margin: 0;
  color: #dbeafe;
  line-height: 1.45;
}

.action-menu {
  display: grid;
  gap: 8px;
}

.skill-buttons {
  display: grid;
  gap: 8px;
  margin-top: -2px;
}

.skill-buttons.is-hidden {
  display: none;
}

.action-menu.is-hidden {
  display: none;
}

.action-menu button,
.touch-move {
  border: 1px solid #1d4ed8;
  border-radius: 8px;
  background: #1e3a8a;
  color: #dbeafe;
  padding: 10px 12px;
  min-height: 44px;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.action-menu button:hover:not(:disabled),
.touch-move:hover:not(:disabled) {
  transform: translateY(-1px);
  background: #1d4ed8;
  box-shadow: 0 10px 18px rgba(29, 78, 216, 0.25);
}

.action-menu button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.touch-controls {
  display: none;
  width: 100%;
  gap: 12px;
  align-items: center;
  justify-content: flex-end;
  z-index: 4;
}

.touch-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 72px));
  grid-template-rows: repeat(2, 72px);
  gap: 8px;
  justify-content: start;
}

.touch-up {
  grid-column: 2;
  grid-row: 1;
}

.touch-left {
  grid-column: 1;
  grid-row: 2;
}

.touch-down {
  grid-column: 2;
  grid-row: 2;
}

.touch-right {
  grid-column: 3;
  grid-row: 2;
}

.touch-move.is-pressed {
  background: #2563eb;
  transform: translateY(-1px) scale(0.98);
  box-shadow: 0 10px 18px rgba(37, 99, 235, 0.35);
}

.touch-tip {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.battle-log {
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 110px;
  max-height: 220px;
  overflow-y: auto;
  padding: 8px;
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--muted);
  background: #020617;
}

.battle-log p {
  margin: 0;
}

.rotate-notice {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at top, rgba(34, 211, 238, 0.18), rgba(15, 23, 42, 0.88) 44%),
    rgba(2, 6, 23, 0.92);
  text-align: center;
}

.rotate-card {
  max-width: 360px;
  border: 1px solid rgba(103, 232, 249, 0.3);
  border-radius: 18px;
  padding: 22px 20px;
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.96), rgba(8, 47, 73, 0.9));
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
}

.rotate-eyebrow {
  margin: 0 0 8px;
  color: #67e8f9;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.76rem;
}

.rotate-title {
  margin: 0;
  font-size: 1.6rem;
}

.rotate-text {
  margin: 12px 0 0;
  color: #cbd5e1;
  line-height: 1.55;
  font-size: 0.95rem;
}

@keyframes impact-shake {
  0% { transform: translate3d(0, 0, 0); }
  20% { transform: translate3d(-8px, 4px, 0); }
  40% { transform: translate3d(6px, -5px, 0); }
  60% { transform: translate3d(-5px, 3px, 0); }
  80% { transform: translate3d(4px, -2px, 0); }
  100% { transform: translate3d(0, 0, 0); }
}

@keyframes flash-hit {
  0% { opacity: 0; }
  18% { opacity: 1; }
  100% { opacity: 0; }
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .side-panel {
    overflow: visible;
  }
}

@media (max-width: 980px) and (orientation: landscape) and (pointer: coarse) {
  body {
    overflow: hidden;
  }

  .layout {
    max-width: none;
    height: 100dvh;
    min-height: 100dvh;
    padding: 6px;
    gap: 6px;
    grid-template-columns: minmax(0, 1fr) minmax(170px, 186px);
  }

  .game-area {
    padding: 6px;
    gap: 6px;
    min-height: calc(100dvh - 12px);
  }

  .side-panel {
    padding: 6px;
    gap: 6px;
    overflow: hidden;
  }

  .side-panel > .panel-title {
    display: none;
  }

  .panel-title {
    font-size: 0.74rem;
  }

  .hint {
    display: none;
  }

  #gameCanvas {
    width: auto;
    height: min(calc(100dvh - 78px), calc((100vw - 204px) * 0.75));
    max-width: 100%;
    max-height: 100%;
  }

  .touch-controls {
    display: flex;
    align-self: flex-start;
    width: auto;
    margin-left: 0;
    padding: 4px;
    gap: 4px;
    border-radius: 12px;
    background: rgba(2, 6, 23, 0.72);
    backdrop-filter: blur(8px);
  }

  .touch-grid {
    grid-template-columns: repeat(3, minmax(0, 40px));
    grid-template-rows: repeat(2, 40px);
    gap: 4px;
  }

  .touch-move {
    min-height: 40px;
    padding: 0;
    font-size: 0.68rem;
    touch-action: none;
  }

  .touch-tip {
    display: none;
  }

  .status-list {
    gap: 4px;
  }

  .status-item {
    padding: 4px 6px;
    font-size: 0.68rem;
    background: rgba(2, 6, 23, 0.78);
    border-color: rgba(148, 163, 184, 0.28);
    backdrop-filter: blur(6px);
  }

  .panel-block {
    padding: 6px;
    background: rgba(2, 6, 23, 0.78);
    border-color: rgba(148, 163, 184, 0.28);
    backdrop-filter: blur(6px);
  }

  #resourcePanel {
    order: 2;
  }

  #actionPanel {
    order: 3;
    width: 100%;
  }

  #classPanel {
    order: 4;
  }

  #logPanel {
    order: 5;
  }

  .action-menu {
    gap: 6px;
  }

  .action-menu button {
    min-height: 34px;
    padding: 6px 8px;
    font-size: 0.72rem;
  }

  .resource-strip {
    gap: 4px;
    margin-bottom: 6px;
  }

  .resource-pill {
    padding: 5px 4px;
    font-size: 0.65rem;
  }

  .detail-button {
    min-height: 32px;
    font-size: 0.68rem;
  }

  .class-meta {
    gap: 2px;
    margin-bottom: 6px;
    font-size: 0.62rem;
  }

  .class-summary {
    font-size: 0.66rem;
    line-height: 1.35;
  }

  .enemy-panel .status-item {
    gap: 4px;
  }

  .battle-log {
    min-height: 40px;
    max-height: 40px;
    height: 40px;
    padding: 5px 6px;
    overflow: hidden;
    font-size: 0.66rem;
    line-height: 1.35;
  }

  .scene-overlay {
    padding: 10px;
  }

  .scene-overlay-card {
    width: min(96vw, 560px);
    max-height: min(88dvh, 320px);
    padding: 14px 12px;
  }

  .overlay-title {
    font-size: 1.18rem;
  }

  .class-pick-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .overlay-button {
    min-height: 38px;
    padding: 7px 10px;
    font-size: 0.78rem;
  }
}

@media (max-width: 980px) and (orientation: portrait) and (pointer: coarse) {
  .rotate-notice {
    display: flex;
  }

  .layout {
    filter: blur(1px) brightness(0.5);
    pointer-events: none;
    user-select: none;
  }
}

@media (max-width: 980px) and (orientation: landscape) and (pointer: coarse) and (max-height: 430px) {
  #gameCanvas {
    height: min(calc(100dvh - 68px), calc((100vw - 190px) * 0.75));
  }

  .touch-grid {
    grid-template-columns: repeat(3, minmax(0, 34px));
    grid-template-rows: repeat(2, 34px);
  }

  .touch-move {
    min-height: 34px;
    font-size: 0.6rem;
  }

  .scene-overlay-card {
    max-height: min(88dvh, 286px);
  }

  .side-panel {
    gap: 4px;
  }

  .panel-block,
  .game-area,
  .side-panel {
    padding: 5px;
  }

  .battle-log {
    min-height: 36px;
    max-height: 36px;
    height: 36px;
  }
}
