:root {
  color-scheme: light;
  --ink: #10202b;
  --muted: #657786;
  --line: #d8e4ea;
  --panel: rgba(255, 255, 255, 0.92);
  --blue: #075985;
  --cyan: #0ea5e9;
  --teal: #0f766e;
  --gold: #d97706;
  --rose: #be123c;
  --bg: #eef8fb;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: var(--bg);
  background-image:
    linear-gradient(180deg, rgba(255, 255, 255, 0.86), rgba(238, 248, 251, 0.66) 48%, rgba(246, 248, 244, 0.92)),
    repeating-linear-gradient(90deg, rgba(7, 89, 133, 0.045) 0 1px, transparent 1px 82px);
}

body.modal-open {
  overflow: hidden;
}

button,
select,
input {
  min-width: 0;
  font: inherit;
}

.app-shell {
  width: min(100%, 720px);
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 14px calc(28px + env(safe-area-inset-bottom));
}

.app-header {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 14px;
  padding: 16px;
  border: 1px solid rgba(216, 228, 234, 0.96);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 12px 30px rgba(8, 47, 73, 0.08);
}

.eyebrow,
.section-title p {
  margin: 0 0 5px;
  color: #0f766e;
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 1.55rem;
  line-height: 1.05;
}

h2 {
  font-size: 1.16rem;
}

.header-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 7px;
}

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}

.segmented {
  display: grid;
  flex: 1;
  grid-template-columns: 1fr 1fr;
  min-height: 46px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.74);
}

.segmented button,
.icon-button {
  border: 0;
  color: var(--ink);
  background: transparent;
}

.segmented button {
  border-radius: 6px;
  font-weight: 800;
}

.segmented button.active {
  color: #fff;
  background: var(--blue);
  box-shadow: 0 8px 18px rgba(7, 89, 133, 0.24);
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.82);
}

.icon-button.active {
  color: #fff;
  border-color: var(--blue);
  background: var(--blue);
}

