/* ==========================================================================
   IPHI CODING LAB - MOBILE-FIRST RESPONSIVE STYLESHEET
   Comprehensive Layout Adaptations for Smartphone, Tablet, and Desktop
   MA IPHI Pamarican - Guru Pengampu: Idrus Abdul Kholik
   ========================================================================== */

/* --------------------------------------------------------------------------
   GLOBAL VIEWPORT & OVERFLOW GUARDS
   -------------------------------------------------------------------------- */
html, body {
  overflow-x: hidden !important;
  max-width: 100vw;
  width: 100%;
}

/* Fluid responsive container */
.container {
  width: 100%;
  max-width: var(--container-max);
  margin-left: auto;
  margin-right: auto;
  padding-left: max(16px, env(safe-area-inset-left, 16px));
  padding-right: max(16px, env(safe-area-inset-right, 16px));
}

/* Ensure images, svgs, and iframes never cause horizontal overflow */
img, svg, iframe, canvas, video {
  max-width: 100%;
}

pre, code {
  max-width: 100%;
}

/* ==========================================================================
   1. TABLETS & SMALLER DESKTOPS (<= 1024px)
   ========================================================================== */
@media (max-width: 1024px) {
  :root {
    --header-height: 60px;
  }

  /* Desktop Top Navigation links are hidden; mobile uses bottom nav + quick drawer */
  .main-nav {
    display: none !important;
  }

  /* Mobile hamburger/quick menu toggle is visible */
  .mobile-nav-toggle {
    display: flex;
  }

  /* Bottom Navigation Bar is active */
  .mobile-bottom-nav {
    display: flex;
  }

  /* Add bottom clearance for page content so nothing is covered by bottom nav */
  body {
    padding-bottom: calc(62px + env(safe-area-inset-bottom, 0px));
  }

  /* When workspace is active, maximize editor screen area */
  body.body-workspace-active {
    padding-bottom: 0 !important;
  }

  body.body-workspace-active .mobile-bottom-nav {
    display: none !important;
  }

  /* App Header layout */
  .header-container {
    height: 100%;
    padding-left: 12px;
    padding-right: 12px;
  }

  /* Hero Banner layout */
  .hero-banner-inner {
    flex-direction: column;
    text-align: center;
    gap: 28px;
    padding: 20px 0 10px;
  }

  .hero-content {
    max-width: 100%;
  }

  .hero-cta-group {
    justify-content: center;
  }

  .hero-graphic {
    max-width: 480px;
    width: 100%;
    margin: 0 auto;
  }

  /* Modals on tablets */
  .challenge-modal-layout {
    grid-template-columns: 1fr;
    max-height: 85vh;
  }

  .profile-hero {
    flex-direction: column;
    text-align: center;
    gap: 18px;
  }
}

/* ==========================================================================
   2. SMARTPHONES & PORTRAIT TABLETS (<= 768px)
   ========================================================================== */
