/* Lap Timer app (PWA). Mobile first; colours come from ../css/theme.css tokens. */

:root {
    --app-bar-bg: linear-gradient(135deg, #1565c0 0%, #1976d2 55%, #42a5f5 100%);
    --tab-h: 58px;
    --bar-h: 52px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --radius: 12px;
    --mono: ui-monospace, 'SF Mono', Menlo, Consolas, 'Courier New', monospace;
    --gold: #f9a825;
    --silver: #90a4ae;
    --bronze: #bf7b3a;
}
html[data-theme="dark"] {
    --app-bar-bg: linear-gradient(135deg, #0d47a1 0%, #1565c0 100%);
    --gold: #ffd54f;
    --silver: #b0bec5;
    --bronze: #d49a5c;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: 'Roboto', system-ui, -apple-system, 'Segoe UI', sans-serif;
    font-size: 15px;
    line-height: 1.4;
    color: var(--theme-text);
    background: var(--theme-bg);
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: none;
}
body.no-scroll { overflow: hidden; }
a { color: var(--theme-accent); text-decoration: none; }
h1, h2, h3, p { margin: 0; }
img { max-width: 100%; }
.material-icons { font-size: 20px; line-height: 1; flex-shrink: 0; }

/* ---- app bar ---------------------------------------------------------- */
.appbar {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 10px;
    height: calc(var(--bar-h) + var(--safe-top));
    padding: var(--safe-top) 12px 0;
    background: var(--app-bar-bg);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
.appbar-logo { height: 30px; width: auto; padding: 2px 6px; border-radius: 8px; background: #fff; }
.appbar-title { flex: 1; min-width: 0; font-size: 18px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-back {
    width: 44px; height: 44px; margin-left: -8px;
    display: flex; align-items: center; justify-content: center;
    border: 0; border-radius: 50%; background: transparent; color: #fff; cursor: pointer;
}
.appbar-back:active { background: rgba(255, 255, 255, .2); }
.appbar-back .material-icons { font-size: 24px; }

/* ---- layout ----------------------------------------------------------- */
.view {
    max-width: 720px;
    margin: 0 auto;
    padding: 14px 16px calc(var(--tab-h) + var(--safe-bottom) + 24px);
    min-height: calc(100vh - var(--bar-h));
    outline: none;
}
/* Vertical rhythm between the blocks of a screen (the element selector keeps it above per-component margins). */
main.view > * + * { margin-top: 14px; }
main.view > .section-title { margin-top: 22px; }

.tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 100;
    display: flex;
    height: calc(var(--tab-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--theme-card-bg);
    border-top: 1px solid var(--theme-border);
}
.tabbar a {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    color: var(--theme-text-secondary);
    font-size: 11px;
    font-weight: 500;
}
.tabbar a .material-icons { font-size: 24px; }
.tabbar a.active { color: var(--theme-accent); }
.tabbar a:active { background: var(--theme-hover); }

/* ---- generic pieces ---------------------------------------------------- */
.card {
    display: block;
    padding: 14px 16px;
    background: var(--theme-card-bg);
    border: 1px solid var(--theme-border-light);
    border-radius: var(--radius);
    box-shadow: 0 1px 3px var(--theme-card-shadow);
    color: inherit;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-title { display: flex; align-items: center; gap: 6px; font-size: 16px; font-weight: 500; }
.card-title .material-icons { color: var(--theme-accent); }
.section-title { margin-top: 22px; font-size: 13px; font-weight: 500; letter-spacing: .06em; text-transform: uppercase; color: var(--theme-text-muted); }
.muted { color: var(--theme-text-secondary); font-size: 13px; }
.center { text-align: center; }
.pre { white-space: pre-wrap; line-height: 1.55; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chips-quiet { margin-top: 6px; }
.chip {
    display: inline-flex; align-items: center; gap: 4px;
    min-height: 24px; padding: 2px 9px;
    border: 0; border-radius: 12px;
    background: var(--theme-badge-bg); color: var(--theme-badge-text);
    font: inherit; font-size: 12px; font-weight: 500;
}
.chip .material-icons { font-size: 15px; }
.chips-quiet .chip { background: var(--theme-bg); color: var(--theme-text-secondary); border: 1px solid var(--theme-border-light); }
.chip-link { cursor: pointer; border: 1px solid var(--theme-accent); background: transparent; color: var(--theme-accent); }
.chip-good { background: var(--theme-success-bg); color: var(--theme-success-text); }
.chip-setup { background: var(--theme-info-bg); color: var(--theme-info-text); }
.ballast-badge { display: inline-flex; align-items: center; min-height: 24px; padding: 2px 8px; border-radius: 12px; background: var(--theme-error-bg); color: var(--theme-error-text); font-size: 12px; font-weight: 700; }
.fixed-setup-badge { display: inline-flex; align-items: center; min-height: 24px; padding: 2px 8px; border-radius: 12px; background: var(--theme-warning-bg); color: var(--theme-warning-text); font-size: 12px; font-weight: 500; }
.flag { width: 20px; height: 15px; object-fit: cover; border-radius: 2px; vertical-align: -2px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 44px; padding: 0 18px;
    border: 0; border-radius: 22px;
    background: var(--theme-accent); color: #fff;
    font: inherit; font-weight: 500; cursor: pointer;
}
html[data-theme="dark"] .btn { color: #0b1b2b; }
.btn:active { filter: brightness(.9); }
.btn:disabled { opacity: .6; }
.btn-block { display: flex; width: 100%; }
.btn-small { min-height: 36px; padding: 0 14px; font-size: 13px; }
.btn-outline { background: transparent; color: var(--theme-accent); border: 1px solid var(--theme-accent); }
html[data-theme="dark"] .btn-outline { color: var(--theme-accent); }
.btn .material-icons { font-size: 18px; }

/* ---- states ----------------------------------------------------------- */
.state { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 48px 16px; text-align: center; color: var(--theme-text-secondary); }
.state .material-icons { font-size: 44px; color: var(--theme-text-muted); }
.spinner { width: 32px; height: 32px; border: 3px solid var(--theme-border); border-top-color: var(--theme-accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- lists ------------------------------------------------------------ */
.list { background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-radius: var(--radius); overflow: hidden; box-shadow: 0 1px 3px var(--theme-card-shadow); }
.row {
    display: flex; align-items: center; gap: 12px;
    width: 100%; min-height: 60px; padding: 10px 14px;
    border: 0; border-bottom: 1px solid var(--theme-border-light);
    background: transparent; color: var(--theme-text);
    font: inherit; text-align: left;
}
.row:last-child { border-bottom: 0; }
a.row:active, .row-button:active { background: var(--theme-hover); }
.row-button { cursor: pointer; }
.row-static { cursor: default; }
.row-icon { width: 28px; display: flex; align-items: center; justify-content: center; color: var(--theme-accent); }
.row-icon .material-icons { font-size: 24px; }
.row-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.row-title { font-weight: 500; overflow-wrap: anywhere; }
.row-sub { font-size: 12.5px; color: var(--theme-text-secondary); }
.row-value { font-family: var(--mono); font-size: 13px; font-weight: 700; color: var(--theme-accent); white-space: nowrap; }
.row-chevron { color: var(--theme-text-muted); }

.search { position: relative; display: flex; align-items: center; }
.search .material-icons { position: absolute; left: 12px; color: var(--theme-text-muted); }
.search input {
    width: 100%; min-height: 44px; padding: 0 14px 0 40px;
    border: 1px solid var(--theme-input-border); border-radius: 22px;
    background: var(--theme-input-bg); color: var(--theme-text);
    font: inherit; font-size: 16px; /* 16px stops iOS zooming into the field */
}
.select {
    width: 100%; min-height: 46px; margin-top: 6px; padding: 0 12px;
    border: 1px solid var(--theme-input-border); border-radius: 10px;
    background: var(--theme-input-bg); color: var(--theme-text);
    font: inherit; font-size: 16px;
}
.field-label { display: block; font-size: 13px; color: var(--theme-text-secondary); }

.segmented { display: flex; gap: 2px; padding: 3px; border-radius: 22px; background: var(--theme-border-light); }
.segmented button {
    flex: 1; min-height: 38px; padding: 0 6px; white-space: nowrap;
    border: 0; border-radius: 19px; background: transparent; color: var(--theme-text-secondary);
    font: inherit; font-size: 13.5px; font-weight: 500; cursor: pointer;
}
.segmented button.active { background: var(--theme-card-bg); color: var(--theme-accent); box-shadow: 0 1px 3px var(--theme-card-shadow); }

/* ---- games home ------------------------------------------------------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 12px 4px; background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-radius: var(--radius); }
.stat .material-icons { color: var(--theme-accent); }
.stat strong { font-size: 20px; font-weight: 700; }
.stat small { font-size: 11px; color: var(--theme-text-secondary); }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { display: flex; flex-direction: column; gap: 3px; padding: 12px; background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-left: 3px solid var(--theme-accent); border-radius: var(--radius); color: var(--theme-text); }
.tile .material-icons { color: var(--theme-accent); margin-bottom: 2px; }
.tile strong { font-size: 13.5px; }
.tile small { font-size: 11.5px; color: var(--theme-text-secondary); }

.circuit-card-title { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

/* ---- ranked laptimes -------------------------------------------------- */
.laps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.lap { display: flex; gap: 12px; padding: 12px 14px; background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-radius: var(--radius); }
.lap-first { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
.lap-pos { width: 30px; flex-shrink: 0; padding-top: 2px; font-size: 18px; font-weight: 700; text-align: center; color: var(--theme-text-secondary); }
.lap-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.lap-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.lap-time { font-family: var(--mono); font-size: 20px; font-weight: 700; color: var(--theme-accent); }
.lap-gap { font-family: var(--mono); font-size: 12.5px; color: var(--theme-text-secondary); }
.lap-driver { font-weight: 500; overflow-wrap: anywhere; }
.lap-car { font-size: 13px; color: var(--theme-text-secondary); overflow-wrap: anywhere; }
.lap-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 4px; }
.lap-latest .lap-meta + .btn { margin-top: 8px; align-self: flex-start; }
.more { margin-top: 12px; }
.dp-driver-link { text-decoration: underline dotted var(--theme-text-muted); text-underline-offset: 3px; }

/* ---- track vs class --------------------------------------------------- */
.tvc-list { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.tvc-item { padding: 12px 14px; background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-radius: var(--radius); }
.tvc-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.tvc-time { font-family: var(--mono); font-weight: 700; color: var(--theme-accent); }
.tvc-bar { height: 12px; margin: 8px 0; border-radius: 6px; background: var(--theme-border-light); overflow: hidden; }
.tvc-bar span { display: block; height: 100%; border-radius: 6px; }
.tvc-meta { font-size: 13px; color: var(--theme-text-secondary); display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }

/* ---- cars ------------------------------------------------------------- */
.group { margin-top: 10px; background: var(--theme-card-bg); border: 1px solid var(--theme-border-light); border-radius: var(--radius); overflow: hidden; }
.group summary, .race summary {
    display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 8px 14px;
    list-style: none; cursor: pointer; font-weight: 500;
}
.group summary::-webkit-details-marker, .race summary::-webkit-details-marker { display: none; }
.group summary span:first-child { flex: 1; }
.group summary small { color: var(--theme-text-muted); }
.group summary .material-icons, .race summary .material-icons { color: var(--theme-text-muted); transition: transform .2s; }
.group[open] summary .material-icons, .race[open] summary .material-icons { transform: rotate(180deg); }
.group .list { border: 0; border-top: 1px solid var(--theme-border-light); border-radius: 0; box-shadow: none; }
#carsBy { margin-bottom: 4px; }

/* ---- time trial ------------------------------------------------------- */
.event-card { border-left: 4px solid var(--theme-text-muted); }
.event-card.is-open { border-left-color: var(--theme-success); }
.event-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.status { padding: 2px 9px; border-radius: 10px; font-size: 11px; font-weight: 700; letter-spacing: .04em; }
.status-open { background: var(--theme-success-bg); color: var(--theme-success-text); }
.status-closed { background: var(--theme-border-light); color: var(--theme-text-secondary); }
.hero { background: linear-gradient(135deg, #0d47a1, #1976d2 60%, #42a5f5); color: #fff; border: 0; }
.hero .chip { background: rgba(255, 255, 255, .2); color: #fff; }
.hero .status { background: rgba(255, 255, 255, .25); color: #fff; }

.bar-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.bar-rank { width: 16px; font-weight: 700; color: var(--theme-text-secondary); text-align: center; }
.bar-name { width: 34%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; height: 10px; border-radius: 5px; background: var(--theme-border-light); overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 5px; background: var(--theme-accent); }
.bar-fill.gold { background: var(--gold); }
.bar-fill.silver { background: var(--silver); }
.bar-fill.bronze { background: var(--bronze); }
.bar-value { width: 32px; text-align: right; font-weight: 700; }

.scroll-x { overflow-x: auto; margin: 0 -16px; padding: 0 16px; -webkit-overflow-scrolling: touch; }
.board { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13.5px; }
.board th, .board td { padding: 8px 10px; border-bottom: 1px solid var(--theme-border-light); white-space: nowrap; text-align: left; background: var(--theme-card-bg); }
.board th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--theme-text-secondary); background: var(--theme-table-header); }
.board .num { text-align: center; }
.board td.num small { display: block; font-family: var(--mono); font-size: 11px; color: var(--theme-text-secondary); }
.board .sticky { position: sticky; z-index: 1; }
.board .c1 { left: 0; min-width: 38px; text-align: center; }
.board .c2 { left: 38px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; box-shadow: 2px 0 4px -2px var(--theme-card-shadow); }
.board .pts { padding: 2px 8px; border-radius: 10px; background: var(--theme-badge-bg); color: var(--theme-badge-text); }
.board .pending { opacity: .5; }
.board th.pending small { display: block; font-size: 9px; text-transform: none; }
.board td.struck .pos-chip, .board td.struck small, .board td.struck { text-decoration: line-through; }
.board td.none { color: var(--theme-text-muted); }
.pos-chip { display: inline-block; min-width: 30px; padding: 1px 6px; border-radius: 10px; background: var(--theme-border-light); font-weight: 700; font-size: 12px; }
.pos-chip.gold { background: var(--gold); color: #3a2a00; }
.pos-chip.silver { background: var(--silver); color: #1e2a30; }
.pos-chip.bronze { background: var(--bronze); color: #2b1800; }
.board tr.top-1 td.c1, .board tr.top-1 td.c2 { background: color-mix(in srgb, var(--gold) 14%, var(--theme-card-bg)); }
.legend { display: flex; flex-direction: column; gap: 3px; margin-top: 10px; font-size: 12px; color: var(--theme-text-secondary); }

.race { padding: 0; overflow: hidden; }
.race-no { padding: 3px 8px; border-radius: 8px; background: var(--theme-badge-bg); color: var(--theme-badge-text); font-weight: 700; font-size: 13px; }
.race-title { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.race-title small { color: var(--theme-text-secondary); font-weight: 400; }
.race-body { padding: 4px 12px 14px; }

/* ---- install / update banner ------------------------------------------ */
.banner {
    display: flex; align-items: center; gap: 10px;
    margin: 0; padding: 10px 14px;
    background: var(--theme-info-bg); color: var(--theme-info-text);
    border-bottom: 1px solid var(--theme-border);
    font-size: 13.5px;
}
.banner-text { flex: 1; }
.banner-close { width: 36px; height: 36px; border: 0; border-radius: 50%; background: transparent; color: inherit; font-size: 24px; line-height: 1; cursor: pointer; }

/* ---- bottom sheet ------------------------------------------------------ */
.sheet-overlay { position: fixed; inset: 0; z-index: 9000; display: flex; align-items: flex-end; justify-content: center; background: var(--theme-modal-overlay); }
.sheet {
    width: 100%; max-width: 720px; max-height: 88vh;
    display: flex; flex-direction: column;
    background: var(--theme-modal-bg); color: var(--theme-text);
    border-radius: 18px 18px 0 0;
    animation: sheet-up .22s ease-out;
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: .6; } }
.sheet-header { display: flex; align-items: center; gap: 8px; padding: 14px 8px 8px 18px; border-bottom: 1px solid var(--theme-border-light); }
.sheet-header h2 { flex: 1; font-size: 17px; font-weight: 500; }
.sheet-close { width: 44px; height: 44px; border: 0; border-radius: 50%; background: transparent; color: var(--theme-text-secondary); font-size: 28px; line-height: 1; cursor: pointer; }
.sheet-body { overflow-y: auto; padding: 14px 16px calc(20px + var(--safe-bottom)); -webkit-overflow-scrolling: touch; }
.sheet-body > * + * { margin-top: 12px; }
.sheet-body .state { padding: 28px 8px; }

.info-card { padding: 12px 14px; border: 1px solid var(--theme-border-light); border-radius: 10px; background: var(--theme-bg-tertiary); }
.info-card + .info-card { margin-top: 8px; }
.info-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.info-card p { font-size: 14px; color: var(--theme-text-secondary); }
.info-list { margin: 0; padding-left: 20px; }
.info-list li + li { margin-top: 6px; }
.setup-row { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 2px 12px; padding: 6px 0; border-top: 1px solid var(--theme-border-light); font-size: 14px; }
.setup-row small { flex-basis: 100%; color: var(--theme-text-secondary); }
.risk { padding: 2px 8px; border-radius: 10px; color: #fff; font-size: 11px; font-weight: 500; white-space: nowrap; }
.risk-low { background: #43a047; }
.risk-medium { background: #fb8c00; }
.risk-high { background: #e53935; }

.prose h3 { margin: 16px 0 6px; font-size: 15px; }
.prose h3:first-child { margin-top: 0; }
.prose p, .prose li { font-size: 14px; color: var(--theme-text-secondary); }
.prose ul { margin: 6px 0; padding-left: 20px; }
.points-table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.points-table td { padding: 4px 18px 4px 0; border-bottom: 1px solid var(--theme-border-light); }
.points-table td:last-child { font-weight: 700; }

/* ---- tablets / desktop preview ----------------------------------------- */
@media (min-width: 720px) {
    .tabbar { left: 50%; right: auto; width: 720px; transform: translateX(-50%); border-left: 1px solid var(--theme-border); border-right: 1px solid var(--theme-border); }
    .sheet-overlay { align-items: center; }
    .sheet { border-radius: 18px; }
}
@media (max-width: 359px) {
    .stats { grid-template-columns: repeat(2, 1fr); }
    .tiles { grid-template-columns: 1fr; }
}
