:root {
  --bg: #222222;
  --surface: #1a1a1a;
  --raised: #262626;
  --raised-hover: #2f2f2f;
  --border: rgba(255, 255, 255, .06);
  --border-strong: rgba(255, 255, 255, .12);
  --text: #ededed;
  --text-2: #a1a1a1;
  --text-3: #6b6b6b;
  --side: #C084FC;
  --ok: #4ADE80;
  --warn: #FBBF24;
  --gutter: clamp(16px, 5vw, 72px);
  --radius: 20px;
  --mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  --ease: cubic-bezier(.2, .7, .2, 1);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: Inter, 'Segoe UI', Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { flex: none; }

:focus-visible { outline: 2px solid rgba(255, 255, 255, .8); outline-offset: 2px; }

.app {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* ---------- Cabecera ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px var(--gutter) 0;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--text);
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -.02em;
  text-decoration: none;
}
.brand-mark { width: 26px; height: 26px; color: var(--text); }

.top-actions { display: flex; align-items: center; gap: 8px; }

.lang-link {
  display: grid;
  place-items: center;
  height: 40px;
  min-width: 46px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .04em;
  text-decoration: none;
  transition: color .15s, border-color .15s;
}
.lang-link:hover { color: var(--text); border-color: var(--border-strong); }

/* Aviso "también disponible en otro idioma" */
.lang-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100% - 32px);
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: #1c1c1c;
  box-shadow: 0 14px 40px rgba(0, 0, 0, .45);
  color: var(--text-2);
  font-size: 13px;
  transform: translateX(-50%);
  animation: fade .3s var(--ease);
}
.lang-toast a { color: var(--text); font-weight: 600; text-decoration: none; white-space: nowrap; }
.lang-toast a:hover { text-decoration: underline; }
.lang-toast button {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: var(--raised);
  color: var(--text-2);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.lang-toast button:hover { color: var(--text); }
/* En escritorio va arriba, en el hueco entre el logo y los botones, para no tapar nada. */
@media (min-width: 900px) {
  .lang-toast { top: 20px; bottom: auto; }
}

.size-picker { position: relative; }

.size-btn {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  height: 40px;
  padding: 0 16px 0 20px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.size-btn:hover { border-color: var(--border-strong); }
.caret { width: 11px; height: 8px; color: var(--text-2); transition: transform .2s var(--ease); }
.size-btn[aria-expanded="true"] .caret { transform: rotate(180deg); }

.menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 10;
  min-width: 168px;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: #1c1c1c;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .5);
  animation: pop .16s var(--ease);
}
.menu li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 8px 12px;
  border-radius: 9px;
  font-weight: 600;
  cursor: pointer;
}
.menu li svg { width: 15px; height: 15px; }
.menu li[aria-selected="false"] svg { visibility: hidden; }
.menu li:not([aria-disabled="true"]):hover { background: var(--raised); }
.size-btn.loading { cursor: progress; }
.size-btn.loading #sizeLabel { opacity: .5; }
.menu li[aria-selected="true"] { background: var(--raised); }
.menu li[aria-disabled="true"] { color: var(--text-3); cursor: default; }
.menu small {
  padding: 1px 7px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
}

@keyframes pop {
  from { opacity: 0; transform: translateY(-4px) scale(.98); }
}

/* ---------- Barra de selección ---------- */

.command {
  display: flex;
  justify-content: center;
  padding: 14px var(--gutter) 0;
}

.command-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .22);
}

.slot {
  display: inline-flex;
  align-items: center;
  height: 46px;
  border-radius: 999px;
  transition: background .15s, box-shadow .15s;
}
.slot:hover { background: rgba(255, 255, 255, .035); }
.slot.is-active { background: rgba(255, 255, 255, .05); box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, .5); }

.slot-main {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  height: 100%;
  min-width: 178px;
  padding: 0 16px 0 8px;
  border: 0;
  border-radius: 999px;
  background: none;
  cursor: pointer;
}
.slot.is-filled .slot-main { padding-right: 4px; }

