:root {
  --bg: #14261c;
  --bg2: #1b3326;
  --panel: #f6eedb;
  --panel-ink: #2a1a10;
  --ink: #fff7e6;
  --muted: #b9d3c0;
  --outline: #1d1220;
  --yellow: #ffcc33;
  --green: #6fd04a;
  --blue: #4a95ff;
  --red: #ec4a3e;
  --wood: #8a5528;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Fredoka', system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

#app { position: fixed; inset: 0; overflow: hidden; }

.screen { display: none; position: absolute; inset: 0; flex-direction: column; }
.screen.active { display: flex; }

canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }
img { image-rendering: pixelated; }

.hidden { display: none !important; }
.spacer { flex: 1; }

/* ---------------------------------------------------------------- botones */
.btn {
  font: inherit;
  font-weight: 600;
  color: var(--panel-ink);
  background: #f3ead7;
  border: 3px solid var(--outline);
  border-radius: 10px;
  padding: 8px 14px;
  box-shadow: 0 4px 0 var(--outline);
  cursor: pointer;
  transition: transform 0.05s, box-shadow 0.05s, filter 0.1s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--outline); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }
.btn-yellow { background: var(--yellow); }
.btn-green { background: var(--green); }
.btn-blue { background: var(--blue); color: #fff; }
.btn-purple { background: #b07aff; color: #fff; }
.btn-red, .btn.danger { background: var(--red); color: #fff; }
.btn-big { font-size: 1.35rem; padding: 12px 26px; min-width: min(300px, 80vw); }
.btn-small { font-size: 0.92rem; padding: 5px 10px; border-width: 2px; box-shadow: 0 3px 0 var(--outline); }
.btn-small:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--outline); }
.btn-icon { font-size: 1rem; width: 38px; height: 38px; padding: 0; border-width: 2px; box-shadow: 0 3px 0 var(--outline); }

.text-input {
  font: inherit;
  color: var(--panel-ink);
  background: #fffaf0;
  border: 2px solid var(--outline);
  border-radius: 8px;
  padding: 6px 10px;
  min-width: 0;
}

/* ---------------------------------------------------------------- menú */
.menu-bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.menu-content {
  position: relative;
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 16px;
  text-align: center;
}
.logo {
  font-family: 'Press Start 2P', monospace;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  font-size: clamp(2rem, 7vw, 4.2rem);
  color: var(--yellow);
  text-shadow:
    4px 0 var(--outline), -4px 0 var(--outline), 0 4px var(--outline), 0 -4px var(--outline),
    4px 4px var(--outline), -4px 4px var(--outline), 4px -4px var(--outline), -4px -4px var(--outline),
    0 10px 0 #a0441c;
  transform: rotate(-3deg);
}
.logo span:last-child { color: #ff7a3a; }
.tagline {
  margin: 4px 0 6px;
  font-size: 1.15rem;
  font-weight: 600;
  background: rgba(29, 18, 32, 0.6);
  padding: 4px 14px;
  border-radius: 999px;
}
.menu-buttons { display: flex; flex-direction: column; gap: 14px; }
.controls-hint {
  margin-top: 8px;
  font-size: 0.9rem;
  background: rgba(29, 18, 32, 0.55);
  padding: 4px 12px;
  border-radius: 8px;
}

/* ---------------------------------------------------------------- barras */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg2);
  border-bottom: 3px solid var(--outline);
  flex-wrap: wrap;
}
.topbar h2 { margin: 0 8px; font-size: 1.3rem; }

