/* Shared components: cards, buttons, forms, tables, badges, stat tiles,
   progress, modal, toast, skeleton, chart chrome. */

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-title { font-size: 14px; font-weight: 600; }
.card-sub { color: var(--text-3); font-size: 12px; margin-top: 2px; }
.card-actions { display: flex; gap: 6px; align-items: center; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }
.card-foot { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-2); }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px;
  border-radius: var(--radius); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text);
  font-size: 13px; font-weight: 500; white-space: nowrap; text-decoration: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--text-on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }
.btn-danger { border-color: transparent; background: transparent; color: var(--critical-ink); }
.btn-danger:hover { background: var(--critical-soft); }
.btn-strava { background: var(--strava); border-color: var(--strava); color: #fff; }
.btn-strava:hover { background: #e34402; border-color: #e34402; }
.btn-sm { height: 28px; padding: 0 9px; font-size: 12px; }
.btn-sm svg { width: 14px; height: 14px; }
.btn-icon { width: 32px; padding: 0; flex-shrink: 0; }
.btn-icon.btn-sm { width: 28px; }
.btn-block { width: 100%; }
.btn .spin { animation: spin 0.9s linear infinite; }

/* ---- Segmented control ---- */
.seg { display: inline-flex; padding: 2px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); }
.seg button {
  height: 26px; padding: 0 10px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text-2); font-size: 12px; font-weight: 500;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25); }

/* ---- Forms ---- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .label { font-size: 12px; font-weight: 500; color: var(--text-2); }
.input {
  height: 36px; width: 100%; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font-size: 14px;
}
textarea.input { height: auto; min-height: 84px; padding: 8px 10px; resize: vertical; line-height: 1.45; }
select.input {
  appearance: none; padding-right: 30px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888e99' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
.input-sm { height: 32px; width: auto; font-size: 13px; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: var(--focus-ring); }
.input:disabled { background: var(--surface-2); color: var(--text-3); }
.input::placeholder { color: var(--text-3); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 16px; }
.form-actions .status { margin-right: auto; font-size: 13px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
.alert { padding: 10px 12px; border-radius: var(--radius); font-size: 13px; border: 1px solid transparent; }
.alert-error { background: var(--critical-soft); border-color: rgba(208, 59, 59, 0.35); }
.alert-info { background: var(--accent-soft); border-color: rgba(57, 135, 229, 0.3); }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th {
  text-align: left; padding: 8px 10px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--text-3); border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; color: var(--text-2); }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table td.primary { color: var(--text); font-weight: 500; }
.table .cell-sub { display: block; color: var(--text-3); font-size: 12px; font-weight: 400; }
.table tbody tr.link { cursor: pointer; }
.table tbody tr.link:hover { background: var(--surface-2); }
.table th.sortable { cursor: pointer; user-select: none; }
.table th.sortable:hover { color: var(--text); }
.table th[aria-sort] { color: var(--text); }
.table th[aria-sort]::after { content: ' ↑'; }
.table th[aria-sort="descending"]::after { content: ' ↓'; }
.table td.actions { text-align: right; white-space: nowrap; }
.table-compact td { padding: 7px 10px; }

/* ---- Badges ---- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  height: 20px; padding: 0 7px; border-radius: 999px;
  font-size: 11px; font-weight: 600; white-space: nowrap;
  background: var(--surface-3); color: var(--text-2);
}
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-good { background: var(--good-soft); color: var(--good-ink); }
.badge-warning { background: var(--warning-soft); color: var(--warning-ink); }
.badge-serious { background: var(--serious-soft); color: var(--serious-ink); }
.badge-critical { background: var(--critical-soft); color: var(--critical-ink); }
.badge svg { width: 11px; height: 11px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ---- Stat tile ---- */
.stat { padding: 14px 16px; }
.stat-label { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--text-2); }
.stat-value { margin-top: 6px; font-size: 28px; font-weight: 600; line-height: 1.1; letter-spacing: -0.02em; }
.stat-value small { font-size: 13px; color: var(--text-2); font-weight: 500; margin-left: 4px; letter-spacing: 0; }
.stat-delta { display: flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 12px; color: var(--text-2); }
.stat-delta.good { color: var(--good-ink); }
.stat-delta.bad { color: var(--critical-ink); }
.stat-spark { margin-top: 10px; height: 28px; }
.stat-spark svg { width: 100%; height: 28px; }
.stat-foot { margin-top: 8px; font-size: 12px; color: var(--text-3); }
.info-tip { position: relative; display: inline-flex; color: var(--text-3); cursor: help; }
.info-tip svg { width: 14px; height: 14px; }
.info-tip:hover { color: var(--text-2); }
.info-tip .tip {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 6; width: 240px;
  padding: 8px 10px; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid var(--border-strong); box-shadow: var(--shadow-pop);
  color: var(--text-2); font-size: 12px; line-height: 1.45; font-weight: 400;
  opacity: 0; pointer-events: none; transition: opacity 0.12s;
}
.info-tip:hover .tip, .info-tip:focus-within .tip { opacity: 1; }