.slot-num {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 1.5px solid var(--text-3);
  border-radius: 50%;
  color: var(--text-3);
  font-size: 12px;
  font-weight: 700;
  transition: all .15s;
}
.slot.is-active .slot-num { border-color: #fff; color: #fff; }
.slot.is-filled .slot-num { border-color: #fff; background: #fff; color: #111; }

.slot-placeholder { color: var(--text-3); }
.slot.is-active .slot-placeholder { color: var(--text-2); }
.hand { width: 18px; height: 18px; margin-left: auto; color: var(--text-3); }
.slot.is-active .hand { color: var(--text); }

.dots { display: inline-flex; align-items: center; gap: 3px; }
.dots i { width: 10px; height: 10px; border-radius: 3px; background: var(--c); }
.dots i.main { width: 16px; height: 16px; border-radius: 4px; box-shadow: 0 0 0 1.5px var(--surface), 0 0 0 3px rgba(255, 255, 255, .85); margin-right: 3px; }

.slot-name { font-family: var(--mono); font-size: 14px; font-weight: 700; }
.slot-type { color: var(--text-3); font-size: 12px; }

.slot-clear {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  margin-right: 8px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
}
.slot-clear svg { width: 14px; height: 14px; }
.slot-clear:hover { background: var(--raised); color: var(--text); }

.por { padding: 0 6px; color: var(--text-3); font-size: 13px; }

.btn-primary {
  height: 46px;
  margin-left: 6px;
  padding: 0 26px;
  border: 0;
  border-radius: 999px;
  background: #fff;
  color: #111;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, color .15s, transform .1s;
}
.btn-primary:hover:not(:disabled) { background: #e8e8e8; }
.btn-primary:active:not(:disabled) { transform: scale(.97); }
.btn-primary:disabled { background: var(--raised); color: var(--text-3); cursor: not-allowed; }

/* ---------- Zona de trabajo ---------- */

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(340px, 1fr);
  gap: clamp(20px, 3vw, 40px);
  width: 100%;
  max-width: 1360px;
  margin: 0 auto;
  padding: 14px var(--gutter) 20px;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.viewport { position: absolute; inset: 0; }
.viewport canvas { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
.viewport canvas:active { cursor: grabbing; }

.view-badge {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(26, 26, 26, .8);
  backdrop-filter: blur(6px);
  color: var(--text-3);
  font-size: 12px;
  pointer-events: none;
}
.view-badge b { color: var(--text-2); font-weight: 600; }
.view-badge .sep-dot { margin: 0 6px; }

.swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin: 0 5px 0 1px;
  border-radius: 3px;
  vertical-align: -1px;
  background: var(--c);
}

.stage-foot {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}
.stage-foot > * { pointer-events: auto; }

.hint { margin: 0; color: var(--text-3); font-size: 12px; line-height: 1.7; }
.keys { display: block; }
kbd {
  display: inline-block;
  min-width: 17px;
  margin-right: 2px;
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  color: var(--text-2);
  font-family: var(--mono);
  font-size: 10.5px;
  line-height: 16px;
  text-align: center;
}

.stage-tools { display: flex; gap: 6px; }

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 13px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: rgba(26, 26, 26, .8);
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.tool-btn svg { width: 14px; height: 14px; }
.tool-btn:hover { color: var(--text); border-color: rgba(255, 255, 255, .25); }

/* ---------- Panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  align-self: center;
  min-width: 0;
  max-height: 100%;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 6px 10px 8px;
}
.panel-title { margin: 0; font-size: 15px; font-weight: 700; }

/* Pestañas de modo: Permutar / Patrones */
.tabs {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
}
.tab {
  height: 30px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-2);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { background: var(--raised-hover); color: var(--text); }

/* En modo patrones no se eligen piezas */
.mode-patterns .command,
.mode-patterns #orientToggle { display: none; }

.pat-intro { margin: 0; color: var(--text-2); font-size: 13.5px; }

.head-actions { display: flex; gap: 6px; }

/* Indicador de capa elegida con el teclado (2 + R...) */
.key-prefix {
  position: absolute;
  top: 0;
  right: 0;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: rgba(26, 26, 26, .9);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-3);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.toggle:hover { color: var(--text-2); }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track {
  position: relative;
  width: 28px;
  height: 16px;
  border-radius: 999px;
  background: var(--raised-hover);
  transition: background .15s;
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #9a9a9a;
  transition: transform .18s var(--ease), background .15s;
}
.toggle input:checked + .toggle-track { background: #ededed; }
.toggle input:checked + .toggle-track::after { transform: translateX(12px); background: #1a1a1a; }
.toggle input:checked ~ span:last-child { color: var(--text); }
.toggle input:focus-visible + .toggle-track { outline: 2px solid rgba(255, 255, 255, .8); outline-offset: 2px; }

.algo-box {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 250px;
  overflow-y: auto;
  padding: 20px 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  scrollbar-width: thin;
  scrollbar-color: #3a3a3a transparent;
}
.algo-box > * { animation: fade .25s var(--ease); }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
}

/* Estado vacío */
.steps { display: grid; gap: 14px; margin: 0; padding: 0; list-style: none; }
.steps li { display: flex; gap: 12px; align-items: flex-start; }
.step-n {
  flex: none;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  margin-top: 1px;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  color: var(--text-2);
  font-size: 11.5px;
  font-weight: 700;
}
.steps li.done .step-n { border-color: var(--ok); color: var(--ok); }
.steps b { display: block; font-weight: 600; }
.steps span { color: var(--text-2); font-size: 13px; }
.tip {
  margin: 2px 0 0;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  color: var(--text-3);
  font-size: 12.5px;
}

.loading {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-2);
}
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--raised-hover);
  border-top-color: var(--text);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Mensajes */
.msg { display: flex; gap: 12px; }
.msg-icon {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(251, 191, 36, .14);
  color: var(--warn);
  font-weight: 800;
  font-size: 13px;
}
.msg p { margin: 0; font-weight: 600; }
.msg p + p { margin-top: 4px; color: var(--text-2); font-size: 13px; font-weight: 400; }

/* Resultado */
.algo-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.algo-meta { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.count { color: var(--text-2); font-size: 13px; }
.count b { color: var(--text); font-size: 20px; font-weight: 700; margin-right: 2px; }
.algo-name { color: var(--text-3); font-size: 13px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.icon-btn svg { width: 15px; height: 15px; }
.icon-btn:hover { color: var(--text); border-color: rgba(255, 255, 255, .25); }
.icon-btn.copied { color: var(--ok); border-color: var(--ok); }

.moves { display: flex; flex-wrap: wrap; gap: 6px; }
.mv {
  min-width: 40px;
  padding: 6px 8px;
  border-radius: 10px;
  background: var(--raised);
  font-family: var(--mono);
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  transition: background .2s, color .2s, opacity .2s;
}
.moves.dense { gap: 5px; }
.moves.dense .mv { min-width: 32px; padding: 4px 6px; border-radius: 8px; font-size: 14.5px; }
.moves.denser { gap: 4px; }
.moves.denser .mv { min-width: 28px; padding: 3px 5px; border-radius: 7px; font-size: 13px; }
.moves.densest { gap: 3px; }
.moves.densest .mv { min-width: 24px; padding: 2px 4px; border-radius: 6px; font-size: 12px; }

details.structure { margin: -4px 0 0; }
details.structure summary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-3);
  font-size: 12.5px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
details.structure summary::-webkit-details-marker { display: none; }
details.structure summary::before {
  content: '';
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform .15s;
}
details.structure[open] summary::before { transform: rotate(45deg); }
details.structure summary:hover { color: var(--text-2); }
details.structure p { margin: 6px 0 0; }
.mv.done { opacity: .3; }
.mv.next { background: #fff; color: #111; }

.structure { margin: -4px 0 0; color: var(--text-3); font-size: 12.5px; line-height: 1.8; }
.structure b { color: var(--text-2); font-weight: 600; }
.structure code {
  padding: 2px 6px;
  border-radius: 5px;
  background: var(--raised);
  color: var(--text-2);
  font-family: var(--mono);
  font-size: 11.5px;
}

.player { display: flex; align-items: center; gap: 6px; }
.ctrl {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--raised);
  color: var(--text);
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
.ctrl svg { width: 13px; height: 13px; }
.ctrl:hover:not(:disabled) { background: var(--raised-hover); }
.ctrl:disabled { opacity: .3; cursor: default; }
.ctrl.play {
  display: inline-flex;
  gap: 8px;
  width: auto;
  min-width: 124px;
  padding: 0 16px;
  border-radius: 999px;
  background: #fff;
  color: #111;
  font-size: 13.5px;
  font-weight: 700;
}
.ctrl.play:hover:not(:disabled) { background: #e8e8e8; }
.ctrl.speed { width: auto; padding: 0 12px; border-radius: 999px; font-family: var(--mono); font-size: 12px; font-weight: 600; }
.player-status { margin-left: auto; color: var(--text-3); font-family: var(--mono); font-size: 12px; white-space: nowrap; }
.player-status.ok { color: var(--ok); font-family: inherit; font-weight: 600; }

.facts {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.fact { display: flex; gap: 10px; margin: 0; color: var(--text-2); font-size: 13px; }
.fact b { color: var(--text); font-weight: 600; }
.fact-dot { width: 8px; height: 8px; margin: 6px 1px 0; border-radius: 50%; background: var(--side); box-shadow: 0 0 0 3px rgba(192, 132, 252, .18); }
.fact-icon { width: 10px; height: 10px; margin: 5px 0 0; color: var(--text-3); }
.fact .muted { color: var(--text-3); }
.fact.ok .fact-icon { width: 13px; height: 13px; margin-top: 3px; color: var(--ok); }
.fact.ok b { color: var(--ok); }
.fact code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--raised);
  color: var(--text);
  font-family: var(--mono);
  font-size: 11.5px;
}
.alt.clean::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ok);
}

.alts { display: grid; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.alts-title { margin: 0; color: var(--text-3); font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
.alts-list { display: flex; flex-wrap: wrap; gap: 6px; }
.alt {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: transparent;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.alt span { color: var(--text-3); font-family: var(--mono); font-size: 11.5px; font-weight: 500; }
.alt code { font-family: var(--mono); font-size: 11.5px; font-weight: 600; }
.alt:hover { border-color: rgba(255, 255, 255, .28); }
.alt[aria-pressed="true"] { border-color: #fff; background: #fff; color: #111; }
.alt[aria-pressed="true"] span { color: #555; }

/* ---------- Escritorio: todo cabe en la pantalla ---------- */

@media (min-width: 900px) {
  body { overflow: hidden; }
  .app { height: 100vh; height: 100dvh; }
  .workspace { flex: 1; min-height: 0; grid-template-rows: minmax(0, 1fr); }
}

@media (min-width: 900px) and (max-height: 640px) {
  .topbar { padding-top: 12px; }
  .command { padding-top: 8px; }
  .workspace { padding-top: 8px; padding-bottom: 12px; }
  .algo-box { gap: 12px; padding: 16px 18px; min-height: 0; }
  .keys { display: none; }
}

/* ---------- Móvil / tablet ---------- */

@media (max-width: 899px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .stage { height: 420px; }
  .panel { align-self: stretch; }
}

@media (max-width: 640px) {
  .command-bar {
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
    border-radius: 26px;
  }
  .slot, .slot-main { flex: 1; min-width: 0; }
  .por { display: none; }
  .btn-primary { width: 100%; margin: 2px 0 0; }
  .stage { height: 360px; }
  .keys { display: none; }
  .algo-box { padding: 18px 16px; }
  .mv { font-size: 15px; min-width: 34px; }
}
