/* ============================================================
   PyQuest — Styles
   Mobile-first. On wide screens the app sits in a phone frame.
   ============================================================ */

:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-2: #f0f1f8;
  --text: #1f2233;
  --text-soft: #5b6076;
  --muted: #9aa0b5;
  --border: #e6e8f2;
  --brand: #5b6cff;
  --brand-2: #8a5bff;
  --brand-soft: #eef0ff;
  --good: #20b574;
  --good-soft: #e3f7ee;
  --bad: #ef4d6a;
  --bad-soft: #fde7eb;
  --warn: #f5a524;
  --gold: #f6c544;
  --streak: #ff7a3d;
  --code-bg: #1f2330;
  --code-text: #e7e9f5;
  --shadow: 0 10px 30px rgba(31, 34, 51, 0.10);
  --shadow-sm: 0 3px 10px rgba(31, 34, 51, 0.08);
  --radius: 18px;
  --radius-sm: 12px;
  --maxw: 460px;
  --nav-h: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1018;
    --surface: #171a26;
    --surface-2: #1e2230;
    --text: #f2f3fa;
    --text-soft: #b6bbd0;
    --muted: #757b95;
    --border: #272c3d;
    --brand-soft: #20243a;
    --good-soft: #14352a;
    --bad-soft: #3a1e26;
    --code-bg: #0b0d15;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    --shadow-sm: 0 3px 10px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input { font-family: inherit; }

/* ---- App shell: phone frame on desktop ---- */
#app {
  position: relative;
  width: 100%;
  max-width: var(--maxw);
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}
@media (min-width: 640px) {
  body { display: flex; align-items: center; justify-content: center; padding: 24px 0;
    background: radial-gradient(1200px 600px at 50% -10%, var(--brand-soft), var(--bg) 60%); }
  #app {
    height: min(880px, calc(100dvh - 48px));
    min-height: unset;
    border-radius: 32px;
    box-shadow: var(--shadow);
    overflow: hidden;
    border: 1px solid var(--border);
  }
}

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.topbar .logo { font-weight: 800; font-size: 18px; letter-spacing: -0.3px; }
.topbar .logo .dot { color: var(--brand); }
.topbar .spacer { flex: 1; }
.stat-chip {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--surface-2); border-radius: 999px;
  padding: 6px 11px; font-weight: 700; font-size: 13px;
}
.stat-chip.streak { color: var(--streak); }
.stat-chip.xp { color: var(--brand); }

/* ---- Scrollable view area ---- */
.view {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--nav-h) + 24px);
}
.view.hidden { display: none; }

/* ---- Level bar ---- */
.level-card {
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; border-radius: var(--radius); padding: 16px 18px;
  box-shadow: var(--shadow-sm); margin-bottom: 18px;
}
.level-card .lv-row { display: flex; align-items: baseline; justify-content: space-between; }
.level-card .lv-title { font-weight: 800; font-size: 17px; }
.level-card .lv-sub { font-size: 12.5px; opacity: 0.9; }
.level-card .bar { height: 9px; border-radius: 999px; background: rgba(255,255,255,0.28); margin-top: 12px; overflow: hidden; }
.level-card .bar > span { display: block; height: 100%; background: #fff; border-radius: 999px; transition: width .5s ease; }

/* ---- Unit + lesson path ---- */
.unit { margin-bottom: 26px; }
.unit-head { display: flex; align-items: center; gap: 10px; margin: 6px 2px 14px; }
.unit-head .u-icon {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
  background: var(--brand-soft); font-size: 20px;
}
.unit-head .u-name { font-weight: 800; font-size: 16px; }
.unit-head .u-meta { font-size: 12px; color: var(--muted); }

.path { display: flex; flex-direction: column; align-items: center; gap: 0; }
.node-row { width: 100%; display: flex; justify-content: center; position: relative; }
.node-row .connector { position: absolute; top: -2px; height: 22px; width: 3px; background: var(--border); }
.lesson-node {
  display: flex; align-items: center; gap: 12px;
  width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 13px 15px; margin: 7px 0;
  box-shadow: var(--shadow-sm);
  transition: transform .12s ease, box-shadow .12s ease;
}
.lesson-node:active { transform: scale(0.99); }
.lesson-node .bubble {
  width: 42px; height: 42px; border-radius: 50%; flex: 0 0 auto;
  display: grid; place-items: center; font-size: 19px; font-weight: 800;
  background: var(--surface-2); color: var(--text-soft);
}
.lesson-node .l-title { font-weight: 700; font-size: 15px; }
.lesson-node .l-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.lesson-node .l-right { margin-left: auto; color: var(--muted); font-size: 18px; }

.lesson-node.done .bubble { background: var(--good); color: #fff; }
.lesson-node.current { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft), var(--shadow-sm); }
.lesson-node.current .bubble { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; }
.lesson-node.locked { opacity: 0.55; }
.lesson-node.locked .bubble { background: var(--surface-2); }
.lesson-node.locked .l-right::before { content: "🔒"; }

/* ---- Lesson player (overlay) ---- */
.player {
  position: absolute; inset: 0; z-index: 20;
  background: var(--bg); display: flex; flex-direction: column;
}
.player.hidden { display: none; }
.player-head { display: flex; align-items: center; gap: 12px; padding: 14px 16px; }
.player-head .close { font-size: 22px; color: var(--muted); width: 30px; }
.progress { flex: 1; height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--brand), var(--brand-2)); border-radius: 999px; transition: width .35s ease; }

