:root {
  --bg: #fff7e6;
  --paper: #ffffff;
  --ink: #2d2a32;
  --muted: #6b6772;
  --accent: #ff5a36;
  --accent-dark: #e0421f;
  --yellow: #ffc233;
  --green: #3ecf8e;
  --blue: #3b82f6;
  --purple: #a855f7;
  --card: #ffffff;
  --line: #f0e4cc;
  --radius: 18px;
  --shadow: 0 6px 18px rgba(80, 50, 0, 0.08);
  --header-h: 52px;
  --sidebar-w: 290px;
  --font-display: 'Fredoka', 'Nunito', system-ui, sans-serif;
  --font-body: 'Nunito', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--ink);
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- Header (minimal) ---------- */
.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  height: var(--header-h);
  background: var(--paper);
  border-bottom: 2px solid var(--line);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); }
.brand-emoji { font-size: 24px; }
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: 20px; letter-spacing: 0.2px; }

.levels { display: flex; justify-content: center; gap: 6px; }
.level-chip {
  border: 2px solid var(--line);
  background: #fffdf7;
  color: var(--muted);
  border-radius: 999px;
  padding: 5px 12px;
  font-weight: 800;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: transform 0.12s, background 0.12s;
}
.level-chip small { font-weight: 700; opacity: 0.75; }
.level-chip:hover { transform: translateY(-1px); }
.level-chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.level-chip.active small { opacity: 0.8; }

.stats { display: flex; align-items: center; gap: 8px; }
.rank { font-family: var(--font-display); font-weight: 600; font-size: 14px; white-space: nowrap; }
.xpbar { width: 90px; height: 10px; background: var(--line); border-radius: 999px; overflow: hidden; }
.xpfill { height: 100%; width: 0; background: linear-gradient(90deg, var(--yellow), var(--accent)); border-radius: 999px; transition: width 0.4s; }
.iconbtn {
  border: 2px solid var(--line);
  background: #fffdf7;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 14px;
  font-weight: 800;
  display: flex;
  gap: 4px;
  align-items: center;
}
.iconbtn:hover { background: #fff3d6; }
#menuBtn { display: none; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: calc(100vh - var(--header-h));
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--paper);
  border-right: 2px solid var(--line);
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}
.sidebar-top { position: sticky; top: 0; background: var(--paper); padding: 10px 12px; z-index: 2; border-bottom: 1px solid var(--line); }
#search {
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 700;
  outline: none;
}
#search:focus { border-color: var(--accent); }
.animal-list { padding: 6px 10px 30px; }
.habitat-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14px;
  color: var(--muted);
  margin: 12px 4px 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.habitat-title .count { font-size: 12px; font-weight: 800; background: var(--line); border-radius: 999px; padding: 1px 8px; }