/* ---- Progress and meter ---- */
.progress { height: 6px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.progress > span { display: block; height: 100%; width: 0; border-radius: 999px; background: var(--accent); transition: width 0.4s ease; }
.progress.is-done > span { background: var(--good); }
.progress.is-error > span { background: var(--critical); }
.progress.indeterminate > span { width: 30% !important; animation: slide 1.2s ease-in-out infinite; }
.meter { height: 8px; border-radius: 999px; background: var(--accent-soft); overflow: hidden; }
.meter > span { display: block; height: 100%; width: 0; border-radius: 999px; background: var(--accent); transition: width 0.4s ease; }

/* ---- Sync panel (floating, any page) ---- */
.sync-status { font-size: 12px; color: var(--text-3); white-space: nowrap; }
.sync-panel {
  position: fixed; right: 24px; bottom: 24px; z-index: 30;
  width: 380px; max-width: calc(100vw - 32px);
  box-shadow: var(--shadow-pop);
}
.sync-panel-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; font-size: 13px; font-weight: 600; }
.sync-panel-head .pct { color: var(--text-2); font-weight: 500; font-variant-numeric: tabular-nums; }
.sync-panel-detail { display: flex; justify-content: space-between; gap: 10px; margin-top: 8px; font-size: 12px; color: var(--text-2); }
.sync-panel-detail .count { flex-shrink: 0; font-variant-numeric: tabular-nums; color: var(--text-3); }
.sync-panel-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }

/* ---- Modal ---- */
.modal { position: fixed; inset: 0; z-index: 50; display: none; align-items: center; justify-content: center; padding: 16px; background: rgba(0, 0, 0, 0.55); }
.modal.open { display: flex; }
.modal-dialog {
  width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto;
  padding: 20px; outline: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-pop);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.modal-head h2 { font-size: 16px; }

/* ---- Toast ---- */
.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 60;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--text); color: var(--bg); font-size: 13px; font-weight: 500;
  box-shadow: var(--shadow-pop); transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--critical); color: #fff; }

/* ---- Loading states ---- */
.skeleton { position: relative; overflow: hidden; background: var(--surface-2); border-radius: var(--radius-sm); color: transparent !important; }
.skeleton::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.06), transparent);
  animation: shimmer 1.4s infinite;
}
.is-loading { opacity: 0.5; pointer-events: none; transition: opacity 0.15s; }
.empty { padding: 28px 16px; text-align: center; color: var(--text-2); font-size: 13px; }
.empty strong { display: block; color: var(--text); font-weight: 600; margin-bottom: 4px; }
.empty .btn { margin-top: 12px; }

