/* ════════════════════════════════════════════════════════
   style.css — Karel en Català
   Seccions:
   1.  Variables i tema
   2.  Reset
   3.  Capçalera (topbar)
   4.  Layout principal (index.html)
   5.  Columna del mapa
   6.  Columna de codi
   7.  Barra d'estat
   8.  Controls d'execució
   9.  Indicador d'estat
   10. Botons
   11. Editor de codi (numeració + ressaltat)
   12. Colors del ressaltat sintàctic
   13. Log i handle de redimensió
   14. Modals
   15. Reptes
   16. Layout edit-mapa.html
   17. Llegenda (edit-mapa)
   18. Mode clar
   19. Responsive
════════════════════════════════════════════════════════ */


/* ── 1. VARIABLES I TEMA ──────────────────────────── */

:root {
  --bg:         #0d0d0d;
  --surface:    #151515;
  --border:     #2b2b2b;
  --text:       #ddddd8;
  --muted:      #52524e;
  --accent:     #f0c030;
  --accent-dim: rgba(240, 192, 48, 0.12);
  --error:      #e05555;
  --error-dim:  rgba(224, 85, 85, 0.12);
  --mono:       'Space Mono', 'Courier New', monospace;
  --cell-size:  44px;
  --topbar-h:   44px;
}


/* ── 2. RESET ─────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--mono);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-size: 13px;
}


/* ── 3. CAPÇALERA ─────────────────────────────────── */

.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 14px;
  flex-shrink: 0;
  z-index: 10;
}

.logo {
  font-family: 'Syne', sans-serif;
  font-size: 0.9rem;
  font-weight: 800;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 7px;
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
  align-items: center;
}

a.btn { text-decoration: none; display: inline-flex; align-items: center; }


/* ── 4. LAYOUT PRINCIPAL (index.html) ─────────────── */
/*
   Marges laterals: 5% a cada costat
   Columna mapa:   ~30% de la pantalla total
   Columna codi:   ~60% de la pantalla total
   (1fr + 2fr dins del 90% restant = 30% + 60%)
*/

.main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 2fr;
  padding: 0 5%;
  gap: 0;
  overflow: hidden;
  min-height: 0;
}


/* ── 5. COLUMNA DEL MAPA ──────────────────────────── */

.map-col {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
  min-height: 0;
}

/* Àrea del món: ocupa l'espai restant i centra la graella */
.world-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  overflow: hidden;
  background: var(--bg);
  position: relative;
  min-height: 0;
}

#world-grid {
  display: inline-grid;
  gap: 2px;
  background: var(--border);
  padding: 2px;
  border-radius: 5px;
  flex-shrink: 0;
}


/* ── 6. COLUMNA DE CODI ───────────────────────────── */

.code-col {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}


/* ── 7. BARRA D'ESTAT ─────────────────────────────── */

.status-bar {
  display: flex;
  gap: 12px;
  font-size: 0.65rem;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 5px 14px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.status-bar strong { color: var(--text); }


/* ── 8. CONTROLS D'EXECUCIÓ ───────────────────────── */

.exec-controls {
  padding: 10px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btns { display: flex; gap: 5px; flex-wrap: wrap; }

.speed-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.62rem;
}

input[type=range] { flex: 1; accent-color: var(--accent); cursor: pointer; }

#speed-lbl { color: var(--text); min-width: 62px; }


/* ── 9. INDICADOR D'ESTAT ─────────────────────────── */

#state-badge {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}

#state-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.25s, box-shadow 0.25s;
  flex-shrink: 0;
}

#state-dot.running {
  background: var(--accent);
  box-shadow: 0 0 7px var(--accent);
  animation: pulse-dot 1s ease-in-out infinite;
}

#state-dot.step  { background: var(--accent); box-shadow: 0 0 7px var(--accent); }
#state-dot.error { background: var(--error);  box-shadow: 0 0 7px var(--error); }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.35; }
}


/* ── 10. BOTONS ───────────────────────────────────── */

