/* ==========================================================================
   Magazynier - Stylesheet
   Modern, tactile, responsive with 4 visual themes
   ========================================================================== */

:root {
  --bg-primary: #121824;
  --bg-secondary: #1a2233;
  --bg-card: #222d42;
  --border-color: #334155;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #f59e0b;
  --accent-hover: #d97706;
  --success: #10b981;
  --danger: #ef4444;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  
  /* Tile Theme Defaults (Warehouse) */
  --tile-bg: #1e293b;
  --tile-border: #334155;
  --wall-bg: #475569;
  --wall-top: #64748b;
  --crate-bg: #b45309;
  --crate-border: #78350f;
  --crate-accent: #f59e0b;
  --crate-goal-bg: #059669;
  --crate-goal-border: #047857;
  --goal-color: #f59e0b;
  --goal-bg: rgba(245, 158, 11, 0.15);
}

/* ================= THEMES ================= */

/* Theme 1: Klasyczny Magazyn */
body.theme-warehouse {
  --bg-primary: #111827;
  --bg-secondary: #1f2937;
  --bg-card: #374151;
  --border-color: #4b5563;
  --tile-bg: #2b3240;
  --tile-border: #384252;
  --wall-bg: #4b5563;
  --wall-top: #6b7280;
  --crate-bg: #b45309;
  --crate-border: #78350f;
  --crate-accent: #fbbf24;
  --crate-goal-bg: #059669;
  --crate-goal-border: #065f46;
  --goal-color: #fbbf24;
}

/* Theme 2: Industrialny Dok / Kontenery */
body.theme-dock {
  --bg-primary: #0b1523;
  --bg-secondary: #132238;
  --bg-card: #1c314e;
  --border-color: #2b4970;
  --tile-bg: #18283e;
  --tile-border: #233a57;
  --wall-bg: #1e3a5f;
  --wall-top: #2e598d;
  --crate-bg: #0284c7;
  --crate-border: #0369a1;
  --crate-accent: #38bdf8;
  --crate-goal-bg: #0d9488;
  --crate-goal-border: #0f766e;
  --goal-color: #38bdf8;
}

/* Theme 3: Retro Arcade 8-Bit */
body.theme-retro {
  --bg-primary: #0a0a14;
  --bg-secondary: #141428;
  --bg-card: #1e1e38;
  --border-color: #3a3a6a;
  --tile-bg: #121224;
  --tile-border: #242445;
  --wall-bg: #6b21a8;
  --wall-top: #9333ea;
  --crate-bg: #ea580c;
  --crate-border: #9a3412;
  --crate-accent: #fb923c;
  --crate-goal-bg: #16a34a;
  --crate-goal-border: #15803d;
  --goal-color: #ec4899;
}

/* Theme 4: Kamienny Ogród Zen */
body.theme-zen {
  --bg-primary: #262c2f;
  --bg-secondary: #333a3e;
  --bg-card: #41494e;
  --border-color: #555f65;
  --tile-bg: #373f44;
  --tile-border: #475157;
  --wall-bg: #5a666e;
  --wall-top: #74828b;
  --crate-bg: #78716c;
  --crate-border: #57534e;
  --crate-accent: #a8a29e;
  --crate-goal-bg: #047857;
  --crate-goal-border: #065f46;
  --goal-color: #34d399;
}

/* ================= BASE RESET ================= */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  overflow-x: hidden;
  transition: background-color 0.3s ease;
}

/* ================= APP CONTAINER ================= */
#app {
  width: 100%;
  max-width: 900px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
}

/* ================= HEADER ================= */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 10px 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  margin-bottom: 8px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.gry-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-main);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
  transition: transform 0.15s ease, background 0.15s ease;
}

.gry-back:hover {
  background: var(--bg-secondary);
  transform: translateY(-1px);
}

.gry-back:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.logo-icon {
  font-size: 26px;
  line-height: 1;
}

.title-area h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tier-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  background: rgba(245, 158, 11, 0.2);
  color: var(--accent);
  border: 1px solid rgba(245, 158, 11, 0.4);
  padding: 2px 8px;
  border-radius: 20px;
  margin-top: 2px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Top Action Buttons */
