/* ==========================================================================
   SQL MASTER — MOBILE RESPONSIVENESS SUITE
   Comprehensive mobile and tablet responsiveness overrides.
   Preserves desktop layout completely while delivering an intuitive,
   touch-friendly experience on screens from 320px to 991px.
   ========================================================================== */

/* ==========================================================================
   1. GLOBAL OVERFLOW & BASE LAYOUT (customer_base_user)
   ========================================================================== */

@media (max-width: 991px) {
    html, body {
        overflow-x: hidden !important;
        width: 100%;
        max-width: 100vw;
    }

    .app-layout {
        overflow-x: hidden !important;
        width: 100%;
        max-width: 100vw;
    }

    .main-wrapper {
        overflow-x: hidden !important;
        width: 100%;
        min-width: 0;
        max-width: 100vw;
    }

    .content-container {
        width: 100%;
        max-width: 100vw;
        box-sizing: border-box;
    }

    /* Enhanced contrast for open sidebar drawer on mobile */
    .sidebar.show {
        box-shadow: 0 0 50px rgba(0, 0, 0, 0.85) !important;
    }
}

/* Tablets (768px - 991px) */
@media (max-width: 991px) and (min-width: 768px) {
    .content-container {
        padding: 1.25rem 1.25rem !important;
    }

    .main-header {
        padding: 0 1.25rem !important;
    }

    .header-greeting h2 {
        font-size: 1.15rem;
    }

    .header-greeting p {
        font-size: 0.76rem;
    }
}

/* Mobile & Phablets (max-width: 767px) */
@media (max-width: 767px) {
    /* Header layout */
    .main-header {
        padding: 0 0.85rem !important;
        gap: 0.5rem;
        height: 60px !important;
        min-height: 60px !important;
    }

    .header-title-section {
        min-width: 0;
        flex: 1 1 auto;
        overflow: hidden;
        gap: 0.5rem;
    }

    .header-greeting {
        min-width: 0;
        flex: 1 1 auto;
        overflow: hidden;
    }

    /* Fix the "Welcome Back" heading so it never clips or wraps awkwardly */
    .header-greeting h2 {
        font-size: 0.98rem;
        font-weight: 700;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;
        margin: 0;
        line-height: 1.2;
    }

    .header-greeting p {
        display: none !important;
    }

    .header-actions {
        gap: 0.5rem;
        flex-shrink: 0;
    }

    .icon-button {
        width: 36px;
        height: 36px;
        border-radius: 8px;
    }

    .profile-avatar.header-avatar {
        width: 34px;
        height: 34px;
        font-size: 0.82rem;
    }

    /* Notification dropdown safety */
    .dropdown-menu.notification-menu {
        width: min(290px, calc(100vw - 24px)) !important;
        max-width: calc(100vw - 24px) !important;
        right: -30px !important;
    }

    .dropdown-menu.profile-menu {
        right: 0 !important;
    }

    /* Content container mobile padding */
    .content-container {
        padding: 1rem 0.85rem 1.5rem !important;
    }

    /* Footer: stack cleanly on mobile */
    .footer.customer-footer {
        flex-direction: column;
        gap: 8px;
        text-align: center;
        padding: 12px 14px !important;
        font-size: 0.72rem;
    }
}

/* Small mobile devices (320px - 430px) */
@media (max-width: 480px) {
    .main-header {
        padding: 0 0.65rem !important;
        gap: 0.35rem;
    }

    .header-title-section {
        gap: 0.4rem;
    }

    .header-actions {
        gap: 0.35rem;
    }

    .header-greeting h2 {
        font-size: 0.90rem;
    }

    .sidebar-toggler {
        padding: 0.35rem 0.5rem !important;
        font-size: 1.1rem !important;
    }

    .content-container {
        padding: 0.85rem 0.65rem 1.25rem !important;
    }
}


/* ==========================================================================
   2. DASHBOARD (customer_dashboard)
   ========================================================================== */

@media (max-width: 991px) and (min-width: 521px) {
    .feature-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 1rem;
    }
}

