:root {
    --primary:        #0057FF;
    --primary-dark:   #003FCC;
    --primary-light:  #CCE0FF;
    --primary-text:   #FFFFFF;
    --accent:         #38B6FF;
    --on-surface:     #212121;
    --on-surface-med: #757575;
    --on-surface-dis: #BDBDBD;
    --surface:        #FFFFFF;
    --background:     #F5F5F5;
    --divider:        rgba(0,0,0,0.12);
    --error:          #D32F2F;
    --error-light:    #FFEBEE;
    --success:        #388E3C;
    --success-light:  #E8F5E9;
    --warning:        #F57C00;
    --shadow-1: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    --shadow-2: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
    --shadow-4: 0 6px 20px rgba(0,0,0,0.19), 0 8px 17px rgba(0,0,0,0.20);
    --radius:    4px;
    --radius-lg: 8px;
    --transition:      200ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    color: var(--on-surface);
    background: var(--background);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.app-bar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 56px;
    background: var(--primary);
    color: var(--primary-text);
    box-shadow: var(--shadow-2);
    z-index: 100;
}

.app-bar__content {
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 8px 0 16px;
    gap: 12px;
}

.app-bar__title {
    flex: 1;
    font-size: 20px;
    font-weight: 500;
    letter-spacing: 0.005em;
    white-space: nowrap;
}

.app-bar__actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.icon-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--primary-text);
    cursor: pointer;
    overflow: hidden;
    transition: background var(--transition);
}

.icon-btn:hover  { background: rgba(255,255,255,0.15); }
.icon-btn:active { background: rgba(255,255,255,0.25); }

.icon-btn--danger       { color: var(--error); }
.icon-btn--danger:hover { background: var(--error-light); }

.icon-btn .material-icons { font-size: 22px; position: relative; z-index: 1; }

.ripple {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255,255,255,0.35) 0%, transparent 70%);
    transform: scale(0);
    pointer-events: none;
}

@keyframes ripple-anim {
    0%   { transform: scale(0);   opacity: 1; }
    100% { transform: scale(2.5); opacity: 0; }
}

.main-content {
    margin-top: 56px;
    padding: 20px 16px 32px;
    min-height: calc(100vh - 56px);
}

.container {
    max-width: 960px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.status-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
}

.status-chip {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 12px 6px 8px;
    background: var(--surface);
    border: 1px solid var(--divider);
    border-radius: 20px;
    box-shadow: var(--shadow-1);
    white-space: nowrap;
    transition: box-shadow var(--transition);
}

.status-chip__label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--on-surface-med);
}

.status-chip__sep {
    width: 1px;
    height: 10px;
    background: var(--divider);
    flex-shrink: 0;
}

.status-chip__value {
    font-size: 12px;
    font-weight: 500;
    color: var(--on-surface);
}

.status-chip__value--online  { color: var(--success); }
.status-chip__value--offline { color: var(--error); }

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status-dot--connected {
    background: var(--success);
    box-shadow: 0 0 0 2px rgba(56,142,60,0.3);
    animation: pulse-green 2s infinite;
}

.status-dot--disconnected {
    background: var(--error);
    box-shadow: 0 0 0 2px rgba(211,47,47,0.3);
}

@keyframes pulse-green {
    0%, 100% { box-shadow: 0 0 0 2px rgba(56,142,60,0.3); }
    50%       { box-shadow: 0 0 0 5px rgba(56,142,60,0.1); }
}

.card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-1);
    overflow: hidden;
    transition: box-shadow var(--transition);
}

.card:hover { box-shadow: var(--shadow-2); }

.table-wrapper { overflow-x: auto; }

.table {
    width: 100%;
    border-collapse: collapse;
}

.table__th {
    padding: 12px 16px;
    text-align: left;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--on-surface-med);
    border-bottom: 1px solid var(--divider);
    white-space: nowrap;
    user-select: none;
}

.table__th--center { text-align: center; }

.table__th-icon {
    font-size: 14px;
    vertical-align: middle;
    margin-right: 4px;
    opacity: 0.7;
}

.table__row {
    transition: background var(--transition);
    border-bottom: 1px solid var(--divider);
}

.table__row:last-child    { border-bottom: none; }
.table__row:hover         { background: rgba(0,87,255,0.04); }
.table__row[data-athlete] { cursor: pointer; }
.row--highlighted         { background: rgba(0,87,255,0.13) !important; }