.btn {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 3px;
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.1s, color 0.1s, background 0.1s, transform 0.08s;
  white-space: nowrap;
}

.btn:hover  { border-color: var(--accent); color: var(--accent); }
.btn:active { transform: scale(0.96); }

.btn.p { background: var(--accent); color: #000; border-color: var(--accent); }
.btn.p:hover { background: #e8b800; color: #000; border-color: #e8b800; }

.btn.g { background: transparent; color: var(--accent); border-color: var(--accent); }
.btn.g:hover { background: var(--accent-dim); }

.btn.r { background: transparent; color: var(--muted); border-color: var(--border); }
.btn.r:hover { border-color: var(--muted); color: var(--text); }

.btn:disabled       { opacity: 0.4; cursor: default; }
.btn:disabled:hover { border-color: var(--border); color: var(--text); }

.btn.full { width: 100%; text-align: left; padding: 8px 12px; font-size: 0.7rem; }
.btn.small { padding: 2px 7px; font-size: 0.65rem; }


/* ── 11. EDITOR DE CODI ───────────────────────────── */

.editor-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
  background: #0a0a0a;
  min-height: 0;
}

#line-numbers {
  width: 36px;
  padding: 12px 6px 12px 0;
  background: #0a0a0a;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.75;
  text-align: right;
  user-select: none;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  overflow: hidden;
  pointer-events: none;
}

#line-numbers div { height: calc(0.8rem * 1.75); }

.editor-inner {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#code-highlight {
  position: absolute;
  inset: 0;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.75;
  tab-size: 2;
  white-space: pre;
  overflow: hidden;
  pointer-events: none;
  color: var(--text);
  margin: 0;
}

#code-editor {
  position: absolute;
  inset: 0;
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  font-family: var(--mono);
  font-size: 0.8rem;
  border: none;
  outline: none;
  resize: none;
  padding: 12px 14px;
  line-height: 1.75;
  tab-size: 2;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 1;
}

#code-editor::selection { background: rgba(240, 192, 48, 0.2); }

/* Línia activa / error durant l'execució */
.code-line       { display: block; min-height: calc(0.8rem * 1.75); border-left: 2px solid transparent; padding-left: 2px; transition: background 0.1s; }
.code-line.active { background: var(--accent-dim); border-left-color: var(--accent); }
.code-line.error  { background: var(--error-dim);  border-left-color: var(--error); }


/* ── 12. COLORS DEL RESSALTAT SINTÀCTIC ────────────── */

.hl-kw   { color: var(--accent); font-weight: 700; }  /* si, mentre, repeteix… */
.hl-cmd  { color: #9cdcfe; }                           /* avança, gira.dreta… */
.hl-cond { color: #c586c0; }                           /* veu-paret, veu-aigua… */
.hl-user { color: #4ec9b0; }                           /* procediments de l'usuari */
.hl-num  { color: var(--accent); }
.hl-cm   { color: var(--muted); font-style: italic; }
.hl-br   { color: var(--muted); }


/* ── 13. LOG I HANDLE DE REDIMENSIÓ ────────────────── */

#log-wrap {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  height: 130px;
  min-height: 50px;
  max-height: 55%;
  border-top: 1px solid var(--border);
}

#log-resize {
  height: 5px;
  background: var(--border);
  cursor: ns-resize;
  flex-shrink: 0;
  transition: background 0.15s;
  position: relative;
}

#log-resize::after {
  content: '';
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 28px; height: 2px;
  border-radius: 1px;
  background: var(--muted);
  opacity: 0.5;
}

#log-resize:hover,
#log-resize.dragging { background: var(--accent); }

#log-resize:hover::after,
#log-resize.dragging::after { background: #000; opacity: 0.5; }

#log {
  flex: 1;
  overflow-y: auto;
  background: #080808;
  padding: 6px 12px;
  font-size: 0.68rem;
  line-height: 1.65;
}

#log p      { margin: 0; white-space: pre-wrap; }
#log .ok    { color: var(--accent); }
#log .err   { color: var(--error); font-weight: 700; }
#log .inf   { color: var(--text); opacity: 0.75; }
#log .dim   { color: var(--muted); }
#log .cmd   { color: var(--muted); }
#log .proc  { color: #4ec9b0; }