@media (max-width: 520px) {
    .feature-grid {
        grid-template-columns: 1fr !important;
        gap: 0.85rem;
    }

    .feature-card {
        padding: 1.25rem 1rem;
        width: 100%;
        box-sizing: border-box;
    }

    .feature-icon {
        width: 44px;
        height: 44px;
        font-size: 1.15rem;
        border-radius: 12px;
    }

    .feature-title {
        font-size: 0.90rem;
    }

    .feature-desc {
        font-size: 0.75rem;
    }

    /* Profile hero card */
    .profile-hero {
        flex-direction: column;
        text-align: center;
        gap: 1.2rem;
        padding: 1.25rem 1rem;
    }

    .hero-left {
        flex-direction: column;
        gap: 0.75rem;
    }

    .hero-avatar {
        width: 60px;
        height: 60px;
        font-size: 1.4rem;
    }

    .hero-username {
        font-size: 1.15rem;
    }

    .hero-actions {
        width: 100%;
        justify-content: center;
        gap: 0.5rem;
    }

    .hero-btn {
        flex: 1 1 auto;
        justify-content: center;
        padding: 0.5rem 0.85rem;
        font-size: 0.78rem;
    }
}


/* ==========================================================================
   3. SQL NOTES SELECTION (notes_selection)
   ========================================================================== */

