:root {
  --bg: #f8fafc; --panel: #ffffff; --border: #e2e8f0; --text: #0f172a; --muted: #64748b;
  --primary: #2563eb; --primary-dark: #1d4ed8; --danger: #dc2626; --ok: #16a34a;
  --code-bg: #0f172a; --code-fg: #e2e8f0;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  font-size: 14px; color: var(--text); background: var(--bg);
  display: flex; flex-direction: column; overflow: hidden;
}
button {
  font: inherit; padding: 6px 12px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); cursor: pointer;
}
button:hover { background: #f1f5f9; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-dark); }
button.danger { color: var(--danger); }
button.mini { padding: 2px 8px; font-size: 12px; }
select { font: inherit; padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel); }

#topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.logo { width: 22px; height: 16px; border-radius: 4px; background: var(--primary); position: relative; }
.logo::after { content: ""; position: absolute; left: 5px; top: 4px; width: 12px; height: 8px; border-radius: 2px; background: #fff; }
.project { font: inherit; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; width: 170px; }
.group { display: flex; gap: 4px; }
.modes button.active { background: #dbeafe; border-color: #93c5fd; color: var(--primary-dark); font-weight: 600; }
.spacer { flex: 1; }

#banner { padding: 8px 14px; background: #fef3c7; border-bottom: 1px solid #fcd34d; color: #78350f; font-size: 13px; }
#banner code { background: #fff7ed; padding: 1px 5px; border-radius: 4px; }

#main { flex: 1; display: flex; min-height: 0; }
#left { flex: 1 1 60%; position: relative; min-width: 200px; }
#blocklyDiv { position: absolute; inset: 0; }
#textOverlay {
  position: absolute; inset: 0; background: rgba(248,250,252,.85); display: flex;
  align-items: center; justify-content: center; text-align: center; z-index: 5;
}
#textOverlay p { margin: 6px 0; }
#textOverlay .small { color: var(--muted); font-size: 12px; }
#splitter { width: 6px; cursor: col-resize; background: var(--border); }
#splitter:hover { background: #cbd5e1; }
#right { flex: 0 0 40%; display: flex; flex-direction: column; min-width: 240px; border-left: 1px solid var(--border); }
.pane { display: flex; flex-direction: column; min-height: 0; }
#codePane { flex: 1 1 50%; }
#consolePane { flex: 1 1 50%; border-top: 1px solid var(--border); }
.paneHead {
  display: flex; justify-content: space-between; align-items: center; padding: 4px 10px;
  background: #f1f5f9; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 12px;
  color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
}
.paneTools { display: flex; gap: 4px; }
.code {
  flex: 1; margin: 0; padding: 10px 12px; overflow: auto; background: var(--code-bg); color: var(--code-fg);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; font-size: 13px;
  line-height: 1.5; white-space: pre; tab-size: 4;
}
textarea.code { border: 0; outline: none; resize: none; }
.status { font-weight: 400; text-transform: none; letter-spacing: 0; margin-left: 8px; }
.status.running { color: var(--ok); }
.status.error { color: var(--danger); }
#stdinForm { display: flex; border-top: 1px solid var(--border); }
#stdinInput {
  flex: 1; font: inherit; font-family: ui-monospace, Menlo, Consolas, monospace; padding: 7px 10px; border: 0; outline: none;
}
#stdinInput:disabled { background: #f8fafc; }

