/**
 * RAM Speed Test
 *
 * All values use design tokens from main.css. Only px exception: container
 * max-width and reserved minimum heights. The rb- prefix keeps styles scoped
 * to this app.
 */

/* ========================================
   App Container Centering (REQUIRED)
   ======================================== */

.rb-container {
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
}

/* ========================================
   Utilities
   ======================================== */

/* Accessible visually-hidden utility. main.css defines .visually-hidden but
   not .sr-only; this app scopes its own copy so .sr-only elements (live
   regions, table captions) stay available to screen readers while being
   hidden from sighted users. */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

/* ========================================
   Icon buttons (help toggle + help close)
   ======================================== */

.btn-icon {
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-full);
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--color-text-muted);
    background: var(--color-bg-alt);
    border: 1px solid var(--color-border);
    cursor: pointer;
    transition: background var(--transition-base), border-color var(--transition-base), color var(--transition-base);
}

.btn-icon:hover {
    background: var(--color-border);
    color: var(--color-text);
}

.btn-icon:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

/* ========================================
   Panels
   ======================================== */

.rb-panel {
    background: var(--color-bg-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: var(--space-6);
}

/* The controls, intro, progress, result and history panels are all kept in
   the flow with hidden-attribute toggling (JS flips `hidden`), so spacing
   uses plain adjacent-sibling rules that hold regardless of which panels are
   visible at a given moment. */
.rb-controls + .rb-intro,
.rb-intro + .rb-progress,
.rb-intro + .rb-error,
.rb-intro + .rb-result,
.rb-progress + .rb-error,
.rb-progress + .rb-result,
.rb-progress + .rb-idle,
.rb-error + .rb-idle,
.rb-result + .rb-idle,
.rb-idle + .rb-honest,
.rb-honest + .rb-tiers,
.rb-tiers + .rb-history {
    margin-top: var(--space-6);
}

/* Panels toggled via the hidden attribute need the attribute to actually
   remove them from flow. */
[hidden] {
    display: none !important;
}

/* ========================================
   Controls
   ======================================== */

.rb-controls__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: center;
}

.rb-run {
    padding: var(--space-3) var(--space-6);
    font-size: var(--font-size-xl);
    font-weight: 600;
}

.rb-run:focus-visible {
    outline: 3px solid var(--color-primary-text);
    outline-offset: 2px;
}

/* The Run button doubles as Stop while a run is in flight (JS swaps the
   class). `filter: brightness` keeps the hover state within the design
   tokens. */
.rb-run.rb-run--stop {
    background: var(--color-error);
    color: var(--color-text-inverse);
}

.rb-run.rb-run--stop:hover {
    background: var(--color-error);
    filter: brightness(0.9);
}

.rb-run.rb-run--stop:focus-visible {
    outline-color: var(--color-error-text);
}

.rb-run:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ========================================
   Intro
   ======================================== */

.rb-intro__title {
    margin: 0 0 var(--space-2);
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--color-text);
}

.rb-intro__text {
    margin: 0;
    font-size: var(--font-size-base);
    color: var(--color-text);
}

.rb-intro__privacy {
    margin: var(--space-3) 0 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

/* ========================================
   Progress
   ======================================== */

.rb-progress__head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--space-3);
    margin-bottom: var(--space-2);
}

.rb-progress__label {
    margin: 0;
    font-size: var(--font-size-base);
    font-weight: 600;
}

.rb-progress__pct {
    margin: 0;
    font-family: var(--font-mono);
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    font-variant-numeric: tabular-nums;
}

