:root {
  --bg: #070a14;
  --bg2: #0d1326;
  --panel: rgba(20, 28, 54, 0.82);
  --panel-solid: #141c36;
  --line: rgba(130, 160, 255, 0.18);
  --text: #e6ecff;
  --muted: #8f9bc2;
  --accent: #5fb4ff;
  --accent2: #b07bff;
  --gold: #ffd27a;
  --danger: #ff5a6a;
  --ok: #57f0a0;
  --radius: 12px;
  --topbar: 56px;
  --tray: 96px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
body { -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
#app { position: fixed; inset: 0; }

.screen { position: absolute; inset: 0; display: none; flex-direction: column; background: radial-gradient(ellipse at top, #121a36 0%, var(--bg) 70%); }
.screen.active { display: flex; }

/* ----------------------------------------------------------- buttons */
.btn { background: rgba(80, 110, 200, 0.14); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; cursor: pointer; transition: background .15s, transform .08s, border-color .15s; white-space: nowrap; }
.btn:hover { background: rgba(95, 180, 255, 0.22); border-color: rgba(95, 180, 255, 0.5); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: linear-gradient(135deg, #3a8dff, #8a5cff); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.12); }
.btn.danger { border-color: rgba(255, 90, 106, .45); }
.btn.danger:hover { background: rgba(255, 90, 106, .2); }
.btn.ghost { background: transparent; }
.btn.big { font-size: 1.1rem; padding: 12px 18px; width: 100%; }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn.icon { min-width: 40px; padding: 8px 10px; font-size: 1.05rem; line-height: 1; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ------------------------------------------------------------ topbar */
.topbar { height: var(--topbar); flex: 0 0 var(--topbar); display: flex; align-items: center; gap: 12px; padding: 0 12px; border-bottom: 1px solid var(--line); background: rgba(8, 12, 26, 0.75); backdrop-filter: blur(6px); }
.topbar h2 { margin: 0; font-size: 1.15rem; font-weight: 600; flex: 1; }
.hud-title { flex: 1; display: flex; align-items: baseline; gap: 10px; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-weight: 600; }
.lvl-id { color: var(--accent); font-variant-numeric: tabular-nums; }
.hud-buttons { display: flex; gap: 6px; align-items: center; }
.goals { color: var(--muted); font-size: .9rem; margin-right: 6px; white-space: nowrap; }
.goals .ok { color: var(--ok); }
.goals .bad { color: var(--danger); }
.name-input { flex: 1; min-width: 80px; max-width: 320px; background: rgba(0, 0, 0, .25); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; }

/* -------------------------------------------------------------- menu */
#scr-menu { align-items: center; justify-content: center; }
#menu-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.menu-inner { position: relative; width: min(360px, 90vw); text-align: center; padding: 28px 24px; background: rgba(8, 12, 28, 0.55); border: 1px solid var(--line); border-radius: 20px; backdrop-filter: blur(8px); }
.logo { margin: 0; font-size: clamp(1.6rem, 7vw, 2.3rem); letter-spacing: .06em; white-space: nowrap; font-weight: 800; background: linear-gradient(90deg, #ff5c5c, #ffb347, #fff45c, #5cff8a, #5ce1ff, #6b7dff, #c07bff); -webkit-background-clip: text; background-clip: text; color: transparent; }
.logo span { font-weight: 300; }
.tagline { color: var(--muted); margin: 8px 0 22px; }
.menu-buttons { display: flex; flex-direction: column; gap: 10px; }
.menu-progress { color: var(--muted); font-size: .9rem; margin: 16px 0 0; }

/* ------------------------------------------------------------ worlds */
.worlds-grid { flex: 1; overflow: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; padding: 20px; align-content: start; }
.world-card { position: relative; border-radius: 16px; padding: 18px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; overflow: hidden; min-height: 170px; display: flex; flex-direction: column; gap: 8px; transition: transform .12s, border-color .12s; }
.world-card:hover { transform: translateY(-2px); border-color: rgba(95, 180, 255, .55); }
.world-card.locked { cursor: not-allowed; opacity: .5; }
.world-card .num { font-size: .8rem; letter-spacing: .15em; color: var(--muted); }
.world-card h3 { margin: 0; font-size: 1.3rem; }
.world-card p { margin: 0; color: var(--muted); font-size: .9rem; flex: 1; }
.world-card .bar { height: 6px; border-radius: 4px; background: rgba(255, 255, 255, .08); overflow: hidden; }
.world-card .bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent2)); }
.world-card .meta { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); }
.world-card canvas { position: absolute; right: -10px; top: -10px; width: 120px; height: 120px; opacity: .55; pointer-events: none; }

/* ------------------------------------------------------------ levels */
.world-blurb { margin: 14px 20px 0; color: var(--muted); }
.levels-grid { flex: 1; overflow: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding: 16px 20px 24px; align-content: start; }
.level-btn { border-radius: 12px; border: 1px solid var(--line); background: var(--panel); padding: 12px; cursor: pointer; text-align: left; display: flex; flex-direction: column; gap: 4px; min-height: 92px; transition: border-color .12s, transform .12s; }
.level-btn:hover { border-color: rgba(95, 180, 255, .55); transform: translateY(-1px); }
.level-btn .n { font-size: 1.4rem; font-weight: 700; color: var(--accent); }
.level-btn .nm { font-size: .88rem; }
.level-btn .st { color: var(--gold); letter-spacing: 2px; font-size: .9rem; }
.level-btn .st .off { color: rgba(255, 255, 255, .15); }
.level-btn.locked { opacity: .35; cursor: not-allowed; }
.level-btn.done { border-color: rgba(87, 240, 160, .35); }

/* -------------------------------------------------------------- game */
.board-wrap { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 8px; overflow: hidden; }
.board-wrap canvas { display: block; border-radius: 10px; box-shadow: 0 10px 40px rgba(0, 0, 0, .5), 0 0 0 1px var(--line); touch-action: none; }
.tray { flex: 0 0 var(--tray); height: var(--tray); display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-top: 1px solid var(--line); background: rgba(8, 12, 26, .8); overflow-x: auto; }
.tray .label { color: var(--muted); font-size: .85rem; margin-right: 4px; white-space: nowrap; }
.tray-item { position: relative; flex: 0 0 auto; width: 74px; height: 74px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); cursor: grab; touch-action: none; display: flex; flex-direction: column; align-items: center; justify-content: center; user-select: none; }
.tray-item:hover { border-color: rgba(95, 180, 255, .6); }
.tray-item canvas { width: 56px; height: 50px; pointer-events: none; }
.tray-item .cnt { position: absolute; top: 3px; right: 6px; font-size: .8rem; font-weight: 700; color: var(--accent); }
.tray-item .nm { font-size: .64rem; color: var(--muted); pointer-events: none; }
.tray-item.empty { opacity: .3; cursor: default; }
.tray .done-note { color: var(--muted); font-size: .9rem; }

.tip { display: none; gap: 12px; align-items: center; justify-content: center; padding: 8px 14px; background: rgba(20, 40, 80, .55); border-bottom: 1px solid rgba(95, 180, 255, .35); font-size: .95rem; line-height: 1.35; flex: 0 0 auto; }
.tip.show { display: flex; }
.rotbar { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); display: none; gap: 6px; padding: 6px; border-radius: 12px; background: rgba(12, 18, 40, .9); border: 1px solid var(--line); }
.rotbar.show { display: flex; }

/* ------------------------------------------------------------- modal */
.modal { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(3, 5, 12, .55); backdrop-filter: blur(3px); z-index: 20; }
.modal.show { display: flex; }
.modal-card { width: min(420px, 92vw); background: var(--panel-solid); border: 1px solid var(--line); border-radius: 18px; padding: 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .6); animation: pop .25s ease-out; }
.modal-card.wide { width: min(640px, 94vw); }
.modal-card h3 { margin: 0 0 10px; font-size: 1.35rem; }
.modal-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
@keyframes pop { from { transform: scale(.92); opacity: 0; } to { transform: none; opacity: 1; } }
.stars { font-size: 2.4rem; letter-spacing: 6px; color: var(--gold); text-align: center; margin: 6px 0 8px; text-shadow: 0 0 18px rgba(255, 210, 122, .5); }
.stars .off { color: rgba(255, 255, 255, .12); text-shadow: none; }
.win-list { list-style: none; padding: 0; margin: 0; color: var(--muted); font-size: .92rem; }
.win-list li { padding: 3px 0; }
.win-list li.ok::before { content: "★ "; color: var(--gold); }
.win-list li.no::before { content: "☆ "; color: rgba(255, 255, 255, .3); }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 18px; font-size: .92rem; color: #c8d2f2; }
.help-grid b { color: var(--accent); display: block; }
textarea { width: 100%; background: rgba(0, 0, 0, .3); border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-family: ui-monospace, monospace; font-size: .8rem; resize: vertical; }

.toast { position: absolute; left: 50%; bottom: 120px; transform: translateX(-50%); background: rgba(12, 18, 40, .95); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 30; }
.toast.show { opacity: 1; }

/* ------------------------------------------------------------ editor */
.editor-body { flex: 1; min-height: 0; display: flex; }
.palette { flex: 0 0 150px; overflow-y: auto; padding: 10px; border-right: 1px solid var(--line); background: rgba(8, 12, 26, .6); display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-content: start; }
.palette .tray-item { width: 100%; height: 70px; }
.palette h4 { grid-column: 1 / -1; margin: 6px 0 0; font-size: .75rem; letter-spacing: .12em; color: var(--muted); text-transform: uppercase; }
.props { flex: 0 0 240px; overflow-y: auto; padding: 12px; border-left: 1px solid var(--line); background: rgba(8, 12, 26, .6); font-size: .9rem; }
.props h4 { margin: 0 0 10px; }
.props .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.props .field label { color: var(--muted); font-size: .8rem; }
.props .field .inline { display: flex; gap: 8px; align-items: center; }
.props input[type=range] { flex: 1; }
.props input[type=number], .props select { background: rgba(0, 0, 0, .3); border: 1px solid var(--line); border-radius: 6px; padding: 5px 6px; width: 100%; }
.props input[type=number] { width: 72px; }
.props .help { color: var(--muted); font-size: .82rem; line-height: 1.4; }
.props .btn { width: 100%; margin-top: 6px; }

/* ------------------------------------------------------------ custom */
.custom-list { flex: 1; overflow: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; }
.custom-row { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.custom-row .nm { flex: 1; font-weight: 600; min-width: 120px; }
.custom-row .meta { color: var(--muted); font-size: .85rem; }
.empty-note { color: var(--muted); text-align: center; margin-top: 40px; }

/* ------------------------------------------------------------- codex */
.codex { flex: 1; overflow: auto; padding: 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; align-content: start; }
.codex article { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; line-height: 1.5; font-size: .93rem; color: #cdd6f5; }
.codex article h3 { margin: 0 0 8px; color: var(--text); }
.codex .eq { font-family: ui-monospace, "Cambria Math", serif; background: rgba(0, 0, 0, .3); padding: 6px 10px; border-radius: 8px; display: inline-block; margin: 4px 0; color: #fff; }
.codex table { border-collapse: collapse; width: 100%; font-size: .82rem; margin-top: 6px; }
.codex td, .codex th { padding: 3px 4px; text-align: right; border-bottom: 1px solid rgba(255, 255, 255, .06); }
.codex td:first-child, .codex th:first-child { text-align: left; }
.codex canvas { width: 100%; height: 150px; border-radius: 10px; margin-top: 8px; background: #070a14; }

/* ---------------------------------------------------------- settings */
.settings { padding: 20px; max-width: 560px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.settings .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.settings input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }

/* ------------------------------------------------------------ mobile */
.rotate-note { display: none; text-align: center; color: var(--muted); font-size: .85rem; padding: 4px 0 8px; }
@media (orientation: portrait) and (max-width: 760px) { .rotate-note { display: block; } }
.tray-item .nm { text-align: center; line-height: 1.1; }
@media (max-width: 760px) {
  :root { --topbar: 50px; --tray: 84px; }
  .hud-title #g-name { display: none; }
  .goals { display: none; }
  .btn.icon { min-width: 34px; padding: 7px 8px; }
  .tray-item { width: 64px; height: 64px; }
  .palette { flex-basis: 96px; grid-template-columns: 1fr; }
  .props { position: absolute; right: 0; bottom: 0; top: auto; max-height: 45%; width: 100%; border-left: 0; border-top: 1px solid var(--line); z-index: 5; display: none; }
  .props.show { display: block; }
  .editor-body { position: relative; }
  .help-grid { grid-template-columns: 1fr; }
  #e-share, #e-new { display: none; }
}
@media (max-height: 520px) {
  :root { --topbar: 46px; --tray: 76px; }
}
.settings select { background: rgba(0, 0, 0, .3); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }

/* ---------------------------------------------------------- online */
.online-only { display: none !important; }
body.online .btn.online-only { display: inline-block !important; }
body.online .menu-buttons .btn.online-only { display: block !important; }
body.online .account.online-only { display: flex !important; }
.account { position: absolute; top: 12px; right: 12px; z-index: 2; gap: 8px; align-items: center; padding: 6px 8px; border-radius: 12px; background: rgba(8, 12, 28, .6); border: 1px solid var(--line); }
.account a.btn { text-decoration: none; }
.acct-name { color: var(--muted); font-size: .9rem; padding: 0 4px; }
.form-row { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; color: var(--muted); font-size: .85rem; }
.form-row input { background: rgba(0, 0, 0, .3); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; color: var(--text); font-size: 1rem; }
.form-err { color: var(--danger); min-height: 1.2em; margin: 4px 0; font-size: .9rem; }
.pub-check p { margin: 8px 0; padding: 8px 10px; border-radius: 8px; font-size: .92rem; }
.pub-check .ok { background: rgba(87, 240, 160, .1); color: var(--ok); }
.pub-check .bad { background: rgba(255, 90, 106, .1); color: var(--danger); }
.cm-tabs { display: flex; gap: 6px; padding: 12px 20px 0; flex-wrap: wrap; }
.cm-tabs .btn.on { background: rgba(95, 180, 255, .3); border-color: rgba(95, 180, 255, .7); }
.cm-grid { flex: 1; overflow: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; padding: 14px 20px; align-content: start; }
.cm-card { border-radius: 14px; border: 1px solid var(--line); background: var(--panel); overflow: hidden; display: flex; flex-direction: column; }
.cm-card canvas { width: 100%; aspect-ratio: 16 / 10; display: block; background: #070a14; }
.cm-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 6px; }
.cm-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cm-meta { color: var(--muted); font-size: .82rem; }
.cm-note { color: var(--gold); font-size: .82rem; }
.cm-badges { display: flex; gap: 6px; flex-wrap: wrap; min-height: 4px; }
.badge { font-size: .72rem; padding: 2px 8px; border-radius: 99px; background: rgba(255, 255, 255, .08); color: var(--muted); }
.badge.official { background: rgba(95, 180, 255, .2); color: #bfe2ff; }
.badge.featured { background: rgba(255, 210, 122, .2); color: var(--gold); }
.badge.status-pending { background: rgba(176, 123, 255, .2); color: #dcc8ff; }
.badge.status-rejected { background: rgba(255, 90, 106, .2); color: #ffb3bb; }
.badge.status-draft { background: rgba(255, 255, 255, .1); }
.cm-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.cm-actions .btn.liked { color: #ff8fb1; border-color: rgba(255, 143, 177, .5); }
.cm-more { text-align: center; padding: 0 0 18px; }