.table__td {
    padding: 14px 16px;
    font-size: 14px;
    color: var(--on-surface);
    vertical-align: middle;
}

.table__td--mono   { font-family: 'Roboto Mono', monospace; font-size: 13px; color: var(--on-surface-med); }
.table__td--center { text-align: center; }

.athlete-cell {
    display: flex;
    align-items: center;
    gap: 10px;
}

.athlete-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.athlete-name { display: block; font-weight: 500; font-size: 14px; }

.athlete-uuid {
    display: block;
    font-size: 11px;
    font-family: 'Roboto Mono', monospace;
    color: var(--on-surface-med);
}

.time-value {
    font-family: 'Roboto Mono', monospace;
    font-size: 15px;
    font-weight: 500;
    color: var(--primary);
}

.time-pending { color: var(--on-surface-med); }

.gap-value {
    font-family: 'Roboto Mono', monospace;
    font-size: 13px;
    font-weight: 500;
    color: var(--on-surface-med);
}

.gap-value--leader { font-size: 18px; color: var(--primary); line-height: 1; }
.gap-none          { font-size: 13px; color: var(--on-surface-dis); }

.badge {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    gap: 3px;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.04em;
}

.badge--dnf { background: var(--error-light); color: var(--error); }
.badge--top { background: #EBF2FF; color: var(--primary); }

.tabs {
    display: flex;
    align-items: stretch;
    position: relative;
    border-bottom: 1px solid var(--divider);
    overflow: hidden;
}

.tab {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex: 1;
    height: 48px;
    padding: 0 12px;
    border: none;
    background: transparent;
    color: var(--on-surface-med);
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: pointer;
    transition: color var(--transition), background var(--transition);
    user-select: none;
    white-space: nowrap;
}

.tab:hover   { background: rgba(0,87,255,0.06); }
.tab--active { color: var(--primary); }
.tab__icon   { font-size: 20px; }
.tab__label  { font-size: 13px; }

.tab-indicator {
    position: absolute;
    bottom: 0;
    height: 2px;
    background: var(--primary);
    transition: left var(--transition), width var(--transition);
    border-radius: 2px 2px 0 0;
}

.tab-panel         { display: none; }
.tab-panel--active { display: block; }

.lap-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--primary);
    background: rgba(0,87,255,0.07);
    border-bottom: 1px solid rgba(0,87,255,0.15);
}

.lap-header .material-icons { font-size: 14px; }

.lap-count {
    margin-left: auto;
    font-size: 11px;
    font-weight: 400;
    letter-spacing: 0.04em;
    color: var(--on-surface-med);
    text-transform: none;
}

.lap-header-row       { cursor: default; }
.lap-header-row:hover { background: transparent !important; }
.lap-header-row > td  { padding: 0; border: none; }

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 64px 24px;
    gap: 8px;
    text-align: center;
}

.empty-state__icon { font-size: 56px; color: var(--on-surface-dis); }

.empty-state__text {
    font-size: 16px;
    font-weight: 500;
    color: var(--on-surface-med);
    margin-top: 8px;
}

.empty-state__sub { font-size: 13px; color: var(--on-surface-dis); }

@keyframes spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

.dialog {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.dialog--open    { display: flex; animation: dialog-fade-in var(--transition-slow) both; }
.dialog--closing { animation: dialog-fade-out 220ms both; }

@keyframes dialog-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

@keyframes dialog-fade-out {
    from { opacity: 1; }
    to   { opacity: 0; }
}

.dialog__scrim {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
}

.dialog__surface {
    position: relative;
    z-index: 1;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-4);
    width: 100%;
    max-width: 400px;
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: dialog-slide-in var(--transition-slow) both;
}

.dialog--closing .dialog__surface { animation: dialog-slide-out 220ms both; }
.dialog__surface--wide            { max-width: 560px; }

@keyframes dialog-slide-in {
    from { transform: scale(0.9) translateY(16px); opacity: 0; }
    to   { transform: scale(1)   translateY(0);    opacity: 1; }
}

@keyframes dialog-slide-out {
    from { transform: scale(1)   translateY(0);    opacity: 1; }
    to   { transform: scale(0.9) translateY(16px); opacity: 0; }
}

