:root {
  --ink: #f3e9d2;
  --ink-dim: #c9b89a;
  --panel: rgba(18, 14, 12, 0.88);
  --panel-solid: #1a1512;
  --line: #8a6a3a;
  --line-soft: rgba(138, 106, 58, 0.45);
  --gold: #ffc857;
  --gold-dim: #d4a017;
  --hp: #e74c3c;
  --hp-glow: rgba(231, 76, 60, 0.45);
  --mp: #3498db;
  --mp-glow: rgba(52, 152, 219, 0.4);
  --xp: #f1c40f;
  --xp-glow: rgba(241, 196, 15, 0.35);
  --safe: #2ecc71;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.55);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #12100e;
  color: var(--ink);
  font-family: "Sarabun", "Noto Sans Thai", system-ui, sans-serif;
  user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

#game {
  position: fixed;
  inset: 0;
  display: block;
}

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

#hud > * { position: absolute; }

/* ========== STATS (ซ้ายบน) ========== */
#stats {
  top: 12px;
  left: 12px;
  width: 228px;
  padding: 12px 14px;
  background: linear-gradient(145deg, rgba(28, 22, 18, 0.94), rgba(18, 14, 12, 0.92));
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 200, 87, 0.08);
  backdrop-filter: blur(8px);
}

#lvl {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 8px;
  color: var(--gold);
  letter-spacing: 0.3px;
  text-shadow: 0 0 12px rgba(255, 200, 87, 0.35);
}

.bar {
  position: relative;
  height: 16px;
  margin: 5px 0;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.bar i {
  display: block;
  height: 100%;
  width: 100%;
  transition: width 0.18s ease-out;
  border-radius: 3px;
  position: relative;
}

.bar i::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.22) 0%, transparent 55%);
  pointer-events: none;
}

.bar.hp i {
  background: linear-gradient(90deg, #c0392b, #e74c3c 60%, #ff6b5a);
  box-shadow: 0 0 10px var(--hp-glow);
}

.bar.mp i {
  background: linear-gradient(90deg, #2471a3, #3498db 60%, #5dade2);
  box-shadow: 0 0 10px var(--mp-glow);
}

.bar.xp i {
  background: linear-gradient(90deg, #b7950b, #f1c40f 60%, #f7dc6f);
  box-shadow: 0 0 8px var(--xp-glow);
}

.bar span {
  position: absolute;
  inset: 0;
  text-align: center;
  font-size: 10px;
  font-weight: 600;
  line-height: 16px;
  text-shadow: 0 1px 2px #000, 0 0 4px #000;
  letter-spacing: 0.2px;
}

#gold {
  font-size: 12.5px;
  margin-top: 8px;
  color: var(--ink-dim);
  line-height: 1.45;
  letter-spacing: 0.2px;
}

/* ========== SIDE (ขวาบน: minimap + quest) ========== */
#side {
  top: 12px;
  right: 12px;
  width: 156px;
}

#side > * { position: static; }

#mini {
  display: block;
  width: 156px;
  height: 117px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #1a2218;
  box-shadow: var(--shadow), inset 0 0 20px rgba(0, 0, 0, 0.4);
}

#quest {
  margin-top: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.45;
  background: linear-gradient(145deg, rgba(28, 22, 18, 0.94), rgba(18, 14, 12, 0.92));
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  color: var(--ink-dim);
}

/* ========== CHAT ========== */
#chat {
  left: 12px;
  bottom: 64px;
  width: min(340px, 58vw);
  padding: 8px 12px;
  font-size: 12.5px;
  line-height: 1.5;
  max-height: 160px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  background: linear-gradient(180deg, transparent, rgba(12, 10, 8, 0.75) 30%);
  border: none;
  border-radius: var(--radius-sm);
  text-shadow: 0 1px 3px #000;
}

#chat b { color: #8ec5ff; }
#chat .sys { color: var(--gold); }

/* ========== SKILL BUTTONS ========== */
#skills {
  right: 12px;
  bottom: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 220px;
  justify-content: flex-end;
  pointer-events: auto;
}

#skills button {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  border: 2px solid var(--line);
  background: linear-gradient(160deg, #3a2a18 0%, #241a12 100%);
  color: var(--ink);
  font: 700 15px inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 200, 87, 0.12);
  transition: transform 0.1s, background 0.15s, border-color 0.15s, box-shadow 0.15s;
  position: relative;
  overflow: hidden;
}

#skills button::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 50%);
  pointer-events: none;
}

#skills button small {
  font-size: 10px;
  font-weight: 500;
  opacity: 0.9;
  margin-top: 1px;
}

#skills button:hover {
  border-color: var(--gold-dim);
  background: linear-gradient(160deg, #4a3520 0%, #2e2218 100%);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5), 0 0 12px rgba(255, 200, 87, 0.15);
}

#skills button:active {
  transform: scale(0.94);
  background: #5b3d1e;
}

#skills button:focus-visible,
#panel button:focus-visible,
#reset:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* ========== HELP ========== */
#help {
  left: 12px;
  bottom: 12px;
  padding: 6px 10px;
  font-size: 11px;
  max-width: calc(100vw - 360px);
  pointer-events: auto;
  background: rgba(18, 14, 12, 0.75);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  color: var(--ink-dim);
  line-height: 1.4;
}

#reset {
  margin-left: 8px;
  font-size: 10px;
  background: rgba(0, 0, 0, 0.4);
  color: var(--ink-dim);
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  padding: 2px 7px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#reset:hover {
  background: rgba(192, 57, 43, 0.35);
  color: #f5b7b1;
  border-color: #c0392b;
}

