/* ==========================================================================
   🐺 หมาป่าหลงทาง (Wolf Maze) — Dark RPG Glassmorphism Theme
   Designed to match Nisit Zombie aesthetic:
   - Deep dark slate canvas (#080c16 / #0f172a)
   - Kanit typography with bold gaming hierarchy
   - Gold & amber borders and bevel highlights
   - Tactile 3D RPG action buttons
   ========================================================================== */

:root {
  --bg-deep: #080c16;
  --bg-surface: #0f172a;
  --bg-card: rgba(18, 26, 44, 0.9);
  --border-card: rgba(251, 191, 36, 0.28);
  --border-card-hover: rgba(251, 191, 36, 0.65);
  --glow-gold: rgba(245, 158, 11, 0.25);

  --move: #0ea5e9;
  --move-dark: #0284c7;
  --turn: #a855f7;
  --turn-dark: #7e22ce;
  --loop: #f59e0b;
  --loop-dark: #d97706;

  --gold-300: #fde68a;
  --gold-400: #fbbf24;
  --gold-500: #f59e0b;

  --text-main: #f8fafc;
  --text-body: #cbd5e1;
  --text-muted: #94a3b8;
  --text-dim: #64748b;

  --radius: 16px;
  --font-thai: 'Kanit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'Fira Code', ui-monospace, SFMono-Regular, Menlo, monospace;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-thai);
  background-color: var(--bg-deep);
  background-image: 
    radial-gradient(ellipse at 50% 0%, rgba(30, 58, 138, 0.28) 0%, transparent 60%),
    radial-gradient(ellipse at 85% 90%, rgba(245, 158, 11, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 15% 75%, rgba(168, 85, 247, 0.08) 0%, transparent 40%);
  background-attachment: fixed;
  color: var(--text-main);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

button, a { font-family: inherit; }

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Topbar */
.topbar {
  background: var(--bg-card);
  border: 1.5px solid var(--border-card);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(14px);
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.title-box { display: flex; align-items: center; gap: 14px; }

.logo {
  font-size: 2rem;
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.25), rgba(168, 85, 247, 0.25));
  border: 1.5px solid var(--gold-400);
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 14px var(--glow-gold);
}

.topbar h1 {
  font-size: 1.35rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: -0.2px;
}

.topbar p {
  font-size: 0.88rem;
  color: var(--text-muted);
}

.top-actions { display: flex; gap: 10px; align-items: center; }

.btn-home {
  background: rgba(251, 191, 36, 0.1);
  border: 1px solid rgba(251, 191, 36, 0.35);
  color: var(--gold-300);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 8px 14px;
  border-radius: 10px;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.btn-home:hover {
  background: rgba(251, 191, 36, 0.22);
  color: #fff;
  transform: translateY(-1px);
}

.btn-icon {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #fff;
  border-radius: 10px;
  width: 40px;
  height: 40px;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.12);
}

/* Level tabs */
.level-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.level-tab {
  background: var(--bg-card);
  border: 1.5px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 700;
  font-size: 0.88rem;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.2s ease;
  backdrop-filter: blur(12px);
}

.level-tab:hover {
  border-color: rgba(251, 191, 36, 0.4);
  background: rgba(26, 38, 64, 0.95);
  transform: translateY(-2px);
}

.level-tab.active {
  border-color: var(--gold-400);
  color: #fff;
  background: linear-gradient(145deg, rgba(26, 38, 66, 0.95), rgba(40, 24, 60, 0.85));
  box-shadow: 0 0 20px rgba(245, 158, 11, 0.25);
}

.level-tab .tab-stars {
  font-size: 0.85rem;
  letter-spacing: 1px;
  color: var(--gold-400);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(340px, 1fr);
  gap: 18px;
  align-items: start;
}

.stage-card, .panel {
  background: var(--bg-card);
  border: 1.5px solid var(--border-card);
  border-radius: var(--radius);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(14px);
  padding: 16px;
}

.stage-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.mission {
  display: flex;
  gap: 10px;
  align-items: center;
  background: rgba(10, 15, 28, 0.75);
  border: 1px solid rgba(251, 191, 36, 0.25);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.92rem;
  color: var(--gold-300);
}

.mission-tag {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
  font-weight: 800;
  font-size: 0.72rem;
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
}

.canvas-box {
  position: relative;
  display: flex;
  justify-content: center;
  background: #090e18;
  border: 2px solid rgba(251, 191, 36, 0.25);
  border-radius: 14px;
  padding: 10px;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.8), 0 8px 24px rgba(0, 0, 0, 0.5);
}

#mazeCanvas {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.toast {
  position: absolute;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  max-width: 92%;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--gold-400);
  color: #fff;
  padding: 8px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
  text-align: center;
  pointer-events: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.6);
}

.toast.hidden { display: none; }

.run-bar { display: flex; gap: 10px; flex-wrap: wrap; }