.icon-button svg {
  width: 22px;
  height: 22px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.summary-grid article,
.panel,
.performance-card {
  border: 1px solid rgba(216, 228, 234, 0.96);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 30px rgba(8, 47, 73, 0.08);
}

.summary-grid article {
  min-height: 92px;
  padding: 12px 10px;
}

.summary-grid span,
.summary-grid small,
.meta,
.place,
.sync-state {
  color: var(--muted);
}

.summary-grid span,
.summary-grid small {
  display: block;
  font-size: 0.75rem;
  font-weight: 700;
}

.summary-grid strong {
  display: block;
  margin: 6px 0 2px;
  color: var(--blue);
  font-size: 1.55rem;
}

.panel {
  margin-bottom: 14px;
  padding: 14px;
}

.section-title {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

select,
input {
  width: 100%;
  min-height: 38px;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fff;
  font-weight: 750;
}

input {
  padding: 0 12px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
}

.chart-controls {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(86px, 0.7fr) minmax(0, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}

.chart-title-row {
  align-items: flex-start;
}

.toggle-button {
  display: inline-flex;
  min-height: 32px;
  align-items: center;
  gap: 7px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: #fff;
  font-size: 0.74rem;
  font-weight: 850;
}

.toggle-button::before {
  width: 9px;
  height: 9px;
  border: 2px solid currentColor;
  border-radius: 999px;
  content: "";
}

.toggle-button.active {
  color: #8a4b00;
  border-color: rgba(217, 119, 6, 0.34);
  background: rgba(217, 119, 6, 0.12);
}

.chart-wrap {
  width: 100%;
  aspect-ratio: 1.55 / 1;
  min-height: 252px;
  border: 1px solid rgba(216, 228, 234, 0.82);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(245, 251, 250, 0.9)),
    repeating-linear-gradient(0deg, transparent 0 42px, rgba(15, 118, 110, 0.035) 42px 43px);
  overflow: hidden;
}

#progress-chart {
  width: 100%;
  height: 100%;
  min-height: 252px;
}

.chart-empty {
  display: grid;
  min-height: 252px;
  place-items: center;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
  text-align: center;
}

.chart-insights {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: 10px;
  border-top: 1px solid rgba(216, 228, 234, 0.86);
  border-bottom: 1px solid rgba(216, 228, 234, 0.64);
}

.chart-insight {
  min-width: 0;
  padding: 10px 10px 9px;
  border-left: 1px solid rgba(216, 228, 234, 0.74);
}

.chart-insight:first-child {
  border-left: 0;
}

.chart-insight span,
.chart-insight small {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-insight strong {
  display: block;
  overflow: hidden;
  margin: 4px 0 3px;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 900;
  line-height: 1.05;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chart-insight.positive strong {
  color: var(--teal);
}

.chart-insight.negative strong {
  color: var(--rose);
}

.chart-tooltip {
  min-width: 190px;
  padding: 10px;
  border: 1px solid rgba(216, 228, 234, 0.9);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 18px 40px rgba(8, 47, 73, 0.16);
}

.chart-tooltip-row {
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
}

.chart-tooltip-row + .chart-tooltip-row {
  margin-top: 9px;
  padding-top: 9px;
  border-top: 1px solid rgba(216, 228, 234, 0.8);
}

.chart-tooltip-row > span {
  width: 10px;
  height: 10px;
  margin-top: 3px;
  border-radius: 999px;
}

.chart-tooltip strong,
.chart-tooltip small,
.chart-tooltip em {
  display: block;
}

.chart-tooltip strong {
  font-size: 0.78rem;
  font-weight: 900;
}

.chart-tooltip small {
  margin-top: 3px;
  color: var(--blue);
  font-size: 0.86rem;
  font-weight: 900;
}

.chart-tooltip em {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.74rem;
  font-style: normal;
  font-weight: 750;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

.chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.chart-legend i {
  display: inline-block;
  width: 16px;
  height: 3px;
  border-radius: 999px;
}

.legend-primary {
  background: var(--teal);
}

.legend-compare {
  background: var(--rose);
}

.legend-training {
  background: var(--gold);
}

.add-swimmer-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 14px;
}

.primary-button,
.secondary-button,
.small-action-button,
.swimmer-row button {
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: var(--blue);
  font-weight: 850;
}

.primary-button {
  padding: 0 16px;
}

.secondary-button {
  width: 100%;
  margin-bottom: 14px;
  color: var(--blue);
  border: 1px solid var(--line);
  background: #fff;
}

.small-action-button {
  min-height: 34px;
  padding: 0 12px;
  color: var(--blue);
  border: 1px solid var(--line);
  background: #fff;
}

.settings-bottom-action {
  margin-top: 8px;
}

.swimmer-list {
  display: grid;
  gap: 9px;
}

.swimmer-row {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.swimmer-row strong,
.swimmer-row span {
  display: block;
}

.swimmer-row span,
.empty-note {
  color: var(--muted);
  font-size: 0.83rem;
  font-weight: 700;
}

.swimmer-row button {
  padding: 0 12px;
  background: var(--rose);
}

.card-list {
  display: grid;
  gap: 9px;
}

.performance-card {
  padding: 12px;
}

.performance-card > div:first-child,
.score-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.event {
  min-width: 0;
  color: var(--ink);
  font-weight: 850;
}

.event-source {
  display: inline-flex;
  min-width: 0;
  gap: 7px;
  align-items: center;
}

.source-badge {
  flex: 0 0 auto;
  padding: 3px 7px;
  border-radius: 999px;
  font-size: 0.65rem;
  font-weight: 900;
  line-height: 1;
  text-transform: uppercase;
}

.source-official {
  color: var(--blue);
  background: rgba(14, 165, 233, 0.13);
}

.source-training {
  color: #8a4b00;
  background: rgba(217, 119, 6, 0.16);
}

.time {
  color: var(--rose);
  font-size: 1.24rem;
  white-space: nowrap;
}

.meta,
.score-row {
  margin-top: 6px;
  font-size: 0.85rem;
}

.compare-record {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(216, 228, 234, 0.9);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.points {
  color: var(--blue);
  font-weight: 850;
}

.place {
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sync-state {
  margin: 18px 4px 0;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 700;
}

.loading {
  opacity: 0.7;
  pointer-events: none;
}

.hidden {
  display: none !important;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  width: 100%;
  min-width: 0;
  padding: 12px 10px max(12px, env(safe-area-inset-bottom));
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: rgba(16, 32, 43, 0.32);
}

.modal-panel {
  width: min(520px, 100%);
  max-width: 100%;
  min-width: 0;
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0 auto;
  padding: 14px;
  border: 1px solid rgba(216, 228, 234, 0.96);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 24px 60px rgba(8, 47, 73, 0.22);
}

.modal-header {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  min-width: 0;
  margin-bottom: 14px;
}

.modal-header > div {
  min-width: 0;
}

.modal-header p {
  margin: 0 0 5px;
  color: var(--gold);
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
}

.training-form {
  display: grid;
  min-width: 0;
  gap: 11px;
}

.training-form.is-submitting {
  opacity: 0.72;
}

.submit-button {
  display: inline-flex;
  width: 100%;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.button-spinner {
  display: none;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 999px;
  animation: spin 0.8s linear infinite;
}

.training-form.is-submitting .button-spinner {
  display: inline-block;
}

.training-form-grid {
  display: grid;
  grid-template-columns: 0.75fr 1fr;
  min-width: 0;
  gap: 10px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (min-width: 680px) {
  .app-shell {
    padding-top: 24px;
  }

  .chart-wrap {
    aspect-ratio: 1.9 / 1;
  }

  .card-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 420px) {
  .app-header {
    padding: 14px;
  }

  .header-actions {
    gap: 5px;
  }

  .icon-button {
    width: 36px;
    height: 36px;
  }

  .chart-controls,
  .add-swimmer-form,
  .training-form-grid {
    grid-template-columns: 1fr;
  }

  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal-panel {
    width: 100dvw;
    max-width: 100dvw;
    max-height: calc(100dvh - max(8px, env(safe-area-inset-top)));
    margin: 0;
    padding: 12px;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 8px 8px 0 0;
  }

  .chart-wrap,
  #progress-chart,
  .chart-empty {
    min-height: 286px;
  }

  .chart-insight {
    padding-right: 7px;
    padding-left: 7px;
  }
}

@media (max-width: 420px) and (max-height: 560px) {
  .modal-panel {
    max-height: 100dvh;
    padding: 10px 12px;
    border-radius: 0;
  }

  .modal-header {
    margin-bottom: 8px;
  }

  .modal-header p {
    margin-bottom: 2px;
  }

  .modal-header h2 {
    font-size: 1.05rem;
  }

  .training-form {
    gap: 8px;
  }

  .training-form-grid {
    gap: 8px;
  }

  .training-form label {
    gap: 4px;
  }

  .training-form select,
  .training-form input,
  .submit-button {
    min-height: 36px;
  }
}

@media (min-width: 340px) and (max-width: 420px) and (max-height: 560px) {
  .training-form-grid {
    grid-template-columns: 0.75fr 1fr;
  }
}