@media (max-width: 768px) {
  /* Typography scale */
  .hero-title {
    font-size: clamp(1.65rem, 5.5vw, 2.1rem);
    letter-spacing: -0.5px;
    line-height: 1.25;
  }

  .hero-desc {
    font-size: 0.95rem;
    line-height: 1.6;
    margin-bottom: 20px;
  }

  .section-title {
    font-size: 1.35rem;
  }

  .section-subtitle {
    font-size: 0.85rem;
  }

  /* Hero Section spacing */
  .hero-section {
    padding: 16px 0 24px;
  }

  /* Compact Hero Graphic for mobile */
  .hero-graphic {
    display: none; /* Hide heavy code graphic on phones to keep primary CTAs above the fold */
  }

  .hero-cta-group {
    flex-direction: column;
    width: 100%;
    gap: 10px;
  }

  .hero-cta-group .btn {
    width: 100%;
    min-height: 48px;
    font-size: 0.95rem;
    justify-content: center;
  }

  /* Progress Summary: 2x2 Grid as specified in prompt */
  .stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
    margin-bottom: 24px;
  }

  .stat-card {
    padding: 12px 10px;
    gap: 10px;
    border-radius: var(--radius-md);
  }

  .stat-icon-wrap {
    width: 38px;
    height: 38px;
    font-size: 1.15rem;
  }

  .stat-info h3 {
    font-size: 1.1rem;
    font-weight: 800;
  }

  .stat-info p {
    font-size: 0.72rem;
    line-height: 1.3;
  }

  /* Lanjutkan Belajar & Tantangan Hari Ini Cards */
  .resume-card {
    flex-direction: column;
    align-items: stretch;
    padding: 16px;
    gap: 14px;
  }

  .resume-details h4 {
    font-size: 0.75rem;
  }

  .resume-details h3 {
    font-size: 1.1rem;
    margin: 4px 0 6px;
  }

  .resume-details p {
    font-size: 0.85rem;
    line-height: 1.5;
  }

  .resume-action .btn {
    width: 100%;
    min-height: 44px;
    justify-content: center;
  }

  /* Content Cards & Grids */
  .card-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .content-card {
    border-radius: var(--radius-md);
  }

  .content-card .card-body {
    padding: 16px;
  }

  .content-card h3 {
    font-size: 1.05rem;
  }

  /* Filter pill horizontal bars (Swipeable) */
  .filter-bar {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
    gap: 8px;
    scrollbar-width: none;
  }

  .filter-bar::-webkit-scrollbar {
    display: none;
  }

  .filter-btn {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 8px 14px;
    font-size: 0.82rem;
  }

  /* --------------------------------------------------------------------------
     MOBILE CODING WORKSPACE (TOP PRIORITY REDESIGN)
     -------------------------------------------------------------------------- */
  .workspace-wrapper {
    height: calc(100dvh - var(--header-height));
    height: calc(100vh - var(--header-height));
  }

  .ws-toolbar {
    height: 48px;
    padding: 0 8px;
    gap: 6px;
  }

  .ws-project-meta {
    gap: 8px;
    flex: 1;
    min-width: 0;
  }

  .ws-project-title-input {
    max-width: 130px;
    font-size: 0.85rem;
    padding: 3px 6px;
  }

  .ws-status-badge {
    padding: 2px 6px;
    font-size: 0.7rem;
  }

  #wsStatusText {
    display: none; /* Hide status text on small phones, show status dot */
  }

  .ws-actions-primary {
    gap: 6px;
    flex-shrink: 0;
  }

  .btn-run-code {
    min-height: 38px;
    padding: 0 14px;
    font-size: 0.85rem;
  }

  /* Mobile segmented tabs (Editor | Live Preview | Console) */
  .ws-mobile-view-tabs {
    display: flex;
    background: #111827;
    border-bottom: 1px solid #1f2937;
    padding: 4px;
    gap: 4px;
    height: 42px;
  }

  .ws-mobile-tab-btn {
    flex: 1;
    height: 34px;
    padding: 0 6px;
    font-size: 0.82rem;
    font-weight: 700;
    text-align: center;
    color: #94a3b8;
    border-radius: var(--radius-sm);
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all var(--transition-fast);
  }

  .ws-mobile-tab-btn.active {
    background: var(--primary);
    color: #ffffff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  }

  /* Single-Pane Mode Switching on Mobile */
  .ws-main-split {
    display: flex;
    flex-direction: column;
    flex: 1;
    height: calc(100% - 90px);
    overflow: hidden;
  }

  .ws-main-split .ws-editor-pane,
  .ws-main-split .ws-preview-pane {
    flex: 1;
    width: 100%;
    height: 100%;
    display: none;
    min-height: 0;
  }

  .ws-main-split.mobile-mode-editor .ws-editor-pane {
    display: flex;
  }

  .ws-main-split.mobile-mode-preview .ws-preview-pane {
    display: flex;
  }

  .ws-main-split.mobile-mode-console .ws-preview-pane {
    display: flex;
  }

  .ws-main-split.mobile-mode-console .ws-console-drawer {
    display: flex;
    height: 100%;
  }

  .ws-main-split.mobile-mode-console .ws-preview-stage {
    display: none;
  }

  /* Editor Tabs & Quick Assist Bar */
  .ws-editor-tabs-bar {
    height: 38px;
    padding: 0 6px;
  }

  .ws-lang-tab {
    padding: 0 12px;
    font-size: 0.8rem;
  }

  .ws-editor-views-wrapper {
    flex: 1;
    min-height: 0;
  }

  /* Fallback Textarea Editor Touch Optimization */
  .simple-code-editor-container {
    font-size: 14px;
    line-height: 1.5;
  }

  .editor-gutter {
    width: 42px;
    padding: 10px 6px 10px 0;
    font-size: 13px;
  }

  .editor-code-textarea {
    padding: 10px 12px;
    font-size: 14px;
    line-height: 1.5;
    -webkit-overflow-scrolling: touch;
  }

  /* Live Preview on Mobile */
  .ws-preview-bar {
    padding: 0 10px;
    height: 42px;
  }

  .ws-preview-size-controls {
    display: none; /* Mobile view automatically occupies full viewport simulation */
  }

  .preview-frame-container {
    width: 100%;
    height: 100%;
    border: none;
    border-radius: 0;
  }

  /* --------------------------------------------------------------------------
     MOBILE LEARNING (LESSON READER MODAL)
     -------------------------------------------------------------------------- */
  .modal-backdrop {
    padding: 0;
    align-items: flex-end; /* Slides up like a native mobile bottom sheet */
  }

  .modal-box {
    max-height: 92vh;
    border-radius: 20px 20px 0 0;
    width: 100%;
    border-bottom: none;
  }

  .modal-box.lesson-modal-content {
    max-height: 94vh;
  }

  .modal-header {
    padding: 14px 18px;
    position: sticky;
    top: 0;
    background: var(--bg-surface);
    z-index: 10;
  }

  .modal-title {
    font-size: 1.1rem;
  }

  .modal-body {
    padding: 16px 18px;
    max-height: calc(92vh - 70px);
    -webkit-overflow-scrolling: touch;
  }

  .lesson-article {
    font-size: 0.95rem;
    line-height: 1.7;
  }

  .lesson-code-block {
    padding: 14px;
    font-size: 0.85rem;
  }

  /* Quiz on Mobile */
  .quiz-container {
    padding: 16px 12px;
  }

  .quiz-question-card {
    padding: 16px;
  }

  .quiz-question-text {
    font-size: 1.05rem;
    line-height: 1.5;
  }

  .quiz-options-list {
    gap: 10px;
  }

  .quiz-option-btn {
    padding: 12px 14px;
    font-size: 0.9rem;
    min-height: 48px;
  }

  /* Mobile Footer */
  .app-footer {
    padding: 30px 0 20px;
  }

  .footer-top {
    flex-direction: column;
    gap: 20px;
  }

  .footer-bottom {
    flex-direction: column;
    text-align: center;
    gap: 10px;
    font-size: 0.78rem;
  }
}