.animal-card {
  width: 100%;
  display: grid;
  grid-template-columns: 52px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border: 2px solid transparent;
  border-radius: 14px;
  background: transparent;
  text-align: left;
  transition: background 0.12s, transform 0.12s, border-color 0.12s;
}
.animal-card:hover { background: #fff5e0; transform: translateX(2px); }
.animal-card.active { background: #ffe9d9; border-color: var(--accent); }
.animal-card.done { }
.thumb { width: 52px; height: 52px; border-radius: 12px; background: #fffbf2; border: 1px solid var(--line); overflow: hidden; }
.thumb svg { width: 100%; height: 100%; display: block; }
.animal-name { display: block; font-family: var(--font-display); font-weight: 600; font-size: 15px; line-height: 1.1; }
.animal-meta { display: block; font-size: 11px; color: var(--muted); font-weight: 700; margin-top: 3px; }
.stars { font-size: 13px; letter-spacing: -1px; white-space: nowrap; }
.stars .off { opacity: 0.2; }

/* ---------- Tutorial panel (fixed size, fills the right side) ---------- */
.tutorial {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 10px;
  padding: 12px 16px 14px;
  overflow: hidden;
}
.t-head { display: flex; align-items: center; gap: 12px; min-height: 36px; }
.t-title { font-family: var(--font-display); font-weight: 700; font-size: 24px; display: flex; align-items: center; gap: 8px; }
.t-title .emoji { font-size: 28px; }
.pill { font-size: 12px; font-weight: 800; padding: 3px 10px; border-radius: 999px; background: var(--line); color: var(--muted); }
.pill.level { background: var(--ink); color: #fff; }
.t-spacer { flex: 1; }
.stepdots { display: flex; gap: 5px; align-items: center; }
.stepdots span { width: 10px; height: 10px; border-radius: 50%; background: var(--line); transition: transform 0.15s, background 0.15s; }
.stepdots span.done { background: var(--green); }
.stepdots span.cur { background: var(--accent); transform: scale(1.35); }

.stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 16px;
  min-height: 0;
}
.board-wrap { display: flex; align-items: center; justify-content: center; min-height: 0; min-width: 0; position: relative; }
.board {
  background: var(--paper);
  border-radius: 22px;
  box-shadow: var(--shadow), inset 0 0 0 3px #fff, inset 0 0 0 5px var(--line);
  background-image: radial-gradient(#e9dfcb 1px, transparent 1px);
  background-size: 22px 22px;
  background-position: 11px 11px;
  position: relative;
  overflow: hidden;
}
.board svg { width: 100%; height: 100%; display: block; }
.board-tools { position: absolute; top: 12px; right: 12px; display: flex; gap: 6px; }
.tool {
  border: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
}
.tool.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.board-badge {
  position: absolute; left: 14px; top: 12px;
  background: var(--accent); color: #fff; font-family: var(--font-display); font-weight: 700;
  padding: 4px 12px; border-radius: 999px; font-size: 14px;
}

/* Instruction column */
.guide {
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 12px;
  min-height: 0;
}
.step-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
  min-height: 0;
}
.step-num { font-size: 12px; font-weight: 800; color: var(--accent); text-transform: uppercase; letter-spacing: 1px; }
.step-title { font-family: var(--font-display); font-weight: 700; font-size: 26px; line-height: 1.1; margin: 0; }
.step-tip { font-size: 16px; line-height: 1.45; margin: 0; color: #3a3640; font-weight: 600; }
.step-hint { font-size: 13px; color: var(--muted); font-weight: 700; margin-top: auto; display: flex; gap: 6px; align-items: flex-start; }
.fact { background: #fff3d6; border-radius: 12px; padding: 8px 10px; font-size: 13px; font-weight: 700; color: #6a4b00; }

.nav { display: grid; grid-template-columns: 1fr 1.5fr; gap: 10px; }
.navbtn {
  border: none;
  border-radius: 18px;
  padding: 18px 12px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.12);
  transition: transform 0.08s, box-shadow 0.08s, filter 0.12s;
}
.navbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12); }
.navbtn.prev { background: #fff; color: var(--ink); border: 3px solid var(--line); }
.navbtn.prev:hover { background: #fff8ea; }
.navbtn.next { background: var(--accent); color: #fff; }
.navbtn.next:hover { filter: brightness(1.06); }
.navbtn.next.finish { background: var(--green); color: #063d26; }
.navbtn[disabled] { opacity: 0.4; cursor: not-allowed; transform: none; }

/* Welcome */
.welcome {
  grid-row: 1 / -1;
  display: grid;
  place-items: center;
  overflow: auto;
}
.welcome-card {
  max-width: 720px;
  width: 100%;
  background: var(--card);
  border-radius: 28px;
  box-shadow: var(--shadow);
  padding: 34px 36px;
  text-align: center;
}
.welcome h1 { font-family: var(--font-display); font-weight: 700; font-size: 40px; margin: 0 0 6px; }
.welcome p { font-size: 17px; color: var(--muted); font-weight: 600; margin: 0 0 18px; line-height: 1.5; }
.welcome .how { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 18px 0; text-align: left; }
.how div { background: #fff7e6; border-radius: 16px; padding: 14px; font-weight: 700; font-size: 14px; line-height: 1.4; }
.how b { display: block; font-family: var(--font-display); font-size: 16px; margin-bottom: 4px; }
.summary { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; margin-top: 8px; }
.summary div { background: #f6f3ff; border-radius: 14px; padding: 10px 16px; font-weight: 800; font-size: 14px; }
.summary b { display: block; font-size: 22px; font-family: var(--font-display); }
.bigbtn {
  background: var(--accent); color: #fff; border: none; border-radius: 18px; padding: 16px 26px;
  font-family: var(--font-display); font-weight: 700; font-size: 22px; box-shadow: 0 4px 0 rgba(0,0,0,0.15);
}
.bigbtn:active { transform: translateY(3px); box-shadow: none; }
.bigbtn.alt { background: #fff; color: var(--ink); border: 3px solid var(--line); box-shadow: none; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(40, 30, 10, 0.45); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card {
  background: #fff; border-radius: 26px; padding: 28px; width: min(560px, 100%); max-height: 90vh; overflow: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25); text-align: center; animation: pop 0.25s ease-out;
}
@keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h2 { font-family: var(--font-display); font-weight: 700; font-size: 34px; margin: 4px 0 6px; }
.modal p { color: var(--muted); font-weight: 700; margin: 0 0 12px; font-size: 16px; }
.bigstars { font-size: 46px; letter-spacing: 4px; margin: 6px 0; }
.bigstars .off { opacity: 0.18; }
.reward { display: inline-block; background: #fff3d6; color: #6a4b00; font-weight: 800; padding: 6px 14px; border-radius: 999px; margin: 4px; }
.modal-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 16px; }
.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; text-align: center; margin-top: 10px; }
.badge { background: #fff7e6; border-radius: 16px; padding: 12px 8px; font-size: 12px; font-weight: 800; }
.badge .b-emoji { font-size: 34px; display: block; }
.badge.locked { filter: grayscale(1); opacity: 0.45; }
.badge small { display: block; font-weight: 600; color: var(--muted); margin-top: 3px; }
.new-badge { background: var(--purple); color: #fff; padding: 8px 14px; border-radius: 12px; font-weight: 800; display: inline-block; margin: 4px; }
.settings { text-align: left; display: grid; gap: 10px; }
.settings label { font-weight: 800; font-size: 14px; }
.settings input { width: 100%; border: 2px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 15px; font-weight: 700; }
.danger { color: #b3261e; background: #fde8e6; border: none; border-radius: 12px; padding: 10px 14px; font-weight: 800; }

.credit { font-size: 12px; color: var(--muted); font-weight: 600; margin: 14px 0 0; }
.credit a { color: var(--muted); }

.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; opacity: 0; transition: opacity 0.4s; }

/* Draw-on animation for the current step */
.current path { animation: none; }
.current path.draw { stroke-dasharray: var(--len); stroke-dashoffset: var(--len); animation: draw 0.9s ease-out forwards; animation-delay: var(--delay, 0s); }
@keyframes draw { to { stroke-dashoffset: 0; } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px; font-weight: 800; z-index: 25;
  animation: toast 2.4s ease forwards;
}
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, 10px); } 12% { opacity: 1; transform: translate(-50%, 0); } 85% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
  .stage { grid-template-columns: minmax(0, 1fr) 250px; }
  .step-title { font-size: 22px; }
  .navbtn { font-size: 19px; padding: 14px 10px; }
}
@media (max-width: 860px) {
  body { overflow: auto; height: auto; }
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar {
    position: fixed; top: var(--header-h); left: 0; bottom: 0; width: min(320px, 90vw);
    transform: translateX(-105%); transition: transform 0.2s; z-index: 10; box-shadow: 0 0 40px rgba(0,0,0,0.2);
  }
  .sidebar.open { transform: translateX(0); }
  #menuBtn { display: flex; }
  .rank { display: none; }
  .xpbar { width: 60px; }
  .level-chip small { display: none; }
  .tutorial { height: auto; overflow: visible; }
  .stage { grid-template-columns: 1fr; }
  .board-wrap { aspect-ratio: 1; }
  .guide { grid-template-rows: auto auto; }
  .welcome .how { grid-template-columns: 1fr; }
}
