* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: #0a0a14;
  color: #e8e8f0;
  font-family: 'Consolas', 'Courier New', monospace;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  user-select: none;
}

#game {
  width: 800px;
  background: #14141f;
  border: 2px solid #5a4a8a;
  border-radius: 8px;
  box-shadow: 0 0 30px rgba(120, 80, 200, 0.4);
  overflow: hidden;
}

#topbar {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  background: linear-gradient(180deg, #2a1f4a, #1a1530);
  border-bottom: 1px solid #5a4a8a;
  font-size: 13px;
}

#player-info { font-weight: bold; color: #ffd966; }
#location-info { color: #aaccff; }
#gold-info { color: #ffcc44; font-weight: bold; }

#main-area {
  position: relative;
  width: 800px;
  height: 480px;
  background: #000;
}

#canvas {
  display: block;
  width: 800px;
  height: 480px;
  image-rendering: pixelated;
  cursor: crosshair;
}

#overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#bottombar {
  background: #1a1530;
  border-top: 1px solid #5a4a8a;
  padding: 10px;
}

#message-log {
  height: 70px;
  background: #08080f;
  border: 1px solid #3a3055;
  padding: 6px 10px;
  font-size: 12px;
  overflow-y: auto;
  margin-bottom: 8px;
  border-radius: 4px;
}

.log-msg {
  margin: 2px 0;
  line-height: 1.4;
}

