/* Statistics — the quality tab ("Jakość danych"): rules no other tab uses.

   The colours come from the approved mockups. Their hexadecimal values live in the token blocks
   below (one per theme, like `css/stats.css`); the rules use the tokens. The three accent colours
   the app already defines (`--warn`, `--accent`, `--success`) are used by name, not repeated here. */

/* ── Tokens ────────────────────────────────────────────────────────────────── */

#view-stats {
    --q-ok: #10b981;          /* zatrzymał się, zwykłe odczyty */
    --q-soft: #7dd3fc;        /* bez potwierdzenia, odrobione */
    --q-poza: #475569;        /* poza zasięgiem GPS */
    --q-brak: #64748b;        /* brak w GPS, z krańca, słupki poniżej progu */
    --q-nieznany: #8391a7;    /* nieznany słupek */
    --q-zamalo: #334155;      /* za mało danych, brak wartości w siatce */
    --q-grey: 100, 116, 139;  /* #64748b as channels, for the grey steps of the grid */
    --q-cell-sel: #f8fafc;    /* ring of the selected grid cell */
}

html[data-theme="light"] #view-stats {
    --q-ok: #059669;
    --q-soft: #0369a1;
    --q-poza: #94a3b8;
    --q-brak: #94a3b8;
    --q-nieznany: #64748b;
    --q-zamalo: #cbd5e1;
    --q-grey: 100, 116, 139;
    --q-cell-sel: #0f172a;
}

html[data-theme="midnight"] #view-stats {
    --q-ok: #34d399;
    --q-soft: #bae6fd;
    --q-poza: #57534e;
    --q-brak: #78716c;
    --q-nieznany: #a8a29e;
    --q-zamalo: #44403c;
    --q-grey: 120, 113, 108;
    --q-cell-sel: #fafaf9;
}

/* ── Tones (bars, segments, cells, swatches) ───────────────────────────────── */

.stats-ui-tone--q-ok { background: var(--q-ok); }
.stats-ui-tone--q-soft { background: var(--q-soft); }
.stats-ui-tone--q-warn { background: var(--warn); }
.stats-ui-tone--q-poza { background: var(--q-poza); }
.stats-ui-tone--q-brak { background: var(--q-brak); }
.stats-ui-tone--q-nieznany { background: var(--q-nieznany); }
.stats-ui-tone--q-zamalo { background: var(--q-zamalo); }
.stats-ui-tone--q-missing { background: var(--q-zamalo); }

/* The grid's scale: four grey steps below 5 %, amber from 5 % (weaker) and 10 % (stronger). */
.stats-ui-tone--q-g1 { background: rgba(var(--q-grey), 0.18); }
.stats-ui-tone--q-g2 { background: rgba(var(--q-grey), 0.36); }
.stats-ui-tone--q-g3 { background: rgba(var(--q-grey), 0.58); }
.stats-ui-tone--q-g4 { background: rgba(var(--q-grey), 0.82); }
.stats-ui-tone--q-mid { background: color-mix(in srgb, var(--warn) 45%, transparent); }
.stats-ui-tone--q-hi { background: color-mix(in srgb, var(--warn) 85%, transparent); }

/* ── Cards ─────────────────────────────────────────────────────────────────── */

.stats-q-card {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 16px;
    border-radius: 6px;
    background: var(--stats-card-bg);
    border: 1px solid var(--stats-rule);
    min-width: 0;
}

.stats-q-h2 { margin: 0; font-size: 19px; line-height: 1.25; font-weight: 700; color: var(--stats-strong); }
.stats-q-h3 { margin: 0; font-size: 16px; line-height: 1.3; font-weight: 700; color: var(--stats-strong); }
.stats-q-desc { margin: 0; font-size: 13px; line-height: 1.5; color: var(--stats-meter); }
.stats-q-strong { color: var(--stats-strong); font-weight: 700; }
.stats-q-sub { font-size: 13px; font-weight: 600; padding-top: 4px; color: var(--stats-meter); }
.stats-q-foot { display: block; font-size: 12px; line-height: 1.5; color: var(--stats-meter); }
.stats-q-empty { margin: 0; font-size: 13px; color: var(--stats-meter); }

.stats-q-callout {
    padding: 10px 12px;
    border-radius: 4px;
    background: color-mix(in srgb, var(--accent) 8%, transparent);
    border-left: 3px solid var(--accent);
    font-size: 13px;
    line-height: 1.5;
    color: var(--stats-meter);
}
.stats-q-callout strong { color: var(--stats-strong); }

