/* ============================================
   Critter Haven Bot — Design System
   Dark mode, glassmorphism, premium aesthetic
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');

/* ---- CSS Variables ---- */
:root {
  --bg-primary: #0a0e1a;
  --bg-secondary: #111827;
  --bg-tertiary: #1a2035;
  --bg-glass: rgba(17, 24, 39, 0.7);
  --bg-glass-hover: rgba(26, 32, 53, 0.85);
  --border-glass: rgba(255, 255, 255, 0.08);
  --border-glass-hover: rgba(255, 255, 255, 0.15);

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --accent-primary: #6366f1;
  --accent-primary-glow: rgba(99, 102, 241, 0.3);
  --accent-secondary: #8b5cf6;
  --accent-success: #10b981;
  --accent-success-glow: rgba(16, 185, 129, 0.3);
  --accent-warning: #f59e0b;
  --accent-danger: #ef4444;
  --accent-info: #3b82f6;

  --gradient-primary: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #a78bfa 100%);
  --gradient-success: linear-gradient(135deg, #10b981 0%, #34d399 100%);
  --gradient-dark: linear-gradient(180deg, #0a0e1a 0%, #111827 100%);
  --gradient-card: linear-gradient(135deg, rgba(99, 102, 241, 0.05) 0%, rgba(139, 92, 246, 0.05) 100%);

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 20px var(--accent-primary-glow);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;

  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 400ms cubic-bezier(0.4, 0, 0.2, 1);

  /* Block colors */
  --block-1: #f59e0b;
  --block-2: #ef4444;
  --block-3: #3b82f6;
  --block-4: #f97316;
  --block-5: #10b981;
  --block-6: #8b5cf6;
  --block-7: #ec4899;
  --block-8: #06b6d4;
  --block-9: #84cc16;
  --block-10: #f43f5e;
}

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

/* ---- Custom Scrollbar ---- */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-glass-hover);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--gradient-dark);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ---- Background Effect ---- */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(ellipse 600px 400px at 20% 10%, rgba(99, 102, 241, 0.08) 0%, transparent 70%),
    radial-gradient(ellipse 500px 300px at 80% 80%, rgba(139, 92, 246, 0.06) 0%, transparent 70%);
  pointer-events: none;
  z-index: 0;
}

/* ---- Layout ---- */
.app-container {
  position: relative;
  z-index: 1;
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

header {
  text-align: center;
  padding: 32px 0 24px;
}

header h1 {
  font-size: 2rem;
  font-weight: 800;
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
  margin-bottom: 0;
}

.header-title-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.header-logo {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  image-rendering: pixelated;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

header .subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 6px;
  font-weight: 400;
}

.main-layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 24px;
  align-items: start;
}

@media (max-width: 900px) {
  .main-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  header h1 {
    font-size: 1.6rem;
    text-align: center;
  }
  
  .header-title-container {
    flex-direction: column;
    gap: 8px;
  }
}

/* ---- Glass Card ---- */
.card {
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-lg);
  padding: 20px;
  transition: border-color var(--transition-normal);
}

.card:hover {
  border-color: var(--border-glass-hover);
}

.card-title {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title .icon {
  font-size: 1rem;
}

/* ---- Sidebar ---- */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- Tabs ---- */
.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  padding: 4px;
}

.tab-btn {
  flex: 1;
  padding: 10px 16px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.tab-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.05);
}

.tab-btn.active {
  color: var(--text-primary);
  background: var(--accent-primary);
  box-shadow: var(--shadow-glow);
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- Upload Zone ---- */
.upload-zone {
  border: 2px dashed var(--border-glass);
  border-radius: var(--radius-md);
  padding: 32px 20px;
  text-align: center;
  cursor: pointer;
  transition: all var(--transition-normal);
  position: relative;
  overflow: hidden;
}

.upload-zone:hover,
.upload-zone.dragover {
  border-color: var(--accent-primary);
  background: rgba(99, 102, 241, 0.05);
}

.upload-zone .upload-icon {
  font-size: 2.5rem;
  margin-bottom: 12px;
  display: block;
}

.upload-zone .upload-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.upload-zone .upload-text strong {
  color: var(--accent-primary);
}

.upload-zone input[type="file"] {
  display: none;
}

/* ---- Crop Preview ---- */
.crop-preview {
  position: relative;
  margin-top: 12px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  display: none;
}

.crop-preview img {
  width: 100%;
  display: block;
}

.crop-preview canvas {
  width: 100%;
  display: block;
  cursor: crosshair;
}

/* ---- Form Controls ---- */
.form-row {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}

.form-group {
  flex: 1;
}

.form-group label {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.85rem;
  transition: border-color var(--transition-fast);
  outline: none;
}

.form-group input:focus,
.form-group select:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px var(--accent-primary-glow);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--gradient-primary);
  color: white;
  box-shadow: var(--shadow-glow);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 0 30px var(--accent-primary-glow);
}

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