.log-msg.system { color: #888; }
.log-msg.combat { color: #ff8866; }
.log-msg.heal { color: #88ff88; }
.log-msg.gold { color: #ffcc44; }
.log-msg.item { color: #aaccff; }
.log-msg.quest { color: #ffaaff; }

#actions {
  display: flex;
  gap: 6px;
}

button {
  background: linear-gradient(180deg, #4a3a7a, #2a1f4a);
  color: #fff;
  border: 1px solid #6a5a9a;
  padding: 6px 12px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.15s;
}

button:hover {
  background: linear-gradient(180deg, #6a5a9a, #4a3a7a);
  box-shadow: 0 0 8px rgba(120, 90, 200, 0.6);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#modal-bg {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

#modal-bg.hidden { display: none; }

#modal {
  background: #1a1530;
  border: 2px solid #6a5a9a;
  border-radius: 8px;
  padding: 20px;
  min-width: 500px;
  max-width: 700px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 0 40px rgba(120, 90, 200, 0.6);
}

.modal-title {
  font-size: 20px;
  margin-bottom: 14px;
  color: #ffd966;
  border-bottom: 1px solid #5a4a8a;
  padding-bottom: 8px;
  text-align: center;
}

.modal-section {
  margin: 10px 0;
}

.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  margin: 4px 0;
  background: #14101f;
  border: 1px solid #2a2440;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.item-row:hover {
  background: #251a45;
  border-color: #6a5a9a;
}

.item-row.equipped {
  border-color: #ffcc44;
  background: #2a2010;
}

.item-info { flex: 1; }
.item-name { font-weight: bold; }
.item-stats { font-size: 11px; color: #aacccc; margin-top: 2px; }
.item-price { color: #ffcc44; font-weight: bold; margin: 0 12px; }
.item-actions { display: flex; gap: 6px; }
.item-actions button { font-size: 11px; padding: 4px 8px; }

.tab-bar {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  border-bottom: 1px solid #4a3a7a;
}

.tab {
  padding: 6px 14px;
  background: #14101f;
  border: 1px solid #2a2440;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
  font-size: 12px;
}

.tab.active {
  background: #2a1f4a;
  border-color: #6a5a9a;
  color: #ffd966;
}

.close-btn {
  position: relative;
  display: block;
  margin: 14px auto 0;
  padding: 8px 24px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  font-size: 13px;
}

.stat-row {
  background: #14101f;
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid #2a2440;
}

.stat-row b { color: #ffd966; }

.bar {
  height: 14px;
  background: #08080f;
  border: 1px solid #3a3055;
  border-radius: 7px;
  overflow: hidden;
  position: relative;
  margin: 4px 0;
}

.bar-fill {
  height: 100%;
  transition: width 0.3s;
}

.bar-fill.hp { background: linear-gradient(90deg, #aa2233, #ee4455); }
.bar-fill.mp { background: linear-gradient(90deg, #2255aa, #4488ff); }
.bar-fill.xp { background: linear-gradient(90deg, #228855, #44dd88); }

.bar-text {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 11px;
  color: #fff;
  text-shadow: 1px 1px 2px #000;
}

.combat-screen {
  text-align: center;
}

.enemy-display {
  font-size: 80px;
  margin: 20px 0;
}

.combat-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 16px;
}

.combat-actions button {
  padding: 12px;
  font-size: 14px;
}

.region-card {
  background: #14101f;
  border: 1px solid #4a3a7a;
  border-radius: 6px;
  padding: 12px;
  margin: 6px 0;
  cursor: pointer;
}

.region-card:hover {
  background: #251a45;
  border-color: #ffcc44;
}

.region-card.locked {
  opacity: 0.4;
  cursor: not-allowed;
}

.region-name { font-weight: bold; color: #ffd966; }
.region-desc { font-size: 11px; color: #aacccc; margin-top: 4px; }
.region-level { font-size: 11px; color: #ff8866; margin-top: 4px; }

.dialogue {
  background: #14101f;
  border: 1px solid #4a3a7a;
  padding: 14px;
  border-radius: 6px;
  margin: 10px 0;
  font-style: italic;
  color: #ddccff;
}

.choices {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}

#class-select {
  text-align: center;
}

.class-card {
  display: inline-block;
  width: 180px;
  margin: 10px;
  padding: 16px;
  background: #14101f;
  border: 2px solid #4a3a7a;
  border-radius: 8px;
  cursor: pointer;
  vertical-align: top;
}

.class-card:hover {
  border-color: #ffcc44;
  background: #251a45;
}

.class-icon { font-size: 60px; margin: 10px 0; }
.class-name { font-size: 18px; font-weight: bold; color: #ffd966; }
.class-desc { font-size: 11px; color: #aacccc; margin-top: 8px; }

.death-screen {
  text-align: center;
  color: #ff4455;
  padding: 30px;
}

.death-screen h1 {
  font-size: 36px;
  margin-bottom: 20px;
}

.victory-screen {
  text-align: center;
  color: #ffcc44;
  padding: 30px;
}

.notification {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(20, 16, 31, 0.95);
  border: 1px solid #ffcc44;
  border-radius: 4px;
  padding: 8px 14px;
  font-size: 12px;
  color: #ffd966;
  animation: slideIn 0.3s, fadeOut 0.5s 2.5s forwards;
}

@keyframes slideIn {
  from { transform: translateX(120%); }
  to { transform: translateX(0); }
}

@keyframes fadeOut {
  to { opacity: 0; transform: translateX(120%); }
}

.dmg-popup {
  position: absolute;
  font-weight: bold;
  font-size: 24px;
  pointer-events: none;
  animation: dmgFloat 1s forwards;
}

@keyframes dmgFloat {
  0% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-60px); opacity: 0; }
}

/* ==========================
   MOBILE CONTROLS (hidden by default)
   ========================== */
.mobile-only { display: none; }

#joystick {
  position: absolute;
  bottom: 20px;
  left: 20px;
  width: 120px;
  height: 120px;
  background: rgba(0, 0, 0, 0.45);
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-radius: 50%;
  pointer-events: auto;
  touch-action: none;
  z-index: 50;
  box-shadow: 0 0 12px rgba(0, 0, 0, 0.6);
}

#stick {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 56px;
  height: 56px;
  background: radial-gradient(circle, rgba(255,255,255,0.7), rgba(180,180,200,0.5));
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 50%;
  pointer-events: none;
  transition: transform 0.05s linear;
}

#skill-bar {
  position: absolute;
  bottom: 20px;
  right: 20px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  z-index: 50;
  pointer-events: auto;
}

.skill-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  font-size: 16px;
  background: linear-gradient(180deg, rgba(80, 60, 130, 0.95), rgba(40, 25, 75, 0.95));
  border: 2px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  font-weight: bold;
  pointer-events: auto;
  touch-action: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.skill-btn:active {
  background: linear-gradient(180deg, rgba(160, 130, 220, 1), rgba(100, 70, 170, 1));
  transform: scale(0.92);
}

.skill-btn.atk-btn {
  background: linear-gradient(180deg, rgba(180, 60, 60, 0.95), rgba(110, 30, 30, 0.95));
}
.skill-btn.heal-btn {
  background: linear-gradient(180deg, rgba(60, 160, 80, 0.95), rgba(30, 100, 50, 0.95));
}

#mob-menu {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(180deg, rgba(80, 60, 130, 0.95), rgba(40, 25, 75, 0.95));
  border: 2px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  font-size: 22px;
  z-index: 50;
  pointer-events: auto;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* ==========================
   MOBILE LAYOUT
   ========================== */
@media (max-width: 850px), (orientation: portrait) {
  body {
    padding: 0;
    margin: 0;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    overscroll-behavior: none;
  }

  #game {
    width: 100vw;
    height: 100vh;
    border: none;
    border-radius: 0;
    box-shadow: none;
    display: flex;
    flex-direction: column;
  }

  #topbar {
    font-size: 10px;
    padding: 5px 8px;
    flex: 0 0 auto;
    flex-wrap: wrap;
    gap: 4px;
  }

  #player-info { font-size: 10px; }
  #location-info { display: none; }

  #main-area {
    flex: 1 1 auto;
    width: 100vw;
    height: auto;
    background: #000;
    position: relative;
    overflow: hidden;
  }

  #canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain;
    cursor: default;
  }

  #bottombar {
    flex: 0 0 auto;
    padding: 4px 8px;
  }

  #message-log {
    height: 36px;
    font-size: 9px;
    padding: 3px 6px;
  }

  #actions { display: none; }

  .mobile-only { display: flex; }
  #joystick.mobile-only { display: block; }
  #mob-menu.mobile-only { display: block; }

  #modal {
    min-width: 0;
    width: 92vw;
    max-width: 92vw;
    max-height: 80vh;
    padding: 14px;
    font-size: 13px;
  }

  .modal-title { font-size: 16px; }
  .item-row { padding: 6px; font-size: 12px; }
  .item-actions button { padding: 6px 10px; font-size: 12px; }
  .class-card { width: 90%; max-width: 320px; margin: 6px auto; display: block; }
  .region-card { padding: 8px; }
}

@media (orientation: portrait) and (max-width: 850px) {
  /* In portrait, joystick/skills should not overlap each other too much */
  #joystick { bottom: 30px; left: 12px; width: 100px; height: 100px; }
  #stick { width: 46px; height: 46px; }
  #skill-bar { bottom: 30px; right: 12px; gap: 6px; }
  .skill-btn { width: 48px; height: 48px; font-size: 14px; }
}