.card-area { flex: 1; overflow-y: auto; padding: 8px 18px 18px; }
.q-kicker { font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--brand); margin-bottom: 8px; }
.q-title { font-size: 21px; font-weight: 800; line-height: 1.25; margin: 0 0 10px; letter-spacing: -0.3px; }
.q-body { font-size: 15.5px; line-height: 1.6; color: var(--text-soft); white-space: pre-wrap; }
.q-prompt { font-size: 16.5px; line-height: 1.5; font-weight: 600; white-space: pre-wrap; margin-bottom: 14px; }

/* code blocks */
.code-block {
  background: var(--code-bg); color: var(--code-text);
  border-radius: var(--radius-sm); padding: 14px 14px; margin: 14px 0;
  font-family: "SF Mono", ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 13.5px; line-height: 1.6; white-space: pre; overflow-x: auto;
}
.run-mini { display: inline-flex; align-items: center; gap: 6px;
  background: var(--brand-soft); color: var(--brand); font-weight: 700; font-size: 13px;
  padding: 7px 13px; border-radius: 999px; margin-top: -4px; }
.mini-out {
  margin: 8px 0 0; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px;
  font-family: ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; color: var(--text-soft);
}
.mini-out.hidden { display: none; }

/* choices (mc) */
.choices { display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }
.choice {
  text-align: left; background: var(--surface); border: 2px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px 16px; font-size: 15px; font-weight: 600;
  transition: border-color .1s, background .1s;
}
.choice:active { transform: scale(0.995); }
.choice.selected { border-color: var(--brand); background: var(--brand-soft); }
.choice.correct { border-color: var(--good); background: var(--good-soft); }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); }
.choice.disabled { pointer-events: none; }

/* fill in the blank */
.blank-code {
  background: var(--code-bg); color: var(--code-text); border-radius: var(--radius-sm);
  padding: 14px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px;
  line-height: 1.9; white-space: pre-wrap; word-break: break-word;
}
.blank-input {
  background: #fff8; color: #1a1c28; border: 2px solid var(--brand); border-radius: 8px;
  font-family: inherit; font-size: 14px; padding: 2px 8px; min-width: 70px; width: 84px;
  text-align: center; outline: none;
}

