/* ==========================================================================
   TIMSS 2019 Building Lab - Dedicated Mobile & Responsive Stylesheet (mobile.css)
   Engineered for iOS Safari, Android Chrome, Tablets, Foldables & Ultra-wide displays
   ========================================================================== */

/* ── 1. Root & App Shell Foundation (Mobile Only <= 768px) ─────────────────── */
@media (max-width: 768px) {
  :root {
    --mobile-header-height: 54px;
    --mobile-segment-height: 44px;
    --mobile-bottom-bar-height: 72px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
  }

  html, body {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    touch-action: manipulation;
  }

  body {
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    background-color: #060a14;
    background-image: 
      radial-gradient(circle at 50% 0%, rgba(56, 189, 248, 0.12) 0%, transparent 60%),
      linear-gradient(180deg, #070c18 0%, #03060d 100%);
    background-attachment: fixed;
    overscroll-behavior-y: contain;
  }

  /* ── 2. Native Mobile Header Bar ───────────────────────────────────────── */
  .lab-header {
    height: auto;
    min-height: var(--mobile-header-height);
    padding: 8px 12px;
    position: sticky;
    top: 0;
    z-index: 500;
    background: rgba(7, 12, 24, 0.94);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid rgba(56, 189, 248, 0.2);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  .header-brand {
    gap: 8px;
    flex-shrink: 1;
    min-width: 0;
  }

  .brand-icon {
    width: 34px;
    height: 34px;
    font-size: 1.05rem;
    border-radius: 9px;
    flex-shrink: 0;
  }

  .brand-logo-badge {
    height: 34px;
    padding: 2px 7px;
    border-radius: 9px;
    background: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .brand-logo-img {
    height: 22px;
    width: auto;
    max-width: 68px;
    object-fit: contain;
    display: block;
  }

  .brand-info h1 {
    font-size: 0.92rem;
    font-weight: 800;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .brand-info div {
    display: none; /* Subtitle hidden to keep app bar single-row */
  }

  .header-stage-flow {
    display: none; /* Replaced by slim progress line on mobile */
  }

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

  .mode-badge {
    height: 32px;
    padding: 0 10px;
    font-size: 0.72rem;
    gap: 5px;
    border-radius: 9999px;
  }

  .action-btn.menu-toggle-btn {
    height: 34px;
    padding: 0 10px;
    font-size: 0.78rem;
    gap: 5px;
    border-radius: 9999px;
  }

  /* Slim Continuous Mobile Progress Line */
  .mobile-progress-strip {
    display: block !important;
    position: sticky;
    top: var(--mobile-header-height);
    left: 0;
    right: 0;
    width: 100%;
    height: 4px;
    background: rgba(255, 255, 255, 0.08);
    z-index: 499;
  }

  .mobile-progress-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #38bdf8, #6366f1, #10b981);
    transition: width 0.35s ease;
    box-shadow: 0 0 8px rgba(56, 189, 248, 0.7);
  }

  /* ── 3. Native Segmented Mode Switcher (Mobile Tabs) ────────────────────── */
  .mobile-app-segmented-bar {
    display: flex !important;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 6px 12px;
    background: rgba(13, 20, 36, 0.96);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    position: sticky;
    top: calc(var(--mobile-header-height) + 4px);
    z-index: 450;
  }

  .mobile-segment-btn {
    flex: 1;
    height: 36px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: #94a3b8;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
    user-select: none;
  }

  .mobile-segment-btn i {
    font-size: 0.86rem;
  }

  .mobile-segment-btn.active {
    background: linear-gradient(135deg, #0284c7 0%, #2563eb 100%);
    border-color: rgba(56, 189, 248, 0.5);
    color: #ffffff;
    font-weight: 800;
    box-shadow: 0 2px 10px rgba(2, 132, 199, 0.45);
    transform: translateY(-1px);
  }

  .mobile-segment-btn:active {
    transform: scale(0.96);
  }

  /* ── 4. Dynamic Workspace Layout Modes ──────────────────────────────────── */
  .lab-main-container {
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 10px 10px calc(var(--mobile-bottom-bar-height) + var(--safe-bottom) + 24px);
    max-width: 100vw;
  }

  /* Mode: Viewport Focus */
  .lab-main-container.mobile-mode-viewport .visual-viewport-card {
    display: flex !important;
    height: calc(100dvh - 170px);
    max-height: none;
    min-height: 400px;
  }

  .lab-main-container.mobile-mode-viewport .task-control-card {
    display: none !important;
  }

  .lab-main-container.mobile-mode-viewport .mobile-viewport-switch-pill {
    display: flex !important;
  }

  /* Mode: Task Focus */
  .lab-main-container.mobile-mode-task .visual-viewport-card {
    display: none !important;
  }

  .lab-main-container.mobile-mode-task .task-control-card {
    display: flex !important;
    width: 100%;
    min-height: calc(100dvh - 190px);
  }

  /* Mode: Split (Default) */
  .lab-main-container.mobile-mode-split .visual-viewport-card {
    display: flex !important;
    height: 38dvh;
    min-height: 250px;
    max-height: 340px;
  }

  .lab-main-container.mobile-mode-split .task-control-card {
    display: flex !important;
  }

  /* ── 5. Mobile 3D Viewport & Touch Protection ──────────────────────────── */
  .visual-viewport-card {
    border-radius: 16px;
    border: 1px solid rgba(56, 189, 248, 0.25);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    position: relative;
    overflow: hidden;
  }

  #building-3d-canvas,
  #wall-grid-canvas,
  #grid-tool-container {
    touch-action: none !important; /* Critical: prevents finger drawing from triggering page scroll */
    user-select: none !important;
    -webkit-user-select: none !important;
  }

  .viewport-toolbar {
    padding: 6px 10px;
    gap: 6px;
    background: rgba(13, 20, 38, 0.95);
  }

  .viewport-tabs-compact {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    gap: 6px;
  }

  .viewport-tabs-compact::-webkit-scrollbar {
    display: none;
  }

  .view-tab {
    height: 32px;
    padding: 0 10px;
    font-size: 0.78rem;
    border-radius: 8px;
    flex-shrink: 0;
  }

  .custom-dropdown-trigger {
    height: 32px;
    padding: 0 10px;
    font-size: 0.76rem;
    border-radius: 8px;
    max-width: 160px;
  }

  .custom-dropdown-panel {
    width: 270px;
    max-height: 60vh;
    overflow-y: auto;
  }

  .view-action-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    font-size: 0.82rem;
  }

  /* Quick Floating Switch Pill inside Viewport */
  .mobile-viewport-switch-pill {
    display: none;
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #0284c7 0%, #2563eb 100%);
    border: 1px solid rgba(56, 189, 248, 0.6);
    border-radius: 9999px;
    padding: 10px 22px;
    color: #ffffff;
    font-family: inherit;
    font-size: 0.86rem;
    font-weight: 800;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    box-shadow: 0 6px 22px rgba(2, 132, 199, 0.55), 0 0 15px rgba(56, 189, 248, 0.3);
    z-index: 50;
    white-space: nowrap;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-viewport-switch-pill:active {
    transform: translateX(-50%) scale(0.95);
  }

  /* ── 6. Mobile Task Control Card & Typography ──────────────────────────── */
  .task-control-card {
    padding: 16px 14px calc(var(--mobile-bottom-bar-height) + var(--safe-bottom) + 30px);
    border-radius: 16px;
    background: rgba(13, 20, 38, 0.94);
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
    gap: 14px;
    overflow: visible;
  }

  .task-meta-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .task-stage-badge {
    font-size: 0.78rem;
    padding: 4px 10px;
    border-radius: 9999px;
  }

  .task-cognitive-domain {
    font-size: 0.72rem;
  }

  .task-title {
    font-size: 1.15rem;
    line-height: 1.35;
    font-weight: 800;
  }

  .task-description {
    font-size: 0.88rem;
    line-height: 1.55;
    color: #cbd5e1;
  }

  .given-info-box {
    padding: 12px;
    border-radius: 12px;
    font-size: 0.84rem;
  }

  .given-info-list {
    gap: 5px;
  }

  /* ── 7. Mobile MCQ Cards ───────────────────────────────────────────────── */
  .mcq-options-container {
    gap: 8px;
  }

  .mcq-option-label {
    padding: 12px 14px;
    min-height: 48px;
    border-radius: 12px;
    gap: 12px;
  }

  .mcq-option-text {
    font-size: 0.94rem;
    line-height: 1.35;
  }

  .mcq-radio-custom {
    width: 22px;
    height: 22px;
  }

  /* ── 8. Mobile Numerical Inputs & Virtual Keypad ───────────────────────── */
  .numeric-input-wrapper {
    max-width: 100%;
    gap: 8px;
  }

  .numeric-input-field,
  input[type="text"],
  input[type="number"] {
    font-size: 18px !important; /* Prevents iOS auto-zoom on input focus */
    height: 48px;
    border-radius: 12px;
    text-align: center;
    font-weight: 800;
  }

  .interactive-numpad-wrapper {
    border-radius: 14px;
    padding: 8px;
    gap: 6px;
    max-width: 100%;
  }

  .numpad-active-target-badge {
    padding: 4px 10px;
    font-size: 0.78rem;
  }

  .numpad-grid {
    gap: 5px;
  }

  .numpad-btn {
    height: 46px;
    font-size: 1.2rem;
    border-radius: 10px;
  }

  .numpad-btn.action {
    font-size: 0.95rem;
  }

  /* ── 9. Docked Mobile Bottom Action Bar ────────────────────────────────── */
  .task-actions-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 400;
    background: rgba(10, 16, 32, 0.96);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border-top: 1px solid rgba(56, 189, 248, 0.3);
    padding: 10px 14px calc(10px + var(--safe-bottom));
    margin-top: 0;
    box-shadow: 0 -8px 25px rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: nowrap;
  }

  .task-actions-bar .btn-check {
    flex: 2;
    height: 48px;
    border-radius: 12px;
    font-size: 0.98rem;
    font-weight: 800;
  }

  .task-actions-bar .btn-next {
    flex: 2;
    height: 48px;
    border-radius: 12px;
    font-size: 0.98rem;
    font-weight: 800;
  }

  .task-actions-bar .btn-prev {
    flex: 1;
    min-width: 44px;
    height: 48px;
    padding: 0 10px;
    border-radius: 12px;
    font-size: 0.85rem;
    justify-content: center;
  }

  .task-actions-bar .btn-help-inline {
    flex: 1;
    min-width: 44px;
    height: 48px;
    padding: 0 10px;
    border-radius: 12px;
    font-size: 0.85rem;
    justify-content: center;
    margin-right: 0;
  }

  .task-actions-bar .btn-ghost-danger {
    flex: 1;
    height: 48px;
    padding: 0 10px;
    font-size: 0.85rem;
  }

  /* ── 10. Track Selection Modal for Mobile ──────────────────────────────── */
  .track-modal-overlay {
    padding: 10px;
    align-items: flex-end;
  }

  .track-modal-dialog {
    max-width: 100%;
    width: 100%;
    border-radius: 20px 20px 0 0;
    padding: 18px 14px calc(16px + var(--safe-bottom));
    max-height: calc(100dvh - 40px);
    gap: 12px;
  }

  .track-cards-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .track-card {
    padding: 12px 14px;
    border-radius: 12px;
  }

  .track-card-desc {
    font-size: 0.78rem;
  }

  .track-card-features {
    font-size: 0.74rem;
  }

  .btn-start-track {
    height: 48px;
    font-size: 1rem;
  }

  /* ── 11. Mobile Floating Calculator Bottom Sheet ───────────────────────── */
  .floating-calculator {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 22px 22px 0 0;
    box-shadow: 0 -10px 45px rgba(0, 0, 0, 0.85);
    z-index: 600;
    transform: translateY(115%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    padding: 14px 14px calc(14px + var(--safe-bottom));
  }

  .floating-calculator.show {
    transform: translateY(0);
  }

  .calc-display {
    font-size: 1.6rem;
    height: 52px;
    padding: 6px 12px;
  }

  .calc-keypad {
    gap: 6px;
  }

  .calc-key {
    height: 46px;
    font-size: 1.1rem;
    border-radius: 10px;
  }

  /* ── 12. Help Drawer Full Screen on Mobile ─────────────────────────────── */
  .help-side-panel {
    width: 100vw;
    max-width: 100vw;
    border-right: none;
    z-index: 700;
    padding: 16px 14px calc(16px + var(--safe-bottom));
  }

  .help-drawer-trigger {
    bottom: calc(var(--mobile-bottom-bar-height) + var(--safe-bottom) + 12px);
    left: 12px;
    padding: 9px 15px;
    font-size: 0.82rem;
    z-index: 410;
  }

  /* ── 13. Screen 7 Showcase on Mobile ───────────────────────────────────── */
  .score-hero-card {
    padding: 16px 12px;
  }

  .score-circle-display {
    font-size: 2.2rem;
  }

  .skills-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
}

/* ── 15. Mobile Landscape Orientation (Game-pad layout, <= 540px height) ── */
@media (max-height: 540px) and (orientation: landscape) {
  :root {
    --mobile-header-height: 44px;
    --mobile-bottom-bar-height: 0px;
  }

  .lab-header {
    min-height: 44px;
    padding: 4px 12px;
  }

  .mobile-app-segmented-bar {
    display: none !important; /* Hide tabs in landscape; side-by-side split is active */
  }

  .lab-main-container {
    grid-template-columns: 1.15fr 1fr !important;
    gap: 10px;
    padding: 6px 10px 10px !important;
    height: calc(100dvh - 46px);
    overflow: hidden;
  }

  .visual-viewport-card {
    height: 100% !important;
    max-height: none !important;
    min-height: 0 !important;
    display: flex !important;
  }

  .task-control-card {
    height: 100% !important;
    max-height: none !important;
    overflow-y: auto !important;
    padding: 12px 14px 14px !important;
    display: flex !important;
  }

  .task-actions-bar {
    position: static !important;
    box-shadow: none !important;
    background: transparent !important;
    border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
    padding: 10px 0 0 !important;
  }

  .track-modal-dialog {
    max-height: calc(100dvh - 16px) !important;
    padding: 12px 16px !important;
  }

  .track-cards-grid {
    grid-template-columns: 1fr 1fr !important;
  }
}

/* ── 16. Tablets & iPads (769px to 1024px) ───────────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
  .lab-main-container {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 14px 16px 80px;
    max-width: 100%;
  }

  .visual-viewport-card {
    height: 45vh;
    min-height: 360px;
    max-height: 460px;
  }

  .task-control-card {
    max-height: none;
    overflow: visible;
  }

  .progress-track {
    max-width: 360px;
  }

  .warehouse-elements-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ── 17. Ultra-Small Phones (<= 360px, e.g. Galaxy Z Fold outer) ─────────── */
@media (max-width: 360px) {
  .brand-info h1 {
    font-size: 0.8rem;
  }

  .mode-badge {
    display: none !important;
  }

  .mobile-segment-btn {
    font-size: 0.72rem;
    gap: 3px;
    padding: 0 2px;
  }

  .mobile-segment-btn span {
    font-size: 0.7rem;
  }

  .task-actions-bar .btn-prev span,
  .task-actions-bar .btn-help-inline span {
    display: none;
  }
}
