/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #2e2e2e;
  --bg-dark:      #252525;
  --cell-blank:   #c8c8c8;
  --cell-void:    #1e1e1e;
  --cell-wall:    yellow/*#2a2a2a*/;
  --cell-ice:     #a8d8ea;
  --cell-lava:    #e25822;
  --cell-active-border: #00bfff;
  --tile-bg:      #c8c8c8;
  --tile-fg:      #111111;
  --tile-selected:#f0e68c;
  --text-light:   #e0e0e0;
  --text-dim:     #888888;
  --accent:       #00bfff;
  --valid:        #44cc66;
  --invalid:      #cc4444;
  --cell-size:    44px;
  --tile-size:    64px;
  --grid-line:    #3d3d3d;
}

body {
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 32px 32px;
  color: var(--text-light);
  font-family: 'Courier New', Courier, monospace;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#app {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* ── Start Screen ── */
#start-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

#start-screen h1 {
  font-size: 3rem;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent);
}

#start-screen p {
  color: var(--text-dim);
  font-size: 1rem;
  text-align: center;
  max-width: 360px;
  line-height: 1.6;
}

.btn {
  padding: 12px 32px;
  font-size: 1rem;
  font-family: inherit;
  letter-spacing: 0.1em;
  background: transparent;
  color: var(--accent);
  border: 2px solid var(--accent);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}

.btn:hover {
  background: var(--accent);
  color: var(--bg);
  box-shadow: 0 0 16px var(--accent);
}

/* ── Game Layout ── */
#game-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 16px;
}

/* ── Map ── */
#map-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Wrapper that holds col-labels on top + (row-labels | grid) below */
#map-grid-wrapper {
  display: flex;
  flex-direction: column;
}

/* Column label strip */
#map-col-labels {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 2px;
  padding-left: 0; /* will be offset by the corner spacer */
}

.map-axis-corner {
  /* same width as the row-label column */
  width: 22px;
  flex-shrink: 0;
}

.map-col-label {
  width: var(--cell-size);
  text-align: center;
  font-size: 0.6rem;
  color: var(--text-dim);
  flex-shrink: 0;
  /* account for the 2px gap between cells */
  margin-right: 2px;
}
.map-col-label:last-child { margin-right: 0; }

/* Row: labels left + grid right */
#map-grid-body {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
}

#map-row-labels {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 22px;
  flex-shrink: 0;
  margin-right: 2px;
}

.map-row-label {
  height: var(--cell-size);
  line-height: var(--cell-size);
  text-align: right;
  padding-right: 4px;
  font-size: 0.6rem;
  color: var(--text-dim);
  /* account for the 2px gap between rows */
  margin-bottom: 2px;
}
.map-row-label:last-child { margin-bottom: 0; }

#map-grid {
  display: grid;
  gap: 2px;
  background: var(--bg-dark);
  padding: 4px;
  border: 1px solid #333;
}

.map-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: bold;
  position: relative;
  user-select: none;
  transition: background 0.12s, outline-color 0.12s;
}