@media (max-width: 767px) {
    .notes-page-header {
        margin-bottom: 20px;
    }

    .notes-page-header h1 {
        font-size: 1.6rem !important;
        gap: 8px;
        line-height: 1.25;
    }

    .notes-page-header h1 i {
        font-size: 1.4rem;
    }

    .notes-page-header p {
        font-size: 0.84rem;
        line-height: 1.45;
        max-width: 100%;
    }

    /* Level cards: stack vertically */
    .notes-level-card {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
        padding: 18px 16px !important;
    }

    .nlc-icon {
        width: 48px;
        height: 48px;
        font-size: 1.3rem;
    }

    .nlc-title {
        font-size: 1.15rem;
    }

    .nlc-desc {
        font-size: 0.78rem;
        line-height: 1.45;
    }

    .nlc-tags {
        gap: 5px;
    }

    .nlc-tag {
        font-size: 0.66rem;
        padding: 2px 7px;
    }

    .nlc-action {
        grid-column: 1 !important;
        margin-top: 8px;
    }

    .nlc-btn {
        width: 100% !important;
        height: 44px;
        font-size: 0.85rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    body:has(.notes-page) .content-container {
        padding: 12px 12px 20px !important;
    }
}

@media (max-width: 430px) {
    .notes-page-header h1 {
        font-size: 1.4rem !important;
    }

    .notes-page-header p {
        font-size: 0.78rem;
    }

    .notes-level-card {
        padding: 14px 12px !important;
    }
}


/* ==========================================================================
   4. BEGINNER NOTES 3D FLIP BOOK (beginner_note_book)
   ========================================================================== */

@media (max-width: 768px) {
    /* Book wrapper */
    .beginner-book-wrapper {
        padding: 2px 4px 8px !important;
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: hidden !important;
        box-sizing: border-box;
    }

    /* Compact Mobile Toolbar: 2 neat rows */
    .book-toolbar {
        display: flex !important;
        flex-wrap: wrap !important;
        height: auto !important;
        min-height: unset !important;
        padding: 6px 8px !important;
        gap: 6px !important;
        justify-content: space-between;
    }

    /* Row 1 left: Back button + Chapter selector */
    .book-toolbar-left {
        order: 1;
        gap: 6px;
        flex: 1 1 auto;
    }

    /* Row 1 right: Fullscreen button */
    .book-toolbar-right {
        order: 2;
        flex: 0 0 auto;
    }

    /* Row 2: Prev, Current Chapter Pill, Next (centered) */
    .book-toolbar-center {
        order: 3;
        width: 100%;
        justify-content: center;
        gap: 8px;
        padding-top: 2px;
    }

    .chapter-selector-wrap {
        max-width: 160px;
    }

    .chapter-select {
        max-width: 110px !important;
        font-size: 0.72rem !important;
    }

    .current-chapter-pill {
        max-width: 140px !important;
        font-size: 0.72rem !important;
        padding: 2px 8px;
    }

    .btn-flip-nav {
        padding: 0 10px;
        font-size: 0.72rem;
        height: 32px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Book stage: responsive height without overflow */
    .book-stage {
        min-height: 420px !important;
        margin: 2px auto 6px !important;
        max-width: 100% !important;
        overflow: hidden;
    }

    /* SQL Block in book pages: allow touch scrolling */
    .page-sql-block .sql-pre {
        padding: 8px 10px !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        white-space: pre-wrap !important;
        word-break: break-word !important;
    }

    .page-sql-block .sql-code {
        font-size: 0.72rem !important;
    }
}

@media (max-width: 480px) {
    .book-toolbar {
        padding: 4px 6px !important;
    }

    .chapter-selector-wrap {
        max-width: 130px;
        padding: 0 6px 0 4px;
    }

    .chapter-select {
        max-width: 90px !important;
        font-size: 0.68rem !important;
    }

    .current-chapter-pill {
        max-width: 100px !important;
        font-size: 0.68rem !important;
    }

    .btn-book-action.btn-icon-only {
        width: 30px;
        height: 30px;
        font-size: 0.88rem;
    }

    /* Page interior typography adjustments */
    .page-inner,
    .toc-inner,
    .chapter-divider-inner {
        padding: 10px 10px !important;
    }

    .cover-inner,
    .back-cover-inner {
        padding: 14px 12px !important;
    }

    .page-title {
        font-size: 0.95rem !important;
        line-height: 1.3 !important;
    }

    .page-text-body {
        font-size: 0.74rem !important;
        line-height: 1.55 !important;
    }

    .note-callout {
        padding: 8px 10px !important;
        font-size: 0.74rem !important;
    }

    .cover-title {
        font-size: 1.2rem !important;
    }

    .cover-subtitle {
        font-size: 0.78rem;
    }

    .cover-desc {
        font-size: 0.70rem;
    }

    .back-cover-title {
        font-size: 1.15rem !important;
    }

    .back-cover-desc {
        font-size: 0.70rem;
    }

    .toc-item {
        padding: 6px 8px;
        min-height: 36px;
    }

    .toc-item-title {
        font-size: 0.72rem;
    }

    .toc-item-desc {
        display: none;
    }

    .toc-heading {
        font-size: 0.92rem;
    }

    .ch-main-title {
        font-size: 1.15rem !important;
    }

    .ch-description {
        font-size: 0.74rem;
    }

    .page-top-bar {
        gap: 6px;
    }

    .page-topic-title {
        font-size: 0.66rem;
    }

    .page-order-num {
        font-size: 0.62rem;
        padding: 1px 5px;
    }

    /* Page bottom bar turn triggers */
    .page-turn-btn {
        padding: 3px 8px;
        font-size: 0.70rem;
        min-height: 32px;
    }

    .page-bottom-meta {
        gap: 4px;
    }

    .page-watermark {
        display: none;
    }

    /* Book stage on compact phones */
    .book-stage {
        min-height: 360px !important;
    }

    /* Fullscreen view on mobile */
    .book-fullscreen-active {
        padding: 4px 6px !important;
    }

    .book-fullscreen-active .book-stage {
        height: calc(100vh - 64px) !important;
    }
}

/* Touch targets for page-turn actions */
@media (hover: none) and (pointer: coarse) {
    .page-turn-btn {
        min-height: 34px;
        min-width: 44px;
    }

    .page-corner-hotspot {
        width: 44px;
        height: 44px;
    }

    .btn-flip-nav {
        min-height: 34px;
        min-width: 42px;
    }

    .toc-item {
        min-height: 40px;
    }
}


/* ==========================================================================
   5. PRACTICE SELECTION (practice-selection)
   ========================================================================== */

@media (max-width: 900px) and (min-width: 601px) {
    .practice-selection__intro h1 {
        font-size: 2.1rem !important;
    }

    .practice-selection__grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 16px;
    }

    .practice-selection__benefits {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

@media (max-width: 600px) {
    .practice-selection__intro h1 {
        font-size: 1.65rem !important;
        line-height: 1.25;
    }

    .practice-selection__subtitle {
        font-size: 0.82rem;
        line-height: 1.45;
    }

    .practice-selection__eyebrow {
        font-size: 0.62rem;
        padding: 3px 8px;
    }

    .practice-selection__highlights {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .highlight-item {
        font-size: 0.72rem;
        padding: 4px 8px;
        gap: 5px;
    }

    .practice-selection__header {
        min-height: auto;
        margin-bottom: 16px;
    }

    /* Stack cards in 1 column */
    .practice-selection__grid {
        grid-template-columns: 1fr !important;
        gap: 14px;
    }

    .practice-selection__card {
        padding: 18px 16px 16px;
        box-sizing: border-box;
    }

    .practice-selection__card h2 {
        font-size: 1.15rem;
    }

    .practice-selection__card p {
        font-size: 0.76rem;
        min-height: auto;
    }

    .practice-selection__checklist {
        gap: 7px;
    }

    .practice-selection__checklist li {
        font-size: 0.76rem;
        line-height: 1.35;
    }

    .practice-selection__button {
        height: 42px;
        font-size: 0.84rem;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Benefits bar */
    .practice-selection__benefits {
        grid-template-columns: 1fr !important;
        gap: 10px;
        padding: 12px 14px;
    }

    .benefit-text b {
        font-size: 0.80rem;
    }

    .benefit-text small {
        font-size: 0.68rem;
    }

    body:has(.practice-selection) .content-container {
        padding: 12px 12px 20px !important;
    }
}

@media (max-width: 430px) {
    .practice-selection__intro h1 {
        font-size: 1.45rem !important;
    }

    .practice-selection__subtitle {
        font-size: 0.78rem;
    }
}


/* ==========================================================================
   6. SQL PRACTICE CONSOLE (practice_console)
   The complete 4-panel SQL Workspace.
   Logical mobile ordering:
     1. Question Details (#panelQuestion)
     2. SQL Editor (#panelEditor)
     3. Query Results (#panelResults)
     4. Table Schema (#panelSchema)
   ========================================================================== */

/* ---- Tablet (768px - 991px) ---- */
@media (max-width: 991px) {
    .sql-practice-console {
        height: auto !important;
        min-height: calc(100vh - 70px - 60px);
        padding: 8px 10px 14px !important;
        margin: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Vertical stack workspace using display: contents to reorder panels logically */
    .practice-workspace {
        display: flex !important;
        flex-direction: column !important;
        overflow: visible !important;
        gap: 10px !important;
    }

    .workspace-left,
    .workspace-right {
        display: contents !important;
    }

    .workspace-col {
        width: 100% !important;
        flex: none !important;
        min-width: unset !important;
    }

    /* Hide desktop draggable resize handles */
    .resize-handle.resize-col,
    .resize-handle.resize-row {
        display: none !important;
    }

    /* Reorder panels: Question -> Editor -> Results -> Schema */
    .question-panel {
        order: 1 !important;
        width: 100% !important;
        flex: none !important;
        height: auto !important;
        min-height: 140px;
        max-height: 280px;
    }

    .sql-editor-panel {
        order: 2 !important;
        width: 100% !important;
        flex: none !important;
        height: 270px !important;
        min-height: 200px;
    }

    .results-panel {
        order: 3 !important;
        width: 100% !important;
        flex: none !important;
        height: 260px !important;
        min-height: 200px;
    }

    .schema-panel {
        order: 4 !important;
        width: 100% !important;
        flex: none !important;
        height: 220px !important;
        min-height: 160px;
    }

    /* Console topbar: flexible wrapping */
    .console-topbar {
        height: auto !important;
        min-height: auto !important;
        flex-wrap: wrap;
        gap: 8px;
        padding: 6px 4px;
        margin-bottom: 8px;
    }

    .topbar-left {
        flex-wrap: wrap;
        gap: 8px;
    }

    .topbar-right {
        gap: 6px;
    }
}

/* ---- Mobile (max-width: 767px) ---- */
@media (max-width: 767px) {
    .sql-practice-console {
        padding: 6px 4px 12px !important;
        margin: 0 !important;
        width: 100% !important;
    }

    /* Topbar: clean stacked layout */
    .console-topbar {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
        padding: 6px 4px;
        margin-bottom: 8px;
    }

    .topbar-left {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 6px;
    }

    .back-link {
        font-size: 0.74rem;
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }

    .back-link span {
        display: inline-block;
    }

    .topbar-divider {
        display: none;
    }

    .badge-level {
        font-size: 0.68rem;
        padding: 2px 7px;
    }

    .console-timer {
        font-size: 0.74rem;
        padding: 3px 8px;
        gap: 4px;
    }

    .console-timer .timer-label {
        display: none;
    }

    .topbar-right {
        width: 100%;
    }

    /* Console utilities: 3 equal, clearly labeled buttons */
    .console-utilities {
        display: flex;
        align-items: center;
        gap: 6px;
        width: 100%;
    }

    .console-theme-dropdown {
        flex: 1 1 0;
    }

    .btn-console-tool {
        width: 100%;
        flex: 1 1 0;
        justify-content: center;
        font-size: 0.70rem;
        padding: 6px 6px;
        white-space: nowrap;
        gap: 4px;
        min-height: 34px;
    }

    .btn-console-tool span {
        display: inline-block;
    }

    .theme-menu {
        min-width: 160px;
        right: 0 !important;
        left: auto !important;
        z-index: 1060;
    }

    /* Panel Headers */
    .panel-header {
        height: auto !important;
        min-height: 38px;
        padding: 5px 10px;
    }

    .panel-title h2 {
        font-size: 0.80rem;
    }

    .panel-icon-cyan {
        font-size: 0.88rem;
    }

    /* 1. Question Panel */
    .question-panel {
        order: 1 !important;
        height: auto !important;
        min-height: 120px;
        max-height: 260px;
    }

    .question-body {
        padding: 10px 12px !important;
        -webkit-overflow-scrolling: touch;
    }

    .question-note-title {
        font-size: 0.95rem !important;
        line-height: 1.35 !important;
    }

    .question-statement-body p {
        font-size: 0.78rem !important;
        line-height: 1.5 !important;
    }

    /* 2. SQL Editor Panel */
    .sql-editor-panel {
        order: 2 !important;
        height: 240px !important;
        min-height: 180px;
    }

    .editor-footer {
        height: 42px;
        min-height: 42px;
        padding: 0 8px;
    }

    .editor-actions-left {
        gap: 6px;
    }

    .btn-console {
        font-size: 0.74rem;
        padding: 5px 10px;
        gap: 4px;
        min-height: 34px;
    }

    .btn-console-primary {
        font-weight: 600;
    }

    .shortcut-kbd {
        display: none !important;
    }

    .editor-status {
        font-size: 0.68rem;
    }

    /* 3. Query Results Panel */
    .results-panel {
        order: 3 !important;
        height: 250px !important;
        min-height: 180px;
    }

    .results-panel .panel-header {
        height: auto !important;
        min-height: 38px;
        padding: 5px 8px !important;
        flex-wrap: wrap;
        gap: 4px;
    }

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

    .results-meta {
        gap: 3px !important;
        flex-wrap: wrap;
    }

    .meta-tag,
    .meta-count,
    .meta-time {
        font-size: 0.66rem !important;
        padding: 2px 5px !important;
    }

    .btn-panel-action {
        font-size: 0.70rem !important;
        padding: 4px 7px !important;
        min-height: 30px;
        gap: 4px;
    }

    .btn-panel-action span {
        display: none;
    }

    .table-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }

    .console-table {
        font-size: 0.72rem;
    }

    .console-table th,
    .console-table td {
        padding: 6px 8px !important;
        white-space: nowrap;
    }

    /* 4. Table Schema Panel */
    .schema-panel {
        order: 4 !important;
        height: 200px !important;
        min-height: 150px;
    }

    .schema-table-select {
        font-size: 0.70rem;
        max-width: 140px;
        height: 28px;
    }

    /* Test Result Evaluation Card */
    .test-result-card {
        padding: 12px !important;
    }

    .test-result-header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .test-result-title {
        font-size: 0.90rem !important;
    }

    .test-result-subtitle {
        font-size: 0.70rem !important;
    }

    .test-metrics-row {
        flex-wrap: wrap !important;
        gap: 6px !important;
    }

    .test-metric-pill {
        flex: 1 1 auto !important;
        min-width: 75px !important;
        padding: 6px 8px !important;
    }

    .test-metric-pill .metric-label {
        font-size: 0.62rem !important;
    }

    .test-metric-pill .metric-value {
        font-size: 0.74rem !important;
    }

    .test-actions-row {
        flex-wrap: wrap;
        gap: 6px !important;
    }

    .test-actions-row .btn-console {
        flex: 1 1 auto;
        justify-content: center;
        min-height: 38px;
    }

    /* Leaderboard container */
    .coc-rank-card {
        padding: 12px !important;
    }

    .coc-rank-header {
        flex-wrap: wrap;
        gap: 6px;
    }

    .coc-rank-title {
        font-size: 0.95rem !important;
    }

    .coc-podium-section {
        transform: scale(0.85);
        transform-origin: top center;
        margin-bottom: -15px;
    }

    .coc-avatar-frame {
        width: 44px !important;
        height: 44px !important;
    }
}

/* ---- Small mobile (320px - 430px) ---- */
@media (max-width: 430px) {
    .sql-practice-console {
        padding: 4px 2px 10px !important;
    }

    /* Editor gutter and code sync */
    .editor-gutter {
        width: 30px !important;
        min-width: 30px !important;
        font-size: 0.70rem !important;
        padding: 8px 4px 8px 0 !important;
    }

    .editor-code-container {
        width: calc(100% - 30px) !important;
    }

    .sql-code-area,
    .editor-highlight {
        font-size: 0.76rem !important;
        padding: 8px 10px !important;
    }

    .sql-editor-panel {
        height: 215px !important;
    }

    .results-panel {
        height: 220px !important;
    }

    .question-panel {
        max-height: 240px !important;
    }

    .schema-panel {
        height: 180px !important;
    }

    .schema-table-select {
        max-width: 120px;
    }

    .btn-console-tool {
        font-size: 0.66rem;
        padding: 5px 4px;
    }

    .btn-console-tool .tool-chevron {
        display: none;
    }

    /* Podium scale down on 320px */
    .coc-podium-section {
        transform: scale(0.78);
        margin-bottom: -25px;
    }
}


/* ==========================================================================
   7. PROGRESS, RANKINGS & PROFILE REFINEMENTS
   ========================================================================== */

@media (max-width: 576px) {
    .unlock-container {
        padding: 1.5rem 1rem !important;
        min-height: 45vh;
    }

    .unlock-icon {
        width: 72px;
        height: 72px;
        font-size: 1.8rem;
        margin-bottom: 1rem;
    }

    .unlock-title {
        font-size: 1.3rem;
    }

    .unlock-desc {
        font-size: 0.82rem;
        margin-bottom: 1.25rem;
    }

    .unlock-btn {
        width: 100%;
        max-width: 260px;
        justify-content: center;
        padding: 0.65rem 1.25rem;
    }
}


/* ==========================================================================
   8. PANEL MAXIMIZE — MOBILE FULLSCREEN OVERRIDE
   ========================================================================== */

@media (max-width: 767px) {
    .console-panel.panel--maximized {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 9999 !important;
        border-radius: 0 !important;
        margin: 0 !important;
    }
}