/* ── 14. MODALS ────────────────────────────────────── */

.modal-bg {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .75);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.modal-bg.open { display: flex; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 440px;
  max-width: 95vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .7);
}

.modal-wide { width: 680px; }

.modal-hd {
  padding: 10px 14px;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.modal-hd button { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 0 2px; }
.modal-hd button:hover { color: var(--text); }

.modal-ft {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  background: var(--surface);
  flex-shrink: 0;
}

.blank-form {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #0a0a0a;
}

.blank-form .hint { font-size: 0.6rem; color: var(--muted); line-height: 1.6; }

.blank-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.68rem;
  color: var(--muted);
}

.blank-form input[type=number] {
  width: 64px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.78rem;
  padding: 5px 8px;
  outline: none;
}

.blank-form input[type=number]:focus { border-color: var(--accent); }


/* ── 15. REPTES ────────────────────────────────────── */

#challenges-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 14px;
  overflow-y: auto;
}

.ch-card {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 5px;
  cursor: pointer;
  background: #0d0d0d;
  transition: border-color 0.1s, background 0.1s;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.ch-card:hover { border-color: var(--accent); background: var(--accent-dim); }

.ch-num   { font-size: 0.55rem; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); }
.ch-title { font-size: 0.78rem; font-weight: 700; color: var(--text); }
.ch-desc  { font-size: 0.63rem; color: var(--muted); line-height: 1.65; }
.ch-desc em   { color: var(--accent); font-style: normal; }
.ch-desc code { color: #9cdcfe; font-family: var(--mono); }

.ch-tag { display: inline-block; margin-top: 4px; padding: 1px 6px; border-radius: 3px; background: var(--border); color: var(--muted); font-size: 0.55rem; text-transform: uppercase; letter-spacing: 0.8px; }
.ch-tag.easy   { background: rgba(80,180,80,.15);  color: #80c880; }
.ch-tag.medium { background: rgba(240,192,48,.15); color: var(--accent); }
.ch-tag.hard   { background: rgba(224,85,85,.15);  color: var(--error); }


/* ── 16. LAYOUT EDIT-MAPA.HTML ──────────────────────── */

body.edit-page {
  overflow: hidden;
}

.edit-layout {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

/* Panel d'eines d'edició (columna esquerra) */
.edit-tools {
  width: 200px;
  flex-shrink: 0;
  background: #0a0a0a;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
}

.tool-section {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tool-lbl {
  font-size: 0.58rem;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  margin-bottom: 2px;
}

.tool-btns { display: flex; flex-direction: column; gap: 5px; }

/* Àrea del mapa (sempre en mode edició) */
.edit-world-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.world-area.editing-always { cursor: crosshair; }
.world-area.editing-always .cell { cursor: crosshair; }

.world-area.editing-always .cell::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 3px;
  border: 2px solid transparent;
  pointer-events: none;
  transition: border-color 0.05s;
}

.world-area.editing-always .cell:hover::after { border-color: var(--accent); }

.edit-status { border-top: 1px solid var(--border); border-bottom: none; }

/* Botons de pinzell */
.brush-row { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }

.brush-btn {
  padding: 6px 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  font-family: var(--mono);
  font-size: 0.62rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface);
  color: var(--muted);
  transition: all 0.1s;
  text-align: center;
}

.brush-btn:hover  { border-color: var(--accent); color: var(--accent); }
.brush-btn.active { background: var(--accent); color: #000; border-color: var(--accent); }

/* Botons de direcció */
.dir-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; }

.dir-btn {
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--surface);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: center;
  transition: all 0.1s;
}

.dir-btn:hover  { border-color: var(--accent); color: var(--accent); }
.dir-btn.active { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); }

/* Controls de mida */
.resize-table { display: grid; grid-template-columns: auto 1fr; gap: 5px 8px; align-items: center; font-size: 0.62rem; color: var(--muted); }
.resize-ctrl  { display: flex; align-items: center; gap: 5px; }