/* ---- Key/value rows ---- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; font-size: 13px; }
.kv dt { color: var(--text-2); }
.kv dd { color: var(--text); font-weight: 500; text-align: right; }
.list-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: 0; }
.list-row:first-child { padding-top: 0; }

/* ---- Chart chrome ---- */
.chart { position: relative; min-width: 0; }
.chart svg { width: 100%; display: block; overflow: visible; }
.chart svg:focus-visible { box-shadow: none; outline: 1px solid var(--accent); outline-offset: 4px; border-radius: 4px; }
.chart text { font-family: var(--font); }
.chart .axis-text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis-line { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .end-label { fill: var(--text-2); font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums; }
.chart .bar-label { fill: var(--text-2); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .cat-label { fill: var(--text-2); font-size: 12px; }
.chart .cat-sub { fill: var(--text-3); font-size: 11px; }
.chart .panel-title { fill: var(--text-2); font-size: 12px; font-weight: 600; }
.chart .band-label { fill: var(--text-3); font-size: 10px; }
.chart .crosshair { stroke: var(--text-3); stroke-width: 1; shape-rendering: crispEdges; pointer-events: none; }
.chart .marker-line { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 2 3; shape-rendering: crispEdges; }
.chart .marker-label { fill: var(--text-2); font-size: 11px; font-weight: 600; }
.chart .x-band-label { fill: var(--text-2); font-size: 10px; font-weight: 600; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round; }
.chart .hover-dot { stroke: var(--surface); stroke-width: 2; pointer-events: none; }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .bar-hit { fill: transparent; }
.chart .bar.is-hover { filter: brightness(1.15); }
.chart-tip {
  position: absolute; z-index: 5; pointer-events: none; min-width: 150px;
  padding: 8px 10px; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid var(--border-strong); box-shadow: var(--shadow-pop);
  font-size: 12px; opacity: 0; transition: opacity 0.1s;
}
.chart-tip.show { opacity: 1; }
.chart-tip .tip-title { color: var(--text-2); margin-bottom: 6px; font-weight: 500; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.chart-tip .tip-key { width: 12px; height: 2px; border-radius: 1px; flex-shrink: 0; }
.chart-tip .tip-key.square { width: 10px; height: 10px; border-radius: 2px; }
.chart-tip .tip-label { color: var(--text-2); flex: 1; }
.chart-tip .tip-value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.chart-tip .tip-note { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); color: var(--text-2); }
.chart-toolbar { margin-bottom: 8px; }
.chart-toolbar > .btn { margin-left: auto; }
.chart-toolbar:has(.legend.hidden):has(.btn.hidden) { display: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-2); }
.legend button { display: inline-flex; align-items: center; gap: 6px; padding: 2px 0; border: 0; background: none; color: inherit; font-size: inherit; }
.legend button[aria-pressed="false"] { opacity: 0.45; text-decoration: line-through; }
.legend .key { width: 14px; height: 2px; border-radius: 1px; flex-shrink: 0; }
.legend .key.square { width: 10px; height: 10px; border-radius: 2px; }
.legend .key.dot { width: 8px; height: 8px; border-radius: 50%; }
.legend .key.dashed { background: repeating-linear-gradient(90deg, currentColor 0 4px, transparent 4px 7px); }
.chart-table { max-height: 260px; overflow: auto; margin-top: 12px; border: 1px solid var(--border); border-radius: var(--radius); }
.chart-table .table { font-size: 12px; }
.chart-table .table th { position: sticky; top: 0; background: var(--surface); }

/* ---- Phones and touch screens ---- */
/* Card actions drop below the title when they would squeeze it under 160 px.
   A segmented control always takes its own line. */
@media (max-width: 560px) {
  .card-head { flex-wrap: wrap; }
  .card-head > :first-child { flex: 1 1 160px; }
  .card-actions:has(.seg) { flex-basis: 100%; justify-content: flex-start; }
  .sync-panel { right: 16px; bottom: 16px; }
  .toast { width: max-content; max-width: calc(100vw - 32px); }
}
/* A finger needs a bigger target than a mouse pointer: controls are at least
   36 px (chart legend toggles 32 px). Mouse layouts do not change. */
@media (pointer: coarse) {
  .btn, .btn-sm, .input-sm, .seg button { height: 36px; }
  .btn-icon, .btn-icon.btn-sm { width: 36px; }
  .check { min-height: 36px; }
  .legend button { min-height: 32px; }
  .info-tip { padding: 11px; margin: -11px; }
  /* Padding grows the target and the negative margin keeps the text in place. */
  .table td a:not(.btn), .page-head .sub a { display: inline-block; padding-block: 10px; margin-block: -10px; }
}

@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
@keyframes slide { 0% { transform: translateX(-120%); } 100% { transform: translateX(430%); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* Weekly digest controls on the profile page */
.digest-actions { flex-wrap: wrap; justify-content: flex-end; }
.digest-hint { padding: 0 0 10px; }
.digest-body {
  margin: 0 0 12px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  color: var(--text-2);
}