/* Syntax colours */
.k { color: #c084fc; } .s { color: #86efac; } .n { color: #fdba74; } .c { color: #94a3b8; font-style: italic; } .f { color: #7dd3fc; }
.err { color: #fca5a5; } .info { color: #94a3b8; } .in { color: #fde68a; }

/* Modal */
#modal { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modalBox { background: var(--panel); border-radius: 10px; width: min(640px, 92vw); max-height: 85vh; display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modalHead { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 16px; }
#modalBody { padding: 12px 16px; overflow: auto; }
#modalBody h3 { margin: 12px 0 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
#modalBody h3:first-child { margin-top: 0; }
.list { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px; }
.list button { text-align: left; padding: 8px 10px; }
.list .sub { display: block; font-size: 11px; color: var(--muted); }
#modalBody code { background: #f1f5f9; padding: 1px 5px; border-radius: 4px; font-size: 12px; }
#modalBody pre { background: #f1f5f9; padding: 8px 10px; border-radius: 6px; font-size: 12px; overflow: auto; }
#modalBody table { border-collapse: collapse; width: 100%; font-size: 13px; }
#modalBody td { padding: 4px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.yes { color: var(--ok); font-weight: 600; } .no { color: var(--danger); font-weight: 600; }

@media (max-width: 800px) {
  body { height: auto; min-height: 100%; overflow: auto; }
  #main { flex-direction: column; min-height: 80vh; }
  #left { flex: 1 1 auto; min-height: 45vh; }
  #splitter { display: none; }
  #right { flex: 1 1 auto; min-height: 40vh; border-left: 0; border-top: 1px solid var(--border); }
  .project { width: 120px; }
}
#target { max-width: 190px; }

/* Program waiting for input */
#stdinForm.waiting { border-top: 2px solid #f59e0b; background: #fffbeb; animation: pulse 1.2s ease-in-out infinite; }
#stdinForm.waiting #stdinInput { background: #fffbeb; }
#stdinForm.waiting #stdinInput::placeholder { color: #b45309; font-weight: 600; }
#console.waiting::after { content: "\258B"; color: #fbbf24; animation: blink 1s step-end infinite; }
@keyframes pulse { 0%, 100% { box-shadow: inset 0 0 0 0 rgba(245,158,11,0); } 50% { box-shadow: inset 0 0 0 2px rgba(245,158,11,.5); } }
@keyframes blink { 50% { opacity: 0; } }

/* Editor notices */
#toast {
  position: fixed; right: 16px; bottom: 16px; z-index: 60; max-width: 420px;
  background: #0f172a; color: #f8fafc; padding: 10px 14px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
#toast.error { background: #7f1d1d; }

/* In-browser game (web edition) */
#gamePane { flex: 1 1 50%; }
#gameWrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; background: #111827; padding: 6px; }
#gameCanvas { max-width: 100%; max-height: 100%; outline: none; image-rendering: auto; }
#gameCanvas:focus { box-shadow: 0 0 0 2px #fbbf24; }

/* Account */
.chip { font-size: 12px; color: var(--muted); padding: 4px 8px; background: #f1f5f9; border-radius: 999px; }
#shareLink { width: 100%; font: inherit; font-size: 12px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }

/* Explain-this-block panel */
#helpPanel { border-top: 1px solid var(--border); background: #fffbeb; color: #78350f; padding: 8px 12px; font-size: 13px; max-height: 40%; overflow: auto; }
#helpPanel b { color: #92400e; }
#helpPanel code { background: #fef3c7; padding: 1px 5px; border-radius: 4px; font-size: 12px; }
#helpPanel .why { margin-top: 4px; color: #451a03; }

/* Web page preview */
#preview { flex: 1; width: 100%; border: 0; background: #fff; }

/* Dark mode */
:root[data-theme="dark"] {
  --bg: #0b1220; --panel: #111a2e; --border: #24304a; --text: #e5e7eb; --muted: #94a3b8;
  --code-bg: #05080f; --code-fg: #e2e8f0;
}
:root[data-theme="dark"] button { background: #172035; color: var(--text); border-color: var(--border); }
:root[data-theme="dark"] button:hover { background: #1e2a44; }
:root[data-theme="dark"] button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
:root[data-theme="dark"] .modes button.active { background: #1e3a8a; border-color: #3b82f6; color: #bfdbfe; }
:root[data-theme="dark"] select, :root[data-theme="dark"] .project, :root[data-theme="dark"] #stdinInput { background: #172035; color: var(--text); border-color: var(--border); }
:root[data-theme="dark"] .paneHead { background: #172035; }
:root[data-theme="dark"] #banner { background: #3b2f0b; border-color: #78350f; color: #fde68a; }
:root[data-theme="dark"] #banner code { background: #1f2937; }
:root[data-theme="dark"] .modalBox { background: var(--panel); }
:root[data-theme="dark"] #modalBody code, :root[data-theme="dark"] #modalBody pre { background: #172035; }
:root[data-theme="dark"] .list button { background: #172035; }
:root[data-theme="dark"] .chip { background: #172035; }
:root[data-theme="dark"] #helpPanel { background: #2a2208; color: #fde68a; }
:root[data-theme="dark"] #helpPanel b { color: #fcd34d; }
:root[data-theme="dark"] #helpPanel code { background: #3b2f0b; }
:root[data-theme="dark"] #helpPanel .why { color: #fef3c7; }
:root[data-theme="dark"] #stdinForm.waiting, :root[data-theme="dark"] #stdinForm.waiting #stdinInput { background: #3b2f0b; }
:root[data-theme="dark"] #textOverlay { background: rgba(11,18,32,.85); }

/* Sign-in gate (web edition) */
#gate { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: flex; align-items: center; justify-content: center; }
.gateCard { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 36px 40px; max-width: 460px; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.12); }
.gateCard .brand { justify-content: center; font-size: 22px; margin-bottom: 14px; }
.gateCard h2 { margin: 0 0 8px; font-size: 18px; }
.gateCard p { color: var(--muted); margin: 6px 0; }
.gateCard button { margin-top: 10px; padding: 10px 18px; }
.gateMsg { color: #b91c1c; min-height: 1.2em; }

/* Solo editor */
#soloPane { position: absolute; inset: 0; display: flex; flex-direction: column; }
#soloWrap { flex: 1; display: flex; min-height: 0; }
#soloGutter { flex: 0 0 auto; margin: 0; padding: 10px 8px 10px 12px; text-align: right; color: #64748b; user-select: none; overflow: hidden; background: var(--code-bg); border-right: 1px solid #1e293b; }
#soloEdit { flex: 1; margin: 0; border: 0; outline: none; resize: none; }
.modes button#modeSolo.active { background: #dcfce7; border-color: #86efac; color: #166534; }

/* Linux terminal + labs */
#linuxPane { position: absolute; inset: 0; display: flex; flex-direction: column; background: #0f172a; }
#terminal { flex: 1; min-height: 0; padding: 6px; }
#terminal .xterm { height: 100%; }
#labsPane { flex: 1 1 50%; }
#labsList { flex: 1; overflow: auto; padding: 10px 12px; }
.lab { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; background: var(--panel); }
.lab h4 { margin: 0 0 6px; font-size: 14px; display: flex; justify-content: space-between; align-items: center; }
.lab pre { white-space: pre-wrap; font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace; color: var(--muted); margin: 0 0 8px; }
.lab .result { font-weight: 600; margin-left: 8px; }
.lab .result.pass { color: #16a34a; } .lab .result.fail { color: #dc2626; }
.lab .hint { font-size: 12px; color: var(--muted); margin-top: 6px; display: none; }
.lab.open .hint { display: block; }

/* Highlighted editors: a coloured layer behind a transparent textarea */
.editWrap { position: relative; flex: 1; min-height: 0; display: flex; }
.editWrap .hl { position: absolute; inset: 0; margin: 0; pointer-events: none; overflow: hidden; }
.editWrap textarea { position: relative; flex: 1; background: transparent; color: transparent; caret-color: #f8fafc; }
.editWrap textarea::selection { background: rgba(96,165,250,.35); }
.hl .p { color: #f472b6; }  /* pascal directives / html tags reuse */
#soloWrap .editWrap { flex: 1; }