.btn-run {
  flex: 1;
  min-height: 48px;
  border: 1px solid #34d399;
  border-radius: 12px;
  cursor: pointer;
  text-decoration: none;
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  color: #fff;
  font-weight: 800;
  font-size: 1.05rem;
  box-shadow: 0 4px 14px rgba(16, 185, 129, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
  transition: all 0.15s ease;
}

.btn-run:hover:not(:disabled) {
  background: linear-gradient(135deg, #34d399 0%, #10b981 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(16, 185, 129, 0.55);
}

.btn-run:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-ghost {
  min-height: 48px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-muted);
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.hidden { display: none !important; }

/* Code side */
.code-card { display: flex; flex-direction: column; gap: 14px; }

.panel h3 {
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  margin-bottom: 10px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}

.counter {
  font-size: 0.82rem;
  font-weight: 700;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 3px 10px;
  border-radius: 999px;
  color: var(--gold-400);
  white-space: nowrap;
}

.counter.full {
  background: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
  color: #f87171;
}

.palette { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.pal-block {
  min-height: 46px;
  border: 1px solid;
  border-radius: 10px;
  color: #fff;
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
  text-align: left;
  padding: 0 14px;
  display: flex;
  align-items: center;
  transition: all 0.15s ease;
}

.pal-block:hover:not(:disabled) {
  transform: translateY(-2px);
  filter: brightness(1.15);
}

.pal-block:disabled { opacity: 0.35; cursor: not-allowed; }

.b-move {
  background: linear-gradient(135deg, rgba(14, 165, 233, 0.25), rgba(2, 132, 199, 0.35));
  border-color: #38bdf8;
  color: #bae6fd;
}

.b-turn {
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.25), rgba(126, 34, 206, 0.35));
  border-color: #c084fc;
  color: #e9d5ff;
}

.b-loop {
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.25), rgba(217, 119, 6, 0.35));
  border-color: #fbbf24;
  color: #fde68a;
}

.program {
  min-height: 120px;
  background: rgba(10, 15, 26, 0.7);
  border: 1.5px dashed rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.program.locked { opacity: 0.85; pointer-events: none; }
.program-empty { color: var(--text-dim); font-size: 0.88rem; text-align: center; padding: 30px 0; }
.program-tip { font-size: 0.78rem; color: var(--text-muted); margin-top: 8px; }

.prog-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  border-radius: 8px;
  padding: 6px 6px 6px 12px;
  width: fit-content;
  min-width: 160px;
  background: rgba(20, 30, 50, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.prog-block .num { opacity: 0.6; font-size: 0.75rem; margin-right: 6px; font-family: var(--font-mono); }
.prog-block.active, .prog-loop.active > .loop-head {
  outline: 2px solid var(--gold-400);
  outline-offset: 2px;
  filter: brightness(1.2);
}
.prog-block.crashed { outline: 2px solid #ef4444; outline-offset: 2px; }

.del {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--text-muted);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.1s;
}

.del:hover {
  background: rgba(239, 68, 68, 0.25);
  color: #fca5a5;
}

.prog-loop {
  background: rgba(245, 158, 11, 0.08);
  border: 1.5px solid var(--loop);
  border-radius: 10px;
  overflow: hidden;
}

.prog-loop.target {
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.35);
}

.loop-head {
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.3), rgba(217, 119, 6, 0.4));
  border-bottom: 1px solid rgba(245, 158, 11, 0.3);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  cursor: pointer;
  flex-wrap: wrap;
}

.loop-head .count-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loop-head .count {
  background: rgba(0, 0, 0, 0.5);
  color: var(--gold-300);
  min-width: 28px;
  text-align: center;
  border-radius: 6px;
  padding: 2px 6px;
  font-family: var(--font-mono);
  font-weight: 700;
}

.loop-head .target-label {
  font-size: 0.72rem;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--gold-300);
}

.loop-body {
  padding: 8px 8px 8px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  border-left: 4px solid var(--loop);
  margin-left: 6px;
}

.loop-empty { font-size: 0.8rem; color: var(--gold-300); opacity: 0.8; padding: 6px 0; }

.py {
  background: #090e18;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #7dd3fc;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.6;
  padding: 12px 14px;
  border-radius: 10px;
  min-height: 60px;
  white-space: pre;
  overflow-x: auto;
}

.goal-stars { font-size: 0.78rem; color: var(--gold-400); text-align: right; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 7, 14, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 1000;
}

.modal-card {
  background: var(--bg-card);
  border: 2px solid var(--gold-400);
  border-radius: 20px;
  max-width: 460px;
  width: 100%;
  padding: 28px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 0 50px rgba(245, 158, 11, 0.35), 0 20px 40px rgba(0, 0, 0, 0.7);
}

.stars-big {
  font-size: 2.8rem;
  letter-spacing: 4px;
  filter: drop-shadow(0 4px 12px rgba(251, 191, 36, 0.5));
}

.modal-card h2 { font-size: 1.4rem; color: #fff; font-weight: 800; }
.modal-card p { color: var(--text-body); font-size: 0.95rem; }

.learned {
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 10px;
  padding: 10px;
  color: #38bdf8;
  font-size: 0.88rem;
}

.modal-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 6px; }
.modal-actions > * { flex: 1; }

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

@media (max-width: 560px) {
  .topbar { flex-direction: column; align-items: flex-start; }
  .top-actions { width: 100%; justify-content: space-between; }
  .palette { grid-template-columns: 1fr 1fr; }
  .prog-block { min-width: 0; width: 100%; }
}
