/* ── Mobile & Tablet Responsive Overrides ──────────────────────
   Loaded last — overrides desktop-first styles from other CSS files.
   Breakpoints: 768px (tablets), 600px (phones), 400px (small phones)
   ─────────────────────────────────────────────────────────────── */

/* ================================================================
   TABLET  (≤ 768px)
   ================================================================ */
@media (max-width: 768px) {
    /* Header — keep single row, scroll toolbar if needed */
    header {
        flex-direction: column;
        gap: 4px;
        padding: 4px 0 6px;
        text-align: center;
    }

    .header-toolbar {
        justify-content: center;
    }

    /* Container */
    .container {
        padding: 10px;
    }

    /* Cards grid — allow narrower cards on tablets */
    .displays-grid,
    .devices-grid {
        grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
        gap: 14px;
    }

    /* Modals — near full width */
    .modal-content {
        width: 95%;
        max-width: none;
        margin: 10px;
    }

    .modal-content-wide {
        min-width: 0;
        width: 95%;
        max-width: none;
    }

    /* Modal padding reduction */
    .modal-header {
        padding: 16px 16px 12px;
    }

    .modal-header h2 {
        font-size: 1.25rem;
    }

    .modal-body {
        padding: 16px;
    }

    .modal-footer {
        padding: 12px 16px 16px;
    }

    /* Section headings */
    h2 {
        font-size: 1.25rem;
        margin-bottom: 14px;
    }

    /* Segment range fields — allow wrapping */
    .segment-range-fields {
        flex-wrap: wrap;
    }

    .segment-range-fields input[type="number"] {
        width: 60px;
    }

    /* Composite layer editor */
    .composite-layer-blend {
        width: 80px;
    }

    /* Display picker */
    .display-picker-content {
        width: 95%;
    }
}


/* ================================================================
   PHONE  (≤ 600px)
   ================================================================ */
@media (max-width: 600px) {
    /* Prevent horizontal scroll (clip instead of hidden to preserve sticky) */
    html, body {
        overflow-x: clip;
    }

    /* ── Header ── */
    header {
        padding: 4px 0 6px;
    }

    .header-title {
        gap: 6px;
    }

    .header-toolbar {
        gap: 1px;
        padding: 2px 3px;
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .header-toolbar::-webkit-scrollbar {
        display: none;
    }

    .header-toolbar-sep {
        display: none;
    }

    .header-link {
        display: none;
    }

    .header-btn {
        min-width: 32px;
        min-height: 32px;
        padding: 4px 6px;
        flex-shrink: 0;
    }

    .header-locale {
        flex-shrink: 0;
        width: auto;
        max-width: 48px;
    }

    h1 {
        font-size: 1.1rem;
    }

    #server-version {
        display: none;
    }

    /* ── Bottom Tab Bar ── */
    .tab-bar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: var(--z-sticky);
        background: var(--card-bg);
        border-bottom: none;
        border-top: 1px solid var(--border-color);
        margin-bottom: 0;
        display: flex;
        flex-wrap: nowrap;
        justify-content: space-around;
        padding: 0;
        padding-bottom: env(safe-area-inset-bottom, 0px);
        box-shadow: 0 -2px 8px var(--shadow-color);
        gap: 0;
    }

    .tab-btn {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        padding: 8px 4px 6px;
        font-size: 0.65rem;
        border-bottom: none;
        border-top: 2px solid transparent;
        margin-bottom: 0;
        position: relative;
    }

    .tab-btn.active {
        border-bottom-color: transparent;
        border-top-color: var(--primary-color);
    }

    .tab-btn .icon {
        width: 20px;
        height: 20px;
        display: block;
    }

    .tab-btn > span[data-i18n] {
        font-size: 0.6rem;
        line-height: 1.2;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Tab badge repositioned to top-right of icon */
    .tab-badge {
        position: absolute;
        top: 2px;
        right: calc(50% - 18px);
        font-size: 0.55rem;
        padding: 0 4px;
        min-width: 14px;
        line-height: 1.2;
        margin-left: 0;
    }

    /* Body padding for fixed bottom bar */
    body {
        padding-bottom: 64px;
    }

    /* Graph editor: account for fixed bottom tab bar (~64px) */
    .graph-container {
        height: calc(100vh - var(--header-height, 60px) - 74px);
    }

    /* ── Container ── */
    .container {
        padding: 8px;
    }

    /* ── Cards — single column ── */
    .displays-grid,
    .devices-grid,
    .templates-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .card {
        padding: 10px 14px 14px;
    }

    .card-title {
        font-size: 1.05rem;
    }

    .card-header {
        padding-right: 24px;
    }

    .add-device-card {
        min-height: 100px;
    }

    .add-device-icon {
        font-size: 2rem;
    }

    /* ── Modals — full screen ── */
    .modal-content {
        width: 100%;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        margin: 0;
    }

    .modal-content-wide {
        min-width: 0;
        width: 100%;
        max-width: 100%;
        border-radius: 0;
    }

    .modal-header {
        padding: 12px 14px 10px;
    }

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

    .modal-body {
        padding: 14px;
    }

    .modal-footer {
        padding: 10px 14px 14px;
    }

    /* Inline fields stack vertically */
    .inline-fields {
        flex-direction: column;
        gap: 8px;
    }

    /* Segment rows — stack vertically */
    .segment-row-fields {
        flex-direction: column;
        align-items: stretch;
    }

    .segment-range-fields {
        flex-wrap: wrap;
    }

    .segment-range-fields input[type="number"] {
        width: 100%;
        flex: 1;
    }

    /* Buttons */
    .btn {
        min-width: 0;
    }

    .modal-footer .btn-icon {
        min-width: 50px;
        padding: 8px 16px;
    }

    /* Form groups */
    .form-group {
        margin-bottom: 12px;
    }

    /* Gradient stop rows — tighter */
    .gradient-stop-row {
        gap: 4px;
        padding: 4px 6px;
    }

    .gradient-stop-pos {
        width: 60px;
        max-width: 60px;
    }

    /* Composite layers */
    .composite-layer-row {
        flex-wrap: wrap;
    }

    .composite-layer-blend {
        width: 100%;
    }

    /* Metrics grid — single column */
    .metrics-grid {
        grid-template-columns: 1fr;
    }

    /* Timing legend */
    .timing-legend {
        gap: 4px;
        font-size: 0.7rem;
    }

    /* Audio test stats */
    .audio-test-stats,
    .vs-test-stats {
        flex-wrap: wrap;
        gap: 10px;
    }

    /* Section */
    section {
        margin-bottom: 24px;
    }

    h2 {
        font-size: 1.15rem;
        margin-bottom: 10px;
    }

    /* Section tip */
    .section-tip {
        font-size: 0.78rem;
        padding: 6px 10px;
    }

    /* Card subtitle gap */
    .card-subtitle {
        gap: 8px;
        margin-bottom: 10px;
    }

    /* Footer */
    .app-footer {
        margin-bottom: 50px;
    }

    /* Command palette */
    #command-palette {
        padding-top: 5vh;
    }

    .cp-dialog {
        width: 95vw;
    }

    /* Stream sub-tabs */
    .stream-tab-bar {
        flex-wrap: wrap;
        gap: 2px;
        margin-bottom: 10px;
    }

    .stream-tab-btn {
        padding: 6px 8px;
        font-size: 0.8rem;
    }

    .stream-tab-count {
        font-size: 0.6rem;
        padding: 0 4px;
    }

    .cs-expand-collapse-group {
        gap: 1px;
    }

    .btn-expand-collapse {
        width: 26px;
        height: 26px;
        font-size: 0.75rem;
    }

    /* Display picker */
    .display-picker-content {
        width: 98%;
    }

    .display-picker-canvas {
        padding: 12px;
    }

    .display-picker-title {
        font-size: 1.1rem;
        margin-bottom: 12px;
    }

    /* Lightbox */
    .lightbox-stats {
        font-size: 0.7rem;
        gap: 10px;
        padding: 6px 10px;
    }

    /* Pattern canvas — prevent horizontal overflow */
    .pattern-canvas-container { min-width: 0; width: 100%; }
}


