/* ==============================
   PyKids — Mobile-first styles
   ============================== */

:root {
  --primary:   #7c3aed;
  --primary-d: #6d28d9;
  --accent:    #f59e0b;
  --green:     #10b981;
  --red:       #ef4444;
  --blue:      #3b82f6;
  --bg:        #fdf4ff;
  --card:      #ffffff;
  --text:      #1e1b4b;
  --muted:     #6b7280;
  --border:    #e9d5ff;
  --code-bg:   #1e1b4b;
  --radius:    16px;
  --shadow:    0 4px 24px rgba(124,58,237,0.10);
  /* Safe area insets for notched phones */
  --safe-top:    env(safe-area-inset-top,    0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left,   0px);
  --safe-right:  env(safe-area-inset-right,  0px);
}

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

html { height: 100%; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  /* Prevent pull-to-refresh interfering */
  overscroll-behavior-y: contain;
}

/* ---- HEADER ---- */
header {
  background: linear-gradient(135deg, var(--primary) 0%, #a855f7 100%);
  color: white;
  padding: calc(0.6rem + var(--safe-top)) 1rem 0.6rem;
  box-shadow: 0 2px 12px rgba(124,58,237,0.3);
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: 1100px;
  margin: 0 auto;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}
.snake { font-size: 1.6rem; line-height: 1; }
.logo-text {
  font-size: 1.4rem;
  font-weight: 900;
  letter-spacing: -0.5px;
}

/* Progress dots */
.progress-steps {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  flex-wrap: wrap;
  flex: 1;
  justify-content: flex-end;
}

.step-dot {
  width: 34px;
  height: 34px;
  min-width: 34px;
  border-radius: 50%;
  background: rgba(255,255,255,0.22);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  color: white;
  cursor: pointer;
  border: 2px solid rgba(255,255,255,0.4);
  transition: background 0.2s, transform 0.15s;
  /* Tap highlight */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.step-dot:active { transform: scale(0.9); }
.step-dot.active { background: var(--accent); border-color: #fde68a; color: #78350f; }
.step-dot.done   { background: var(--green);  border-color: #6ee7b7; }

/* ---- LOADING ---- */
#loading-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem 1rem;
  min-height: calc(100dvh - 56px);
}
.loading-box {
  text-align: center;
  padding: 2.5rem 1.5rem;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 320px;
}
.loading-snake { font-size: 3.5rem; animation: bounce 1s infinite; }
.loading-box h2 { margin: 0.75rem 0 0.4rem; color: var(--primary); font-size: 1.2rem; }
.loading-box p  { color: var(--muted); font-size: 0.9rem; }

.spinner {
  margin: 1.25rem auto 0;
  width: 36px; height: 36px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin   { to { transform: rotate(360deg); } }
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-10px); }
}

/* ---- UTILITY ---- */
.hidden { display: none !important; }

/* ---- TAB BAR (mobile only) ---- */
.tab-bar {
  display: flex;
  background: var(--card);
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 56px; /* below header */
  z-index: 40;
}

.tab-btn {
  flex: 1;
  padding: 0.7rem 0.5rem;
  font-size: 0.95rem;
  font-weight: 700;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: color 0.15s, border-color 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* ---- MAIN APP ---- */
#app {
  max-width: 1100px;
  margin: 0 auto;
  padding-bottom: calc(1rem + var(--safe-bottom));
}

/* ---- TAB PANELS ---- */
.tab-panel {
  display: none;
  padding: 1rem;
  flex-direction: column;
  gap: 1.1rem;
}
.tab-panel.active { display: flex; }

/* ---- LESSON PANEL ---- */
.lesson-panel {
  /* no card wrapper on mobile — full width */
}

.lesson-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.lesson-badge {
  background: var(--primary);
  color: white;
  font-size: 0.7rem;
  font-weight: 800;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.lesson-panel h1 {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1.2;
}

.lesson-intro {
  font-size: 1rem;
  line-height: 1.65;
}
.lesson-intro strong { color: var(--primary); }
.lesson-intro code {
  background: #f3e8ff;
  color: var(--primary);
  padding: 0.1em 0.3em;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.92em;
  word-break: break-all;
}
.lesson-intro ul { padding-left: 1.2rem; }
.lesson-intro li { margin-bottom: 0.2rem; }

/* Example block */
.example-block {
  background: var(--code-bg);
  border-radius: 12px;
  padding: 0.9rem 0.9rem 0.75rem;
}

.block-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.4rem;
}
.example-block .block-label { color: #a78bfa; }

.code-example {
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.9rem;
  color: #e2e8f0;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.65;
  margin-bottom: 0.7rem;
  overflow-x: auto;
}

/* Challenge block */
.challenge-block {
  background: #fffbeb;
  border: 2px solid #fde68a;
  border-radius: 12px;
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.challenge-label-text { color: #92400e; }
.challenge-text {
  font-size: 0.98rem;
  line-height: 1.6;
  color: #78350f;
}
.challenge-text code {
  background: #fef3c7;
  padding: 0.1em 0.3em;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  word-break: break-all;
}

.hint {
  font-size: 0.9rem;
  color: #065f46;
  background: #d1fae5;
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  line-height: 1.55;
}
.hint code {
  background: #a7f3d0;
  padding: 0.1em 0.28em;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  word-break: break-all;
}

/* ---- EDITOR PANEL ---- */
.editor-panel { /* full width on mobile */ }

/* Mobile keyboard helper toolbar */
.kbd-toolbar {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  padding-bottom: 0.2rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.kbd-toolbar::-webkit-scrollbar { display: none; }

.kbd-key {
  flex-shrink: 0;
  padding: 0.45rem 0.7rem;
  background: var(--code-bg);
  color: #e2e8f0;
  border: 1px solid #334155;
  border-radius: 7px;
  font-family: 'Courier New', monospace;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  white-space: nowrap;
}
.kbd-key:active { background: #334155; }

/* Code editor */
#code-editor {
  width: 100%;
  min-height: 160px;
  background: var(--code-bg);
  color: #e2e8f0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;         /* never smaller than 16px — prevents iOS zoom */
  padding: 0.85rem;
  border-radius: 12px;
  border: 2px solid transparent;
  resize: none;            /* no resize handle — we auto-grow instead */
  outline: none;
  line-height: 1.65;
  transition: border-color 0.2s;
  overflow: hidden;        /* auto-grow works with this */
}
#code-editor:focus { border-color: var(--primary); }
#code-editor::placeholder { color: #4b5563; }

.editor-actions {
  display: flex;
  gap: 0.65rem;
  flex-wrap: wrap;
}

/* Output */
.output-box {
  min-height: 80px;
  background: #0f172a;
  border-radius: 12px;
  padding: 0.85rem;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.95rem;
  line-height: 1.6;
  color: #86efac;
  white-space: pre-wrap;
  word-break: break-word;
  border: 2px solid #1e293b;
}
.output-placeholder { color: #475569; font-style: italic; }
.output-error   { color: #fca5a5 !important; }
.output-success { color: #86efac; }

/* Nav buttons */
.nav-buttons {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-top: 0.25rem;
}
.lesson-counter {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
}

/* ---- BUTTONS ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.6rem 1.2rem;
  min-height: 44px;        /* WCAG / Apple tap target minimum */
  border-radius: 999px;
  font-size: 1rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  text-decoration: none;
}
.btn:active  { transform: scale(0.95); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-run {
  background: var(--green);
  color: white;
  box-shadow: 0 3px 12px rgba(16,185,129,0.35);
  font-size: 1.05rem;
  padding: 0.65rem 1.6rem;
  flex: 1;
}

.btn-secondary {
  background: #f3e8ff;
  color: var(--primary);
  font-size: 0.9rem;
  padding: 0.5rem 1rem;
  min-height: 40px;
}

.btn-clear {
  background: #fee2e2;
  color: #b91c1c;
  font-size: 0.95rem;
  padding: 0.6rem 1rem;
  flex: 0 0 auto;
}

.btn-hint {
  background: #fef3c7;
  color: #92400e;
  font-size: 0.88rem;
  padding: 0.45rem 0.9rem;
  min-height: 40px;
  align-self: flex-start;
}

.btn-nav {
  background: var(--primary);
  color: white;
  padding: 0.55rem 1rem;
  font-size: 0.9rem;
}
.btn-nav:disabled { background: #c4b5fd; }

/* ---- CELEBRATION ---- */
.celebration {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1.5rem;
  animation: fadeIn 0.25s ease;
}
.celebration-box {
  background: white;
  border-radius: 24px;
  padding: 2rem 1.75rem 1.75rem;
  text-align: center;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  animation: popIn 0.3s cubic-bezier(0.34,1.56,0.64,1);
}
.celebration-emoji { font-size: 3.5rem; margin-bottom: 0.6rem; }
.celebration-box h2 { font-size: 1.5rem; color: var(--primary); margin-bottom: 0.4rem; }
.celebration-box p  { color: var(--muted); font-size: 0.97rem; margin-bottom: 1.25rem; line-height: 1.5; }
.celebration-box .btn-run { width: 100%; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
  from { transform: scale(0.7); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

/* Confetti */
.confetti-piece {
  position: fixed;
  border-radius: 2px;
  animation: fall 1.8s ease-in forwards;
  z-index: 199;
  pointer-events: none;
}
@keyframes fall {
  0%   { transform: translateY(-20px) rotate(0deg);   opacity: 1; }
  100% { transform: translateY(105vh) rotate(720deg); opacity: 0; }
}


/* ================================================================
   DESKTOP — two-column layout, hide mobile-only controls
   ================================================================ */
@media (min-width: 720px) {

  body { font-size: 17px; }

  header { padding: 0.75rem 2rem; }
  .logo-text { font-size: 1.75rem; }

  /* Hide tab bar on desktop */
  .tab-bar { display: none; }

  /* Always show both panels side by side */
  #app {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    padding: 1.75rem 1.5rem 3rem;
    align-items: start;
  }

  .tab-panel {
    display: flex !important;   /* override mobile tab hide */
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 2px solid var(--border);
    padding: 1.75rem;
    gap: 1.25rem;
  }

  /* Hide the duplicate nav inside lesson panel on desktop */
  .lesson-panel .nav-buttons { display: none; }

  /* Hide keyboard toolbar on desktop (Tab key works fine) */
  .kbd-toolbar { display: none; }

  #code-editor {
    min-height: 200px;
    resize: vertical;
    overflow: auto;
  }

  .code-example  { font-size: 0.95rem; }
  .lesson-panel h1 { font-size: 1.45rem; }
}