/* ==========================================================================
   3. SMALL SMARTPHONES (<= 430px: iPhone 13/14/15/16 Pro Max, Plus, Pixel)
   ========================================================================== */
@media (max-width: 430px) {
  .brand-logo-svg {
    width: 175px;
    height: 38px;
  }

  .user-xp-pill {
    padding: 2px 8px 2px 4px;
    gap: 6px;
  }

  .xp-level-badge {
    padding: 2px 6px;
    font-size: 0.7rem;
  }

  .xp-count-text {
    font-size: 0.78rem;
  }

  /* Workspace Header tighter fit */
  .ws-project-title-input {
    max-width: 105px;
  }

  .ws-lang-tab {
    padding: 0 8px;
    font-size: 0.78rem;
  }

  .ws-symbol-btn {
    min-width: 35px;
    height: 35px;
    font-size: 14px;
  }
}

/* ==========================================================================
   4. ULTRA-SMALL SMARTPHONES (320px - 360px: iPhone SE, Galaxy A01, etc.)
   ========================================================================== */
@media (max-width: 360px) {
  .brand-logo-svg {
    width: 155px;
    height: 34px;
  }

  .user-xp-pill {
    display: none; /* Hide header pill on 320px to prevent header overflow */
  }

  .hero-title {
    font-size: 1.5rem;
  }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px !important;
  }

  .stat-card {
    padding: 10px 8px;
  }

  .stat-icon-wrap {
    width: 32px;
    height: 32px;
    font-size: 1rem;
  }

  .stat-info h3 {
    font-size: 1rem;
  }

  .btn-run-code {
    padding: 0 10px;
    font-size: 0.78rem;
  }

  .ws-symbol-btn {
    min-width: 32px;
    height: 32px;
    font-size: 13px;
    padding: 0 6px;
  }

  .mobile-nav-label {
    font-size: 0.62rem;
  }
}

/* ==========================================================================
   5. DESKTOP & LARGE WORKSTATION ENHANCEMENTS (>= 1025px)
   ========================================================================== */
@media (min-width: 1025px) {
  /* Show Top Navbar */
  .main-nav {
    display: flex !important;
  }

  /* Hide Mobile Bottom Navigation */
  .mobile-bottom-nav {
    display: none !important;
  }

  /* Hide Mobile Drawer Toggle */
  .mobile-nav-toggle {
    display: none !important;
  }

  /* Desktop Split Workspace (Side-by-side Editor & Preview) */
  .ws-main-split {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
  }

  .ws-main-split .ws-editor-pane,
  .ws-main-split .ws-preview-pane {
    display: flex !important;
  }

  /* Hide Mobile Segmented Mode Tabs on Desktop */
  .ws-mobile-view-tabs {
    display: none !important;
  }

  /* Hide Back to Editor Button on Desktop Preview */
  #btnPreviewBackToEditor {
    display: none !important;
  }
}

/* ==========================================================================
   6. ACCESSIBILITY: PREFERS-REDUCED-MOTION
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