/* reorder */
.reorder-target {
  min-height: 50px; border: 2px dashed var(--border); border-radius: var(--radius-sm);
  padding: 8px; display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px;
  background: var(--surface-2);
}
.reorder-bank { display: flex; flex-direction: column; gap: 8px; }
.tile {
  background: var(--code-bg); color: var(--code-text);
  border-radius: 10px; padding: 11px 14px; font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 13.5px; white-space: pre; text-align: left; box-shadow: var(--shadow-sm);
}
.tile.placed { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.bank-empty { color: var(--muted); font-size: 13px; text-align: center; padding: 6px; }

/* code editor */
.editor-wrap { margin-top: 6px; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--border); }
.editor-bar { display: flex; align-items: center; gap: 8px; background: var(--surface-2); padding: 7px 10px; font-size: 12px; color: var(--muted); }
.editor-bar .dots { display: flex; gap: 5px; }
.editor-bar .dots i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.editor-bar .dots i:nth-child(1){ background:#ff5f57; } .editor-bar .dots i:nth-child(2){ background:#febc2e; } .editor-bar .dots i:nth-child(3){ background:#28c840; }
.code-editor {
  width: 100%; min-height: 150px; resize: vertical; border: none; outline: none;
  background: var(--code-bg); color: var(--code-text); padding: 14px;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 13.5px; line-height: 1.6;
  tab-size: 4; white-space: pre; overflow-wrap: normal;
}
.editor-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.btn-run { background: var(--surface-2); color: var(--text); font-weight: 700; font-size: 14px; padding: 11px 16px; border-radius: 12px; border: 1px solid var(--border); }
.btn-link { background: none; color: var(--muted); font-weight: 600; font-size: 13.5px; padding: 11px 6px; }
.console {
  margin-top: 12px; background: var(--code-bg); color: var(--code-text); border-radius: var(--radius-sm);
  padding: 12px 14px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px;
  line-height: 1.5; white-space: pre-wrap; min-height: 20px; max-height: 220px; overflow: auto;
}
.console.hidden { display: none; }
.console .c-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; display: block; margin-bottom: 6px; }
.console .err { color: #ff8da3; }
.console .spin { color: var(--brand); }

/* feedback footer */
.footer {
  border-top: 1px solid var(--border); background: var(--surface);
  padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
}
.feedback { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 14.5px; margin-bottom: 12px; min-height: 22px; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }
.feedback .em { font-size: 20px; }
.btn-primary {
  width: 100%; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  font-weight: 800; font-size: 16px; padding: 15px; border-radius: 15px; box-shadow: var(--shadow-sm);
  transition: transform .1s, opacity .1s; letter-spacing: 0.2px;
}
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.45; box-shadow: none; }
.btn-primary.good { background: var(--good); }
.btn-primary.bad { background: var(--bad); }

/* ---- Lesson complete ---- */
.complete { text-align: center; padding: 40px 24px; }
.complete .big { font-size: 64px; line-height: 1; margin-bottom: 8px; animation: pop .5s ease; }
.complete h2 { font-size: 26px; margin: 6px 0 4px; letter-spacing: -0.4px; }
.complete p { color: var(--text-soft); margin: 0 0 22px; }
.reward-row { display: flex; gap: 12px; justify-content: center; margin-bottom: 26px; flex-wrap: wrap; }
.reward {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; box-shadow: var(--shadow-sm); min-width: 96px;
}
.reward .r-val { font-size: 22px; font-weight: 800; }
.reward .r-lab { font-size: 12px; color: var(--muted); margin-top: 2px; }
.reward.xp .r-val { color: var(--brand); }
.reward.streak .r-val { color: var(--streak); }

@keyframes pop { 0%{transform:scale(0);} 70%{transform:scale(1.15);} 100%{transform:scale(1);} }

/* ---- Achievements ---- */
.section-title { font-size: 20px; font-weight: 800; margin: 4px 2px 16px; letter-spacing: -0.3px; }
.ach-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.ach {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 14px; text-align: center; box-shadow: var(--shadow-sm);
}
.ach .a-icon { font-size: 34px; filter: grayscale(1); opacity: .4; }
.ach.earned .a-icon { filter: none; opacity: 1; }
.ach .a-name { font-weight: 700; font-size: 13.5px; margin-top: 8px; }
.ach .a-desc { font-size: 11.5px; color: var(--muted); margin-top: 3px; line-height: 1.4; }
.ach.earned { border-color: var(--gold); }

/* ---- Profile ---- */
.profile-hero { text-align: center; padding: 14px 0 22px; }
.profile-hero .avatar {
  width: 84px; height: 84px; border-radius: 50%; margin: 0 auto 12px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  display: grid; place-items: center; font-size: 36px; box-shadow: var(--shadow-sm);
}
.profile-hero .p-level { font-size: 20px; font-weight: 800; }
.profile-hero .p-sub { color: var(--muted); font-size: 13px; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 22px; }
.stat-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow-sm); }
.stat-box .s-val { font-size: 24px; font-weight: 800; }
.stat-box .s-lab { font-size: 12px; color: var(--muted); margin-top: 3px; }
.btn-danger { width: 100%; background: var(--bad-soft); color: var(--bad); font-weight: 700; padding: 13px; border-radius: 13px; font-size: 14px; }
.help-text { font-size: 12.5px; color: var(--muted); line-height: 1.6; margin-top: 16px; text-align: center; }

