:root {
  --bg: #f4f6f9; --panel: #ffffff; --ink: #1d2b3a; --muted: #6b7a8a; --line: #d9e0e8;
  --accent: #2f6fed; --accent-ink: #ffffff; --ok: #1f8f4e; --warn: #b7791f; --err: #d64545;
  --code-bg: #fbfcfd; --k: #0b5cad; --c: #7a8794; --d: #8a3ffc; --t: #c2410c; --s: #1f7a4d; --x: #d64545;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a; --panel: #171e26; --ink: #e6ebf0; --muted: #8b98a6; --line: #2b3541;
    --accent: #58a6ff; --accent-ink: #0b1118; --ok: #4cc27f; --warn: #e3b341; --err: #ff7b72;
    --code-bg: #12181f; --k: #79c0ff; --c: #7d8a97; --d: #d2a8ff; --t: #ffa657; --s: #7ee787; --x: #ff7b72;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 14px/1.45 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif; display: flex; flex-direction: column; }
button, select { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px; cursor: pointer; }
button:hover:not(:disabled), select:hover { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.link { border: 0; background: none; color: var(--accent); padding: 0; }
code, pre { font-family: 'Cascadia Code', Consolas, 'SF Mono', Menlo, monospace; }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.muted { color: var(--muted); }

.topbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar h1 { margin: 0 10px 0 0; font-size: 18px; letter-spacing: .3px; }
.topbar h1 span { color: var(--accent); }
.spacer { flex: 1; }
.menu { position: relative; }
.menu summary { list-style: none; border: 1px solid var(--line); border-radius: 6px; padding: 5px 10px; cursor: pointer; background: var(--panel); }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: 34px; z-index: 20; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 4px; display: flex; flex-direction: column; min-width: 180px; box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.menu-items button { border: 0; text-align: left; }

#main { --left: 42%; flex: 1; min-height: 0; display: grid; grid-template-columns: var(--left) 8px 1fr; }
#left { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 10px 0 10px 10px; gap: 8px; }
#right { min-width: 0; min-height: 0; display: flex; flex-direction: column; padding: 10px 10px 10px 0; gap: 8px; }
#splitter { cursor: col-resize; background: transparent; }
#splitter:hover { background: var(--line); }
.canvas-wrap { flex: 1; min-height: 0; display: flex; align-items: flex-start; justify-content: center; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
#canvas { width: 100%; height: auto; max-height: 100%; border-radius: 4px; }
#status { display: flex; gap: 14px; align-items: center; color: var(--muted); font-size: 13px; }
.meter { width: 120px; height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--ok); transition: width .3s; }

#palette { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
#palette .label { color: var(--muted); font-size: 12px; margin-right: 2px; }
.chip { padding: 2px 8px; font-size: 12px; border-radius: 999px; }
.chip.filled { border-color: var(--ok); color: var(--ok); }
.chip.sub { border-style: dashed; }

/* Editor */
.editor { flex: 1; min-height: 160px; display: flex; border: 1px solid var(--line); border-radius: 8px; background: var(--code-bg); overflow: hidden; font-family: 'Cascadia Code', Consolas, 'SF Mono', Menlo, monospace; font-size: 13px; line-height: 20px; }
.ed-gutter { width: 44px; flex: none; overflow: hidden; border-right: 1px solid var(--line); background: var(--bg); padding-top: 8px; }
.ed-ln { text-align: right; padding-right: 8px; color: var(--muted); height: 20px; }
.ed-ln.error { color: #fff; background: var(--err); }
.ed-ln.warning { color: #fff; background: var(--warn); }
.ed-ln.hint { color: var(--accent); font-weight: 700; }
.ed-code { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.ed-hl, .ed-ta { position: absolute; inset: 0; margin: 0; padding: 8px 10px; font: inherit; line-height: inherit; white-space: pre; tab-size: 2; border: 0; }
.ed-hl { color: var(--ink); pointer-events: none; will-change: transform; }
.ed-hl .k { color: var(--k); font-weight: 600; } .ed-hl .c { color: var(--c); font-style: italic; }
.ed-hl .d { color: var(--d); font-weight: 600; } .ed-hl .t { color: var(--t); } .ed-hl .s { color: var(--s); }
.ed-hl .b { color: var(--muted); } .ed-hl .x { color: var(--x); text-decoration: underline wavy; }
.ed-ta { background: transparent; color: transparent; caret-color: var(--ink); resize: none; outline: none; overflow: auto; }
.ed-ta::selection { background: rgba(47,111,237,.3); color: transparent; }

#diag { max-height: 140px; overflow: auto; font-size: 13px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
#diag ul { list-style: none; margin: 0; padding: 0; }
#diag li button { border: 0; background: none; text-align: left; padding: 2px 0; width: 100%; }
#diag li.error b { color: var(--err); } #diag li.warning b { color: var(--warn); } #diag li.hint b { color: var(--accent); }
#diag .ok { color: var(--ok); }
#diag summary { cursor: pointer; color: var(--muted); }

/* Tutorial panel */
#tutorial { background: var(--panel); border: 1px solid var(--accent); border-radius: 8px; padding: 10px 12px; max-height: 46%; overflow: auto; }
#tutorial[hidden] { display: none; }
#tutorial h3 { margin: 6px 0 4px; font-size: 16px; }
#tutorial p { margin: 4px 0; }
.tut-head { display: flex; gap: 10px; align-items: center; } .tut-head span { color: var(--muted); flex: 1; }
.dots { display: flex; gap: 4px; margin: 6px 0; }
.dots i { height: 5px; flex: 1; background: var(--line); border-radius: 3px; }
.dots i.done { background: var(--ok); } .dots i.cur { background: var(--accent); }
.tip { color: var(--muted); font-size: 13px; }
.syntax { background: var(--code-bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; margin: 6px 0; font-size: 12px; overflow: auto; }
.goal { margin: 6px 0; font-weight: 600; color: var(--muted); } .goal.met { color: var(--ok); }
.tut-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Dialogs */
dialog { width: min(1000px, 94vw); max-height: 90vh; background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; padding: 16px 20px; }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog header { display: flex; justify-content: space-between; align-items: center; }
dialog h2 { margin: 0; font-size: 20px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; margin-top: 10px; }
.card { display: flex; flex-direction: column; gap: 6px; text-align: left; padding: 10px; border-radius: 10px; }
.card svg { width: 100%; height: auto; border: 1px solid var(--line); border-radius: 6px; }
.card b { font-size: 15px; } .card small { color: var(--accent); } .card span { color: var(--muted); font-size: 13px; }
.card div { display: flex; flex-direction: column; gap: 2px; }
.help table { border-collapse: collapse; width: 100%; font-size: 13px; }
.help td, .help th { border-bottom: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.help code { margin-right: 4px; }

#toast { position: fixed; bottom: 18px; left: 50%; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 900px) {
  body { height: auto; }
  #main { display: block; }
  #left { padding: 10px; } #right { padding: 10px; }
  .editor { min-height: 320px; } #splitter { display: none; }
}
@media print {
  body { background: #fff; display: block; }
  .topbar, #left, #splitter, #status, #toast { display: none !important; }
  #main { display: block; } #right { padding: 0; }
  .canvas-wrap { border: 0; padding: 0; overflow: visible; }
  @page { size: landscape; margin: 8mm; }
}

/* Hover guide popover */
#guide { position: fixed; z-index: 50; display: none; max-width: 320px; padding: 10px 12px; pointer-events: none;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, .22); font-size: 12.5px; line-height: 1.4; }
#guide b { font-size: 13.5px; }
#guide p { margin: 4px 0 6px; }
#guide h4 { margin: 8px 0 2px; font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
#guide ul { margin: 0; padding-left: 16px; }
#guide ul.ex li { color: var(--accent); font-style: italic; }
