:root {
    color-scheme: light;
    --bg: #f0f4f8;
    --surface: #fff;
    --surface-alt: #f4f7fb;
    --text: #142438;
    --subtle: #52637a;
    --line: #d8e2ed;
    --navy: #112842;
    --brand: #d5f465;
    --brand-hover: #c5e84b;
    --brand-ink: #172a17;
    --blue: #21599e;
    --selected: #e5f0ff;
    --mark: #b0d932;
    --warn: #705018;
    --warn-bg: #fff7e4;
    --error: #a72a38;
    --error-bg: #fff0f2;
    --shadow: 0 8px 30px #203d6010;
    --rest: #8792a5;
    --free: #3c84db;
    --back: #1daba9;
    --breast: #e18a37;
    --fly: #9169d6;
    --drill: #d6549f;
    --mixed: #4a5bc4;
    --medley: #976424;
    --unknown: #77838b;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0f1926; --surface: #162437; --surface-alt: #1a2c41; --text: #e7edf7;
    --subtle: #a8b8ce; --line: #34465d; --navy: #0b1727; --blue: #9bc8ff;
    --selected: #263e5e; --warn: #f5d390; --warn-bg: #302821; --error: #ffabba;
    --error-bg: #3a2532; --shadow: 0 8px 30px #0002;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg: #0f1926; --surface: #162437; --surface-alt: #1a2c41; --text: #e7edf7;
        --subtle: #a8b8ce; --line: #34465d; --navy: #0b1727; --blue: #9bc8ff;
        --selected: #263e5e; --warn: #f5d390; --warn-bg: #302821; --error: #ffabba;
        --error-bg: #3a2532; --shadow: 0 8px 30px #0002;
    }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 1rem/1.55 system-ui, -apple-system, sans-serif; }
button, select, input { font: inherit; }
a { color: var(--blue); }
button, select, input[type="range"], input[type="checkbox"], input[type="radio"] { cursor: pointer; }
button:disabled, select:disabled, input:disabled { cursor: default; opacity: .52; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 3px; }
h1[tabindex="-1"]:focus { outline: none; }
input[type="checkbox"], input[type="radio"], input[type="range"] { accent-color: var(--blue); }
input[type="checkbox"], input[type="radio"] { width: 1.05rem; height: 1.05rem; margin: 0; flex: 0 0 auto; }
h1, h2, p { margin: 0; }
h1, h2 { line-height: 1.12; letter-spacing: -.03em; }
h1 { font-size: clamp(2rem, 4vw, 3.35rem); font-weight: 760; }
h2 { font-size: 1.25rem; font-weight: 740; }
p { color: var(--subtle); }
hr { border: 0; border-top: 1px solid var(--line); margin: 1.4rem 0; }