/* The ghost bracket: a rule under the bar, split at the settled share. */
/* The gap matches the bar's segment gap (2px), so the settled rule ends exactly where the decided
   segments end. The undecided label sits at the bar's end, hence right-aligned. */
.stats-q-bracket {
    display: grid;
    gap: 2px;
    font-size: 11px;
    color: var(--stats-meter);
}
.stats-q-bracket > span {
    border-top: 2px solid var(--stats-meter);
    padding-top: 3px;
    min-width: 0;
}
.stats-q-bracket-a { border-color: var(--accent); color: var(--stats-strong); }
.stats-q-bracket-b { text-align: right; }

/* Class rows: swatch, name and share on one line, the description under it. */
.stats-q-rows { display: flex; flex-direction: column; }
.stats-q-row {
    display: grid;
    grid-template-columns: 10px minmax(0, 1fr) auto;
    gap: 4px 10px;
    align-items: baseline;
    padding: 8px 0;
    border-top: 1px solid var(--stats-rule);
    font-variant-numeric: tabular-nums;
}
.stats-q-swatch { width: 10px; height: 10px; border-radius: 2px; align-self: center; }
.stats-q-row-name { font-size: 14px; font-weight: 600; color: var(--stats-strong); }
.stats-q-row-val { font-size: 14px; white-space: nowrap; color: var(--stats-strong); }
.stats-q-row-n { color: var(--stats-meter); font-size: 12px; }
.stats-q-row-desc { grid-column: 2 / -1; font-size: 12px; line-height: 1.45; color: var(--stats-meter); }
.stats-q-badge-ok { margin-left: 6px; font-size: 11px; font-weight: 600; color: var(--q-ok); }

.stats-q-card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.stats-q-card-head-link { flex-shrink: 0; }

.stats-q-group { display: flex; flex-direction: column; }
.stats-q-group-head {
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding-top: 6px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--stats-meter);
}
.stats-q-group-name { flex-grow: 1; }
.stats-q-group-head strong { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--stats-strong); }

.stats-q-rej { display: flex; flex-direction: column; gap: 4px; }
.stats-q-rej-label { font-size: 12px; color: var(--stats-meter); }

.stats-q-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }

.stats-q-card-foot { margin-top: auto; }

/* ── Table (the ghosts in a wide layout) ───────────────────────────────────── */

.stats-q-table td,
.stats-q-table th { vertical-align: top; }
.stats-q-name { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--stats-strong); }

/* ── Links under the section (phone) ───────────────────────────────────────── */

.stats-q-links { display: flex; flex-direction: column; gap: 4px; padding-top: 4px; }
.stats-q-caveats { min-height: 44px; display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; }
.stats-q-action {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--accent);
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

/* ── GPS hour chart ────────────────────────────────────────────────────────── */

.stats-q-chart { display: flex; flex-direction: column; gap: 4px; }
.stats-q-chart-body { display: grid; grid-template-columns: 34px minmax(0, 1fr); gap: 6px; }
.stats-q-yaxis { position: relative; height: 90px; }
.stats-q-tick {
    position: absolute;
    right: 2px;
    font-size: 10px;
    line-height: 1;
    color: var(--stats-meter);
    font-variant-numeric: tabular-nums;
}
.stats-q-plot {
    position: relative;
    height: 90px;
    display: grid;
    grid-template-columns: repeat(24, minmax(0, 1fr));
    gap: 2px;
    align-items: end;
    border-bottom: 1px solid var(--stats-axis);
}
.stats-q-grid-line {
    position: absolute;
    left: 0;
    right: 0;
    border-top: 1px dashed var(--stats-axis);
    pointer-events: none;
}
.stats-q-bar {
    position: relative;
    height: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    display: flex;
    align-items: flex-end;
    cursor: pointer;
}
.stats-q-bar:disabled { cursor: default; }
.stats-q-fill { display: block; width: 100%; border-radius: 2px 2px 0 0; }
.stats-q-fill--hi { background: var(--warn); }
.stats-q-fill--lo { background: var(--q-brak); }
.stats-q-plot--sel .stats-q-bar { opacity: 0.35; }
.stats-q-plot--sel .stats-q-bar.stats-q-bar--sel { opacity: 1; }
.stats-q-bar--sel .stats-q-fill { box-shadow: 0 0 0 1px var(--stats-seg-on-bg); }
.stats-q-badge {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    display: none;
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
    background: var(--stats-chip);
    color: var(--stats-strong);
    font-variant-numeric: tabular-nums;
}
.stats-q-bar--sel .stats-q-badge { display: block; }
.stats-q-xlabels {
    display: flex;
    justify-content: space-between;
    padding-left: 40px;
    font-size: 10px;
    color: var(--stats-meter);
}
.stats-q-hour-table { display: none; width: 100%; border-collapse: collapse; font-size: 13px; }
.stats-q-hour-table th,
.stats-q-hour-table td { padding: 4px 6px; text-align: left; border-top: 1px solid var(--stats-rule); }