.btn-success {
  background: var(--gradient-success);
  color: white;
  box-shadow: 0 0 20px var(--accent-success-glow);
}

.btn-success:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 0 30px var(--accent-success-glow);
}

.btn-outline {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-glass);
}

.btn-outline:hover:not(:disabled) {
  color: var(--text-primary);
  border-color: var(--border-glass-hover);
  background: rgba(255, 255, 255, 0.03);
}

.btn-danger {
  background: var(--accent-danger);
  color: white;
}

.btn-sm {
  padding: 8px 14px;
  font-size: 0.78rem;
}

.btn-block {
  width: 100%;
}

/* ---- Manual Grid Editor ---- */
.grid-size-controls {
  margin-bottom: 16px;
}

.manual-grid-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin: 12px 0;
}

.manual-grid-row {
  display: flex;
  gap: 2px;
}

.manual-grid-cell {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  border: 2px solid var(--border-glass);
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: rgba(255,255,255,0.7);
}

.manual-grid-cell:hover {
  transform: scale(1.1);
  border-color: var(--accent-primary);
  z-index: 2;
}

.manual-grid-cell.empty {
  background: var(--bg-tertiary);
  border-style: dashed;
}

/* ---- Color Palette ---- */
.color-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 12px 0;
}

.color-swatch {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: rgba(255,255,255,0.8);
}

.color-swatch:hover {
  transform: scale(1.15);
}

.color-swatch.active {
  border-color: white;
  box-shadow: 0 0 12px rgba(255, 255, 255, 0.3);
}

.color-swatch.eraser {
  background: var(--bg-tertiary) !important;
  border: 2px dashed var(--border-glass);
  font-size: 1rem;
}

/* ---- Template Gallery ---- */
.template-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.template-skeleton {
  width: 52px;
  height: 48px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-glass);
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0.03) 25%,
    rgba(255, 255, 255, 0.08) 37%,
    rgba(255, 255, 255, 0.03) 63%
  );
  background-size: 400% 100%;
  animation: skeleton-loading 1.4s ease infinite;
}

@keyframes skeleton-loading {
  0% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.template-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-glass);
  background: var(--bg-tertiary);
  transition: all var(--transition-fast);
  cursor: default;
  width: 52px;
}

.template-item:hover {
  border-color: var(--accent-primary);
  background: rgba(99, 102, 241, 0.1);
}

.template-item img {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  image-rendering: pixelated;
}

.template-label {
  font-size: 0.55rem;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.1;
  max-width: 48px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Main Content ---- */
.main-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- Grid Display ---- */
.grid-canvas-wrapper {
  position: relative;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg-tertiary);
  aspect-ratio: 1;
  max-height: 500px;
  margin: 0 auto;
  width: 100%;
  max-width: 500px;
}

.grid-canvas-wrapper canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* ---- Solver Controls ---- */
.solver-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.time-limit-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.time-limit-group label {
  font-size: 0.78rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.time-limit-group input {
  width: 70px;
  padding: 8px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.85rem;
  outline: none;
}

.time-limit-group input:focus {
  border-color: var(--accent-primary);
}

/* ---- Progress Bar ---- */
.progress-section {
  display: none;
}

.progress-section.active {
  display: block;
  animation: fadeIn 0.2s ease;
}

.progress-bar-track {
  height: 4px;
  background: var(--bg-tertiary);
  border-radius: 2px;
  overflow: hidden;
  margin: 12px 0;
}

.progress-bar-fill {
  height: 100%;
  background: var(--gradient-primary);
  border-radius: 2px;
  transition: width 0.3s ease;
  position: relative;
}

.progress-bar-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.progress-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.progress-stats .stat-value {
  color: var(--text-primary);
  font-weight: 600;
}

/* ---- Solution / Playback ---- */
.solution-section {
  display: none;
}

.solution-section.active {
  display: block;
  animation: fadeIn 0.3s ease;
}

.solution-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.solution-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
}

