:root {
  --bg: #f4f5f7; --panel: #ffffff; --ink: #16181d; --muted: #646b78; --line: #e2e5ea;
  --accent: #2f6df6; --accent-ink: #ffffff; --ok: #19a463; --warn: #c98a00; --err: #d93d3d;
  --code: #f1f3f6; --radius: 14px; --shadow: 0 1px 2px rgba(16,24,40,.05), 0 4px 16px rgba(16,24,40,.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #171a20; --ink: #e9ebef; --muted: #98a0ad; --line: #2a2f38;
    --accent: #5b8cff; --code: #1f232b; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: .45; }
code { font-family: var(--mono); font-size: .88em; background: var(--code); padding: 1px 5px; border-radius: 5px; }

.top { display: flex; align-items: center; gap: 12px; padding: 12px max(16px, env(safe-area-inset-left)); }
.brand { display: flex; align-items: center; gap: 9px; color: inherit; text-decoration: none; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.tag { color: var(--muted); font-size: 13px; }

.layout { display: grid; grid-template-columns: 360px 1fr; gap: 16px; padding: 0 16px 16px; height: calc(100dvh - 54px); }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.controls { padding: 16px; overflow: auto; display: flex; flex-direction: column; gap: 14px; }

.run-form { display: flex; gap: 8px; }
.run-form input {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; padding: 11px 13px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); outline: none;
}
.run-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 10px; padding: 0 16px; font-weight: 600; min-height: 44px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }

.upload { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.btn.ghost { border: 1px solid var(--line); background: transparent; min-height: 38px; padding: 0 12px; font-size: 13.5px; font-weight: 600; cursor: pointer; }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn.ghost:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.upload .hint { flex-basis: 100%; margin: 0; color: var(--muted); font-size: 12.5px; }
.dropzone { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 70%, transparent); backdrop-filter: blur(2px); pointer-events: none; }
.dropzone[hidden] { display: none; }
.dropzone div { border: 2px dashed var(--accent); border-radius: var(--radius); padding: 40px 56px; background: var(--panel); font-weight: 700; color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.chips-label { color: var(--muted); font-size: 13px; margin-right: 2px; }
.chip { border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 11px; font-size: 13px; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

details { border-top: 1px solid var(--line); padding-top: 10px; }
summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--muted); }
details[open] summary { margin-bottom: 8px; }
.how p { color: var(--muted); font-size: 13.5px; margin: 6px 0; }
.field { display: grid; gap: 5px; font-size: 13.5px; }
.field small { color: var(--muted); font-weight: 400; }
.field textarea { font: 13px var(--mono); padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; font-size: 13.5px; margin-top: 8px; }

.recent h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 6px; }
.recent ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.recent li button { width: 100%; text-align: left; background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 7px 9px; display: grid; gap: 1px; }
.recent li button:hover { border-color: var(--line); background: var(--bg); }
.recent .r-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent .r-meta { font-size: 12px; color: var(--muted); }

.preview { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.addr { display: flex; align-items: center; gap: 8px; min-width: 0; font: 13px var(--mono); color: var(--muted); }
.addr #addrText { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); flex: none; }
.dot.ok { background: var(--ok); } .dot.busy { background: var(--warn); animation: pulse 1s infinite; } .dot.err { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }
.badge { font: 600 11px system-ui; padding: 2px 8px; border-radius: 999px; background: var(--code); color: var(--ink); white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 4px; flex: none; }
.icon { display: grid; place-items: center; width: 36px; height: 36px; border: 0; background: transparent; border-radius: 8px; color: var(--muted); }
.icon:hover:not(:disabled) { background: var(--code); color: var(--ink); }
.seg { display: flex; background: var(--code); border-radius: 8px; padding: 2px; margin-right: 4px; }
.seg button { border: 0; background: transparent; padding: 4px 9px; border-radius: 6px; font-size: 12.5px; color: var(--muted); }
.seg button.on { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }

.notice { padding: 9px 14px; font-size: 13.5px; line-height: 1.45; background: color-mix(in srgb, var(--warn) 13%, var(--panel)); border-bottom: 1px solid color-mix(in srgb, var(--warn) 35%, var(--line)); }
.notice p { margin: 0; }
.notice p + p { margin-top: 4px; }
.stage { position: relative; flex: 1; min-height: 0; background: var(--bg); }
.frame-wrap { position: absolute; inset: 0; display: flex; justify-content: center; overflow: auto; }
.frame-wrap.sized { padding: 16px; }
iframe { border: 0; width: 100%; height: 100%; background: #fff; display: block; }
.frame-wrap.sized iframe { border-radius: 12px; box-shadow: var(--shadow); border: 1px solid var(--line); flex: none; }
.overlay { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 24px; background: var(--bg); }
.overlay[hidden] { display: none; }
.empty-icon { font-size: 44px; color: var(--accent); line-height: 1; margin-bottom: 8px; }
.muted { color: var(--muted); } .small { font-size: 12.5px; font-family: var(--mono); word-break: break-all; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); margin: 0 auto 10px; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error { max-width: 560px; }
.err-title { font-weight: 700; color: var(--err); margin-bottom: 4px; }
#errorText { white-space: pre-wrap; text-align: left; font-size: 14px; }

.console { border-top: 1px solid var(--line); display: flex; flex-direction: column; }
.console-bar { display: flex; justify-content: space-between; align-items: center; padding: 4px 10px; }
.console-toggle { border: 0; background: transparent; font-weight: 600; font-size: 13px; padding: 6px 4px; display: flex; gap: 6px; align-items: center; }
.console-toggle::before { content: "▸"; transition: transform .15s; display: inline-block; }
.console-toggle[aria-expanded="true"]::before { transform: rotate(90deg); }
.count { background: var(--err); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; }
.link { border: 0; background: transparent; color: var(--muted); font-size: 12.5px; }
.console-list { list-style: none; margin: 0; padding: 0 0 6px; max-height: 32vh; overflow: auto; font: 12.5px/1.45 var(--mono); }
.console-list li { padding: 3px 12px; border-top: 1px solid color-mix(in srgb, var(--line) 60%, transparent); white-space: pre-wrap; word-break: break-word; display: flex; gap: 8px; }
.console-list li .src { color: var(--muted); flex: none; min-width: 44px; }
.console-list li.warn { background: color-mix(in srgb, var(--warn) 9%, transparent); }
.console-list li.error { background: color-mix(in srgb, var(--err) 9%, transparent); color: var(--err); }

.toast { position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); z-index: 10; }

/* phones & small tablets: stack, preview gets most of the screen */
@media (max-width: 860px) {
  .tag { display: none; }
  .layout { grid-template-columns: 1fr; height: auto; padding: 0 10px 10px; gap: 10px; }
  .controls { padding: 12px; gap: 10px; }
  .preview { height: calc(100dvh - 80px); }
  .seg { display: none; }
  .upload .hint { display: none; }
  .recent ul { grid-template-columns: 1fr; }
}
