/* ===== Responsive (loaded last — H5 overrides) ===== */

/* ---- Narrow desktop / tablet (≤1100px): charts page stacks so the
   progress table keeps a readable game-name column ---- */
@media (max-width: 1100px) {
    .charts-section {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
        height: auto;
        overflow: visible;
    }

    .charts-section .section--full {
        grid-row: auto;
        margin-bottom: 0;
    }

    .charts-section .section--full .table-wrap {
        max-height: 60dvh;
    }

    .charts-section .charts-right {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: none;
        height: auto;
    }

    .charts-section .chart-card {
        height: clamp(16.25rem, 42dvh, 21.25rem);
    }

    /* Let the charts tab scroll instead of clipping the fixed-height cards */
    #tab-charts {
        overflow-y: auto;
    }

    #tab-charts > [data-module] {
        flex: none;
        overflow: visible;
    }
}

/* ---- Compact desktop / tablet (≤900px): two-column grids collapse ---- */
@media (max-width: 900px) {
    .sections-grid,
    .charts-section {
        grid-template-columns: 1fr;
    }
}

/* ---- H5 (≤768px): page scrolls, panels become collapsible cards ----
   Desktop keeps the original layout: the toggles below are hidden /
   inert outside this media query. */
@media (max-width: 768px) {
    .tab-content {
        overflow: visible;
    }

    .tab-content--active {
        flex: none;
    }

    .table-wrap {
        border-radius: var(--radius);
    }

    /* ===== Stats: one-line summary strip, expands to the 2x2 grid ===== */
    .stats-wrap {
        margin-bottom: 0.5rem;
    }

    /* Charts tab only shows data & visualization, without consumption stats */
    body.tab-charts-active .stats-wrap {
        display: none;
    }

    .stats-toggle {
        display: flex;
        align-items: center;
        gap: 0.25rem;
        width: 100%;
        padding: 0.5rem 0.75rem;
        border: none;
        border-radius: var(--radius);
        background: var(--color-surface);
        box-shadow: var(--shadow);
        font-family: inherit;
        /* Matches the H5 .section__title size so the stats label and the
           section toggles read at the same scale. */
        font-size: 0.875rem;
        font-weight: 600;
        color: var(--color-text);
        text-align: left;
        cursor: pointer;
    }

    .stats-toggle__value {
        margin-left: auto;
        font-size: 0.875rem;
        font-weight: 700;
        color: var(--color-success);
    }

    .stats-toggle__value--danger {
        color: var(--color-danger);
    }

    /* Same arrow placement as .section-toggle__arrow: right after the label,
       offset by the toggle's 0.25rem gap. The value stays pinned to the far
       right through its margin-left: auto. */
    .stats-toggle__arrow {
        display: inline-block;
        font-size: 0.6875rem;
        color: var(--color-text-secondary);
        transition: transform var(--transition);
    }

    .stats-wrap.is-open .stats-toggle__arrow {
        transform: rotate(180deg);
    }

    .stats-row {
        display: none;
        margin-bottom: 0;
    }

    .stats-wrap.is-open .stats-row {
        display: grid;
        margin-top: 0.5rem;
    }

    /* ===== Sections: collapsible cards, title row stays on one line ===== */
    .sections-grid {
        gap: 0.5rem;
    }

    .section__header {
        align-items: baseline;
        padding: 0.5rem 0.75rem;
        gap: 0.375rem 0.5rem;
    }

    .section__title {
        font-size: 0.875rem;
    }

    /* Whole title row is a tap target, same feel as the stats strip.
       flex-grow eats the free space; the vertical padding + matching
       negative margin fill the header height without moving the text.
       .section__actions stays a separate flex item, so the sort/add
       buttons keep their own hit areas. */
    .section-toggle {
        /* inline-flex + center keeps the whole title row tappable while
           centering the arrow's box on the label's box, so this ▾ and the
           stats toggle's ▾ share one vertical position (baseline rendering
           left the two off by a pixel or two). */
        display: inline-flex;
        align-items: center;
        flex: 1 1 auto;
        pointer-events: auto;
        cursor: pointer;
        padding: 0.5rem 0;
        margin: -0.5rem 0;
    }

    .section-toggle__arrow {
        display: inline-block;
        margin-left: 0.25rem;
        font-size: 0.6875rem;
        color: var(--color-text-secondary);
        transition: transform var(--transition);
    }

    .section--collapsible.is-open .section-toggle__arrow {
        transform: rotate(180deg);
    }

    .section--collapsible .section__filters,
    .section--collapsible .table-wrap {
        display: none;
    }

    .section--collapsible:not(.is-open) .section__header {
        border-bottom: none;
    }

    .section--collapsible.is-open .section__filters {
        display: flex;
    }

    .section--collapsible.is-open .table-wrap {
        display: flex;
        max-height: 60dvh;
    }

    /* Cap the expanded purchases table so the log card's header stays
       visible below it. 23rem ≈ page header + stats strip + filters +
       both card headers; safe-area insets are subtracted separately
       because the page header carries them. Logs/charts keep 60dvh. */
    .section--collapsible.is-open[data-collapsible="purchases"] .table-wrap {
        max-height: 52dvh;
        max-height: min(60dvh, calc(100dvh - 23rem - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)));
    }

    .section__actions {
        margin-left: auto;
        gap: 0.25rem;
    }

    .section__actions .btn--sm {
        padding: 0.375rem 0.5rem;
        font-size: 0.72rem;
    }

    /* ===== Filters: full-width search (platform chips are desktop-only) ===== */
    .section__filters {
        padding: 0.5rem 0.75rem 0.375rem;
        margin-bottom: 0;
        gap: 0.375rem;
    }

    /* H5 filters by tapping the platform badge inside a row, so the chip
       strip stays hidden there and only the search box remains */
    .section__filters .chips-container {
        display: none;
    }

    .section__filters .search-wrapper {
        margin-left: 0;
        width: 100%;
    }

    .search-input {
        width: 100%;
        padding: 0.375rem 1.625rem 0.375rem 0.5rem;
        font-size: 0.75rem;
    }

    .chip {
        flex: 0 0 auto;
        padding: 0.25rem 0.5rem;
        font-size: 0.72rem;
    }

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

    .toolbar__spacer {
        display: none;
    }

    /* ===== Tables: compact columns, secondary columns folded inline ===== */
    th,
    td {
        padding: 0.5rem 0.1875rem;
        font-size: 0.7rem;
    }

    th {
        font-size: 0.68rem;
    }

    /* Purchases: two-line card rows instead of four cramped columns
       line 1: game (+historical badge, note) | price
       line 2: date (+platform badge)         | actions */
    .table--purchases thead {
        display: none;
    }

    /* Drop the table layout so grid rows span the full card width */
    .table--purchases,
    .table--purchases tbody {
        display: block;
    }

    .table--purchases colgroup {
        display: none;
    }

    .table--purchases tbody tr:not(.vs-spacer) {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "game price"
            "date actions";
        align-items: center;
        gap: 0.125rem 0.5rem;
        padding: 0.4375rem 0.625rem;
        border-bottom: 1px solid var(--color-border);
    }

    .table--purchases tbody tr:not(.vs-spacer):last-child {
        border-bottom: none;
    }

    .table--purchases tbody tr:not(.vs-spacer) td {
        padding: 0;
        border-bottom: none;
    }

    .table--purchases td:nth-child(1) {
        grid-area: date;
        color: var(--color-text-secondary);
    }

    .table--purchases td:nth-child(2) {
        grid-area: game;
    }

    .table--purchases td:nth-child(3) {
        display: none;
    }

    .table--purchases td:nth-child(4) {
        grid-area: price;
        align-self: start;
        text-align: right;
        font-size: 0.78rem;
        font-weight: 600;
    }

    .table--purchases td:nth-child(5) {
        grid-area: actions;
        justify-self: end;
    }

    .table--purchases td[colspan] {
        grid-column: 1 / -1;
    }

    /* Virtual-scroll spacers must stay real table rows */
    .table--purchases tr.vs-spacer {
        display: table-row;
    }

    .table--purchases tbody tr:not(.vs-spacer):hover {
        background: #fafbfc;
    }

    .table--purchases tbody tr:not(.vs-spacer):hover td {
        background: transparent;
    }

    /* Room to breathe: restore comfortable action buttons on card rows */
    .table--purchases .cell-actions .btn {
        padding: 0.3125rem 0.5rem;
        font-size: 0.7rem;
    }

    /* Logs: date + amount stacked | description (+express info) | [express col
       hidden] | actions. Rows are self-describing on H5, so the sticky header
       table is desktop-only. */
    .table--logs.table--sticky {
        display: none;
    }

    .table--logs col:nth-child(1) {
        width: 6rem !important;
    }

    .table--logs col:nth-child(2) {
        width: 0 !important;
    }

    .table--logs col:nth-child(4) {
        width: 0 !important;
    }

    .table--logs col:nth-child(5) {
        width: 5.125rem !important;
    }

    /* Same row insets as the other H5 cards (purchases rows use 0.625rem) */
    .table--logs td:first-child {
        padding-left: 0.625rem;
    }

    .table--logs td:last-child {
        padding-right: 0.625rem;
        /* Actions hug the right inset, like the purchases card rows */
        text-align: right;
    }

    /* Amount column is hidden — its value is stacked under the date instead */
    .table--logs td:nth-child(2) {
        padding: 0;
        border-bottom: none;
        font-size: 0;
        overflow: hidden;
    }

    .table--logs td:nth-child(2) > * {
        display: none;
    }

    .table--logs td:nth-child(4) {
        padding: 0;
        border-bottom: none;
        font-size: 0;
        overflow: hidden;
    }

    .table--logs td:nth-child(4) > * {
        display: none;
    }

    /* Inline copies of the hidden columns (selector matches table.css specificity) */
    td .cell-game__platform {
        display: inline-block;
        margin-left: 0.25rem;
        vertical-align: 0.0625rem;
    }

    td .cell-desc__express {
        display: block;
        margin-top: 0.0625rem;
        font-size: 0.625rem;
        color: var(--color-text-secondary);
    }

    /* Amount leads the cell (date follows underneath) */
    td .cell-date__amount {
        display: block;
        margin-bottom: 0.0625rem;
        font-weight: 600;
        white-space: nowrap;
    }

    /* Compact touch-friendly action buttons */
    .cell-actions .btn {
        min-height: 1.75rem;
        padding: 0.25rem;
        font-size: 0.68rem;
        margin-right: 0.125rem;
    }

    .cell-actions .btn:last-child {
        margin-right: 0;
    }

    .badge {
        padding: 0.125rem 0.3125rem;
        font-size: 0.62rem;
    }

    /* Progress: game (+price · avg-time) | playtime | status | actions;
       rows are self-describing on H5, so the header is hidden */
    .table--progress thead {
        display: none;
    }

    .table--progress col:nth-child(2),
    .table--progress col:nth-child(5) {
        width: 0 !important;
    }

    .table--progress col:nth-child(3),
    .table--progress col:nth-child(4) {
        width: 3.75rem !important;
    }

    .table--progress col:nth-child(6) {
        width: 3rem !important;
    }

    /* The compact 0.1875rem cell padding keeps the inline controls fitting,
       but leaves the game name flush against the card border — restore the
       card's own 0.75rem inset on the outer edge. */
    .table--progress td:first-child {
        padding-left: 0.75rem;
    }

    .table--progress th:nth-child(2),
    .table--progress td:nth-child(2),
    .table--progress th:nth-child(5),
    .table--progress td:nth-child(5) {
        padding: 0;
        border-bottom: none;
        font-size: 0;
        overflow: hidden;
    }

    .table--progress .playtime-input,
    .table--progress .status-select {
        width: 100%;
        padding: 0.25rem;
        font-size: 0.68rem;
    }

    /* The narrower input needs its text room back from the native steppers */
    .table--progress .playtime-input {
        appearance: textfield;
        -moz-appearance: textfield;
    }

    .table--progress .playtime-input::-webkit-inner-spin-button,
    .table--progress .playtime-input::-webkit-outer-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }

    /* Room for the custom select arrow on browsers without base-select */
    .table--progress .status-select {
        padding-right: 0.875rem;
        background-position: right 0.25rem center;
    }

    @supports (appearance: base-select) {
        .table--progress .status-select {
            padding-right: 0.25rem;
        }
    }

    td .cell-game__meta {
        display: block;
        margin-top: 0.0625rem;
        font-size: 0.625rem;
        color: var(--color-text-secondary);
        white-space: normal;
    }

    /* Progress: source label (custom / shared) is desktop-only */
    .table--progress .cell-game__source {
        display: none;
    }

    /* Progress filters: rate summary on one line, search below */
    .progress-stats {
        width: 100%;
        flex-wrap: nowrap;
        gap: 0.5rem;
    }

    .progress-stat {
        flex: 0 0 auto;
        font-size: 0.68rem;
    }

    .sort-group {
        gap: 0.125rem;
    }

    .cell-express__fee {
        font-size: 0.75rem;
    }

    .empty-state {
        padding: 1.75rem 0.75rem;
    }

    .empty-state__icon {
        font-size: 2rem;
    }

    /* ===== Charts: stacked cards ===== */
    .charts-section {
        gap: 0.75rem;
    }

    .charts-section .section--full {
        grid-row: auto;
        margin-bottom: 0;
    }

    .charts-section .chart-card {
        padding: 0.75rem;
    }

    /* Platform distribution chart is desktop-only; H5 keeps the trend chart */
    .charts-section .chart-card--pie {
        display: none;
    }

    .chart-title {
        margin-bottom: 0.5rem;
        font-size: 0.8125rem;
    }

    .chart-container {
        height: auto;
    }

    .charts-section .charts-right {
        grid-template-columns: 1fr;
        grid-template-rows: none;
        height: auto;
        gap: 0.75rem;
    }
}

/* ---- Small phones (≤380px) ---- */
@media (max-width: 380px) {
    .section__header {
        gap: 0.25rem 0.375rem;
    }

    .section__actions {
        gap: 0.1875rem;
    }

    .section__actions .btn--sm {
        padding: 0.3125rem 0.4375rem;
        font-size: 0.68rem;
    }
}
