:root {
  --bg: #0f1115;
  --card: #1a1d24;
  --card-2: #232732;
  --text: #e8eaed;
  --muted: #9aa3b2;
  --accent: #fc4c02; /* Strava orange */
  --accent-2: #ff7a3d;
  --brand-color: var(--text);
  --border: #2a2f3a;
  --ok: #34d399;
  --err: #f87171;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

a { color: var(--accent-2); }
a:visited { color: #ffad85; }
a:hover { color: #ffd0ba; }
a:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

header {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
}
header .header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 20px; }
#userBox { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; }
.user-avatar {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex-shrink: 0;
  overflow: hidden; border: 1px solid var(--accent); border-radius: 50%;
  background: var(--card-2); color: var(--text); font-size: 12px; font-weight: 600;
}
.user-avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.units-control, .units-options { display: flex; align-items: center; gap: 8px; }
.units-control { color: var(--muted); font-size: 13px; }
.units-options { gap: 5px; }
.units-options button {
  padding: 4px 6px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
}
.units-options button[aria-pressed="true"] { background: var(--card-2); color: var(--text); }
.units-options button:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
header h1 { font-size: 18px; margin: 0; }
.brand-heading { grid-column: 2; }
.brand-banner {
  display: block;
  width: clamp(140px, 24vw, 280px);
  aspect-ratio: 996 / 295;
  background-color: var(--brand-color);
  -webkit-mask: url("/brand-banner.svg") center / contain no-repeat;
  mask: url("/brand-banner.svg") center / contain no-repeat;
  filter: drop-shadow(0 1px 3px rgb(232 234 237 / 35%));
}

main { padding: 24px; max-width: 1200px; margin: 0 auto; }
footer { padding: 24px; color: var(--muted); text-align: center; }

.hidden { display: none !important; }

.tabs {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px; width: 100%; margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.tabs .units-control { flex-shrink: 0; margin-left: auto; }
.tab {
  padding: 10px 16px; border: 0; border-bottom: 2px solid transparent;
  background: transparent; color: var(--muted); cursor: pointer; font-size: 14px; white-space: nowrap;
}
.tab-icon { display: inline-flex; align-items: center; justify-content: center; padding: 10px 12px; }
.tab-icon svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tab-link { text-decoration: none; }
.tab:hover, .tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { min-width: 0; }
.section-heading { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; }
.section-heading h2 { margin-bottom: 8px; }
.segment-actions { margin-top: 12px; }
.btn.danger { color: var(--err); }
.btn.danger:hover { border-color: var(--err); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

@media (max-width: 700px) {
  header { gap: 12px; padding: 12px 16px; }
  header .header-actions { flex-wrap: wrap; justify-content: flex-end; gap: 8px 12px; }
  .section-heading { flex-direction: column; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}
.card h2 { margin-top: 0; font-size: 16px; }
.card p { color: var(--muted); margin-top: 0; }

.span-2 { grid-column: 1 / -1; }

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}
.original-file-options > div { min-width: 0; }
.original-file-options { display: grid; grid-template-columns: 1fr; gap: 24px; }
#configurationPanel > .card > h2 { font-size: 20px; }
.original-file-options h3 { font-size: 18px; }
.original-file-options + p { margin-top: 24px; margin-bottom: 0; }
@media (max-width: 800px) {
  .settings-grid { grid-template-columns: 1fr; }
}
.settings-grid h3 { margin-top: 0; font-size: 14px; }

.stack { display: flex; flex-direction: column; gap: 8px; }
.stack input { width: 100%; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row label { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.row label input[type="number"] { width: 80px; }
details summary {
  display: flex; align-items: center; gap: 8px;
  cursor: pointer; font-size: 13px; list-style: none;
}
details summary::-webkit-details-marker { display: none; }
details summary::before { content: "\25B8"; font-size: 16px; }
details[open] > summary::before { content: "\25BE"; }
details summary h2 { margin: 0; }

.btn {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  font-size: 14px;
}
.btn:hover { border-color: var(--accent); }
.btn.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn.btn-icon, #mappingsTable .btn.btn-icon { display: inline-flex; align-items: center; justify-content: center; padding: 6px; line-height: 0; }
.btn-icon svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }

input, select {
  background: var(--card-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
}
input[type="file"] { padding: 6px; max-width: 100%; }

.segment-id-list { display: grid; gap: 12px; }
.segment-id-row { display: grid; grid-template-columns: 32px minmax(160px, 0.7fr) minmax(0, 2fr); gap: 8px; align-items: start; }
.segment-id-row > label { padding-top: 9px; }
.segment-details { min-width: 0; padding: 7px 0; }
.segment-details > a { display: inline-block; font-weight: 600; overflow-wrap: anywhere; }
.segment-stats { display: flex; flex-wrap: wrap; gap: 6px 18px; margin: 7px 0 0; }
.segment-stats div { display: flex; gap: 5px; font-size: 12px; }
.segment-stats dt { color: var(--muted); }
.segment-stats dd { margin: 0; color: var(--text); font-variant-numeric: tabular-nums; }
@media (max-width: 600px) {
  .segment-id-row { grid-template-columns: 24px minmax(0, 1fr); }
  .segment-details { grid-column: 2; padding-top: 0; }
}

.template-editor { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.template-editor h3 { margin: 0 0 8px; font-size: 14px; }
.template-editor textarea {
  width: 100%; min-height: 110px; resize: vertical;
  background: var(--card-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font: 14px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.template-preview {
  margin: 0; min-height: 110px; padding: 8px 10px;
  border: 1px dashed var(--border); border-radius: 8px;
  white-space: pre-wrap; overflow-wrap: anywhere;
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.template-editor .muted { font-size: 12px; margin: 6px 0 0; }
.template-editor p.status-error { color: var(--err); font-size: 13px; margin: 6px 0 0; }
.template-variables { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 4px 16px; margin-top: 4px; }
.template-variables h4 { grid-column: 1 / -1; margin: 10px 0 2px; font-size: 13px; color: var(--muted); }
.template-variable { display: flex; align-items: baseline; gap: 8px; font-size: 12px; color: var(--muted); }
.template-variable button {
  flex-shrink: 0; padding: 2px 6px; border: 1px solid var(--border); border-radius: 4px;
  background: var(--card-2); color: var(--accent-2); cursor: pointer;
  font: 12px ui-monospace, SFMono-Regular, Consolas, monospace;
}
.template-variable button:hover { border-color: var(--accent); }
@media (max-width: 800px) {
  .template-editor { grid-template-columns: 1fr; }
}

.mapping-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 80px auto;
  grid-template-areas:
    "key key label label"
    "gear gear priority save";
  gap: 8px;
  margin-bottom: 12px;
}
.mapping-form > * { min-width: 0; }
.mapping-form [name="sensor_key"] { grid-area: key; }
.mapping-form [name="sensor_label"] { grid-area: label; }
.mapping-form [name="gear_id"] { grid-area: gear; }
.mapping-form [name="priority"] { grid-area: priority; }
.mapping-form button { grid-area: save; }
@media (min-width: 900px) {
  .mapping-form {
    grid-template-columns: minmax(0, 2fr) minmax(0, 1.5fr) minmax(0, 1.5fr) 80px auto;
    grid-template-areas: "key label gear priority save";
  }
}
@media (max-width: 700px) {
  .mapping-form {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "key" "label" "gear" "priority" "save";
  }
}

table {
  width: 100%; border-collapse: collapse; margin-top: 8px;
  font-size: 13px;
}
th, td {
  padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th { color: var(--muted); font-weight: 600; }
.table-sort {
  padding: 0; border: 0; background: transparent; color: inherit;
  font: inherit; font-weight: inherit; cursor: pointer;
}
.table-sort:hover { color: var(--text); }
.table-sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
tr:last-child td { border-bottom: none; }
.nowrap, .table-actions { white-space: nowrap; }
.table-actions { text-align: right; }
.compact-table th, .compact-table td { padding: 8px 10px; }
.compact-table tbody tr:hover td { background: rgb(255 255 255 / 2%); }
.compact-table code { overflow-wrap: anywhere; }
/* Spare width is shared by the two text columns; the rest stay content-sized. */
#activitiesTable :is(th, td):is(:nth-child(1), :nth-child(4)),
#mappingsTable :is(th, td):is(:nth-child(3), :nth-child(4)) { width: 1%; white-space: nowrap; }
#activitiesTable :is(th, td):is(:nth-child(2), :nth-child(3)),
#mappingsTable :is(th, td):is(:nth-child(1), :nth-child(2)) { width: 50%; }
#activitiesTable td[colspan], #mappingsTable td[colspan] { width: auto; white-space: normal; }
#mappingsTable th:last-child, #mappingsTable td:last-child { text-align: right; }
#mappingsTable td:last-child [role="status"] { display: block; white-space: normal; }
#mappingsTable .btn { padding: 4px 10px; font-size: 13px; }
#mappingsTable td input, #mappingsTable td select { width: 100%; min-width: 0; margin-top: 4px; }
#mappingsTable td select[name="edit_gear"] { width: auto; min-width: 140px; }
#mappingsTable td input[type="number"] { width: 64px; margin-top: 0; }
#mappingsTable td:nth-child(2) select { margin-top: 0; }
@media (max-width: 600px) {
  #gearPanel .card { padding: 14px; overflow-x: auto; }
  .compact-table th, .compact-table td { padding: 6px 4px; }
  .compact-table .table-actions, #mappingsTable td:last-child { white-space: normal; }
  #mappingsTable td select[name="edit_gear"] { min-width: 0; width: 100%; }
  .compact-table .table-actions .btn, #mappingsTable td:last-child .btn:not(.btn-icon) { display: block; width: 100%; margin-bottom: 4px; padding: 4px 8px; white-space: nowrap; }
}

.sensor-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  margin: 6px 0; background: var(--card-2);
}
.sensor-row code { color: var(--accent-2); }

.original-activities-actions { margin-top: 12px; }
.original-activities-actions .muted { font-size: 13px; }

.status-ok { color: var(--ok); }
.status-error, .status-no_mapping, .status-no_activity { color: var(--err); }

.muted { color: var(--muted); }

.webhook-group + .webhook-group { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.webhook-group h3 { margin: 0 0 6px; font-size: 14px; }

.row label.switch { position: relative; gap: 8px; color: var(--text); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; }
.switch-track {
  position: relative; width: 36px; height: 20px; border-radius: 10px;
  background: var(--card-2); border: 1px solid var(--border); transition: background 0.15s;
}
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
  border-radius: 50%; background: var(--muted); transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(16px); background: white; }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.switch input:disabled + .switch-track { opacity: 0.55; }
