/**
 * ATLAS - Responsive Design
 * Full responsive support for mobile, tablet (iPad), and desktop
 */

/* ============================================
   Base
   ============================================ */

* { box-sizing: border-box; }

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

/* ============================================
   Mobile Nav Toggle (hamburger)
   ============================================ */

.mobile-nav-toggle {
    display: none;
    background: none;
    border: 1px solid var(--border-primary);
    border-radius: 6px;
    padding: 6px 10px;
    color: var(--text-primary);
    cursor: pointer;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    font-size: 0.8125rem;
}

.mobile-nav-toggle svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
}

/* ============================================
   Desktop (1200px+) — default, no changes
   ============================================ */

/* ============================================
   Tablet landscape / small desktop (900-1200px)
   ============================================ */

@media (max-width: 1200px) {
    .workspace-content {
        grid-template-columns: 1fr 280px;
    }

    .problem-panel {
        display: none;
    }

    .progress-overview,
    .dashboard-stats,
    .gradebook-stats {
        grid-template-columns: repeat(2, 1fr);
    }

    /* CFG: stack grammar panel narrower */
    .cfg-grammar-panel {
        width: 280px;
        min-width: 240px;
    }

    /* Support button: hide label */
    .support-btn-label {
        display: none;
    }

    .support-btn {
        padding: 6px 8px;
    }
}

/* ============================================
   Tablet portrait / iPad (768-900px)
   ============================================ */