.rb-progress__track {
    height: var(--space-3);
    background: var(--color-bg-alt);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.rb-progress__fill {
    height: 100%;
    width: 0;
    background: var(--color-primary);
    border-radius: var(--radius-full);
    transition: width var(--transition-base);
}

.rb-progress__status {
    margin: var(--space-3) 0 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

/* ========================================
   Idle note / error
   ======================================== */

.rb-idle p {
    margin: 0;
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
}

.rb-error p {
    margin: 0;
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
}

/* ========================================
   Result
   ======================================== */

.rb-score {
    display: flex;
    align-items: baseline;
    gap: var(--space-2);
    flex-wrap: wrap;
}

.rb-score__label {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

.rb-score__value {
    font-family: var(--font-mono);
    font-size: var(--font-size-4xl);
    font-weight: 700;
    line-height: 1;
    color: var(--color-text);
    font-variant-numeric: tabular-nums;
    margin-top: var(--space-1);
}

.rb-tier {
    display: inline-block;
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-full);
    font-size: var(--font-size-sm);
    font-weight: 600;
    border: 1px solid var(--color-border);
    background: color-mix(in srgb, var(--color-text-muted) 15%, transparent);
    color: var(--color-text);
}

.rb-tier--entry {
    background: color-mix(in srgb, var(--color-error) 15%, transparent);
    border-color: var(--color-error);
    color: var(--color-error-text);
}

.rb-tier--mid {
    background: color-mix(in srgb, var(--color-warning) 15%, transparent);
    border-color: var(--color-warning);
    color: var(--color-warning-text);
}

.rb-tier--fast {
    background: color-mix(in srgb, var(--color-primary) 15%, transparent);
    border-color: var(--color-primary);
    color: var(--color-primary-text);
}

.rb-tier--vfast {
    background: color-mix(in srgb, var(--color-success) 15%, transparent);
    border-color: var(--color-success);
    color: var(--color-success-text);
}

.rb-metrics {
    display: grid;
    gap: var(--space-2);
    margin: var(--space-4) 0 0;
}

.rb-metric {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--color-border);
}

.rb-metric__label {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

.rb-metric__value {
    font-family: var(--font-mono);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.rb-metric__unit {
    font-family: var(--font-mono);
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

.rb-meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: baseline;
    margin: var(--space-3) 0 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

.rb-estimate-badge {
    display: inline-block;
    padding: 0 var(--space-2);
    border-radius: var(--radius-full);
    font-size: var(--font-size-xs);
    font-weight: 600;
    border: 1px solid var(--color-warning);
    color: var(--color-warning-text);
    background: color-mix(in srgb, var(--color-warning) 15%, transparent);
}

.rb-method-note {
    margin: var(--space-4) 0 0;
    padding: var(--space-3) var(--space-4);
    border-left: 4px solid var(--color-warning);
    background: color-mix(in srgb, var(--color-warning) 10%, transparent);
    font-size: var(--font-size-sm);
    color: var(--color-text);
}

/* ========================================
   Honest method + tier meanings
   ======================================== */

.rb-honest__title,
.rb-tiers__title {
    margin: 0 0 var(--space-2);
    font-size: var(--font-size-lg);
    font-weight: 600;
}

.rb-honest p {
    margin: 0 0 var(--space-2);
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
}

.rb-honest p:last-child {
    margin-bottom: 0;
}

.rb-tiers__list {
    margin: 0;
    padding-left: var(--space-5);
    display: grid;
    gap: var(--space-2);
}

.rb-tiers__list li {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

/* ========================================
   Run history
   ======================================== */

.rb-history__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    flex-wrap: wrap;
    margin-bottom: var(--space-4);
}

.rb-history__title {
    margin: 0;
    font-size: var(--font-size-xl);
    font-weight: 600;
}

/* Stable-height slot: the empty-state message and the runs table share this
   reserved region, so populating history right after a run never pushes the
   sections below (CLS). The region grows naturally if the table exceeds the
   reserved height. */
.rb-history__body {
    min-height: 11rem;
}

.rb-history__empty {
    margin: 0;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    min-height: 11rem;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0 var(--space-4);
}

.rb-history__tablewrap {
    overflow-x: auto;
}

.rb-table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--font-size-sm);
}

.rb-table caption {
    text-align: left;
}

.rb-table th,
.rb-table td {
    padding: var(--space-2) var(--space-3);
    text-align: left;
    border-bottom: 1px solid var(--color-border);
    font-variant-numeric: tabular-nums;
}

.rb-table th {
    font-weight: 600;
    color: var(--color-text-muted);
    white-space: nowrap;
}

.rb-table tbody tr:nth-child(even) {
    background: var(--color-bg-alt);
}

/* ========================================
   Responsive: run history as stacked cards (<768px)
   ========================================
   Below 768px each table row is restyled into a card and every cell shows
   its column label via a data-label pseudo-element. The labels are copied
   from the already-localized <th> cells at render time (JS), so no extra
   i18n keys are needed and all columns stay visible. */
@media (max-width: 767px) {
    .rb-history__tablewrap {
        overflow-x: visible;
    }

    .rb-table,
    .rb-table thead,
    .rb-table tbody,
    .rb-table tr,
    .rb-table td {
        display: block;
    }

    /* Header row is dropped; each card cell carries its own label instead. */
    .rb-table thead {
        display: none;
    }

    .rb-table tbody tr {
        background: var(--color-bg-alt);
        border: 1px solid var(--color-border);
        border-radius: var(--radius-md);
        padding: var(--space-3);
        margin-bottom: var(--space-3);
    }

    .rb-table td {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        gap: var(--space-3);
        padding: var(--space-1) 0;
        text-align: right;
    }

    /* Last cell of a card needs no separator; the card border closes it. */
    .rb-table td:last-child {
        border-bottom: none;
    }

    .rb-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--color-text-muted);
        text-transform: uppercase;
        letter-spacing: 0.05em;
        font-size: var(--font-size-xs);
        flex: 0 0 auto;
        text-align: left;
    }

    .rb-score__value {
        font-size: var(--font-size-3xl);
    }
}

/* ========================================
   Reduced motion
   ======================================== */

@media (prefers-reduced-motion: reduce) {
    .rb-progress__fill {
        transition: none;
    }
}