/* ── Level-1 tap card ──────────────────────────────────────────────────────── */

.stats-q-tap-body { display: flex; flex-direction: column; gap: 10px; }
/* Label on the left, value pinned to the card's right edge (the mockup's grid). */
.stats-q-dl { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px 12px; margin: 0; font-size: 13px; }
.stats-q-dl dt { color: var(--stats-meter); }
.stats-q-dl dd { margin: 0; text-align: right; font-weight: 600; color: var(--stats-strong); font-variant-numeric: tabular-nums; }
.stats-q-tap-days { display: flex; flex-direction: column; gap: 6px; }
.stats-q-tap-day {
    display: grid;
    grid-template-columns: 56px minmax(0, 1fr) 48px;
    gap: 8px;
    align-items: center;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}
.stats-q-tap-day-label { color: var(--stats-meter); }
.stats-q-tap-day strong { text-align: right; color: var(--stats-strong); }

/* ── Level 2: day × hour grid ──────────────────────────────────────────────── */

.stats-q-g2-head { display: flex; flex-direction: column; gap: 6px; }
.stats-q-g2-title { font-size: 20px; }
.stats-q-g2-tabs { padding: 4px 0 8px; }
.stats-q-g2-body { display: flex; flex-direction: column; gap: 12px; }
/* The grid's header carries every third hour (the wide layout needs them); the phone shows
   every sixth, as its mockup does. The corner is child 1, hour h is child h + 2. */
.stats-q-g2-main .stats-ui-grid { gap: 2px; font-size: 9px; }
.stats-q-g2-main .stats-ui-cell { height: 22px; }
.stats-q-g2-main .stats-ui-cell--sel { box-shadow: 0 0 0 2px var(--q-cell-sel); }
.stats-q-det {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 10px 12px;
    border-radius: 4px;
    background: var(--stats-tile-bg);
    font-size: 13px;
}
.stats-q-det-title { font-size: 14px; color: var(--stats-strong); }
.stats-q-det-line { color: var(--stats-meter); font-variant-numeric: tabular-nums; }

/* ── Phone and wide layouts ────────────────────────────────────────────────── */

/* Phone: one column, the parts in their order. The table of the GPS chart replaces the bars
   only when the reader chose the table (the card carries `stats-q-gps--table`). */
@media (max-width: 959px) {
    .stats-panel--jakosc { display: flex; flex-direction: column; gap: 12px; }
    .stats-q-g2-main .stats-ui-grid-col:not(:nth-child(6n + 2)) { visibility: hidden; }
    .stats-q-gps--table .stats-q-chart-body,
    .stats-q-gps--table .stats-q-xlabels { display: none; }
    .stats-q-gps--table .stats-q-hour-table { display: table; }
}

/* Wide: a 12-column grid as the mockup draws it: the headline card and the "how to read" card
   share a row, the table spans the width, the readings and the GPS card share the last row. */
@media (min-width: 960px) {
    .stats-panel--jakosc {
        display: grid;
        grid-template-columns: repeat(12, minmax(0, 1fr));
        gap: 16px;
        align-items: stretch;
    }
    .stats-q-card--main { grid-column: span 8; }
    .stats-q-card--side { grid-column: span 4; }
    .stats-q-card--full { grid-column: 1 / -1; }
    .stats-q-card--readings { grid-column: span 6; }
    .stats-q-gps-block { grid-column: span 6; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
    .stats-q-gps-block .stats-q-card--gps { flex: 1 1 auto; }
    .stats-q-tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .stats-q-g2-body {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 300px;
        gap: 16px;
        align-items: start;
    }
    .stats-q-g2-main .stats-ui-grid { gap: 3px; font-size: 11px; }
    .stats-q-g2-main .stats-ui-cell { height: 30px; }
}
