:root {
  --bg-cream: #fff7fa;
  --panel-bg: #ffffff;
  --panel-border: #ffd6e7;
  --text-main: #2d3436;
  --text-muted: #636e72;
  --pink-main: #ff6b9d;
  --pink-shadow: #d9487b;
  --mint-main: #2ed573;
  --mint-shadow: #20a657;
  --sky-main: #54a0ff;
  --sky-shadow: #2e86de;
  --amber-main: #ffa502;
  --amber-shadow: #cc8400;
  --purple-main: #9c88ff;
  --purple-shadow: #7158e2;
  --card-radius: 16px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  background: linear-gradient(180deg, #ffeef6 0%, #e8f8ff 100%);
  color: var(--text-main);
  font-family: 'Hiragino Maru Gothic ProN', 'Rounded Mplus 1c', 'Yu Gothic UI', system-ui, -apple-system, sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  max-width: 960px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 4px) 6px env(safe-area-inset-bottom, 6px);
  gap: 6px;
}

/* Top HUD */
#hud {
  background: rgba(255, 255, 255, 0.94);
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  padding: 6px 8px;
  box-shadow: 0 3px 0 rgba(255, 107, 157, 0.16);
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex-shrink: 0;
}

.hud-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

.hud-row-top {
  justify-content: space-between;
}