/* Cell types */
.map-cell.void    { background: var(--cell-void); }
.map-cell.wall    { background: var(--cell-wall); border: 1px solid #3a3a3a; }
.map-cell.wall.wall-locked { background: #2a1a00; border: 2px solid #cc8800; }
.map-cell.door    { background: #1a3a6a; border: 2px solid #4488cc; }
.map-cell.item-cell { outline: 2px dashed #44cc66; outline-offset: -2px; }
.map-cell.ice     { background: var(--cell-ice); color: #336; }
.map-cell.lava    { background: var(--cell-lava); animation: lava-pulse 1s ease-in-out infinite alternate; }
.map-cell.hidden  { background: var(--cell-void); }
.map-cell.complete { background: #3a5a3a; color: #88cc88; }
.map-cell.complete.has-tile { background: #4a7a4a; color: #ccffcc; }

/* Active / droppable blank cells */
.map-cell.blank {
  background: var(--cell-blank);
  color: var(--tile-fg);
  cursor: pointer;
  outline: 2px dashed rgba(0,191,255,0.4);
  outline-offset: -2px;
}

/* Focused puzzle region — brighter border */
.map-cell.blank.focused,
.map-cell.lava.focused {
  outline: 2px solid var(--cell-active-border);
  outline-offset: -2px;
  z-index: 1;
}

/* Tile placed on map cell */
.map-cell.has-tile {
  background: var(--tile-bg);
  color: var(--tile-fg);
  cursor: grab;
}

.map-cell.blank.locked,
.map-cell.has-tile.locked {
  background: #8aaa8a;
  color: #003300;
  cursor: default;
  outline: none;
}

/* Drop hover */
.map-cell.drag-over {
  background: #d0e8ff !important;
  outline: 2px solid var(--accent) !important;
  outline-offset: -2px;
  transform: scale(1.08);
  z-index: 2;
}

.map-cell.dragging { opacity: 0.35; }

.map-cell .item-icon {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 0.5rem;
}

.map-tile-pts {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: 0.55rem;
  font-weight: normal;
  color: #555;
}

/* ── Message bar ── */
#msg-bar {
  height: 1.4rem;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-align: center;
  min-width: 300px;
}

#msg-bar.msg-success { color: var(--valid); }
#msg-bar.msg-error   { color: var(--invalid); }

/* ── Tile Rack ── */
#rack-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#rack-container h3 {
  color: var(--text-dim);
  font-size: 0.7rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

#rack-grid {
  display: grid;
  grid-template-columns: repeat(5, var(--tile-size));
  grid-template-rows: repeat(2, var(--tile-size));
  gap: 8px;
}

.rack-tile {
  width: var(--tile-size);
  height: var(--tile-size);
  background: var(--tile-bg);
  color: var(--tile-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: bold;
  cursor: grab;
  position: relative;
  border: 2px solid #aaa;
  transition: transform 0.1s, box-shadow 0.1s, opacity 0.15s;
  user-select: none;
}

.rack-tile:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.6);
}

.rack-tile.dragging {
  opacity: 0.3;
  cursor: grabbing;
}

.rack-tile .tile-points {
  position: absolute;
  bottom: 4px;
  right: 5px;
  font-size: 0.65rem;
  font-weight: normal;
  color: #555;
}

.rack-tile.empty-slot {
  background: #2a2a2a;
  border: 2px dashed #444;
  cursor: default;
}

/* ── HUD ── */
#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px;
  border: 1px solid #333;
  background: var(--bg-dark);
  width: calc(8 * var(--tile-size) + 7 * 8px + 40px);
}

#hud-score {
  font-size: 1.4rem;
  font-weight: bold;
  font-style: italic;
  color: var(--text-light);
  min-width: 80px;
}

#hud-items {
  display: flex;
  gap: 10px;
  align-items: center;
}

.item-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-light);
  background: #2a2a2a;
  padding: 4px 10px;
  border: 1px solid #555;
}

#use-missile-btn {
  font-size: 0.75rem;
  padding: 5px 12px;
  border-color: #ff8844;
  color: #ff8844;
}

#use-missile-btn:hover {
  background: #ff8844;
  color: #111;
  box-shadow: 0 0 12px #ff8844;
}

#hud-timer {
  font-size: 1.4rem;
  font-weight: bold;
  font-style: italic;
  color: var(--text-light);
  min-width: 80px;
  text-align: right;
}

/* ── Win Screen ── */
#win-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  text-align: center;
}

#win-screen h1 {
  font-size: 2.5rem;
  color: var(--valid);
  text-shadow: 0 0 20px var(--valid);
}

.score-table { font-size: 1rem; line-height: 2.2; color: var(--text-light); }
.score-table .row { display: flex; justify-content: space-between; gap: 48px; }
.score-table .row .val { color: var(--accent); font-weight: bold; }

/* ── Drag Ghost ── */
#drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  width: var(--tile-size);
  height: var(--tile-size);
  background: var(--tile-selected);
  color: var(--tile-fg);
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: bold;
  border: 2px solid #888;
  opacity: 0.9;
  transform: translate(-50%, -50%);
}

#drag-ghost .tile-points {
  position: absolute;
  bottom: 4px;
  right: 5px;
  font-size: 0.65rem;
}

/* ── Animations ── */
@keyframes lava-pulse {
  from { background: #e25822; }
  to   { background: #ff7744; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25%       { transform: translateX(-5px); }
  75%       { transform: translateX(5px); }
}

.shake { animation: shake 0.35s ease-out; }

@keyframes flash-valid {
  0%   { background: #88ff88; }
  100% { background: var(--cell-blank); }
}

@keyframes flash-invalid {
  0%   { background: #ff4444; }
  100% { background: var(--cell-blank); }
}

.flash-valid   { animation: flash-valid   0.5s ease-out forwards; }
.flash-invalid { animation: flash-invalid 0.5s ease-out forwards; }