/* ================================================================
   SMALL PHONE  (≤ 400px)
   ================================================================ */
@media (max-width: 400px) {
    /* Tighter header */
    h1 {
        font-size: 1rem;
    }

    /* Cards */
    .card {
        padding: 8px 10px 12px;
    }

    .card-title {
        font-size: 0.95rem;
    }

    .card-top-actions {
        gap: 1px;
    }

    .card-remove-btn,
    .card-power-btn,
    .card-autostart-btn {
        width: 32px;
        height: 32px;
    }

    /* Tab buttons even tighter */
    .tab-btn {
        padding: 6px 2px 4px;
    }

    .tab-btn > span[data-i18n] {
        font-size: 0.55rem;
    }

    /* Modal body */
    .modal-body {
        padding: 10px;
    }

    .modal-header {
        padding: 10px 12px 8px;
    }
}


/* ================================================================
   TOUCH DEVICE ENHANCEMENTS
   ================================================================ */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets */
    .card-remove-btn,
    .card-power-btn,
    .card-autostart-btn {
        width: 34px;
        height: 34px;
    }

    .modal-close-btn {
        width: 38px;
        height: 38px;
    }

    .hint-toggle {
        width: 24px;
        height: 24px;
    }

    /* Always show drag handles on touch */
    .card > .card-drag-handle,
    .template-card > .card-drag-handle {
        opacity: 0.4;
    }

    /* Disable hover transform on cards (causes janky scrolling) */
    .card:hover {
        transform: none;
    }

    .add-device-card:hover {
        transform: none;
    }

    /* Color picker dots — larger for fingers */
    .color-picker-dot {
        width: 38px;
        height: 38px;
    }
}


/* ================================================================
   STANDALONE PWA MODE
   ================================================================ */
@media (display-mode: standalone) {
    /* In standalone/PWA mode the browser chrome is gone,
       so we can use full viewport safely */
    header {
        padding-top: env(safe-area-inset-top, 4px);
    }
}