/* ---------------------------------------------------------------- niveles */
.levels-scroll { flex: 1; overflow-y: auto; padding: 8px 16px 32px; }
.levels-scroll h3 { margin: 18px 0 10px; font-size: 1.25rem; color: var(--yellow); }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.level-card {
  background: var(--panel);
  color: var(--panel-ink);
  border: 3px solid var(--outline);
  border-radius: var(--radius);
  box-shadow: 0 5px 0 var(--outline);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.level-card .thumb { width: 100%; height: auto; aspect-ratio: 480 / 272; border-bottom: 3px solid var(--outline); }
.level-card .info { padding: 8px 12px 4px; }
.level-card h4 { margin: 0; font-size: 1.1rem; }
.level-card .meta { font-size: 0.85rem; color: #6b5a48; display: flex; gap: 10px; align-items: center; margin-top: 2px; }
.level-card .badge { font-size: 0.8rem; background: #2a1a10; color: var(--yellow); padding: 1px 8px; border-radius: 999px; }
.level-card .actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px 12px; }
.empty { color: var(--muted); }

/* ---------------------------------------------------------------- juego */
.game-hud {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 12px;
  background: var(--bg2);
  border-bottom: 3px solid var(--outline);
}
.hud-hearts { display: flex; gap: 3px; }
.hud-hearts img { width: 26px; height: 24px; }
.hud-coins { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 1.15rem; }
.hud-coins img { width: 18px; height: 20px; }
.hud-title { flex: 1; text-align: center; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hud-buttons { display: flex; gap: 6px; }

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.stage canvas { border: 3px solid var(--outline); box-sizing: content-box; background: #000; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 16, 12, 0.55);
  padding: 16px;
}
.panel {
  background: var(--panel);
  color: var(--panel-ink);
  border: 4px solid var(--outline);
  border-radius: 16px;
  box-shadow: 0 6px 0 var(--outline);
  padding: 18px 26px;
  text-align: center;
  max-width: 440px;
  animation: pop 0.25s ease-out;
}
.panel h2 { margin: 0 0 8px; font-size: 1.7rem; }
.panel p { margin: 6px 0; font-size: 1.1rem; }
.panel .trophy { font-weight: 700; color: #b06a00; font-size: 1.2rem; }
.panel .coins-line img { width: 16px; vertical-align: -3px; }
.overlay-buttons { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 14px; }
@keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.touch-controls {
  display: none;
  justify-content: space-between;
  align-items: center;
  padding: 8px 18px calc(10px + env(safe-area-inset-bottom));
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
body.touch .touch-controls { display: flex; }
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 52px);
  gap: 6px;
}
.tbtn {
  font: inherit;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--panel-ink);
  background: #f3ead7;
  border: 3px solid var(--outline);
  border-radius: 14px;
  box-shadow: 0 4px 0 var(--outline);
  touch-action: none;
}
.tbtn.on { transform: translateY(3px); box-shadow: 0 1px 0 var(--outline); background: var(--yellow); }
.t-up { grid-column: 2; grid-row: 1; }
.t-left { grid-column: 1; grid-row: 2; }
.t-down { grid-column: 2; grid-row: 2; }
.t-right { grid-column: 3; grid-row: 2; }
.t-jump { width: 104px; height: 104px; border-radius: 50%; background: var(--green); font-size: 1rem; }

/* ---------------------------------------------------------------- editor */
#ed-name { width: 200px; }
.editor-body { flex: 1; display: flex; min-height: 0; }
.palette {
  width: 210px;
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--bg2);
  border-right: 3px solid var(--outline);
  padding: 4px 10px 16px;
}
.palette h4 { margin: 12px 0 6px; color: var(--yellow); font-size: 0.95rem; text-transform: uppercase; letter-spacing: 0.04em; }
.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.tool {
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px 4px;
  background: #2b4a37;
  color: var(--ink);
  border: 2px solid var(--outline);
  border-radius: 10px;
  cursor: pointer;
}
.tool img { width: 34px; height: 34px; object-fit: contain; }
.tool:hover { background: #355a43; }
.tool.active { background: var(--yellow); color: var(--panel-ink); box-shadow: inset 0 0 0 2px #fff; }
.editor-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.minimap-row { display: flex; justify-content: center; padding: 0 8px 6px; }
.minimap {
  max-width: 100%;
  border: 2px solid var(--outline);
  border-radius: 4px;
  cursor: pointer;
  touch-action: none;
  image-rendering: auto;
}
.minimap[hidden] { display: none; }
#ed-width, #ed-height { width: auto; }
#editor-canvas { cursor: crosshair; touch-action: none; }
.editor-info {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 14px;
  min-height: 54px;
  background: var(--bg2);
  border-top: 3px solid var(--outline);
}
.hint { color: var(--muted); font-size: 0.95rem; }
.editor-info .hint { flex: 1 1 260px; }
.inspector { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.insp-label { font-size: 0.9rem; margin-left: 6px; }
.insp-check { display: flex; align-items: center; gap: 4px; font-size: 0.9rem; }
.insp-highlight {
  background: var(--yellow);
  color: var(--panel-ink);
  border: 2px solid var(--outline);
  border-radius: 8px;
  padding: 3px 8px;
  font-weight: 700;
  cursor: pointer;
}
.chan {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--chan);
  border: 2px solid var(--outline);
  cursor: pointer;
}
.chan.active { box-shadow: 0 0 0 3px #fff; }

/* ---------------------------------------------------------------- diálogos */
.dialog {
  background: var(--panel);
  color: var(--panel-ink);
  border: 4px solid var(--outline);
  border-radius: 16px;
  box-shadow: 0 8px 0 var(--outline);
  padding: 18px 22px;
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
}
.dialog.wide { width: min(1000px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(10, 16, 12, 0.6); }
.dialog h2 { margin: 0 0 8px; }
.dialog p { margin: 6px 0 10px; }
.dialog .hint { color: #6b5a48; font-size: 0.9rem; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 14px; }
.field { display: block; margin: 10px 0; font-weight: 600; }
.copy-row { display: flex; gap: 6px; margin-top: 4px; }
.copy-row .text-input { flex: 1; }
textarea.text-input { width: 100%; resize: vertical; }
.export-options { display: flex; flex-wrap: wrap; gap: 10px; }

.import-cols { display: grid; grid-template-columns: 280px 1fr; gap: 18px; }
.import-left h4 { margin: 4px 0 8px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; margin-bottom: 10px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 0.88rem; }
.legend-item .sw { width: 18px; height: 18px; border: 2px solid var(--outline); border-radius: 4px; flex-shrink: 0; }
.legend-item.extra { grid-column: 1 / -1; }
.legend-item code { font-size: 0.72rem; color: #8a7a68; margin-left: auto; }
.dropzone {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 110px;
  padding: 16px;
  border: 3px dashed #8a7a68;
  border-radius: 12px;
  background: #fffaf0;
  cursor: pointer;
  font-weight: 600;
}
.dropzone.over { border-color: var(--blue); background: #eef5ff; }
.import-preview { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.import-preview figure { margin: 0; }
.import-preview figcaption { font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
.import-preview img, .import-preview canvas {
  width: 100%;
  aspect-ratio: 480 / 272;
  object-fit: fill;
  border: 2px solid var(--outline);
  border-radius: 6px;
  background: #fff;
}
.options-grid {
  display: grid;
  grid-template-columns: auto 1fr 3.5em;
  gap: 10px 12px;
  align-items: center;
  margin: 8px 0 14px;
  font-weight: 600;
}
.options-grid input[type="range"] { width: 100%; accent-color: #e2412f; }
.options-grid output { text-align: right; font-variant-numeric: tabular-nums; }
.track-list { list-style: none; margin: 6px 0 10px; padding: 0; max-height: 240px; overflow-y: auto; }
.track-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-bottom: 1px solid #e2d6bd;
}
.track-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.track-list .meta { font-size: 0.82rem; color: #8a7a68; white-space: nowrap; }
.track-list .btn-icon { width: 32px; height: 32px; }
.row-actions { display: flex; gap: 8px; }
.menu-account { position: absolute; top: 12px; right: 12px; z-index: 2; }
.account-slot { display: flex; }
.account-chip { display: flex; align-items: center; gap: 6px; }
.account-chip .nick { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.section-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin-top: 18px; }
.section-head h3 { margin: 0; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.tabs .btn.active { background: var(--yellow); }
.more { display: block; margin: 12px auto 0; }
.banner {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  background: var(--panel); color: var(--panel-ink);
  border: 3px solid var(--outline); border-radius: 12px; padding: 10px 14px; margin-bottom: 12px;
}
.banner p { margin: 0; flex: 1 1 240px; }
.level-card .by { font-size: 0.85rem; color: #6b5a48; }
.status { font-size: 0.75rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; border: 2px solid var(--outline); }
.status.draft { background: #e2d6bd; }
.status.published { background: var(--green); }
.status.hidden { background: var(--red); color: #fff; }
.btn.liked { background: #ff8fb1; }
.oauth-buttons { display: grid; gap: 8px; margin: 10px 0; }
.oauth-buttons .btn { width: 100%; }
.or { display: flex; align-items: center; gap: 10px; color: #8a7a68; font-size: 0.85rem; margin: 6px 0; }
.or::before, .or::after { content: ''; flex: 1; border-top: 2px dashed #d6c8a8; }
.stack { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.stack .field { margin: 0; }
.stack .field .text-input { width: 100%; margin-top: 4px; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 0.92rem; }
.check input { margin-top: 3px; }
.link { background: none; border: none; color: #2f62c4; text-decoration: underline; cursor: pointer; font: inherit; align-self: flex-start; padding: 0; }
.error-text { color: #c0261c; font-weight: 600; margin: 0; min-height: 1.2em; }
.account-extra { margin-top: 14px; border-top: 2px dashed #d6c8a8; padding-top: 10px; }
.admin-row {
  display: grid; grid-template-columns: 160px 1fr auto; gap: 12px; align-items: center;
  background: var(--panel); color: var(--panel-ink); border: 3px solid var(--outline); border-radius: 12px;
  padding: 10px; margin-bottom: 10px;
}
.admin-row canvas { width: 160px; height: auto; border-radius: 6px; border: 2px solid var(--outline); }
.admin-row .actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.admin-row ul { margin: 4px 0 0; padding-left: 18px; font-size: 0.85rem; }
.admin-users { width: 100%; border-collapse: collapse; background: var(--panel); color: var(--panel-ink); border-radius: 12px; overflow: hidden; }
.admin-users th, .admin-users td { padding: 6px 8px; border-bottom: 1px solid #e2d6bd; text-align: left; font-size: 0.9rem; }
.admin-search { display: flex; gap: 8px; margin-bottom: 12px; }
@media (max-width: 760px) { .admin-row { grid-template-columns: 1fr; } }
.random-options { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-bottom: 8px; }
.segmented { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.segmented .btn.active { background: var(--yellow); }
.random-name { margin: 6px 0; }
.shared-thumb { width: 100%; aspect-ratio: 480 / 272; border: 3px solid var(--outline); border-radius: 8px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 40px);
  background: var(--outline);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--red); }

/* ---------------------------------------------------------------- móvil */
@media (max-width: 760px) {
  .editor-body { flex-direction: column-reverse; }
  .palette {
    width: auto;
    border-right: none;
    border-top: 3px solid var(--outline);
    display: flex;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px 8px;
    max-height: 104px;
  }
  .palette h4 { display: none; }
  .tools { display: flex; gap: 6px; }
  .tool { min-width: 62px; }
  #ed-name { width: 130px; }
  .import-cols { grid-template-columns: 1fr; }
  .import-preview { grid-template-columns: 1fr; }
  .hud-title { display: none; }
  .game-hud { gap: 10px; }
}
