/* Tokens de cor: claro por padrão, escuro pelo sistema ou pelo atributo data-theme */
:root {
  --bg: #f3f0e9;
  --surface: #faf8f3;
  --surface-2: #ece8df;
  --text: #1d1d1b;
  --muted: #5c5a53;
  --border: #b7afa0;
  --accent: #a63220;
  --accent-text: #ffffff;
  --code-bg: #1e2233;
  --code-text: #e6e9f5;
  --code-line: #5b6380;
  --kw: #c792ea;
  --str: #c3e88d;
  --com: #7b8199;
  --slot-bg: #2d3350;
  --slot-border: #5b6380;
  --slot-active: #82aaff;
  --ok: #276542;
  --ok-bg: #dff5e8;
  --bad: #c0392b;
  --bad-bg: #fde4e1;
  --radius: 4px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1220;
    --surface: #171b2e;
    --surface-2: #1d2238;
    --text: #e6e9f5;
    --muted: #9aa3bd;
    --border: #2a3050;
    --accent: #82aaff;
    --accent-text: #0f1220;
    --ok-bg: #163a28;
    --bad-bg: #3d1d1a;
    --ok: #5fd08f;
    --bad: #ff8a80;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1220;
  --surface: #171b2e;
  --surface-2: #1d2238;
  --text: #e6e9f5;
  --muted: #9aa3bd;
  --border: #2a3050;
  --accent: #82aaff;
  --accent-text: #0f1220;
  --ok-bg: #163a28;
  --bad-bg: #3d1d1a;
  --ok: #5fd08f;
  --bad: #ff8a80;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

.muted { color: var(--muted); }

/* Topo */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand > div { display: flex; flex-direction: column; line-height: 1.2; }
.logo {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-family: ui-monospace, Consolas, monospace;
  font-weight: 700;
}

/* Layout */
.layout {
  max-width: 1440px;
  margin: 0 auto;
  padding: 16px;
}

/* Área do exercício: código à esquerda, respostas à direita */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
  gap: 16px;
  align-items: start;
}
.col-code {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}
.col-answers {
  position: sticky;
  top: 92px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  max-height: calc(100vh - 108px);
  overflow-y: auto;
}
/* O painel cabe na tela; se algo passar, ainda dá para rolar, mas sem mostrar a barra */
.col-answers,
.options,
.output pre {
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.col-answers::-webkit-scrollbar,
.options::-webkit-scrollbar,
.output pre::-webkit-scrollbar { display: none; }
.instr { padding: 12px 14px; }
.instr > summary { list-style: none; cursor: pointer; display: block; }
.instr > summary::-webkit-details-marker { display: none; }
.instr > summary::after { content: "▾"; float: right; color: var(--muted); }
.instr:not([open]) > summary::after { content: "▸"; }
.instr .eyebrow { display: block; margin: 0; font-size: 0.78rem; color: var(--muted); }
.instr-title { display: block; margin: 2px 0 4px; font-size: 1.1rem; font-weight: 700; line-height: 1.25; }
.instr:not([open]) .instr-title { margin-bottom: 0; }
.instr p { font-size: 0.88rem; line-height: 1.45; }
.instr details.hint { margin-top: 4px; font-size: 0.88rem; }
.editor { max-height: calc(100vh - 112px); }
.col-answers .card { padding: 12px 14px; }
.col-answers .options-head { margin-bottom: 8px; }
.col-answers .options-head p { font-size: 0.85rem; }
.col-answers .option { padding: 6px 10px; font-size: 0.85rem; }
.col-answers .fb { padding: 8px 10px; font-size: 0.86rem; }
.col-answers .banner { padding: 10px 12px; font-size: 0.92rem; }
.options { max-height: 30vh; overflow-y: auto; }

/* Usuário logado e ranking */
.user { display: flex; align-items: center; gap: 8px; font-size: 0.92rem; }
.user[hidden] { display: none; }
.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
}
.ranking h2 { margin-bottom: 10px; }
.ranking ol { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.ranking li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 0.92rem;
}
.ranking li.me { background: var(--surface-2); font-weight: 700; }
.ranking .pos { width: 22px; color: var(--muted); font-variant-numeric: tabular-nums; }
.ranking .nome { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking .pts { color: var(--muted); font-variant-numeric: tabular-nums; }
.ranking .muted { font-size: 0.9rem; }
.error { color: var(--bad); font-size: 0.92rem; margin: 0; min-height: 1.2em; }

/* Tela de nome */
.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(10, 12, 24, 0.6);
}
.gate[hidden] { display: none; }
.gate-card {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.gate-card h2 { margin: 0; font-size: 1.2rem; text-transform: none; letter-spacing: 0; color: var(--text); }
.gate-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}
.gate-card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Conteúdo principal */
.main {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 0;
}

.card h2 {
  margin: 0 0 8px;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.card p { margin: 0; }
code {
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 0.92em;
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 6px;
}

details.hint {
  margin-top: 12px;
  color: var(--muted);
}
details.hint summary { cursor: pointer; color: var(--accent); font-weight: 600; }
details.hint p { margin-top: 6px; color: var(--text); }

/* Editor de código */
.editor {
  background: var(--code-bg);
  color: var(--code-text);
  border-radius: var(--radius);
  overflow: auto;
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  /* Cabe na altura da janela: (altura − cabeçalhos) ÷ (linhas × altura da linha), entre 10px e 13px */
  font-size: clamp(10px, calc((100vh - 172px) / (var(--lines, 40) * 1.4)), 13px);
}
.editor-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid #2f3652;
  color: #aab2d0;
  font-size: 0.8rem;
}
.code {
  margin: 0;
  padding: 6px 0;
  line-height: 1.4;
}
.code-line {
  display: block;
  padding: 0 16px 0 0;
  white-space: pre;
  min-height: 1.4em;
}
.code-line::before {
  content: attr(data-n);
  display: inline-block;
  width: 40px;
  padding-right: 12px;
  text-align: right;
  color: var(--code-line);
  user-select: none;
}