.hud-badge {
  font-size: 12px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.floor-badge {
  background: linear-gradient(135deg, #ff85b3, #ff6b9d);
  color: #fff;
  box-shadow: 0 2px 0 #d9487b;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 42%;
}

.hud-level-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.level-badge {
  background: #ffeaa7;
  color: #8c5a00;
  border: 1.5px solid #fdcb6e;
}

.exp-bar-wrap {
  width: 38px;
  height: 8px;
  background: #f1f2f6;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid #dfe4ea;
}

.exp-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #70a1ff, #9c88ff);
  transition: width 0.2s ease;
}

.gold-badge {
  background: #fff3cd;
  color: #9b6b00;
  border: 1.5px solid #ffd466;
}

.hud-top-actions {
  display: flex;
  gap: 4px;
}

.icon-pill-btn {
  border: 1.5px solid #ffd6e7;
  background: #fff;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 800;
  color: var(--text-main);
  cursor: pointer;
  box-shadow: 0 2px 0 #f3c6d8;
}

.icon-pill-btn:active {
  transform: translateY(1px);
  box-shadow: 0 1px 0 #f3c6d8;
}

.hud-row-bars {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 6px;
  align-items: center;
}

.stat-bar-group {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.stat-icon {
  font-size: 13px;
  flex-shrink: 0;
}

.progress-track {
  position: relative;
  flex: 1;
  height: 18px;
  background: #f1f2f6;
  border-radius: 999px;
  overflow: hidden;
  border: 1.5px solid #dfe4ea;
}

.progress-fill {
  height: 100%;
  width: 100%;
  transition: width 0.2s ease;
}

.hp-fill {
  background: linear-gradient(90deg, #ff7675, #2ed573);
}

.hp-fill.low {
  background: linear-gradient(90deg, #ff4757, #ff6b81);
}

.food-fill {
  background: linear-gradient(90deg, #fdcb6e, #ffa502);
}

.food-fill.low {
  background: linear-gradient(90deg, #ff6348, #ff4757);
}

.progress-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 900;
  color: #2d3436;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.85);
}

.equip-pills {
  display: flex;
  gap: 4px;
  min-width: 0;
}

.equip-pill {
  background: #f7f9fc;
  border: 1.5px solid #d2dae2;
  border-radius: 10px;
  padding: 2px 6px;
  font-size: 11px;
  font-weight: 800;
  color: var(--text-main);
  max-width: 96px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

/* Viewport & Canvases */
#viewport {
  position: relative;
  flex: 1;
  min-height: 180px;
  border-radius: 16px;
  overflow: hidden;
  border: 3px solid #ffffff;
  box-shadow: 0 4px 16px rgba(108, 92, 231, 0.14);
  background: #dff9fb;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#minimap-canvas {
  position: absolute;
  top: 8px;
  right: 8px;
  border-radius: 10px;
  background: rgba(25, 28, 42, 0.62);
  border: 2px solid rgba(255, 255, 255, 0.75);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  z-index: 5;
  transition: transform 0.15s ease;
}

.floor-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #ff6b9d, #9c88ff);
  color: #fff;
  font-weight: 900;
  font-size: 14px;
  padding: 6px 16px;
  border-radius: 999px;
  border: 2px solid #fff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
  z-index: 6;
  white-space: nowrap;
  pointer-events: none;
}

.context-action-btn {
  position: absolute;
  bottom: 52px;
  right: 10px;
  background: linear-gradient(135deg, #ffa502, #ff6b81);
  color: #fff;
  border: 2px solid #fff;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 900;
  box-shadow: 0 4px 0 #c44569, 0 6px 16px rgba(0, 0, 0, 0.22);
  cursor: pointer;
  z-index: 6;
  animation: popPulse 1.4s infinite ease-in-out;
}

.context-action-btn:active {
  transform: translateY(2px);
}

@keyframes popPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

#toast-log {
  position: absolute;
  bottom: 8px;
  left: 8px;
  max-width: calc(100% - 16px);
  display: flex;
  flex-direction: column;
  gap: 3px;
  pointer-events: none;
  z-index: 4;
}

.toast-line {
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(4px);
  border-left: 4px solid var(--pink-main);
  padding: 3px 8px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-main);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast-line.warn { border-left-color: #ff4757; color: #b33939; }
.toast-line.good { border-left-color: #2ed573; color: #1e8449; }
.toast-line.special { border-left-color: #9c88ff; color: #5f27cd; }

/* Bottom Control Deck */
#controls {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  background: rgba(255, 255, 255, 0.94);
  border: 2px solid var(--panel-border);
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 3px 0 rgba(255, 107, 157, 0.16);
  flex-shrink: 0;
  align-items: center;
}

.dpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 44px);
  gap: 4px;
}

.dpad-btn {
  border: 2px solid #dcdde1;
  background: #ffffff;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 900;
  color: var(--text-main);
  cursor: pointer;
  box-shadow: 0 3px 0 #ced6e0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  touch-action: manipulation;
}

.dpad-btn.diag {
  background: #f3f0ff;
  border-color: #d6ccff;
  box-shadow: 0 3px 0 #bfb2ff;
  color: #5f27cd;
}

.dpad-btn.center-wait {
  background: #e8f8f5;
  border-color: #a3e4d7;
  box-shadow: 0 3px 0 #76d7c4;
  font-size: 10px;
  line-height: 1.1;
  color: #117864;
}

.dpad-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #ced6e0;
}

.dpad-btn:disabled,
.dpad-btn.locked {
  opacity: 0.25;
  cursor: not-allowed;
  background: #dfe4ea;
  box-shadow: none;
  transform: none;
}

.action-deck {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.action-subgrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
}

.action-btn {
  border: 2px solid #dfe4ea;
  background: #ffffff;
  border-radius: 12px;
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 800;
  color: var(--text-main);
  cursor: pointer;
  box-shadow: 0 3px 0 #ced6e0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.action-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #ced6e0;
}

.attack-btn {
  background: linear-gradient(135deg, #ff7eb3, #ff6b9d);
  border-color: #ff4f8b;
  color: #fff;
  box-shadow: 0 3px 0 var(--pink-shadow);
  font-size: 14px;
  padding: 9px 8px;
}

.attack-btn:active {
  box-shadow: 0 1px 0 var(--pink-shadow);
}

.bag-btn {
  background: #fff9db;
  border-color: #ffe066;
  box-shadow: 0 3px 0 #f0c419;
  color: #7d5a00;
}

.relics-btn {
  background: #eef6ff;
  border-color: #bde0fe;
  box-shadow: 0 3px 0 #8ecae6;
  color: #1d4ed8;
  padding: 6px;
  font-size: 11px;
}

.toggle-btn.active {
  background: linear-gradient(135deg, #a29bfe, #74b9ff);
  border-color: #6c5ce7;
  color: #fff;
  box-shadow: 0 3px 0 #5f27cd;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(45, 52, 54, 0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  z-index: 50;
}

.hidden {
  display: none !important;
}

.modal-card {
  width: 100%;
  max-width: 520px;
  max-height: 90dvh;
  background: var(--bg-cream);
  border: 3px solid #ffffff;
  border-radius: 22px;
  padding: 14px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}

.wide-card {
  max-width: 640px;
}

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

.modal-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 900;
  color: #d63384;
}

.close-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid #ffd6e7;
  background: #fff;
  font-weight: 900;
  font-size: 15px;
  cursor: pointer;
  color: var(--text-main);
}

.modal-desc {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 700;
}

/* Title Modal */
.title-card {
  overflow-y: auto;
  touch-action: pan-y;
}

.title-hero {
  text-align: center;
  background: linear-gradient(135deg, #fff0f6, #e8f8ff);
  border: 2px solid #ffd6e7;
  border-radius: 16px;
  padding: 12px 10px;
}

.title-emblem {
  font-size: 32px;
  line-height: 1.1;
}

.game-title {
  margin: 4px 0 2px;
  font-size: 20px;
  font-weight: 900;
  color: #e84393;
}

.game-subtitle {
  margin: 0;
  font-size: 11px;
  font-weight: 700;
  color: #636e72;
}

.meta-summary-bar {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.meta-chip {
  background: #fff;
  border: 2px solid #ffeaa7;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 13px;
  font-weight: 800;
}

.gem-chip {
  border-color: #74b9ff;
  color: #0984e3;
}

.title-start-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.primary-3d-btn {
  border: none;
  border-radius: 14px;
  padding: 11px 14px;
  font-size: 15px;
  font-weight: 900;
  color: #fff;
  background: linear-gradient(135deg, #ff7eb3, #ff6b9d);
  box-shadow: 0 4px 0 var(--pink-shadow);
  cursor: pointer;
}

.primary-3d-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--pink-shadow);
}

.primary-3d-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.continue-btn {
  background: linear-gradient(135deg, #2ed573, #1dd1a1);
  box-shadow: 0 4px 0 var(--mint-shadow);
}

.secondary-3d-btn {
  border: 2px solid #ced6e0;
  border-radius: 14px;
  padding: 9px 12px;
  font-size: 13px;
  font-weight: 800;
  background: #fff;
  color: var(--text-main);
  box-shadow: 0 3px 0 #ced6e0;
  cursor: pointer;
}

.meta-shop-section {
  background: #fff;
  border: 2px solid #e8ecf4;
  border-radius: 16px;
  padding: 10px;
}

.section-heading {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 900;
  color: #0984e3;
}

.meta-shop-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.shop-upgrade-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: #f8fbff;
  border: 1.5px solid #dbeafe;
  border-radius: 12px;
  padding: 8px;
}

.shop-upgrade-info {
  min-width: 0;
}

.shop-upgrade-title {
  font-size: 13px;
  font-weight: 900;
}

.shop-upgrade-desc {
  font-size: 11px;
  color: var(--text-muted);
}

.shop-buy-btn {
  flex-shrink: 0;
  border: none;
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 900;
  background: linear-gradient(135deg, #54a0ff, #2e86de);
  color: #fff;
  box-shadow: 0 3px 0 #1e60b0;
  cursor: pointer;
}

.shop-buy-btn:disabled {
  background: #b2bec3;
  box-shadow: none;
  cursor: not-allowed;
}

.controls-hint {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.4;
}

/* Bag / Lists */
.bag-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  flex: 1;
}

.item-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  max-height: 36dvh;
  padding-right: 2px;
  touch-action: pan-y;
}

.compact-list {
  max-height: 28dvh;
}

.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: #fff;
  border: 2px solid #eef2f7;
  border-radius: 12px;
  padding: 8px 10px;
  cursor: pointer;
  text-align: left;
}

.item-row.selected {
  border-color: var(--pink-main);
  background: #fff0f6;
}

.item-row-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.item-emoji {
  font-size: 20px;
  flex-shrink: 0;
}

.item-name-block {
  min-width: 0;
}

.item-name-line {
  font-size: 13px;
  font-weight: 900;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.equipped-tag {
  background: #2ed573;
  color: #fff;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 999px;
}

.rune-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-top: 3px;
}

.rune-tag {
  background: #f3f0ff;
  color: #5f27cd;
  border: 1px solid #d6ccff;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 5px;
  border-radius: 6px;
}

.item-detail-panel {
  background: #fff;
  border: 2px solid #ffd6e7;
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.45;
}

.muted-text {
  margin: 0;
  color: var(--text-muted);
  font-size: 12px;
}

.detail-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.detail-actions button {
  flex: 1;
  min-width: 96px;
}

/* Synthesis */
.synth-slots {
  display: flex;
  align-items: center;
  gap: 6px;
}

.synth-slot {
  flex: 1;
  background: #fff;
  border: 2px dashed #b2bec3;
  border-radius: 12px;
  padding: 8px;
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.synth-slot.filled {
  border-style: solid;
  border-color: var(--purple-main);
  background: #f5f3ff;
}

.synth-plus {
  font-size: 18px;
  font-weight: 900;
  color: var(--pink-main);
}

.synth-preview {
  background: #e8f8f5;
  border: 2px solid #2ed573;
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 800;
}

.modal-footer-btns {
  display: flex;
  gap: 8px;
}

.modal-footer-btns button {
  flex: 1;
}

/* Relic Cards */
.relic-choices-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.relic-card {
  background: #fff;
  border: 2px solid #ffd6e7;
  border-radius: 16px;
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  cursor: pointer;
  box-shadow: 0 4px 0 #f3c6d8;
}

.relic-card:hover,
.relic-card:active {
  border-color: var(--pink-main);
  background: #fff5f9;
  transform: translateY(-2px);
}

.relic-icon {
  font-size: 28px;
}

.relic-name {
  font-size: 13px;
  font-weight: 900;
  color: #d63384;
}

.relic-desc {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.35;
}

/* Status & Log */
.status-scroll-body {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  touch-action: pan-y;
}

.status-block {
  background: #fff;
  border: 2px solid #eef2f7;
  border-radius: 12px;
  padding: 10px;
  font-size: 12px;
  line-height: 1.5;
}

.status-block h3 {
  margin: 0 0 6px;
  font-size: 13px;
  color: #0984e3;
}

.log-history-list {
  overflow-y: auto;
  max-height: 60dvh;
  display: flex;
  flex-direction: column;
  gap: 4px;
  touch-action: pan-y;
}

.log-entry {
  background: #fff;
  border-left: 4px solid #dfe4ea;
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 12px;
  font-weight: 700;
}

.log-entry.warn { border-left-color: #ff4757; color: #b33939; }
.log-entry.good { border-left-color: #2ed573; color: #1e8449; }
.log-entry.special { border-left-color: #9c88ff; color: #5f27cd; }

.gameover-summary {
  background: #fff;
  border: 2px solid #ffd6e7;
  border-radius: 14px;
  padding: 12px;
  font-size: 13px;
  line-height: 1.65;
  font-weight: 700;
}

.gameover-gems-box {
  background: linear-gradient(135deg, #e8f4ff, #f3f0ff);
  border: 2px solid #74b9ff;
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 900;
  color: #0984e3;
  text-align: center;
}

/* Responsive Tweaks for small phones vs wide screens */
@media (max-width: 440px) {
  .hud-row-bars {
    grid-template-columns: 1fr 1fr;
  }
  .equip-pills {
    grid-column: 1 / -1;
    justify-content: space-between;
  }
  .equip-pill {
    max-width: 48%;
    flex: 1;
  }
  .dpad-grid {
    grid-template-columns: repeat(3, 42px);
    grid-template-rows: repeat(3, 40px);
  }
  .relic-choices-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 720px) {
  .dpad-grid {
    grid-template-columns: repeat(3, 52px);
    grid-template-rows: repeat(3, 48px);
  }
}