.dialog__header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 24px 12px;
    border-bottom: 1px solid var(--divider);
    flex-shrink: 0;
}

.dialog__header-icon { font-size: 22px; color: var(--primary); flex-shrink: 0; }

.dialog__title {
    font-size: 18px;
    font-weight: 500;
    color: var(--on-surface);
    letter-spacing: 0.005em;
}

.dialog__content {
    padding: 20px 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.dialog__content--scroll {
    overflow-y: auto;
    overscroll-behavior: contain;
}

.dialog__actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    padding: 8px 12px;
    border-top: 1px solid var(--divider);
    flex-shrink: 0;
}

.dialog__actions .btn--danger { margin-right: auto; }

.dialog__alert-title {
    padding: 24px 24px 12px;
    font-size: 20px;
    font-weight: 500;
    color: var(--on-surface);
    letter-spacing: 0.005em;
    line-height: 1.4;
}

.dialog__alert-body {
    padding: 0 24px 20px;
    font-size: 14px;
    color: var(--on-surface-med);
    line-height: 1.65;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.field__label {
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--on-surface-med);
}

.field__input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.field__icon {
    position: absolute;
    left: 12px;
    font-size: 20px;
    color: var(--on-surface-med);
    pointer-events: none;
    transition: color var(--transition);
}

.field__input {
    width: 100%;
    height: 44px;
    padding: 0 12px 0 44px;
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    color: var(--on-surface);
    background: var(--background);
    border: 1px solid var(--divider);
    border-radius: var(--radius);
    outline: none;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.field__input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(0,87,255,0.2);
    background: #fff;
}

.field__input-wrap:focus-within .field__icon { color: var(--primary); }
.field__input--mono { font-family: 'Roboto Mono', monospace; font-size: 13px; }
.field__input[readonly] { opacity: 0.6; cursor: not-allowed; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 36px;
    padding: 0 16px;
    border: none;
    border-radius: var(--radius);
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 0.04em;
    cursor: pointer;
    text-transform: uppercase;
    transition: background var(--transition), box-shadow var(--transition);
    white-space: nowrap;
    user-select: none;
}

.btn__icon { font-size: 18px; }

.btn--text        { background: transparent; color: var(--primary); }
.btn--text:hover  { background: rgba(0,87,255,0.08); }
.btn--text:active { background: rgba(0,87,255,0.16); }

.btn--danger       { color: var(--error) !important; }
.btn--danger:hover { background: var(--error-light) !important; }

.btn--contained {
    background: var(--primary);
    color: #fff;
    box-shadow: var(--shadow-1);
}

.btn--contained:hover  { background: var(--primary-dark); box-shadow: var(--shadow-2); }
.btn--contained:active { box-shadow: var(--shadow-1); }

.btn--outlined {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}
.btn--outlined:hover  { background: rgba(0,87,255,0.08); }
.btn--outlined:active { background: rgba(0,87,255,0.16); }

.btn--full { width: 100%; }

.section-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
}

.section-divider::before,
.section-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--divider);
}

.section-divider__label {
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--on-surface-med);
    white-space: nowrap;
}

.assoc-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.assoc-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: var(--radius);
    background: var(--background);
    border: 1px solid var(--divider);
    transition: background var(--transition);
}