.rbtn {
  width: 22px; height: 22px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--surface);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.9rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: all 0.1s;
  padding: 0;
}

.rbtn:hover { border-color: var(--accent); color: var(--accent); }
.rval { min-width: 22px; text-align: center; color: var(--text); font-weight: 700; font-size: 0.75rem; }


/* ── 17. LLEGENDA (edit-mapa) ───────────────────────── */

.legend { gap: 5px; }
.li { display: flex; align-items: center; gap: 7px; font-size: 0.62rem; color: var(--muted); }
.lbox { width: 13px; height: 13px; border-radius: 2px; flex-shrink: 0; border: 1px solid var(--border); }
.lbox-karel { background: #1e1500; border-color: var(--accent); }
.lbox-wall  { background: #1e1e1e; }
.lbox-water { background: #181510; }
.lbox-empty { background: #111111; }


/* ── 18. CEL·LES DEL MÓN ───────────────────────────── */

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  user-select: none;
}

.c-e { background: #111111; }
.c-p {
  background: #1e1e1e;
  background-image:
    linear-gradient(45deg,  rgba(0,0,0,.25) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0,0,0,.25) 25%, transparent 25%);
  background-size: 10px 10px;
}
.c-a { background: #181510; }
.c-k { background: #1e1500; }

.wall-i  { color: #3a3a38; line-height: 1; }

.water-i {
  animation: bob 2.5s ease-in-out infinite;
  display: inline-block;
  line-height: 1;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-2px); }
}

.karel-i {
  color: var(--accent);
  filter: drop-shadow(0 0 5px rgba(245,200,66,.7));
  animation: kpulse 1.2s ease-in-out infinite;
  line-height: 1;
}

@keyframes kpulse {
  0%, 100% { filter: drop-shadow(0 0 4px rgba(245,200,66,.6)); }
  50%       { filter: drop-shadow(0 0 9px rgba(245,200,66,1)); }
}


/* ── 19. MODE CLAR ─────────────────────────────────── */

body.light {
  --bg:         #f0f0eb;
  --surface:    #e5e5e0;
  --border:     #ccccc8;
  --text:       #1a1a16;
  --muted:      #888880;
  --accent:     #906800;
  --accent-dim: rgba(144, 104, 0, 0.1);
  --error:      #cc2222;
  --error-dim:  rgba(204, 34, 34, 0.1);
}

body.light .c-e { background: #deded8 !important; }
body.light .c-p {
  background: #c8c8c2 !important;
  background-image:
    linear-gradient(45deg,  rgba(0,0,0,.08) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0,0,0,.08) 25%, transparent 25%) !important;
  background-size: 10px 10px !important;
}
body.light .c-a { background: #d4d0b8 !important; }
body.light .c-k { background: #e8d890 !important; }
body.light .wall-i { color: #b0b0a8 !important; }
body.light .editor-wrap,
body.light #log,
body.light .edit-tools,
body.light .blank-form,
body.light #challenges-list { background: #e8e8e3 !important; }
body.light #code-highlight { color: var(--text) !important; }
body.light #log            { background: #e0e0da !important; }
body.light .hl-cm          { color: #aaa89e !important; }
body.light .ch-card        { background: #e0e0da; }
body.light .ch-card:hover  { background: var(--accent-dim); }
body.light .lbox-empty     { background: #deded8; }


/* ── 20. RESPONSIVE ────────────────────────────────── */

@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; grid-template-rows: 1fr auto; padding: 0; }
  body { overflow: auto; height: auto; }
  .map-col { border-right: none; border-bottom: 1px solid var(--border); }
  .world-area { min-height: 50vw; }
  .edit-layout { flex-direction: column; }
  .edit-tools { width: 100%; flex-direction: row; flex-wrap: wrap; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .tool-section { border-right: 1px solid var(--border); border-bottom: none; flex-shrink: 0; }
  #challenges-list { grid-template-columns: 1fr; }
}