/* ---- Bottom nav ---- */
.nav {
  position: absolute; bottom: 0; left: 0; right: 0; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; z-index: 10;
}
.nav button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); font-size: 11px; font-weight: 600; }
.nav button .ic { font-size: 21px; }
.nav button.active { color: var(--brand); }
.nav.hidden { display: none; }

/* ---- Toast ---- */
.toast-wrap { position: absolute; top: 14px; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 50; pointer-events: none; }
.toast {
  background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 999px;
  font-weight: 700; font-size: 13.5px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 8px;
  animation: toastIn .3s ease, toastOut .3s ease 2.7s forwards;
}
.toast .t-icon { font-size: 18px; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-14px); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-14px); } }

/* ---- Confetti ---- */
.confetti { position: absolute; top: -10px; width: 9px; height: 14px; opacity: 0.9; z-index: 40; pointer-events: none; border-radius: 2px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(900px) rotate(540deg); opacity: 0; } }

/* ---- Pyodide loading splash ---- */
.boot { position: absolute; inset: 0; background: var(--bg); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; z-index: 60; }
.boot.hidden { display: none; }
.boot .ring { width: 44px; height: 44px; border-radius: 50%; border: 4px solid var(--brand-soft); border-top-color: var(--brand); animation: spin 0.9s linear infinite; }
.boot .b-text { color: var(--text-soft); font-size: 13.5px; font-weight: 600; }
@keyframes spin { to { transform: rotate(360deg); } }

.empty-hint { color: var(--muted); font-size: 13px; text-align: center; padding: 18px; }

/* ============================================================
   Desktop two-pane layout (≥ 900px)
   Reflow the phone shell into a real web app: a left sidebar
   (brand + stats + vertical nav) and a wide content pane. The
   lesson-player overlay is confined to the content pane.
   CSS only — no JS/DOM changes.
   ============================================================ */
@media (min-width: 900px) {
  body {
    display: block;
    padding: 0;
    background: radial-gradient(1100px 700px at 80% -10%, var(--brand-soft), var(--bg) 55%);
  }

  #app {
    max-width: 1180px;
    width: 100%;
    height: 100dvh;
    min-height: unset;
    margin: 0 auto;
    border: none;
    border-inline: 1px solid var(--border);
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
    background: var(--bg);
    display: grid;
    grid-template-columns: 264px minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      "topbar  main"
      "nav     main"
      "sidefill main";
  }

  /* Sidebar: brand + stats on top */
  .topbar {
    grid-area: topbar;
    position: static;
    flex-wrap: wrap;
    gap: 8px;
    padding: 20px 18px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
  }
  .topbar .logo { font-size: 20px; width: 100%; }
  .topbar .spacer { display: none; }

  /* Sidebar: vertical nav under the brand */
  .nav {
    grid-area: nav;
    position: static;
    height: auto;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding: 14px 12px;
    background: var(--surface);
    border-top: none;
    border-bottom: 1px solid var(--border);
  }
  .nav button {
    flex: none;
    flex-direction: row;
    justify-content: flex-start;
    gap: 12px;
    padding: 11px 14px;
    border-radius: 11px;
    font-size: 14px;
  }
  .nav button .ic { font-size: 19px; }
  .nav button.active { background: var(--brand-soft); }
  /* Keep the sidebar nav visible during a lesson on desktop (a nav click
     exits the lesson via show()). On mobile the nav still hides full-screen. */
  .nav.hidden { display: flex; }

  /* Left column fills below the nav (sidebar background) */
  #app::before {
    content: "";
    grid-area: sidefill;
    background: var(--surface);
    border-right: 0;
  }

  /* Content pane */
  .view {
    grid-area: main;
    overflow-y: auto;
    padding: 32px clamp(28px, 4vw, 64px) 48px;
  }

  /* Lesson player overlays only the content pane, not the sidebar */
  .player {
    inset: 0 0 0 264px;
    background: var(--bg);
  }
  .card-area { padding: 16px clamp(28px, 4vw, 64px) 24px; }
  .player-head, .footer { padding-inline: clamp(28px, 4vw, 64px); }

  /* Toasts/boot over the content pane */
  .toast-wrap { left: 264px; }
}