.assoc-item:hover { background: #ECEFF1; }

.assoc-item__icon { font-size: 22px; color: var(--primary); flex-shrink: 0; }

.assoc-item__info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.assoc-item__name {
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.assoc-item__uuid {
    font-size: 11px;
    font-family: 'Roboto Mono', monospace;
    color: var(--on-surface-med);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.assoc-item .icon-btn                              { color: var(--on-surface-med); flex-shrink: 0; }
.assoc-item .icon-btn:not(.icon-btn--danger):hover { color: var(--on-surface); background: var(--divider); }
.assoc-item .icon-btn--danger                      { color: var(--error); }
.assoc-item .icon-btn--danger:hover                { color: var(--error); background: var(--error-light); }

.empty-msg {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--on-surface-med);
    font-size: 14px;
}

.empty-msg .material-icons { font-size: 18px; color: var(--on-surface-dis); }

.snackbar {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 280px;
    max-width: 480px;
    padding: 12px 16px;
    border-radius: var(--radius-lg);
    background: #323232;
    color: #fff;
    font-size: 14px;
    font-weight: 400;
    box-shadow: var(--shadow-2);
    z-index: 400;
    opacity: 0;
    transition: transform 300ms cubic-bezier(0.4, 0, 0.2, 1),
                opacity 300ms cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
}

.snackbar--open    { transform: translateX(-50%) translateY(0); opacity: 1; }
.snackbar--success { background: #2E7D32; }
.snackbar--error   { background: #C62828; }
.snackbar__icon    { font-size: 20px; opacity: 0.9; flex-shrink: 0; }
.snackbar__label   { flex: 1; }

[data-theme="dark"] {
    --on-surface:     #E0E0E0;
    --on-surface-med: #9E9E9E;
    --on-surface-dis: #616161;
    --surface:        #1E1E1E;
    --background:     #121212;
    --divider:        rgba(255,255,255,0.10);
    --primary-light:  #002699;
    --error-light:    rgba(211,47,47,0.18);
    --success-light:  rgba(56,142,60,0.18);
}

[data-theme="dark"] body              { background: var(--background); color: var(--on-surface); }
[data-theme="dark"] .card            { background: var(--surface); }
[data-theme="dark"] .card:hover      { box-shadow: var(--shadow-2); }
[data-theme="dark"] .table__row:hover { background: rgba(0,87,255,0.07); }
[data-theme="dark"] .time-value      { color: #80AAFF; }
[data-theme="dark"] .tab:hover       { background: rgba(0,87,255,0.1); }
[data-theme="dark"] .lap-header      { background: rgba(0,87,255,0.1); }
[data-theme="dark"] .lap-header-row > td .lap-header { background: rgba(0,87,255,0.1); }
[data-theme="dark"] .row--highlighted { background: rgba(0,87,255,0.22) !important; }
[data-theme="dark"] .btn--text         { color: #80AAFF; }
[data-theme="dark"] .btn--text:hover   { background: rgba(0,87,255,0.12); }
[data-theme="dark"] .btn--outlined     { color: #80AAFF; border-color: #80AAFF; }
[data-theme="dark"] .btn--outlined:hover { background: rgba(0,87,255,0.12); }
[data-theme="dark"] .gap-value       { color: #9E9E9E; }
[data-theme="dark"] .gap-value--leader { color: #80AAFF; }
[data-theme="dark"] .badge--top { background: #0A1F4D; color: #80AAFF; }
[data-theme="dark"] .dialog__surface { background: #1E1E1E; }

[data-theme="dark"] .field__input {
    background: #2A2A2A;
    color: var(--on-surface);
    border-color: rgba(255,255,255,0.15);
}

[data-theme="dark"] .field__input:focus {
    background: #2E2E2E;
    border-color: var(--primary);
}

[data-theme="dark"] .assoc-item {
    background: #2A2A2A;
    border-color: rgba(255,255,255,0.08);
}

[data-theme="dark"] .assoc-item:hover             { background: #333333; }
[data-theme="dark"] .assoc-item .icon-btn         { color: var(--on-surface-med); }
[data-theme="dark"] .assoc-item .icon-btn--danger { color: var(--error); }

[data-theme="dark"] .status-chip {
    background: #272727;
    border-color: rgba(255,255,255,0.10);
}

[data-theme="dark"] .status-chip__value--online  { color: #66BB6A; }
[data-theme="dark"] .status-chip__value--offline { color: #EF5350; }

@media (max-width: 600px) and (orientation: portrait) {
    .col-partenza      { display: none; }
    .table__th--center { text-align: right; }
    .table__td--center { text-align: right; }
    .col-tempo         { text-align: center; }
}

@media (max-width: 600px) {
    .main-content    { padding: 12px 8px 24px; }
    .table__th,
    .table__td       { padding: 10px 12px; }

    .dialog__surface {
        max-width: 100%;
        margin: 0 12px;
        max-height: calc(100vh - 32px);
    }

    .dialog__header  { padding: 16px 16px 10px; }
    .dialog__content { padding: 16px; }
    .dialog__actions { padding: 6px 8px; }

    .snackbar {
        left: 8px;
        right: 8px;
        bottom: 16px;
        transform: translateY(80px);
        min-width: 0;
        max-width: none;
    }

    .snackbar--open { transform: translateY(0); }
    .app-bar__title { font-size: 17px; }
}