.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.startup-screen { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; }
.startup-screen img { display: block; }
html:lang(en) .startup-screen [lang="it"], html:lang(it) .startup-screen [lang="en"] { display: none; }
.topbar { background: var(--navy); color: #fff; }
.topbar-inner { max-width: 83rem; margin: auto; padding: 1rem clamp(1rem, 4vw, 2.75rem); display: flex; align-items: center; justify-content: space-between; gap: .8rem; }
.brand { display: flex; align-items: center; gap: .8rem; font-size: 1.27rem; font-weight: 750; letter-spacing: -.03em; line-height: 1.2; }
.brand small { display: block; color: #b9cbe2; font-size: .81rem; letter-spacing: 0; font-weight: 500; margin-top: .19rem; }
.brand-icon { display: block; flex-shrink: 0; width: 2.65rem; height: 2.65rem; }
.settings { display: flex; flex-wrap: wrap; justify-content: end; gap: .45rem; }
.settings select { color: #eef5ff; background-color: #ffffff0d; border-color: #ffffff36; font-size: .88rem; }
.settings option { background-color: var(--navy); color: #fff; }

select, input:not([type]), input[type="number"], input[type="datetime-local"], output {
    display: block; color: var(--text); background: var(--surface); border: 1px solid var(--line);
    border-radius: .45rem; padding: .53rem .72rem; min-width: 0;
}
select { appearance: none; padding-right: 2rem; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23899bb0' stroke-width='2' d='m4 6 4 4 4-4'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right .65rem center; }
output { background: var(--surface-alt); }
.button, .text-button, .icon-button {
    display: inline-flex; align-items: center; justify-content: center; gap: .38rem;
    color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: .48rem;
    padding: .56rem .86rem; min-height: 2.5rem; font-size: .9rem; font-weight: 670;
}
.button-icon { display: inline-flex; flex: 0 0 1.15rem; width: 1.15rem; height: 1.15rem; pointer-events: none; }
.button-icon svg { display: block; width: 100%; height: 100%; }
.button:hover:not(:disabled), .icon-button:hover:not(:disabled) { background: var(--surface-alt); border-color: var(--blue); }
.button.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
.button.primary:hover:not(:disabled) { background: var(--brand-hover); border-color: transparent; }
.button.compact { font-size: .86rem; min-height: 2.2rem; padding: .4rem .63rem; }
.text-button { background: none; border: 0; color: var(--blue); padding: 0; min-height: 0; }
.text-button:hover:not(:disabled) { text-decoration: underline; }
.icon-button { min-height: 2rem; min-width: 2rem; padding: 0; font-size: 1.4rem; line-height: 1; }

.shell { max-width: 83rem; margin: 0 auto; padding: 2.25rem clamp(1rem, 4vw, 2.75rem) 4rem; }
.welcome { max-width: 66rem; margin: clamp(1rem, 5vw, 4rem) auto; }
.welcome-title p { margin-top: .8rem; font-size: 1.08rem; }
.intake-grid { display: grid; grid-template-columns: minmax(0, 2.35fr) minmax(13rem, 1fr); gap: 1rem; margin-top: 2.25rem; }
.intake-card, .intake-notes, .panel { background: var(--surface); border: 1px solid var(--line); border-radius: 1rem; box-shadow: var(--shadow); }
.intake-card { padding: 1.05rem; }
.intake-notes { padding: clamp(1rem, 2.5vw, 1.65rem); }
.intake-notes p { margin-top: .55rem; }
.upload-zone { display: grid; justify-items: center; align-content: center; gap: .55rem; position: relative; min-height: 19.8rem; padding: 1.5rem; text-align: center; border: 2px dashed var(--line); border-radius: .7rem; background: var(--surface-alt); }
.upload-zone:hover, .upload-zone:focus-within { border-color: var(--blue); }
.upload-zone.loading { opacity: .7; }
.upload-zone input[type="file"] { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.upload-zone .button { margin-top: .65rem; }
.upload-icon { width: 3.75rem; height: 3.75rem; border-radius: .85rem; margin-bottom: .5rem; display: grid; place-items: center; color: var(--blue); background: var(--selected); }
.upload-icon svg { width: 2.15rem; height: 2.15rem; }

.activity-heading { display: flex; align-items: end; justify-content: space-between; flex-wrap: wrap; gap: 1.1rem; margin-bottom: 1.35rem; }
.activity-heading h1 { margin-top: .55rem; font-size: clamp(1.9rem, 4vw, 2.8rem); }
.activity-meta { display: flex; align-items: center; gap: .4rem .7rem; flex-wrap: wrap; margin-top: .45rem; font-size: .9rem; color: var(--subtle); }
.activity-meta span::before { content: "·"; margin-right: .7rem; }
.filename { overflow-wrap: anywhere; }
.activity-actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.status { background: var(--surface-alt); padding: .34rem .66rem; border-radius: 2rem; font-size: .85rem; font-weight: 680; border: 1px solid var(--line); }
.status.edited { background: var(--brand); color: var(--brand-ink); border-color: transparent; }

.panel { padding: 1.1rem; margin-bottom: 1rem; min-width: 0; }
.section-heading { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; column-gap: 1rem; row-gap: .55rem; margin-bottom: .95rem; }
.section-heading p { margin-top: .3rem; font-size: .9rem; }
.check-line { display: flex; gap: .48rem; align-items: center; font-size: .9rem; cursor: pointer; color: var(--subtle); }
.metric-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .65rem; }
.metric { display: flex; flex-direction: column; gap: .1rem; padding: .78rem .9rem; border-radius: .65rem; background: var(--surface-alt); min-height: 5rem; }
.metric span { font-size: .87rem; color: var(--subtle); }
.metric strong { line-height: 1.2; font-size: clamp(1.1rem, 2vw, 1.5rem); font-variant-numeric: tabular-nums; letter-spacing: -.03em; }
.metric small { font-size: .8rem; color: var(--subtle); margin-top: .2rem; }
.focus-metric { background: #173555; color: #f7fbff; }
.focus-metric span, .focus-metric small { color: #c7d9ee; }
.focus-metric strong { color: var(--brand); }
.notice { border-radius: .62rem; padding: .65rem .88rem; margin: .7rem 0 1rem; font-size: .9rem; }
.notice.error { color: var(--error); background: var(--error-bg); }
.notice.warning { color: var(--warn); background: var(--warn-bg); }
.notice summary { width: fit-content; cursor: pointer; font-weight: 690; }
.notice ul { padding-left: 1.1rem; margin: .5rem 0 .1rem; }

.timeline-scroll, .table-scroll { overflow: auto; max-width: 100%; }
.timeline-content { min-width: 34rem; }
.timeline-svg { width: 100%; height: 4.55rem; display: block; }
.timeline-segment { cursor: pointer; outline: none; }
.timeline-segment .segment-block { fill: var(--stroke); stroke: var(--surface); stroke-width: 1px; vector-effect: non-scaling-stroke; }
.timeline-segment:hover .segment-block { filter: brightness(1.16); }
.timeline-segment.selected .segment-block, .timeline-segment:focus-visible .segment-block { stroke: var(--text); stroke-width: 3px; }
.changed-mark { fill: var(--brand); pointer-events: none; }
.gap-stripe { stroke: var(--line); stroke-width: 3px; }
.timeline-ticks { position: relative; height: 1.5rem; color: var(--subtle); font-size: .83rem; font-variant-numeric: tabular-nums; }
.timeline-ticks span { position: absolute; transform: translateX(-50%); }
.timeline-ticks .first { transform: none; }
.timeline-ticks .last { transform: translateX(-100%); }
.zoom { display: flex; gap: .45rem; align-items: center; color: var(--subtle); font-size: .89rem; }
.zoom input { width: clamp(5.7rem, 13vw, 9rem); }
.zoom output { border: none; padding: .1rem .3rem; font-variant-numeric: tabular-nums; }
.legend { display: flex; flex-wrap: wrap; gap: .45rem .85rem; margin-top: .7rem; color: var(--subtle); font-size: .86rem; }
.legend span, .stroke-cell { display: flex; align-items: center; gap: .35rem; }
.swatch { display: inline-block; width: .68rem; height: .68rem; border-radius: .2rem; background: var(--stroke); flex: 0 0 auto; }
.stroke-freestyle { --stroke: var(--free); }
.stroke-backstroke { --stroke: var(--back); }
.stroke-breaststroke { --stroke: var(--breast); }
.stroke-butterfly { --stroke: var(--fly); }
.stroke-drill { --stroke: var(--drill); }
.stroke-mixed { --stroke: var(--mixed); }
.stroke-individualmedley { --stroke: var(--medley); }
.stroke-unknown { --stroke: var(--unknown); }
.stroke-rest { --stroke: var(--rest); }
.swatch-gap { background: repeating-linear-gradient(135deg, var(--line) 0 2px, transparent 2px 4px); }
.swatch-changed { background: var(--brand); border: 1px solid #799937; }

.filters, .operations, .history-tools, .presets { display: flex; gap: .45rem; flex-wrap: wrap; }
.filters label { display: flex; align-items: center; gap: .35rem; font-size: .86rem; color: var(--subtle); }
.filters select, .stroke-cell select { font-size: .86rem; padding-top: .32rem; padding-bottom: .32rem; }
.operations { background: var(--surface-alt); border: 1px solid var(--line); border-radius: .65rem; padding: .55rem; margin-bottom: .65rem; }
.history-tools { margin-left: auto; }
.table-scroll { max-height: 33rem; }
table { border-spacing: 0; width: 100%; min-width: 55rem; font-size: .875rem; text-align: left; }
td, th { padding: .57rem .55rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: var(--surface); z-index: 1; position: sticky; top: 0; color: var(--subtle); font-weight: 650; }
tr:hover td { background: var(--surface-alt); }
tr.selected td { background: var(--selected); }
tr.changed td:first-child { box-shadow: inset 3px 0 0 var(--mark); }
.index-cell input { vertical-align: text-bottom; margin-right: .35rem; }
.number { font-variant-numeric: tabular-nums; }
.pagination { display: flex; align-items: center; justify-content: end; gap: .55rem; margin-top: .85rem; font-size: .9rem; }
.empty { text-align: center; padding: 1rem; }

html:has(dialog:modal) { overflow: hidden; }
dialog { color: var(--text); background: var(--surface); padding: 0; border-radius: .95rem; border: 1px solid var(--line); box-shadow: 0 1rem 3rem #0d172c50; width: min(34rem, calc(100% - 1.5rem)); overflow: hidden; }
dialog::backdrop { background: #07152ab0; }
.dialog-frame { padding: 1.2rem; max-height: calc(100dvh - 2.5rem); overflow-y: auto; overscroll-behavior-y: contain; }
.dialog-heading, .dialog-actions { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.dialog-heading { border-bottom: 1px solid var(--line); padding-bottom: .7rem; margin-bottom: 1rem; }
.dialog-actions { justify-content: end; margin-top: 1.2rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .7rem; }
.form-grid label, .field { display: grid; gap: .3rem; color: var(--subtle); font-size: .9rem; }
.field { margin-top: .65rem; }
.form-note { grid-column: 1 / -1; font-size: .9rem; }
.preview { margin-top: .85rem; padding: .55rem .8rem; background: var(--selected); color: var(--text); border-radius: .45rem; }
.start-note { margin-top: .6rem; }
.presets { margin-bottom: .75rem; }
.choice-list { display: grid; gap: .7rem; padding: 0; border: 0; margin: 0; }
.choice-list label { display: flex; align-items: start; gap: .55rem; padding: .7rem; border: 1px solid var(--line); border-radius: .5rem; }
.choice-list label input { margin-top: .22rem; }
.choice-list label small { display: block; font-size: .88rem; color: var(--subtle); }
.choice-list label:has(input:checked) { background: var(--selected); }
.export-grid { display: grid; grid-template-columns: 1.15fr 1fr 1fr; gap: .35rem; border-bottom: 1px solid var(--line); padding-bottom: .6rem; margin-bottom: .6rem; }
.export-warnings { padding-left: 1.8rem; }

@media (max-width: 60rem) {
    .metric-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .history-tools { margin-left: 0; }
}

@media (max-width: 42rem) {
    .intake-grid { grid-template-columns: 1fr; }
    .welcome { margin-top: .6rem; }
    .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .panel { padding: .8rem; }
    .topbar-inner { padding-block: .75rem; }
    .brand small { display: none; }
    .filters { justify-content: start; }
}

.endurance-history { display: flex; justify-content: end; margin-bottom: .9rem; }
.activity-plots { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 1rem; }
.activity-route { grid-row: span 2; }
.route-plot, .profile-plot { display: block; width: 100%; }
.route-plot { background: var(--surface-alt); border-radius: .6rem; }
.route-plot polyline, .profile-plot polyline {
    fill: none; stroke: var(--blue); stroke-width: 2px; vector-effect: non-scaling-stroke; stroke-linecap: round; stroke-linejoin: round;
}
.route-plot circle { fill: var(--mark); stroke: var(--text); stroke-width: 1.5px; }
.profile-chart { display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: .5rem; }
.profile-plot { height: 7.5rem; }
.profile-guides { fill: none; stroke: var(--line); stroke-width: 1px; vector-effect: non-scaling-stroke; }
.profile-values, .profile-time { display: flex; font-size: .8rem; color: var(--subtle); font-variant-numeric: tabular-nums; }
.profile-values { flex-direction: column; justify-content: space-between; align-items: end; }
.profile-time { grid-column: 2; justify-content: space-between; }
.endurance-laps table { min-width: 32rem; }
.endurance-laps th[scope="row"] { position: static; }
.device-results { display: grid; list-style: none; padding: 0; margin: .4rem 0 0; gap: .3rem; max-height: 12rem; overflow: auto; }
.device-results button { width: 100%; justify-content: space-between; text-align: start; }
.device-option { display: flex; align-items: center; gap: .38rem; }
.device-results small { white-space: nowrap; }
.device-results small, .device-help { color: var(--subtle); }
.device-help { font-size: .875rem; margin: .65rem 0; }
.export-grid > span { overflow-wrap: anywhere; }

@media (max-width: 42rem) {
    .activity-plots { grid-template-columns: minmax(0, 1fr); }
    .activity-route { grid-row: auto; }
}
