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

body {
  font-family: 'Segoe UI', Arial, sans-serif;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  min-height: 100vh;
  color: #fff;
}

/* ── LOGIN PAGE ── */
.login-page { display: flex; flex-direction: column; align-items: center; padding: 30px 20px; }

.logo { font-size: 3rem; margin-bottom: 5px; }
h1.title { font-size: 2.2rem; color: #ffd700; text-shadow: 0 0 20px rgba(255,215,0,0.5); margin-bottom: 5px; }
.subtitle { color: #a0b4d0; margin-bottom: 30px; }

.login-box {
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 16px;
  padding: 30px;
  width: 100%;
  max-width: 380px;
  margin-bottom: 40px;
}
.login-box h2 { margin-bottom: 20px; color: #7ec8e3; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; color: #ccc; font-size: 0.95rem; }
.form-group input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.1);
  color: #fff;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus { border-color: #7ec8e3; }
.btn-login {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #f7971e, #ffd200);
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: bold;
  color: #1a1a2e;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.2s;
}
.btn-login:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(255,210,0,0.4); }
.login-error { color: #ff6b6b; margin-top: 12px; font-size: 0.9rem; min-height: 20px; }

/* ── LEADERBOARD ── */
.leaderboard { width: 100%; max-width: 800px; }
.leaderboard h2 { color: #ffd700; margin-bottom: 16px; text-align: center; font-size: 1.5rem; }
.leaderboard-table { width: 100%; border-collapse: collapse; background: rgba(255,255,255,0.05); border-radius: 12px; overflow: hidden; }
.leaderboard-table th {
  background: rgba(255,255,255,0.1);
  padding: 12px 10px;
  text-align: center;
  color: #7ec8e3;
  font-size: 0.9rem;
}
.leaderboard-table td { padding: 10px; text-align: center; border-bottom: 1px solid rgba(255,255,255,0.05); font-size: 0.9rem; }
.leaderboard-table tr:last-child td { border-bottom: none; }
.leaderboard-table tr:hover td { background: rgba(255,255,255,0.04); }
.badge-profi { color: #ffd700; font-size: 1.1rem; }
.badge-basic { color: #c0c0c0; font-size: 1.1rem; }
.badge-none { color: #555; }
.locked { color: #444; }

/* ── GAME PAGE ── */
.game-page { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: rgba(0,0,0,0.3);
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}
.game-header .level-info { font-size: 1rem; color: #7ec8e3; }
.game-header .level-name { font-size: 1.1rem; font-weight: bold; color: #ffd700; }
.game-header .limits { font-size: 0.85rem; color: #aaa; }
.btn-back {
  padding: 8px 16px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  color: #fff;
  cursor: pointer;
  font-size: 0.9rem;
  text-decoration: none;
  transition: background 0.2s;
}
.btn-back:hover { background: rgba(255,255,255,0.2); }

.game-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
}

/* Grid */
.grid-panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0,0,0,0.2);
}
#game-grid {
  display: grid;
  grid-template-columns: repeat(10, 52px);
  grid-template-rows: repeat(10, 52px);
  gap: 2px;
  background: rgba(0,0,0,0.4);
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 4px;
}
.cell {
  width: 52px;
  height: 52px;
  background: rgba(255,255,255,0.04);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  position: relative;
  transition: background 0.1s;
}
.cell.obstacle { background: rgba(255,80,80,0.3); }
.cell.obstacle::after { content: '🧱'; font-size: 1.4rem; }
.cell.finish { background: rgba(80,255,80,0.15); }
.cell.finish::after { content: '🏁'; font-size: 1.3rem; }
.cell.build-site-factory { background: rgba(255,200,0,0.15); }
.cell.build-site-factory::after { content: '🏭'; font-size: 1.3rem; opacity: 0.5; }
.cell.build-site-house { background: rgba(200,150,255,0.15); }
.cell.build-site-house::after { content: '🏠'; font-size: 1.3rem; opacity: 0.5; }
.cell.built-factory { background: rgba(255,200,0,0.3); }
.cell.built-factory::after { content: '🏭'; font-size: 1.3rem; }
.cell.built-house { background: rgba(200,150,255,0.3); }
.cell.built-house::after { content: '🏠'; font-size: 1.3rem; }
.cell.robot { background: rgba(100,200,255,0.2); }
.robot-emoji { font-size: 1.6rem; z-index: 2; position: relative; transition: transform 0.2s; }
.cell.exploding .robot-emoji { animation: explode 0.5s forwards; }
@keyframes explode {
  0% { transform: scale(1); }
  50% { transform: scale(2) rotate(20deg); filter: brightness(3); }
  100% { transform: scale(0); opacity: 0; }
}

/* Right panel */
.right-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 12px;
  gap: 12px;
  min-width: 320px;
  max-width: 420px;
}

/* Palette */
.palette-panel { flex-shrink: 0; }
.palette-panel h3 { font-size: 0.85rem; color: #aaa; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 1px; }
.palette-blocks { display: flex; flex-wrap: wrap; gap: 6px; }
.block {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: bold;
  cursor: grab;
  user-select: none;
  border: none;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: transform 0.1s, box-shadow 0.1s;
}
.block:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.3); }
.block:active { cursor: grabbing; transform: scale(0.97); }
.block.dragging { opacity: 0.4; }
.block-right { background: #e74c3c; }
.block-left { background: #e67e22; }
.block-up { background: #27ae60; }
.block-down { background: #2980b9; }
.block-factory { background: #8e44ad; }
.block-house { background: #c0392b; }
.block-repeat { background: #16a085; }

/* Sequence */
.sequence-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sequence-panel h3 { font-size: 0.85rem; color: #aaa; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 1px; flex-shrink: 0; }
#sequence-area {
  flex: 1;
  background: rgba(0,0,0,0.25);
  border: 2px dashed rgba(255,255,255,0.15);
  border-radius: 10px;
  padding: 8px;
  overflow-y: auto;
  min-height: 80px;
}
#sequence-area.drag-over { border-color: #7ec8e3; background: rgba(126,200,227,0.08); }
.seq-block {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  border-radius: 8px;
  margin-bottom: 4px;
  font-size: 0.9rem;
  font-weight: bold;
  color: #fff;
  cursor: grab;
  position: relative;
  user-select: none;
}
.seq-block:active { cursor: grabbing; }
.seq-block.drag-over-top { border-top: 3px solid #7ec8e3; }
.seq-block.drag-over-bottom, .seq-repeat.drag-over-bottom { border-bottom: 3px solid #7ec8e3; }
.seq-block.executing, .seq-repeat.executing { outline: 3px solid #ffd700; box-shadow: 0 0 12px rgba(255,215,0,0.6); transform: scale(1.03); }
.seq-block.failed, .seq-repeat.failed { outline: 3px solid #ff4444; box-shadow: 0 0 14px rgba(255,68,68,0.7); animation: shake 0.4s ease; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}
.seq-block .del-btn {
  margin-left: auto;
  background: rgba(0,0,0,0.3);
  border: none;
  color: #fff;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.seq-block .del-btn:hover { background: rgba(255,0,0,0.5); }

/* Repeat block in sequence */
.seq-repeat {
  background: rgba(22,160,133,0.3);
  border: 2px solid #16a085;
  border-radius: 10px;
  padding: 6px;
  margin-bottom: 4px;
}
.seq-repeat-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  font-weight: bold;
  font-size: 0.9rem;
}
.seq-repeat-header .count-btn {
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.seq-repeat-header .count-val { font-size: 1rem; color: #ffd700; min-width: 20px; text-align: center; }
.repeat-inner {
  background: rgba(0,0,0,0.2);
  border: 1px dashed rgba(22,160,133,0.5);
  border-radius: 8px;
  min-height: 36px;
  padding: 4px;
}
.repeat-inner.drag-over { border-color: #7ec8e3; background: rgba(126,200,227,0.08); }
.drop-hint { color: rgba(255,255,255,0.3); font-size: 0.8rem; padding: 6px; text-align: center; }

/* Controls */
.controls-panel { display: flex; gap: 8px; flex-shrink: 0; }
.btn-run {
  flex: 1;
  padding: 12px;
  background: linear-gradient(135deg, #f7971e, #ffd200);
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: bold;
  color: #1a1a2e;
  cursor: pointer;
  transition: transform 0.1s;
}
.btn-run:hover:not(:disabled) { transform: translateY(-2px); }
.btn-run:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-clear {
  padding: 12px 16px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 10px;
  color: #fff;
  cursor: pointer;
  font-size: 0.9rem;
}
.btn-clear:hover { background: rgba(255,255,255,0.2); }

.move-counter { text-align: center; font-size: 0.85rem; color: #aaa; flex-shrink: 0; }
.move-counter span { color: #ffd700; font-weight: bold; font-size: 1.1rem; }

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}
.modal-overlay.hidden { display: none; }
.modal {
  background: #1a2744;
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 20px;
  padding: 36px;
  text-align: center;
  max-width: 380px;
  width: 90%;
  animation: pop-in 0.3s cubic-bezier(0.175,0.885,0.32,1.275);
}
@keyframes pop-in { from { transform: scale(0.5); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h2 { font-size: 2rem; margin-bottom: 12px; }
.modal p { color: #aaa; margin-bottom: 8px; font-size: 1rem; }
.modal .badge-display { font-size: 3rem; margin: 16px 0; }
.modal-btn {
  display: inline-block;
  margin-top: 16px;
  padding: 12px 28px;
  background: linear-gradient(135deg, #f7971e, #ffd200);
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: bold;
  color: #1a1a2e;
  cursor: pointer;
  transition: transform 0.1s;
  margin-right: 8px;
}
.modal-btn:hover { transform: translateY(-2px); }
.modal-btn.secondary {
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
}

@media (max-width: 900px) {
  .game-body { flex-direction: column; overflow-y: auto; }
  .grid-panel { padding: 10px; }
  #game-grid { grid-template-columns: repeat(10, 38px); grid-template-rows: repeat(10, 38px); }
  .cell { width: 38px; height: 38px; font-size: 1rem; }
  .right-panel { max-width: 100%; }
}