/* ========== PANEL (dialog) ========== */
#panel {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(340px, 92vw);
  max-height: 82vh;
  overflow: auto;
  padding: 18px 20px;
  pointer-events: auto;
  font-size: 14px;
  background: linear-gradient(160deg, #2a2118 0%, #1a1510 100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.65), 0 0 0 1px rgba(255, 200, 87, 0.08), inset 0 1px 0 rgba(255, 200, 87, 0.1);
  backdrop-filter: blur(12px);
  line-height: 1.5;
}

#panel[hidden] { display: none; }

#panel .gold {
  font-size: 12.5px;
  margin: 6px 0 10px;
  color: var(--gold);
}

#panel button {
  display: block;
  width: 100%;
  margin: 7px 0;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(160deg, #4a3319 0%, #352414 100%);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#panel button:hover {
  background: linear-gradient(160deg, #5c4020 0%, #423018 100%);
  border-color: var(--gold-dim);
}

#panel button:active {
  transform: scale(0.98);
}

/* ========== FULLSCREEN / ROTATE / JOYSTICK ========== */
#fs {
  left: calc(12px + env(safe-area-inset-left, 0px));
  top: calc(148px + env(safe-area-inset-top, 0px));
  pointer-events: auto;
  font: 11px inherit;
  padding: 5px 10px;
  background: rgba(18, 14, 12, 0.85);
  color: var(--ink-dim);
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#fs:hover {
  background: rgba(40, 30, 20, 0.95);
  color: var(--ink);
}

#rot {
  display: none;
  left: 50%;
  bottom: calc(160px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  width: max-content;
  max-width: 90vw;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 12.5px;
  background: rgba(18, 14, 12, 0.92);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  pointer-events: auto;
  box-shadow: var(--shadow);
}

#rot button {
  font: inherit;
  background: rgba(0, 0, 0, 0.35);
  color: var(--ink);
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  padding: 3px 8px;
  cursor: pointer;
}

@media (orientation: portrait) and (max-width: 700px) {
  #rot:not(.off) { display: flex; }
}

#jt {
  left: calc(12px + env(safe-area-inset-left, 0px));
  top: calc(180px + env(safe-area-inset-top, 0px));
  pointer-events: auto;
  font: 11px inherit;
  padding: 5px 10px;
  background: rgba(18, 14, 12, 0.85);
  color: var(--ink-dim);
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#jt:hover {
  background: rgba(40, 30, 20, 0.95);
  color: var(--ink);
}

#joy {
  display: none;
  left: calc(20px + env(safe-area-inset-left, 0px));
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  width: 128px;
  height: 128px;
  border-radius: 50%;
  background: rgba(18, 14, 12, 0.5);
  border: 2px solid var(--line-soft);
  pointer-events: auto;
  touch-action: none;
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.4);
}

#knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255, 220, 160, 0.75), rgba(180, 140, 80, 0.55));
  border: 2px solid var(--line);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.4);
}

body.joy-on #joy { display: block; }
body.joy-on #help { display: none; }

body.joy-on #chat {
  left: calc(160px + env(safe-area-inset-left, 0px));
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  width: min(300px, calc(100vw - 390px));
}

/* ========== RESPONSIVE ========== */
@media (max-width: 640px) {
  #help { display: none; }
  #stats { width: 178px; padding: 10px 12px; }
  #side { width: 118px; }
  #mini { width: 118px; height: 88px; }
  #skills { max-width: 200px; }
  #skills button { width: 46px; height: 46px; }
  #panel { max-height: 80vh; }
  #chat { width: min(320px, calc(100vw - 250px)); }
}

@media (orientation: landscape) and (max-height: 520px) {
  #stats {
    width: 172px;
    padding: 6px 10px;
  }
  #lvl { font-size: 12.5px; margin-bottom: 4px; }
  .bar { height: 12px; margin: 3px 0; }
  .bar span { font-size: 9px; line-height: 12px; }
  #gold { font-size: 11px; margin-top: 4px; }
  #fs { top: calc(112px + env(safe-area-inset-top, 0px)); }
  #side { width: 112px; }
  #mini { width: 112px; height: 84px; }
  #quest { font-size: 11px; padding: 5px 7px; }
  #chat {
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    width: min(300px, 34vw);
    font-size: 11px;
    max-height: 80px;
  }
  #help { display: none; }
  #skills { max-width: 190px; gap: 4px; }
  #skills button { width: 42px; height: 42px; font-size: 13px; }
  #panel {
    width: min(400px, 62vw);
    max-height: 88vh;
    font-size: 13px;
    padding: 12px 14px;
  }
  #panel button { padding: 8px; margin: 5px 0; }
  #joy { width: 104px; height: 104px; }
  #jt { top: calc(140px + env(safe-area-inset-top, 0px)); }
}

@media (orientation: portrait) {
  body.joy-on #chat {
    left: calc(12px + env(safe-area-inset-left, 0px));
    bottom: calc(160px + env(safe-area-inset-bottom, 0px));
    width: min(320px, 72vw);
  }
  body.joy-on #rot {
    bottom: calc(260px + env(safe-area-inset-bottom, 0px));
  }
}

/* scrollbar for panel */
#panel::-webkit-scrollbar { width: 6px; }
#panel::-webkit-scrollbar-track { background: transparent; }
#panel::-webkit-scrollbar-thumb {
  background: var(--line-soft);
  border-radius: 3px;
}
