:root {
  --bg: #0d1220; --panel: rgba(18, 24, 40, .88); --panel2: #1a2238; --line: rgba(140, 160, 210, .22);
  --text: #eef2ff; --muted: #9aa6c4; --accent: #ff8a1f; --accent2: #3ad0c0; --good: #3ad06a; --bad: #ff5a5a;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35); --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
:root[data-theme="light"] {
  --bg: #eef3fb; --panel: rgba(255, 255, 255, .92); --panel2: #f1f5fc; --line: rgba(40, 60, 110, .18);
  --text: #16203a; --muted: #5a6788; --accent: #e8700a; --accent2: #13958a; --good: #1e9e4c; --bad: #d63838;
  --shadow: 0 8px 30px rgba(20, 40, 90, .15);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: var(--font); -webkit-tap-highlight-color: transparent; }
canvas#c { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; }
button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; cursor: pointer; transition: transform .08s, background .15s; }
button:hover { border-color: var(--accent2); }
button:active { transform: scale(.97); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.ghost { background: transparent; }
button.big { width: 100%; padding: 12px; font-size: 1.05rem; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; width: 100%; }
input[type=checkbox] { width: auto; }
input[type=range] { padding: 0; }
kbd { background: var(--panel2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 6px; padding: 1px 6px; font-family: ui-monospace, monospace; font-size: .85em; }
.mono { font-family: ui-monospace, "Cascadia Mono", Menlo, monospace; }
.small { font-size: .82rem; }
.muted { color: var(--muted); }
.err { color: var(--bad); }
.good { color: var(--good); font-weight: 600; }
.bad { color: var(--bad); }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.row.center { justify-content: center; }
.row select { width: auto; flex: 1; }
[hidden] { display: none !important; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
.hud-tl, .hud-tr, .hud-bl { position: absolute; display: flex; flex-direction: column; gap: 4px; }
.hud-tl { top: 12px; left: 12px; }
.hud-tr { top: 12px; right: 12px; align-items: flex-end; }
.hud-bl { bottom: 90px; left: 12px; }
.hud-tl > div, .hud-tr > div, .hud-bl > div { background: var(--panel); border: 1px solid var(--line); padding: 4px 10px; border-radius: 10px; width: fit-content; }
.hud-tl > div:empty, .hud-tr > div:empty, .hud-bl > div:empty { display: none; }
#hud-coords { font-size: 1.05rem; font-weight: 700; letter-spacing: .02em; }
#hud-mode { font-weight: 600; }
#menu-btn { pointer-events: auto; font-size: 1.2rem; padding: 6px 12px; }
#crosshair { position: fixed; left: 50%; top: 50%; width: 22px; height: 22px; margin: -11px 0 0 -11px; pointer-events: none; z-index: 5; }
#crosshair::before, #crosshair::after { content: ""; position: absolute; background: #fff; mix-blend-mode: difference; }
#crosshair::before { left: 10px; top: 2px; width: 2px; height: 18px; }
#crosshair::after { top: 10px; left: 2px; height: 2px; width: 18px; }
#hotbar { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); display: flex; gap: 6px; pointer-events: auto; background: var(--panel); padding: 6px; border-radius: 14px; border: 1px solid var(--line); }
.slot { width: 58px; height: 62px; padding: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; }
.slot i { width: 26px; height: 26px; border-radius: 5px; background: var(--c); box-shadow: inset 0 -5px rgba(0,0,0,.25); }
.slot b { position: absolute; top: 2px; right: 5px; font-size: .78rem; }
.slot small { font-size: .62rem; white-space: nowrap; overflow: hidden; max-width: 54px; }
.slot.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

/* ---------- карточки ---------- */
#card { position: fixed; right: 14px; top: 70px; width: min(360px, calc(100vw - 28px)); background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--accent2); border-radius: var(--radius); padding: 12px 16px; z-index: 8; box-shadow: var(--shadow); backdrop-filter: blur(8px); animation: slideIn .25s ease; }
#card.flash { border-left-color: var(--accent); }
#card h3 { margin: 0 24px 6px 0; font-size: 1.05rem; }
#card p { margin: 6px 0 0; line-height: 1.45; white-space: normal; }
#card .x, .modal .x { position: absolute; top: 6px; right: 8px; background: none; border: none; font-size: 1.3rem; padding: 2px 8px; color: var(--muted); }
.formula { font-family: "Cambria Math", "STIX Two Math", Georgia, serif; font-size: 1.15rem; background: var(--panel2); border-radius: 8px; padding: 6px 10px; margin: 6px 0; color: var(--accent2); }
.formula.sm { font-size: .95rem; padding: 3px 8px; }
#look { position: fixed; left: 12px; top: 170px; width: min(280px, 60vw); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 12px; z-index: 6; font-size: .85rem; max-width: min(420px, 90vw); pointer-events: none; white-space: normal; opacity: .95; }
#look b { display: block; }
#prompt { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 14px; z-index: 6; pointer-events: none; }
#dialog { position: fixed; left: 50%; bottom: 140px; transform: translateX(-50%); width: min(620px, calc(100vw - 24px)); display: flex; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; z-index: 9; box-shadow: var(--shadow); cursor: pointer; animation: slideUp .25s ease; }
#dialog .txt p { margin: 4px 0; line-height: 1.45; }
#dialog small { color: var(--muted); }
.face { width: 56px; height: 56px; flex: none; border-radius: 10px; background: var(--c); position: relative; overflow: hidden; }
.face i { position: absolute; left: 12px; top: 8px; width: 32px; height: 32px; background: var(--s); border-top: 9px solid var(--h); border-radius: 4px; }
.face i::after { content: "• •"; position: absolute; top: 4px; width: 100%; text-align: center; font-size: 13px; color: #222; letter-spacing: 3px; }
.face.big { width: 84px; height: 84px; } .face.big i { left: 18px; top: 12px; width: 48px; height: 48px; border-top-width: 13px; } .face.big i::after { top: 8px; font-size: 18px; }
#toasts { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 30; pointer-events: none; align-items: center; }
.toast { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 16px; box-shadow: var(--shadow); animation: slideDown .25s ease; transition: opacity .4s, transform .4s; text-align: center; max-width: min(560px, 92vw); }
.toast.hide { opacity: 0; transform: translateY(-8px); }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } }
@keyframes slideUp { from { transform: translate(-50%, 20px); opacity: 0; } }
@keyframes slideDown { from { transform: translateY(-10px); opacity: 0; } }

/* ---------- модальные окна ---------- */
.modal-wrap { position: fixed; inset: 0; background: rgba(5, 8, 16, .5); z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; width: min(480px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: var(--shadow); position: relative; backdrop-filter: blur(10px); }
.modal.wide { width: min(760px, 100%); }
.modal.big { width: min(1000px, 100%); height: min(760px, calc(100vh - 32px)); }
.modal header { padding: 14px 44px 8px 18px; border-bottom: 1px solid var(--line); }
.modal header h2 { margin: 0; font-size: 1.15rem; }
.modal .body { padding: 14px 18px 18px; overflow: auto; line-height: 1.45; }
.modal .body h3 { margin: 12px 0 6px; font-size: 1rem; }
.form { display: flex; flex-direction: column; gap: 10px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
.form label.chk, label.chk { flex-direction: row; align-items: center; gap: 6px; color: var(--text); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 .span2 { grid-column: span 2; }
fieldset { border: 1px solid var(--line); border-radius: 10px; display: flex; flex-wrap: wrap; gap: 4px 12px; }
label.sm { font-size: .82rem; }
form.ask { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
form.ask input { flex: 1; min-width: 100px; font-size: 1.1rem; }
form.ask .coord { display: flex; align-items: center; gap: 4px; flex: 1; font-weight: 700; }
.prefix { font-family: "Cambria Math", Georgia, serif; font-size: 1.2rem; }
canvas.graph { width: 100%; height: auto; border-radius: 10px; border: 1px solid var(--line); margin-bottom: 8px; }
label.slider { display: flex; flex-direction: column; gap: 6px; }
.formula-box { margin-top: 10px; background: var(--panel2); border-radius: 10px; padding: 10px; font-family: "Cambria Math", Georgia, serif; }
table.chem { width: 100%; border-collapse: collapse; }
table.chem td { padding: 6px; border-bottom: 1px solid var(--line); }
table.chem button { padding: 2px 10px; }
.chem-res { background: var(--panel2); border-radius: 10px; padding: 10px; margin-top: 10px; }
.chem-res .eq { font-size: 1.25rem; font-family: "Cambria Math", Georgia, serif; color: var(--accent2); }
.stars { font-size: 2.2rem; color: #f2c230; text-align: center; letter-spacing: 4px; }
.reward { font-size: 1.3rem; font-weight: 700; color: var(--good); }
.card-inline { background: var(--panel2); border-radius: 12px; padding: 10px 14px; border-left: 4px solid var(--accent); }

/* ---------- экраны ---------- */
#loading { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; background: var(--bg); z-index: 60; gap: 14px; }
.spinner { width: 46px; height: 46px; border-radius: 8px; background: linear-gradient(135deg, var(--accent), var(--accent2)); animation: spin 1.2s infinite ease-in-out; }
@keyframes spin { 50% { transform: rotate(90deg) scale(.8); border-radius: 50%; } 100% { transform: rotate(180deg); } }
#auth, #menu { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; overflow: auto; }
#auth { background: radial-gradient(ellipse at top, #28406e 0%, var(--bg) 70%); }
:root[data-theme="light"] #auth { background: radial-gradient(ellipse at top, #bcd6ff 0%, var(--bg) 70%); }
#menu { background: rgba(5, 8, 16, .55); }
.auth-box, .menu-box { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 22px; width: min(400px, 100%); box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; }
.logo { display: flex; align-items: center; gap: 12px; justify-content: center; }
.logo h1, .logo h2 { margin: 0; letter-spacing: .02em; }
.cube { width: 34px; height: 34px; background: linear-gradient(135deg, var(--accent) 50%, var(--accent2) 50%); border-radius: 6px; box-shadow: inset -6px -6px rgba(0,0,0,.2); }
.logo.sm .cube { width: 24px; height: 24px; }
.tagline { text-align: center; color: var(--muted); margin: 4px 0 10px; }
.tabs { display: flex; gap: 6px; }
.tabs button { flex: 1; }
.tabs button.on { border-color: var(--accent); color: var(--accent); }
.or { text-align: center; color: var(--muted); margin: 4px 0; }
.who { text-align: center; color: var(--muted); margin-bottom: 6px; }
.menu-box button { text-align: left; }

/* каталог */
.filters { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.map-card { background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 4px; border-top: 4px solid var(--c, var(--accent2)); }
.map-card small { color: var(--muted); max-height: 3.6em; overflow: hidden; }
.mc-meta { font-size: .8rem; color: var(--muted); }
.mc-top { display: flex; gap: 4px; flex-wrap: wrap; }
.badge { font-size: .72rem; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px; }
.badge.ok { color: var(--good); border-color: var(--good); }
.subj-physics { --c: #3a8ad0; } .subj-math { --c: #9a5aff; } .subj-chemistry { --c: #3ad06a; } .subj-programming { --c: #ff8a1f; } .subj-mixed { --c: #f2c230; }
.tpl-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.profile-head { display: flex; gap: 14px; align-items: center; }
.heroes { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.hero-card { text-align: left; display: flex; flex-direction: column; gap: 4px; align-items: flex-start; }
.hero-card.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.hero-card p { margin: 2px 0 0; font-size: .82rem; color: var(--muted); }

/* ---------- строитель ---------- */
#builder { position: fixed; left: 10px; top: 120px; bottom: 10px; width: 300px; z-index: 7; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden; box-shadow: var(--shadow); }
#builder .tools { display: grid; grid-template-columns: repeat(6, 1fr); gap: 3px; padding: 6px; border-bottom: 1px solid var(--line); }
#builder .tools button { padding: 6px 2px; display: flex; flex-direction: column; align-items: center; font-size: 1.1rem; }
#builder .tools button small { font-size: .58rem; color: var(--muted); }
#builder .tools button.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--panel2)); }
#builder .help { font-size: .78rem; color: var(--muted); padding: 6px 10px; }
#builder .pal { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 6px; overflow: auto; flex: 0 1 auto; max-height: 40%; }
#builder .pal.ents { grid-template-columns: repeat(3, 1fr); }
#builder .sw { padding: 4px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .62rem; }
#builder .sw::before { content: ""; width: 24px; height: 24px; border-radius: 5px; background: var(--c); box-shadow: inset 0 -4px rgba(0,0,0,.25); }
#builder .sw.on, #builder .en.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
#builder .en { padding: 4px; display: flex; flex-direction: column; align-items: center; font-size: .66rem; gap: 2px; }
#builder .en i { font-style: normal; font-size: 1.2rem; }
#builder .props { overflow: auto; padding: 0 10px; flex: 1; font-size: .85rem; }
#builder .props h4 { margin: 8px 0 4px; }
#builder .props code { font-size: .75rem; color: var(--muted); }
#builder .props label { display: flex; flex-direction: column; gap: 2px; margin: 5px 0; color: var(--muted); font-size: .8rem; }
#builder .props label.chk { flex-direction: row; color: var(--text); }
#builder .props input, #builder .props select, #builder .props textarea { padding: 5px 8px; font-size: .85rem; }
#builder .pos { font-family: ui-monospace, monospace; color: var(--muted); font-size: .8rem; }
#builder .zones { padding: 6px 10px; overflow: auto; max-height: 40%; }
#builder .zrow { display: flex; align-items: center; gap: 6px; margin: 4px 0; }
#builder .zrow b { flex: 1; }
#builder .zrow button { padding: 2px 8px; }
#builder .actions { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px; border-top: 1px solid var(--line); }
#builder .actions button { flex: 1 1 auto; padding: 6px 8px; font-size: .85rem; }
#test-bar { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 9; }
#test-bar button { background: var(--accent); color: #fff; border: none; }

/* ---------- сенсорное управление ---------- */
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 6; }
#touch .stick { position: absolute; width: 100px; height: 100px; border-radius: 50%; border: 2px solid rgba(255,255,255,.35); background: rgba(0,0,0,.15); opacity: 0; transition: opacity .15s; }
#touch .stick.on { opacity: 1; }
#touch .knob { position: absolute; left: 30px; top: 30px; width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,.55); }
#touch .tbtns { position: absolute; right: 14px; bottom: 90px; display: grid; grid-template-columns: 64px 64px; gap: 10px; pointer-events: auto; }
#touch .tbtns button { width: 64px; height: 64px; border-radius: 50%; font-size: 1.3rem; background: rgba(20,26,44,.55); color: #fff; border: 2px solid rgba(255,255,255,.3); }
#touch [data-t=mode][data-mode=break]::after { content: "⛏"; }
#touch [data-t=mode][data-mode=place]::after { content: "🧱"; }

@media (max-width: 720px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .grid2 { grid-template-columns: 1fr; } .grid2 .span2 { grid-column: auto; }
  #builder { left: 6px; right: 6px; top: auto; bottom: 6px; width: auto; height: 42vh; }
  #card { top: 150px; bottom: auto; max-height: 38vh; overflow: auto; }
  .hud-bl { bottom: 150px; }
  #hotbar { bottom: 6px; max-width: calc(100vw - 170px); overflow-x: auto; left: 8px; transform: none; }
  .slot { width: 50px; height: 54px; }
  #prompt { bottom: 170px; }
  #dialog { bottom: 170px; }
}