/* Trechos de código recolhidos (clique para expandir) */
.fold-head {
  width: 100%;
  margin: 0;
  border: 0;
  background: rgba(130, 170, 255, 0.08);
  color: #aab2d0;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.fold-head:hover { background: rgba(130, 170, 255, 0.18); color: #ffffff; }
.fold-count { color: var(--code-line); }
.fold-body[hidden] { display: none; }
.tok-com { color: var(--com); font-style: italic; }
.tok-str { color: var(--str); }
.tok-kw { color: var(--kw); }

/* Lacunas */
.slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 4.5ch;
  height: 1.3em;
  padding: 0 8px;
  margin: 0 2px;
  vertical-align: middle;
  border: 1px dashed var(--slot-border);
  border-radius: 6px;
  background: var(--slot-bg);
  color: var(--code-text);
  font: inherit;
  cursor: pointer;
  white-space: pre;
}
.slot:hover { border-color: var(--slot-active); }
.slot.active {
  border-style: solid;
  border-color: var(--slot-active);
  box-shadow: 0 0 0 2px rgba(130, 170, 255, 0.35);
}
.slot.filled { border-style: solid; }
.slot.ok { background: var(--ok-bg); border-color: var(--ok); color: var(--text); }
.slot.bad { background: var(--bad-bg); border-color: var(--bad); color: var(--text); }

/* Opções */
.options-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.options-head p { color: var(--muted); font-size: 0.92rem; }
.options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.option {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 0.9rem;
  cursor: pointer;
  white-space: pre;
}
.option:hover { border-color: var(--accent); }
.option[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.empty-tip { color: var(--muted); font-style: italic; }

/* Feedback */
.feedback {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}
.fb {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.92rem;
}
.fb.ok { background: var(--ok-bg); }
.fb.bad { background: var(--bad-bg); }
.fb strong { font-weight: 700; }
.fb code { background: transparent; padding: 0; }

.banner {
  padding: 12px 14px;
  border-radius: 10px;
  font-weight: 600;
}
.banner.ok { background: var(--ok-bg); color: var(--ok); }
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner.info { background: var(--surface-2); color: var(--text); font-weight: 500; }

/* Ações */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-start;
}
.actions button.btn { padding: 8px 12px; font-size: 0.9rem; }
.actions .group { display: flex; flex-wrap: wrap; gap: 10px; }
button.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
button.btn:hover { border-color: var(--accent); }
button.btn:disabled { opacity: 0.45; cursor: not-allowed; }
button.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

/* Saída */
.output pre {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--code-bg);
  color: var(--code-text);
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 0.8rem;
  line-height: 1.4;
  max-height: 7em;
  overflow-y: auto;
}

/* Trilha de fases, como as fases de um jogo */
.levels {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  padding: 6px 8px;
}
.level {
  position: relative;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  margin-right: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.level:last-child { margin-right: 0; }
/* linha que liga uma fase à próxima */
.level:not(:last-child)::after {
  content: "";
  position: absolute;
  left: 100%;
  top: 50%;
  width: 22px;
  height: 3px;
  margin-top: -1.5px;
  background: var(--border);
}
.level:hover { border-color: var(--accent); }
.level.done { background: var(--ok); border-color: var(--ok); color: #ffffff; }
.level.done:not(:last-child)::after { background: var(--ok); }
.level.current {
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 0 0 4px rgba(43, 108, 176, 0.28);
  transform: scale(1.12);
}
.level.done.current { color: #ffffff; }
.level:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.top-actions { display: flex; align-items: center; gap: 12px; }
button.btn.small { padding: 6px 12px; font-size: 0.9rem; }

/* XP, aviso rápido e diploma */
.xp {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: 0.9rem;
  white-space: nowrap;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 30;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--surface);
  font-size: 0.9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.toast[hidden] { display: none; }
.success .xp-gain { margin: 0 0 10px; font-weight: 700; color: var(--ok); }
[hidden] { display: none !important; }

/* Parabéns */
.success {
  text-align: center;
  background: var(--ok-bg);
  border-color: var(--ok);
}
.success-icon { font-size: 2rem; line-height: 1; }
.success h2 {
  margin: 6px 0 4px;
  font-size: 1.3rem;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ok);
}
.success p { margin-bottom: 12px; color: var(--text); }

/* Responsivo */
@media (max-width: 860px) {
  .layout { padding: 16px; }
  .levels { order: 3; flex-basis: 100%; justify-content: flex-start; }
  .level { width: 34px; height: 34px; margin-right: 16px; }
  .level:not(:last-child)::after { width: 16px; }
  .workspace { grid-template-columns: 1fr; }
  .col-answers { position: static; max-height: none; overflow: visible; }
  .editor { max-height: none; font-size: 0.8rem; }
  .options { max-height: none; }
}

@media (max-width: 520px) {
  .topbar { padding: 12px 16px; }
  .card { padding: 16px; }
  .actions { flex-direction: column; align-items: stretch; }
  .actions .group { width: 100%; }
  .actions button.btn { flex: 1 1 auto; }
  .code-line::before { width: 30px; }
}

/* Visual do catálogo */
.brand strong, .instr-title, .card h2 { font-family: Georgia, serif; }
.brand strong { font-size: 1.25rem; }
.instr-title, .card h2 { font-weight: 500; }
.logo { border-radius: 50%; }
.topbar { background: var(--bg); }
.card { border-color: var(--border); }
.catalog-link { color: var(--text); font-size: .875rem; text-underline-offset: .2em; }
:where(a, button, summary):focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }