/* ============================================================
   MOBILE OPTIMIZATION PASS — see plans/mobile_optimization_plan.md
   Steps 1–7 + Board-Setup / Feature-Hub pass (2026).
   Loaded after style.css so these rules win on equal specificity.
   !important is only used to override inline styles on elements.
   ============================================================ */

:root {
    --mobile-top-offset: 56px;
    --bottom-chrome: 88px;
}

/* ---------- Step 3: Tablet (768–1024px) breakpoint ---------- */
@media (max-width: 1024px) {
    .dashboard-main-content {
        padding: 24px 20px 60px 20px;
    }

    .card-grid,
    .input-grid {
        gap: 12px;
    }

    /* Perma overview cards: tighten min width so two fit on iPad portrait */
    .perma-overview-grid {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: 12px;
    }

    .section-page-header h2 {
        font-size: 1.5rem;
    }
}

/* ---------- Step 1: Override inline-style overflow traps ---------- */
@media (max-width: 600px) {

    .settings-card [style*="max-width: 150px"],
    .settings-card [style*="max-width: 400px"],
    .settings-card [style*="max-width: 500px"],
    .settings-card [style*="max-width: 220px"] {
        max-width: 100% !important;
        width: 100% !important;
    }

    /* Security redirect hero card */
    .security-redirect-card [style*="max-width: 500px"] {
        max-width: 100% !important;
    }

    .security-redirect-card {
        padding: 30px 18px !important;
    }

    .security-redirect-card .security-icon-wrapper {
        width: 64px !important;
        height: 64px !important;
        font-size: 2rem !important;
    }

    /* Roleplay + Self-Roles flex rows collapse to stack */
    .settings-section[data-category="roleplay"] [style*="flex: 2"],
    .settings-section[data-category="roleplay"] [style*="flex: 3"],
    .settings-section[data-category="extras"] [style*="flex: 2"],
    .settings-section[data-category="extras"] [style*="flex: 3"],
    .settings-card [style*="min-width: 160px"],
    .settings-card [style*="min-width: 200px"] {
        flex: 1 1 100% !important;
        min-width: 0 !important;
        width: 100% !important;
    }

    /* Long inline help paragraphs that use nowrap should wrap */
    .settings-card [style*="white-space: nowrap"] {
        white-space: normal !important;
        word-break: break-word;
    }

    /* Long IDs / webhook URLs in JS-injected list items must not force overflow */
    .team-role-item small {
        white-space: normal !important;
        word-break: break-all;
    }

    /* Perma role pill names: narrower so multi-role rows don't overflow */
    .perma-role-pill .role-name {
        max-width: 140px;
    }

    /* Guild / filter selects with inline min-width traps */
    [style*="min-width: 220px"],
    [style*="min-width: 250px"] {
        min-width: 0 !important;
        max-width: 100% !important;
    }
}

/* ---------- Step 2: Server-select topbar on small screens ---------- */
@media (max-width: 600px) {
    .server-view-topbar {
        flex-wrap: wrap;
        gap: 12px;
        padding: 14px 16px;
    }

    .server-view-topbar h2 {
        font-size: 1.1rem;
        gap: 10px;
        flex: 1 1 100%;
        flex-wrap: wrap;
    }

    .server-view-topbar h2>button,
    .server-view-topbar .btn-refresh {
        width: 100%;
        min-height: 44px;
        justify-content: center;
    }

    .server-view-topbar .topbar-user {
        flex: 1 1 100%;
        align-items: flex-start;
        width: 100%;
    }

    .server-view-main .server-grid-wrapper {
        padding: 16px;
    }
}