.icon-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.icon-btn:hover {
  background: var(--border-color);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: translateY(0);
}

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

/* ================= STATUS BAR ================= */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 8px 16px;
  margin-bottom: 8px;
  flex-shrink: 0;
}

.stats-group {
  display: flex;
  align-items: center;
  gap: 20px;
}

.stat-item {
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 600;
}

.stat-val {
  font-size: 1.1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.level-stars {
  font-size: 1.15rem;
  letter-spacing: 2px;
}

.star-dim {
  color: #475569;
  opacity: 0.6;
}

/* Deadlock Warning Banner */
.deadlock-banner {
  background: rgba(239, 68, 68, 0.9);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  animation: pulse-warn 1.5s infinite;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.35);
  flex-shrink: 0;
}

.deadlock-banner.hidden {
  display: none !important;
}

.deadlock-undo-btn {
  background: #fff;
  color: #b91c1c;
  border: none;
  font-size: 0.75rem;
  font-weight: 800;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  margin-left: 10px;
}

@keyframes pulse-warn {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.85; transform: scale(0.995); }
}

/* ================= BOARD VIEWPORT ================= */
.board-viewport {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  background: radial-gradient(circle at center, rgba(30, 41, 59, 0.5) 0%, rgba(15, 23, 42, 0.8) 100%);
  border-radius: 14px;
  border: 1px solid var(--border-color);
  padding: 12px;
  min-height: 200px;
}

#board {
  display: grid;
  gap: 1px;
  background: var(--border-color);
  border: 2px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
  margin: auto;
  transition: width 0.2s ease, height 0.2s ease;
}

/* Tiles */
.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.tile.void {
  background: transparent;
  visibility: hidden;
}

.tile.wall {
  background: var(--wall-bg);
  box-shadow: inset 0 3px 0 var(--wall-top), inset 0 -3px 0 rgba(0, 0, 0, 0.4);
}

.tile.wall .wall-inner {
  width: 100%;
  height: 100%;
  background: repeating-linear-gradient(
    45deg,
    rgba(0, 0, 0, 0.08),
    rgba(0, 0, 0, 0.08) 6px,
    transparent 6px,
    transparent 12px
  );
}

.tile.floor {
  background: var(--tile-bg);
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.tile.floor:hover {
  background: rgba(255, 255, 255, 0.05);
}

/* Deadlock Highlight (Help Mode) */
.tile.deadlock-zone {
  background: rgba(239, 68, 68, 0.18) !important;
}

.tile.deadlock-zone::after {
  content: '✕';
  position: absolute;
  color: rgba(239, 68, 68, 0.45);
  font-size: 14px;
  font-weight: bold;
  pointer-events: none;
}

/* Goal Pad */
.tile.goal .goal-mark {
  width: 44%;
  height: 44%;
  border-radius: 50%;
  border: 3px solid var(--goal-color);
  background: var(--goal-bg);
  box-shadow: 0 0 10px var(--goal-color);
  animation: pulse-goal 2s infinite ease-in-out;
  pointer-events: none;
}

@keyframes pulse-goal {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.15); opacity: 1; }
}

/* Crates */
.crate {
  position: absolute;
  width: 86%;
  height: 86%;
  background: var(--crate-bg);
  border: 3px solid var(--crate-border);
  border-radius: 6px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4), inset 0 2px 0 var(--crate-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  transition: transform 0.12s ease-out;
}

.crate::before {
  content: '';
  width: 60%;
  height: 60%;
  border: 2px dashed rgba(255, 255, 255, 0.25);
  border-radius: 3px;
}

/* Crate locked on goal */
.crate.on-goal {
  background: var(--crate-goal-bg);
  border-color: var(--crate-goal-border);
  box-shadow: 0 0 12px rgba(16, 185, 129, 0.6);
}

.crate.on-goal::before {
  content: '✓';
  color: #fff;
  font-size: 18px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
}

/* Crate stuck in deadlock */
.crate.stuck {
  border-color: #ef4444;
  box-shadow: 0 0 14px rgba(239, 68, 68, 0.9);
  animation: shake 0.6s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-3px); }
  40%, 80% { transform: translateX(3px); }
}