.solution-badge.optimal {
  background: rgba(16, 185, 129, 0.15);
  color: var(--accent-success);
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.solution-badge.suboptimal {
  background: rgba(245, 158, 11, 0.15);
  color: var(--accent-warning);
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.solution-meta {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.playback-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 16px 0;
}

.playback-controls .btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 1.1rem;
}

.playback-controls .btn-play {
  width: 48px;
  height: 48px;
  font-size: 1.3rem;
}

.step-counter {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 80px;
  text-align: center;
}

.step-info {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 8px;
}

.step-info .highlight {
  color: var(--accent-primary);
  font-weight: 600;
}

/* ---- Move List ---- */
.move-list {
  max-height: 200px;
  overflow-y: auto;
  margin-top: 12px;
}

.move-list::-webkit-scrollbar {
  width: 4px;
}

.move-list::-webkit-scrollbar-track {
  background: transparent;
}

.move-list::-webkit-scrollbar-thumb {
  background: var(--border-glass);
  border-radius: 2px;
}

.move-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
  font-size: 0.8rem;
}

.move-item:hover {
  background: rgba(255, 255, 255, 0.03);
}

.move-item.active {
  background: rgba(99, 102, 241, 0.1);
  border-left: 3px solid var(--accent-primary);
}

.move-item .move-num {
  font-weight: 700;
  color: var(--text-muted);
  min-width: 24px;
}

.move-item .move-color {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  flex-shrink: 0;
}

.move-item .move-detail {
  color: var(--text-secondary);
}

.move-item .move-detail strong {
  color: var(--text-primary);
}

/* ---- Status Messages ---- */
.status-msg {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  display: none;
}

.status-msg.active {
  display: flex;
  align-items: center;
  gap: 8px;
  animation: fadeIn 0.2s ease;
}

.status-msg.info {
  background: rgba(59, 130, 246, 0.1);
  color: var(--accent-info);
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.status-msg.success {
  background: rgba(16, 185, 129, 0.1);
  color: var(--accent-success);
  border: 1px solid rgba(16, 185, 129, 0.2);
}

.status-msg.warning {
  background: rgba(245, 158, 11, 0.1);
  color: var(--accent-warning);
  border: 1px solid rgba(245, 158, 11, 0.2);
}

.status-msg.error {
  background: rgba(239, 68, 68, 0.1);
  color: var(--accent-danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

/* ---- Block Pop Animation ---- */
@keyframes blockPop {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.7; }
  100% { transform: scale(0); opacity: 0; }
}

@keyframes blockFall {
  0% { transform: translateY(-100%); }
  60% { transform: translateY(5%); }
  80% { transform: translateY(-2%); }
  100% { transform: translateY(0); }
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--accent-primary-glow); }
  50% { box-shadow: 0 0 0 8px transparent; }
}

/* ---- Utility ---- */
.hidden { display: none !important; }
.mt-12 { margin-top: 12px; }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(5px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: all var(--transition-normal);
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  width: 90%;
  max-width: 500px;
  max-height: 85vh;
  overflow-y: auto;
  transform: translateY(20px);
  transition: all var(--transition-normal);
}

.modal-overlay.active .modal-content {
  transform: translateY(0);
}

.modal-body {
  font-size: 0.9rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

.modal-body p {
  margin-top: 12px;
  color: var(--text-primary);
}

.modal-body ul {
  margin-left: 20px;
  margin-top: 6px;
}

.modal-body li {
  margin-bottom: 4px;
}

/* ---- Toast Notification ---- */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  border: 1px solid var(--accent-primary);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  box-shadow: var(--shadow-lg), 0 0 15px rgba(99, 102, 241, 0.2);
  z-index: 999;
  transform: translateX(120%);
  transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  max-width: 350px;
}

.toast.active {
  transform: translateX(0);
}

.toast p {
  font-size: 0.85rem;
  color: var(--text-primary);
  margin-bottom: 12px;
}

.toast-actions {
  display: flex;
  gap: 8px;
}

.toast-actions a {
  text-decoration: none;
}

.mt-16 { margin-top: 16px; }
.text-center { text-align: center; }
.text-muted { color: var(--text-secondary); }
.text-sm { font-size: 0.78rem; }
.gap-8 { gap: 8px; }
.flex { display: flex; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