/* ---------- Step 4: J2C permission rows stack on narrow phones ---------- */
@media (max-width: 600px) {
    .permission-row {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .permission-info {
        max-width: 100%;
    }

    .three-state-toggle {
        align-self: flex-end;
    }
}

/* ---------- Step 5: Touch ergonomics & interaction polish ---------- */
@media (max-width: 768px) {

    body {
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    /* Give section header room to clear the fixed hamburger (top + safe-area + 44) */
    .dashboard-main-content {
        padding-top: max(64px, calc(12px + env(safe-area-inset-top, 0px) + 44px));
        padding-bottom: calc(var(--bottom-chrome) + env(safe-area-inset-bottom, 0px));
    }

    .mobile-menu-btn {
        top: max(12px, env(safe-area-inset-top, 0px));
        left: max(12px, env(safe-area-inset-left, 0px));
    }

    /* Slightly larger, well-separated remove buttons */
    .role-controls .btn-danger,
    .role-controls button {
        min-height: 44px;
        min-width: 44px;
        padding: 8px 12px;
    }

    .state-btn {
        min-height: 44px;
        min-width: 44px;
        padding: 8px 14px;
    }

    /* Drag grips are desktop-only; hide on touch to avoid dead affordance */
    .drag-grip,
    .sr-drag-grip {
        display: none !important;
    }

    /* Keep the save footer self-contained */
    .action-footer .status-msg {
        margin-top: 0;
        margin-bottom: 8px;
    }

    .action-footer {
        max-height: 50vh;
        overflow-y: auto;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    }

    /* Team role items — sleek single-row mobile layout */
    .team-role-item {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        justify-content: space-between !important;
        gap: 10px !important;
        padding: 12px 14px !important;
        background: rgba(255, 255, 255, 0.02) !important;
        border: 1px solid rgba(255, 255, 255, 0.06) !important;
        border-radius: 10px !important;
        margin-bottom: 8px !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15) !important;
    }

    .team-role-item > div:first-child {
        flex: 1 !important;
        min-width: 0 !important;
        width: auto !important;
        display: flex !important;
        align-items: center !important;
        gap: 10px !important;
    }

    .team-role-item > div:first-child > div {
        min-width: 0 !important;
        flex: 1 !important;
    }

    .team-role-item span {
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        font-size: 0.95rem !important;
        font-weight: 600 !important;
    }

    .team-role-item small {
        font-size: 0.75rem !important;
        color: #80848e !important;
    }

    .team-role-item .role-controls {
        display: flex !important;
        align-items: center !important;
        width: auto !important;
        min-width: 0 !important;
        flex-shrink: 0 !important;
        margin-left: 8px !important;
        justify-content: flex-end !important;
    }

    .team-role-item .role-controls .btn-danger,
    .team-role-item .btn-danger {
        min-height: 36px !important;
        min-width: 36px !important;
        padding: 6px 12px !important;
        border-radius: 8px !important;
        font-size: 0.85rem !important;
        background: rgba(235, 69, 95, 0.12) !important;
        border: 1px solid rgba(235, 69, 95, 0.3) !important;
        color: #eb455f !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        transition: all 0.2s ease !important;
    }

    .team-role-item .role-controls .btn-danger:active,
    .team-role-item .btn-danger:active {
        background: rgba(235, 69, 95, 0.25) !important;
        transform: scale(0.96) !important;
    }
}

/* ---------- Step 6: Performance for low-end mobile ---------- */
@media (max-width: 768px) and (prefers-reduced-motion: no-preference) {

    .settings-card,
    .switch-wrapper,
    .collapsible-header,
    .music-glass-container,
    .confirm-modal-card {
        backdrop-filter: none !important;
        -webkit-backdrop-filter: none !important;
        background: rgba(20, 20, 28, 0.92);
    }

    .particle-background {
        display: none;
    }
}

/* ---------- Step 7 (minor): unify small-phone band ---------- */
@media (max-width: 480px) {
    .section-page-header h2 {
        font-size: 1.2rem;
        gap: 8px;
    }

    .section-page-header h2 i {
        font-size: 1.1rem;
    }

    .btn-primary {
        padding: 12px 16px;
        font-size: 0.9rem;
    }

    .custom-song-actions button,
    .role-controls button {
        min-height: 44px;
    }

    .perma-overview-grid {
        grid-template-columns: 1fr !important;
        gap: 10px;
    }

    .perma-role-pill .role-remove-btn {
        min-width: 36px;
        min-height: 36px;
        width: 36px;
        height: 36px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

/* ============================================================
   BOARD SETUP: Self-Roles role editor (P0)
   Desktop rows are a single horizontal flex with fixed widths.
   On phones they must wrap into a usable stacked form.
   ============================================================ */
@media (max-width: 768px) {

    .sr-role-edit-item,
    .selfrole-role-item {
        display: flex !important;
        flex-wrap: wrap !important;
        align-items: stretch !important;
        gap: 8px !important;
        max-height: none !important;
    }

    /* Animation previously capped height at 60px — breaks stacked rows */
    .sr-role-edit-item.animate-new-role {
        max-height: none !important;
        animation: none;
    }

    .sr-role-edit-item > .sr-role-picker,
    .selfrole-role-item > .sr-role-picker {
        flex: 1 1 100% !important;
        width: 100% !important;
        min-width: 0 !important;
    }

    .sr-role-edit-item > .input-modern,
    .selfrole-role-item > .input-modern {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        width: auto !important;
        max-width: 100% !important;
        min-height: 44px !important;
        font-size: 16px !important; /* iOS no-zoom */
        padding: 10px 12px !important;
    }

    /* Emoji field stays compact; label + description grow */
    .sr-role-edit-item > .input-modern[placeholder="⭐"],
    .selfrole-role-item > .input-modern[placeholder="⭐"] {
        flex: 0 0 72px !important;
        width: 72px !important;
        text-align: center !important;
    }

    .sr-role-edit-item > .input-modern[placeholder="Label"],
    .selfrole-role-item > .input-modern[placeholder="Label"] {
        flex: 1 1 calc(100% - 90px) !important;
        width: auto !important;
    }

    .sr-role-edit-item > .input-modern[placeholder="Kurze Beschreibung"],
    .selfrole-role-item > .input-modern[placeholder="Kurze Beschreibung"] {
        flex: 1 1 100% !important;
        width: 100% !important;
    }

    .sr-role-edit-item > .btn-secondary,
    .sr-role-edit-item > .btn-danger,
    .selfrole-role-item > .btn-secondary,
    .selfrole-role-item > .btn-danger {
        flex: 1 1 calc(50% - 8px) !important;
        min-height: 44px !important;
        min-width: 44px !important;
        padding: 10px 12px !important;
        justify-content: center;
    }

    .sr-edit-fields-grid {
        grid-template-columns: 1fr !important;
        gap: 10px;
        align-items: stretch;
    }

    .sr-edit-fields-grid > * {
        width: 100% !important;
        max-width: 100% !important;
    }

    .sr-new-group-box > div {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .sr-new-group-box [style*="flex: 0 0 60px"],
    .sr-new-group-box [style*="flex: 2"],
    .sr-new-group-box [style*="flex: 3"] {
        flex: 1 1 100% !important;
        min-width: 0 !important;
        width: 100% !important;
    }

    .sr-new-group-box .btn-primary {
        width: 100%;
        min-height: 44px;
        height: auto !important;
    }

    .sr-edit-btn,
    .sr-delete-btn,
    .sr-card-actions button {
        min-height: 40px;
        min-width: 40px;
        padding: 8px 12px;
    }

    .sr-card-actions {
        flex-wrap: wrap;
        gap: 6px;
        justify-content: flex-end;
    }
}

/* ============================================================
   FLOATING SAVE BAR + dual chrome (P0)
   One bottom surface on phones: floating bar replaces footer
   when unsaved changes are present.
   ============================================================ */
@media (max-width: 768px) {

    .save-changes-bar {
        left: 0;
        right: 0;
        bottom: 0;
        transform: translateY(120%);
        width: 100%;
        max-width: 100%;
        border-radius: 16px 16px 0 0;
        padding: 14px 16px;
        padding-bottom: max(14px, env(safe-area-inset-bottom, 0px));
        box-sizing: border-box;
    }

    .save-changes-bar.visible {
        transform: translateY(0);
    }

    .save-changes-bar-content {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .save-changes-text {
        font-size: 0.88rem;
        justify-content: center;
        text-align: center;
        white-space: normal;
    }

    .save-changes-bar-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        width: 100%;
    }

    /* Fallback when actions wrapper still uses inline flex */
    .save-changes-bar-content > div {
        display: flex;
        flex-direction: column;
        gap: 8px;
        width: 100%;
    }

    .save-changes-bar .btn-secondary,
    .save-changes-bar .btn-save-large {
        width: 100% !important;
        min-height: 48px !important;
        justify-content: center !important;
        white-space: normal !important;
        text-align: center;
        padding: 12px 16px !important;
        font-size: 0.9rem !important;
        line-height: 1.3;
    }

    /* Hide fixed footer while floating bar is up — avoid double chrome */
    body:has(.save-changes-bar.visible) .action-footer {
        opacity: 0;
        pointer-events: none;
        transform: translateY(100%);
    }
}

/* ============================================================
   MOBILE FEATURE HUB — function overview for board setup
   ============================================================ */
.mobile-feature-hub,
.mobile-quick-nav {
    display: none;
}

@media (max-width: 768px) {
    .mobile-feature-hub {
        display: none !important;
    }

    .mobile-feature-hub-header {
        margin-bottom: 14px;
    }

    .mobile-feature-hub-header h2 {
        margin: 0 0 4px 0;
        font-size: 1.15rem;
        font-weight: 700;
        color: #fff;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .mobile-feature-hub-header h2 i {
        color: var(--accent-cyan, #00e5ff);
        font-size: 1rem;
    }

    .mobile-feature-hub-header p {
        margin: 0;
        font-size: 0.82rem;
        color: var(--text-muted, #949ba4);
        line-height: 1.4;
    }

    .mobile-feature-hub-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .mobile-feature-card {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        padding: 14px 12px;
        min-height: 88px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.07);
        background: rgba(255, 255, 255, 0.03);
        color: var(--text-main, #fff);
        text-decoration: none;
        cursor: pointer;
        transition: border-color 0.2s, background 0.2s, transform 0.15s;
        -webkit-tap-highlight-color: transparent;
        text-align: left;
        font-family: inherit;
        width: 100%;
        box-sizing: border-box;
    }

    .mobile-feature-card:active {
        transform: scale(0.98);
    }

    .mobile-feature-card:hover,
    .mobile-feature-card:focus-visible {
        border-color: rgba(0, 229, 255, 0.35);
        background: rgba(0, 229, 255, 0.06);
        outline: none;
    }

    .mobile-feature-card.active {
        border-color: rgba(0, 229, 255, 0.5);
        background: rgba(0, 229, 255, 0.1);
        box-shadow: 0 0 0 1px rgba(0, 229, 255, 0.15);
    }

    .mobile-feature-card .hub-icon {
        width: 36px;
        height: 36px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 229, 255, 0.1);
        color: var(--accent-cyan, #00e5ff);
        font-size: 0.95rem;
        flex-shrink: 0;
    }

    .mobile-feature-card.hub-external .hub-icon {
        background: rgba(157, 0, 255, 0.12);
        color: var(--accent-purple, #9d00ff);
    }

    .mobile-feature-card.hub-external.hub-security .hub-icon {
        background: rgba(235, 69, 95, 0.12);
        color: var(--accent-red, #eb455f);
    }

    .mobile-feature-card .hub-label {
        font-size: 0.84rem;
        font-weight: 650;
        line-height: 1.25;
        color: #fff;
    }

    .mobile-feature-card .hub-desc {
        font-size: 0.7rem;
        color: var(--text-muted, #949ba4);
        line-height: 1.3;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .mobile-feature-hub-external {
        margin-top: 12px;
        padding-top: 12px;
        border-top: 1px solid rgba(255, 255, 255, 0.06);
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .mobile-feature-hub-external .mobile-feature-card {
        flex-direction: row;
        align-items: center;
        min-height: 52px;
        padding: 10px 12px;
    }

    .mobile-feature-hub-external .hub-label {
        flex: 1;
    }

    .mobile-feature-hub-external .hub-desc {
        display: none;
    }

    .mobile-feature-hub-external .hub-chevron {
        color: var(--text-muted, #949ba4);
        font-size: 0.75rem;
    }

    /* Sticky quick-nav chips under hub (optional second row) */
    .mobile-quick-nav {
        display: flex;
        gap: 8px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding: 0 0 12px 0;
        margin: 0 0 4px 0;
        scrollbar-width: none;
    }

    .mobile-quick-nav::-webkit-scrollbar {
        display: none;
    }

    .mobile-quick-nav-chip {
        flex: 0 0 auto;
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 8px 14px;
        min-height: 40px;
        border-radius: 999px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(255, 255, 255, 0.04);
        color: var(--text-muted, #949ba4);
        font-size: 0.78rem;
        font-weight: 600;
        font-family: inherit;
        cursor: pointer;
        white-space: nowrap;
        transition: all 0.15s;
    }

    .mobile-quick-nav-chip.active {
        color: var(--accent-cyan, #00e5ff);
        border-color: rgba(0, 229, 255, 0.4);
        background: rgba(0, 229, 255, 0.1);
    }

    .mobile-quick-nav-chip i {
        font-size: 0.75rem;
    }
}

@media (max-width: 380px) {
    .mobile-feature-hub-grid {
        grid-template-columns: 1fr;
    }
}

/* ============================================================
   QUICK NAV — floating dock on phones
   Sits above the fixed save footer (--bottom-chrome) and fades
   out while the save-changes sheet covers the bottom.
   ============================================================ */
@media (max-width: 768px) {
    .quicknav {
        right: 14px;
        bottom: calc(var(--bottom-chrome, 88px) + 6px);
    }

    /* Chip row scrolls horizontally when it exceeds the viewport */
    .quicknav-chips {
        max-width: calc(100vw - 28px);
        overflow-x: auto;
        scrollbar-width: none;
    }

    .quicknav-chips::-webkit-scrollbar {
        display: none;
    }

    /* Larger touch targets on phones */
    .quicknav-chip {
        width: 44px;
        height: 44px;
        font-size: 1rem;
        flex-shrink: 0;
    }

    /* Tooltips are hover-only — useless on touch */
    .quicknav-chip::after {
        display: none;
    }

    /* Fade the dock out while the save-changes sheet is up */
    body:has(.save-changes-bar.visible) .quicknav {
        opacity: 0;
        transform: translateY(14px);
        visibility: hidden;
        pointer-events: none;
    }
}

/* ============================================================
   Ticket category / expanded settings polish
   ============================================================ */
@media (max-width: 600px) {
    .ticket-category-item {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .expanded-settings {
        padding: 12px !important;
    }

    .expanded-settings .input-grid,
    .expanded-settings .cat-input-grid {
        grid-template-columns: 1fr !important;
    }

    .ticket-form-inline {
        grid-template-columns: 1fr !important;
    }

    .ticket-form-q-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .ticket-form-q-actions {
        width: 100%;
        justify-content: flex-end;
    }

    .ticket-form-q-actions .btn-icon,
    .ticket-form-q-actions .btn-danger {
        min-height: 44px;
    }
}