@media (max-width: 900px) {
    /* Sidebar: collapse to icons */
    .sidebar {
        width: 60px;
    }

    .topic-info,
    .topic-progress,
    .sidebar-title,
    .action-btn span {
        display: none;
    }

    .topic-item {
        justify-content: center;
    }

    .action-btn {
        justify-content: center;
        padding: 0.75rem;
    }

    /* Workspace: single column */
    .workspace-content {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr auto;
    }

    .test-panel {
        border-left: none;
        border-top: 1px solid var(--border-color);
        max-height: 200px;
        overflow-y: auto;
    }

    .dashboard-grid {
        grid-template-columns: 1fr;
    }

    /* CFG: stack vertically */
    .cfg-layout {
        flex-direction: column;
    }

    .cfg-grammar-panel {
        width: 100%;
        max-height: 250px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    /* PDA: stack vertically */
    .pda-layout {
        flex-direction: column;
    }

    .pda-def-panel {
        width: 100%;
        max-height: 250px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .pda-viz-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .cfg-tree-panel {
        min-height: 250px;
    }

    .cfg-guide {
        max-height: 200px;
    }

    /* Quiz toggles */
    .quiz-toggle-grid {
        gap: 6px;
    }

    .quiz-attempts-control {
        margin-left: 8px;
    }

    /* AI chat */
    .ai-widget-panel {
        width: 340px;
        right: 8px;
        bottom: 70px;
    }
}

/* ============================================
   Mobile (max-width: 768px)
   ============================================ */

@media (max-width: 768px) {
    /* Show hamburger menu */
    .mobile-nav-toggle {
        display: flex;
    }

    .header-left {
        gap: 0.75rem;
    }

    /* Hide desktop nav, show via toggle */
    .main-nav {
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border-color);
        padding: 0.5rem;
        flex-direction: column;
        z-index: 100;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    }

    .main-nav.mobile-open {
        display: flex;
    }

    .main-nav .nav-btn {
        width: 100%;
        justify-content: flex-start;
        padding: 0.75rem 1rem;
    }

    .class-indicator {
        display: none;
    }

    .header-right {
        gap: 8px;
    }

    .user-name {
        display: none;
    }

    /* Sidebar: slide-out drawer */
    .sidebar {
        position: fixed;
        left: -280px;
        top: 0;
        height: 100vh;
        width: 280px;
        z-index: 999;
        transition: left 0.3s ease;
        box-shadow: 2px 0 15px rgba(0, 0, 0, 0.4);
        background: var(--bg-primary);
    }

    .sidebar.mobile-open {
        left: 0;
    }

    .sidebar .topic-info,
    .sidebar .topic-progress,
    .sidebar .sidebar-title,
    .sidebar .action-btn span {
        display: block;
    }

    .sidebar .topic-item {
        justify-content: flex-start;
    }

    .sidebar .action-btn {
        justify-content: flex-start;
    }

    /* Workspace */
    .workspace-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        padding: 0.75rem;
    }

    .workspace-actions {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .workspace-content {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr;
    }

    .test-panel {
        display: none;
    }

    .canvas-toolbar {
        flex-wrap: wrap;
        padding: 0.5rem;
        gap: 4px;
    }

    .tool-btn {
        min-width: 36px;
        min-height: 36px;
        padding: 6px;
    }

    .workspace-footer {
        padding: 0.5rem 0.75rem;
        font-size: 0.6875rem;
    }

    .keyboard-hint {
        display: none;
    }

    /* CFG workspace mobile */
    .cfg-layout {
        flex-direction: column;
    }

    .cfg-grammar-panel {
        width: 100%;
        max-height: 220px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .cfg-rules-textarea {
        min-height: 100px;
    }

    .cfg-help {
        max-height: 120px;
        font-size: 0.6875rem;
    }

    .cfg-tree-panel {
        min-height: 220px;
    }

    .cfg-guide {
        max-height: 180px;
        padding: 12px 14px;
    }

    .cfg-guide-content {
        font-size: 0.75rem;
    }

    .cfg-derivation-panel {
        max-height: 100px;
        padding: 0.5rem 0.75rem;
    }

    /* Problems */
    .problems-grid {
        grid-template-columns: 1fr;
    }

    .shortcuts-grid,
    .conversion-options {
        grid-template-columns: 1fr;
    }

    /* Modals */
    .modal-content {
        width: 95%;
        max-width: 95vw;
        max-height: 90vh;
        margin: 1rem;
        padding: 1rem;
    }

    .auth-modal {
        padding: 1.5rem;
    }

    /* Gradebook */
    .gradebook-stats {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .quiz-toggle-row {
        padding: 8px 10px;
        gap: 8px;
        flex-wrap: wrap;
    }

    .quiz-toggle-name {
        font-size: 0.75rem;
    }

    .quiz-attempts-control {
        margin-left: auto;
    }

    .inst-table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .inst-table {
        min-width: 600px;
    }

    /* AI Chat */
    .ai-widget-panel {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        border-radius: 0;
        z-index: 1100;
    }

    .ai-msg-bubble {
        max-width: 90%;
    }

    .ai-diagram-container,
    .ai-mermaid-rendered {
        padding: 8px;
    }

    /* Study view */
    .study-layout {
        flex-direction: column;
    }

    .study-sidebar {
        width: 100%;
        max-height: 150px;
        overflow-y: auto;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    /* Profile modal */
    .profile-content {
        flex-direction: column;
    }

    .profile-sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    /* Progress */
    .progress-overview {
        flex-direction: column;
    }

    .progress-circle {
        width: 150px;
        height: 150px;
    }

    .topic-progress-list {
        grid-template-columns: 1fr;
    }

    /* Practice Quiz responsive */
    .quiz-topic-checkboxes {
        grid-template-columns: 1fr;
    }

    .topic-breakdown-label {
        min-width: 100px;
        font-size: 0.75rem;
    }
}

/* ============================================
   Small Mobile (max-width: 480px)
   ============================================ */

@media (max-width: 480px) {
    html {
        font-size: 14px;
    }

    .app-header {
        padding: 0.5rem 0.75rem;
    }

    .logo-text {
        font-size: 1rem;
    }

    .mode-toggle {
        display: none;
    }

    /* CFG: full-width stacked */
    .cfg-grammar-panel {
        max-height: 180px;
    }

    .cfg-panel-actions {
        flex-wrap: wrap;
        gap: 4px;
    }

    .cfg-panel-actions .btn {
        font-size: 0.6875rem;
        padding: 4px 8px;
    }

    .cfg-guide {
        max-height: 160px;
        padding: 10px;
    }

    .cfg-guide-content {
        font-size: 0.6875rem;
    }

    .cfg-guide-example {
        font-size: 0.6875rem;
        padding: 6px 10px;
    }

    /* Gradebook */
    .gradebook-stats {
        grid-template-columns: 1fr;
    }

    /* Support button: icon only */
    .support-btn-label {
        display: none;
    }

    .support-btn {
        padding: 6px;
    }

    /* Game quiz */
    .quiz-select-grid {
        grid-template-columns: 1fr;
        max-width: 280px;
    }

    /* Practice quiz responsive */
    .quiz-topic-checkboxes {
        grid-template-columns: 1fr;
    }

    .quiz-nav-btn {
        width: 30px;
        height: 30px;
        font-size: 0.6875rem;
    }

    .topic-breakdown-label {
        min-width: 80px;
        font-size: 0.6875rem;
    }

    .review-item {
        padding: 1rem;
    }

    /* Flashcard responsive */
    .flashcard {
        height: 260px;
    }

    .flashcard-front, .flashcard-back {
        padding: 1.5rem;
    }

    .fc-bottom-bar {
        gap: 0.5rem;
    }

    .fc-rate-btn {
        padding: 0.35rem 0.625rem;
        font-size: 0.6875rem;
    }

    .fc-keyboard-hint {
        display: none;
    }

    .fc-swipe-hint {
        display: flex;
    }

    .fc-nav-btn {
        width: 36px;
        height: 36px;
    }

    /* Glossary responsive */
    .gl-alpha-jump {
        gap: 3px;
    }

    .gl-alpha-btn {
        width: 26px;
        height: 26px;
        font-size: 0.625rem;
    }

    /* Quiz pills responsive */
    .quiz-diff-pills {
        flex-wrap: wrap;
    }
}

/* ============================================
   iPad-specific (768px - 1024px, touch)
   ============================================ */

@media (min-width: 768px) and (max-width: 1024px) and (hover: none) {
    /* iPad optimizations */
    .workspace-content {
        grid-template-columns: 1fr 260px;
    }

    .test-panel {
        width: auto;
    }

    .canvas-toolbar .tool-btn {
        min-width: 40px;
        min-height: 40px;
    }

    .cfg-grammar-panel {
        width: 300px;
    }

    /* Bigger touch targets */
    .nav-btn {
        padding: 0.625rem 0.875rem;
        font-size: 0.8125rem;
    }

    .topic-item {
        padding: 0.75rem;
    }

    .menu-btn, .quiz-option-btn {
        padding: 14px;
        font-size: 15px;
    }

    /* AI widget */
    .ai-widget-panel {
        width: 380px;
        max-height: 60vh;
    }
}

/* ============================================
   Touch-Friendly Interactions
   ============================================ */

@media (hover: none) and (pointer: coarse) {
    /* Minimum touch target sizes (Apple HIG: 44px) */
    button,
    .nav-btn,
    .topic-item,
    .tool-btn,
    .menu-btn,
    .quiz-option-btn,
    .quiz-select-card,
    a.support-btn {
        min-height: 44px;
    }

    .dropdown-item {
        padding: 0.875rem 1rem;
    }

    /* Disable hover effects on touch */
    .menu-btn:hover,
    .tool-btn:hover,
    .quiz-select-card:hover {
        background: inherit;
    }

    /* Active state instead of hover for touch */
    .menu-btn:active {
        background: var(--accent-blue);
        color: #000;
    }

    .quiz-select-card:active {
        border-color: var(--accent-blue);
    }

    /* Larger inputs for touch */
    input[type="text"],
    input[type="password"],
    input[type="email"],
    textarea,
    select {
        font-size: 16px; /* Prevents iOS zoom on focus */
        min-height: 44px;
    }

    .quiz-text-input {
        font-size: 16px;
    }

    .quiz-input {
        font-size: 16px;
    }
}

/* ============================================
   Landscape Mobile
   ============================================ */

@media (max-width: 768px) and (orientation: landscape) {
    .workspace-content {
        grid-template-rows: 1fr;
    }

    .cfg-grammar-panel {
        max-height: 160px;
    }

    .cfg-guide {
        max-height: 140px;
    }

    .modal-content {
        max-height: 85vh;
        overflow-y: auto;
    }

    /* Game: ensure canvas fills */
    #game-container {
        height: 100vh;
    }
}

/* ============================================
   Desktop scrollbar
   ============================================ */

@media (min-width: 769px) {
    ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
    }

    ::-webkit-scrollbar-track {
        background: var(--bg-secondary);
    }

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

    ::-webkit-scrollbar-thumb:hover {
        background: var(--text-tertiary);
    }
}

/* ============================================
   Print
   ============================================ */

@media print {
    .app-header,
    .sidebar,
    .canvas-toolbar,
    .modal,
    .toast,
    .mobile-nav-toggle,
    .ai-widget-panel,
    .ai-widget-toggle,
    .support-btn {
        display: none !important;
    }

    body {
        background: white;
        color: black;
    }
}

/* ============================================
   Reduced Motion
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ============================================
   Focus Visible (Keyboard Navigation)
   ============================================ */

@supports selector(:focus-visible) {
    *:focus { outline: none; }
    *:focus-visible {
        outline: 2px solid var(--accent-blue, #58a6ff);
        outline-offset: 2px;
    }
}
