:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #647084;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --ok: #2b8a3e;
  --bad: #c92a2a;
  --code-bg: #f0f2f5;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1b2029;
    --border: #2c3340;
    --text: #e6e9ef;
    --muted: #95a0b3;
    --accent: #748ffc;
    --accent-text: #0d1020;
    --ok: #69db7c;
    --bad: #ff8787;
    --code-bg: #242a35;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  /* Fixed to the viewport: the sidebar and workspace scroll independently */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; margin: 0; font-size: 18px; }
.progress { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.progress-track { width: 160px; height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--ok); transition: width 0.3s; }

/* Layout */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  padding: 16px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.sidebar h3 {
  margin: 16px 8px 6px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.sidebar h3:first-child { margin-top: 0; }
.sidebar button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.sidebar button:hover { background: var(--code-bg); }
.sidebar button[aria-current="true"] { background: var(--accent); color: var(--accent-text); }
.sidebar .status { width: 14px; flex: none; font-size: 12px; color: var(--ok); }
.sidebar button[aria-current="true"] .status { color: inherit; }

.workspace {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto minmax(460px, 1fr) auto;
  grid-template-areas:
    "lesson lesson"
    "editor preview"
    "feedback feedback";
  gap: 14px;
  padding: 16px 20px;
  min-width: 0;
  overflow-y: auto;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 0;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font: 12px var(--mono);
}

.lesson { grid-area: lesson; padding: 16px 20px; }
.lesson h2 { margin: 0 0 8px; font-size: 20px; }
.module-name { margin: 0; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; }
.lesson-body p { margin: 0 0 8px; max-width: 75ch; }
.lesson-body .task { padding: 8px 12px; border-left: 3px solid var(--accent); background: var(--code-bg); border-radius: 0 6px 6px 0; }
code { padding: 1px 5px; border-radius: 4px; background: var(--code-bg); font: 0.92em var(--mono); }
.task code { background: var(--surface); }

.editor { grid-area: editor; display: flex; flex-direction: column; overflow: hidden; }
#css-input {
  flex: 1;
  min-height: 200px;
  padding: 12px;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: inherit;
  font: 14px/1.6 var(--mono);
  tab-size: 2;
}
.markup { border-top: 1px solid var(--border); }
.markup summary { padding: 6px 12px; color: var(--muted); font: 12px var(--mono); cursor: pointer; }
.markup pre { margin: 0; padding: 0 12px 12px; overflow-x: auto; font: 13px/1.5 var(--mono); }

/* Learner's result above the target, at the same size so they compare 1:1 */
.previews { grid-area: preview; display: flex; flex-direction: column; gap: 14px; min-width: 0; min-height: 0; }
/* --preview-width is set for lessons with fixed viewport sizes; a preview wider
   than its panel scrolls sideways */
.preview { flex: 1; display: flex; flex-direction: column; overflow: auto hidden; }
.preview .panel-head { position: sticky; left: 0; min-height: 34px; }
.preview iframe { flex: 1; width: var(--preview-width, 100%); min-height: 180px; border: 0; background: #fff; }
.panel-head .note { font-family: system-ui, sans-serif; }

.feedback {
  grid-area: feedback;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
}
#feedback ul { margin: 4px 0 0; padding-left: 20px; }
#feedback .ok { color: var(--ok); font-weight: 600; }
#feedback .bad { color: var(--bad); font-weight: 600; }

.actions { display: flex; gap: 8px; flex: none; }
.actions button {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.actions button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.actions button.ghost { padding: 2px 8px; font-size: 12px; }
.actions button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button:focus-visible, #css-input:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

@media (max-width: 860px) {
  /* Stacked layout: the page scrolls as a whole, the lesson list stays short */
  body { height: auto; min-height: 100dvh; overflow: visible; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto; }
  .workspace { overflow-y: visible; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); max-height: 180px; }
  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas: "lesson" "editor" "preview" "feedback";
    padding: 12px;
  }
  .feedback { flex-direction: column; align-items: stretch; }
  .progress-track { width: 80px; }
}