/* Worker / Player */
.worker {
  position: absolute;
  width: 90%;
  height: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
  pointer-events: none;
  transition: transform 0.1s ease-out;
}

.worker-svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.4));
}

/* Direction animations */
.worker.dir-left {
  transform: scaleX(-1);
}
.worker.dir-right {
  transform: scaleX(1);
}

/* ================= CONTROLS FOOTER ================= */
footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
  flex-shrink: 0;
  gap: 10px;
}

.nav-buttons, .action-buttons {
  display: flex;
  gap: 8px;
}

.btn {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.btn:hover:not(:disabled) {
  background: var(--border-color);
  transform: translateY(-1px);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #111;
  border-color: var(--accent-hover);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

/* Mobile D-Pad */
.mobile-dpad {
  display: none;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 4px;
  margin: 0 auto;
}

.dpad-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  border-radius: 8px;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.dpad-btn:active {
  background: var(--accent);
  color: #111;
}

#dpad-up { grid-column: 2; grid-row: 1; }
#dpad-left { grid-column: 1; grid-row: 2; }
#dpad-right { grid-column: 3; grid-row: 2; }
#dpad-down { grid-column: 2; grid-row: 3; }

/* Responsive Mobile Layout */
@media (max-width: 680px) {
  #app {
    padding: 6px 8px;
  }
  
  header {
    padding: 8px 10px;
  }

  .title-area h1 {
    font-size: 1rem;
  }

  .mobile-dpad {
    display: grid;
  }

  .footer-center {
    display: flex;
  }

  footer {
    flex-wrap: wrap;
    justify-content: center;
  }

  .nav-buttons, .action-buttons {
    width: 100%;
    justify-content: space-around;
  }

  .btn {
    padding: 8px 12px;
    font-size: 0.8rem;
  }
}

/* ================= MODALS ================= */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.active {
  display: flex;
}

.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 24px;
  width: 90%;
  max-width: 600px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
  z-index: 101;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 12px;
}

.modal-header h2 {
  font-size: 1.3rem;
  color: var(--accent);
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
}

.modal-close:hover {
  color: var(--text-main);
}

/* Level Grid in Modal */
.tier-header {
  font-size: 1rem;
  color: var(--accent);
  margin: 16px 0 8px 0;
  border-left: 3px solid var(--accent);
  padding-left: 8px;
}

.levels-subgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.level-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 6px;
  text-align: center;
  cursor: pointer;
  color: var(--text-main);
  transition: all 0.15s ease;
}

.level-card:hover {
  border-color: var(--accent);
  background: var(--border-color);
  transform: translateY(-2px);
}

.level-card.current {
  border-color: var(--accent);
  box-shadow: 0 0 10px rgba(245, 158, 11, 0.4);
}

.level-card.completed {
  border-color: var(--success);
}

.lvl-num {
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--accent);
}

.lvl-name {
  font-size: 0.72rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin: 2px 0 4px 0;
}

.lvl-stars {
  font-size: 0.78rem;
  letter-spacing: 1px;
}

/* Rules list */
.rules-body ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rules-body li {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.9rem;
  line-height: 1.45;
}

.rules-body strong {
  color: var(--accent);
  display: block;
  margin-bottom: 4px;
}

/* Stats list */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 16px 0;
}

.stat-box {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}

.stat-box-num {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--accent);
}

.stat-box-label {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Victory Modal Specifics */
.win-content {
  text-align: center;
  max-width: 420px;
}

.win-stars {
  font-size: 2.8rem;
  margin: 14px 0;
  animation: bounce 0.6s ease;
}

.win-stats-list {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 12px 18px;
  margin: 16px 0;
  text-align: left;
  font-size: 0.95rem;
}

.win-stats-list div {
  display: flex;
  justify-content: space-between;
  margin: 6px 0;
}

.win-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-20px); }
  60% { transform: translateY(-10px); }
}

/* Confetti pieces */
#confetti-canvas-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 105;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  border-radius: 2px;
  opacity: 0.9;
  animation: fall linear forwards;
}

@keyframes fall {
  to {
    transform: translateY(105vh) rotate(720deg);
  }
}

.rules-credit {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
  font-size: 0.8rem;
  line-height: 1.55;
  color: var(--text-muted);
}
