/* Tokens. Colors are referenced by role. Each holds a light and a dark value,
   and color-scheme picks one: the system setting by default, or the choice made
   with the theme switch, which sets data-theme on the root element. */
:root {
  color-scheme: light dark;
  --page: light-dark(#f9f9f7, #0d0d0d);
  --surface: light-dark(#fcfcfb, #1a1a19);
  --text-primary: light-dark(#0b0b0b, #ffffff);
  --text-secondary: light-dark(#52514e, #c3c2b7);
  --text-muted: #898781;
  --grid: light-dark(#e1e0d9, #2c2c2a);
  --axis: light-dark(#c3c2b7, #383835);
  --border: light-dark(rgba(11, 11, 11, 0.1), rgba(255, 255, 255, 0.1));
  --wash: light-dark(rgba(11, 11, 11, 0.05), rgba(255, 255, 255, 0.06));
  --shadow: light-dark(rgba(11, 11, 11, 0.18), rgba(0, 0, 0, 0.6));
  --good: light-dark(#006300, #0ca30c);
  --bad: light-dark(#d03b3b, #e66767);
  /* Marks a value outside of its usual range. Never the only cue: a label goes with it. */
  --warning: #fab219;
  /* Categorical slots. The order is fixed: it keeps neighbors apart for color-blind readers. */
  /* Heart rate zones, the same everywhere a heart rate appears. The yellow is
     darkened in the light theme, a bright one is unreadable on white. */
  --zone-light: light-dark(#2a78d6, #4d94ea);
  --zone-moderate: light-dark(#1e8a3e, #34b85a);
  --zone-vigorous: light-dark(#b07d00, #e8b923);
  --zone-peak: light-dark(#cf3434, #ef6a6a);
  /* How far a tile of the today dashboard has reached its goal. */
  --progress: light-dark(rgba(30, 138, 62, 0.16), rgba(52, 184, 90, 0.22));
  --series-1: light-dark(#2a78d6, #3987e5);
  --series-2: light-dark(#eb6834, #d95926);
  --series-3: light-dark(#1baf7a, #199e70);
  --series-4: light-dark(#eda100, #c98500);
  --series-5: light-dark(#e87ba4, #d55181);
  --series-6: #008300;
  --series-7: light-dark(#4a3aa7, #9085e9);
  --series-8: light-dark(#e34948, #e66767);
  /* The first slot toned down, for columns that stayed below the goal. */
  --series-1-soft: light-dark(#86b6ef, #184f95);
}

:root[data-theme="light"] {
  color-scheme: light;
}

:root[data-theme="dark"] {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

a {
  color: inherit;
}

/* --- Layout ------------------------------------------------------------ */

/* The bar stays in view while the page scrolls. It stacks above the chart
   tooltips (z-index 1), which would cover it otherwise. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 24px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand {
  font-weight: 600;
}

nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

nav a {
  padding: 4px 10px;
  border-radius: 6px;
  color: var(--text-secondary);
  text-decoration: none;
}

nav a:hover {
  background: var(--wash);
}

nav a[aria-current="page"] {
  background: var(--wash);
  color: var(--text-primary);
  font-weight: 600;
}

.theme {
  margin-left: auto;
}

.settings-link {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  color: var(--text-secondary);
}

.settings-link:hover,
.settings-link[aria-current="page"] {
  background: var(--wash);
  color: var(--text-primary);
}

form.logout {
  margin: 0 0 0 8px;
}

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  min-height: 32px;
  margin-bottom: 16px;
}

#view {
  transition: opacity 0.15s;
}

#view.loading {
  opacity: 0.5;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

.card {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.card > header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  margin-bottom: 8px;
}

.card > header .hint {
  font-size: 12px;
}

.section-title {
  margin: 24px 0 8px;
  color: var(--text-secondary);
}

.section-title:first-child {
  margin-top: 0;
}

.stack {
  display: grid;
  gap: 16px;
  align-content: start;
  min-width: 0;
}

.hint,
.empty {
  color: var(--text-secondary);
}

p.hint {
  margin: 8px 0 0;
  font-size: 12px;
}

.empty {
  padding: 24px 0;
}

/* --- Controls ---------------------------------------------------------- */

.segmented {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.segmented button {
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--text-secondary);
  font: inherit;
  cursor: pointer;
}

.segmented button:hover {
  background: var(--wash);
}

.segmented button[aria-pressed="true"] {
  background: var(--wash);
  color: var(--text-primary);
  font-weight: 600;
}

label.field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
}

select,
input[type="date"],
button.plain {
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-primary);
  font: inherit;
}

button.plain {
  cursor: pointer;
}

button.plain:hover {
  background: var(--wash);
}

/* --- Stat tiles -------------------------------------------------------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

.tile .label {
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}

.tile .value {
  font-size: 28px;
  font-weight: 600;
  line-height: 1.2;
}

.tile .unit {
  margin-left: 4px;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 400;
}

.tile .delta {
  color: var(--text-secondary);
  font-size: 12px;
}

.delta .good {
  color: var(--good);
}

.delta .bad {
  color: var(--bad);
}

.tile .chart {
  margin-top: 8px;
}

a.tile {
  display: block;
  text-decoration: none;
}

a.tile:hover {
  border-color: var(--axis);
}

a.tile:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
}

.tile .head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 4px 8px;
}

/* In a narrow tile the verdict moves below the label instead of squeezing it. */
.tile .head .label {
  overflow-wrap: break-word;
}

/* A short verdict next to a value: an icon in a status color, the words in text color. */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--wash);
  color: var(--text-secondary);
  font-size: 12px;
  white-space: nowrap;
}

.chip i {
  font-size: 10px;
  font-style: normal;
}

.chip.good i {
  color: var(--good);
}

.chip.warning i {
  color: var(--warning);
}

/* --- Figures ------------------------------------------------------------ */

.hero {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 12px;
  margin-bottom: 12px;
}

.hero .value {
  font-size: 48px;
  font-weight: 600;
  line-height: 1.1;
}

.hero .hint {
  flex-basis: 100%;
}

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 12px 16px;
  margin: 0 0 16px;
}

.facts dt {
  color: var(--text-secondary);
  font-size: 12px;
}

.facts dd {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.facts dd small {
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 400;
}

.meter {
  margin-bottom: 14px;
}

.meter .head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0 12px;
}

.meter .head span {
  color: var(--text-secondary);
  font-size: 12px;
}

/* The track is a lighter step of the fill, so the whole bar reads as one state. */
.meter .track {
  position: relative;
  height: 12px;
  margin-top: 4px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--meter-color) 18%, transparent);
}

.meter .fill {
  height: 100%;
  border-radius: 6px;
  background: var(--meter-color);
}

.meter .typical {
  position: absolute;
  top: -3px;
  width: 4px;
  height: 18px;
  margin-left: -2px;
  border: 1px solid var(--surface);
  border-radius: 2px;
  background: var(--text-primary);
}

.hours {
  display: flex;
  gap: 4px;
  margin-top: 12px;
}

.hours div {
  flex: 1;
  color: var(--text-muted);
  font-size: 11px;
  text-align: center;
}

.hours i {
  display: block;
  height: 28px;
  margin-bottom: 2px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--series-1) 18%, transparent);
  color: #ffffff;
  font-style: normal;
  line-height: 28px;
}

.hours .active i {
  background: var(--series-1);
}

/* --- Charts ------------------------------------------------------------ */

.chart {
  position: relative;
}

.chart svg {
  display: block;
  width: 100%;
  overflow: visible;
}

.chart svg:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
}

.chart text {
  fill: var(--text-muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin-bottom: 4px;
  color: var(--text-secondary);
  font-size: 12px;
}

.legend span,
.tooltip .row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.swatch.line {
  height: 2px;
  width: 14px;
  border-radius: 1px;
}

.swatch.dot {
  border-radius: 50%;
}

.swatch.band {
  width: 14px;
  opacity: 0.3;
}

.tooltip {
  position: absolute;
  z-index: 1;
  min-width: 120px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  font-size: 12px;
  pointer-events: none;
  white-space: nowrap;
}

.tooltip .title {
  margin-bottom: 4px;
  color: var(--text-secondary);
}

.tooltip strong {
  font-variant-numeric: tabular-nums;
}

.tooltip .name {
  color: var(--text-secondary);
}

details.table-view {
  margin-top: 8px;
  color: var(--text-secondary);
  font-size: 12px;
}

details.table-view summary {
  cursor: pointer;
}

details.table-view .scroll {
  max-height: 260px;
  margin-top: 8px;
  overflow: auto;
}

/* --- Tables ------------------------------------------------------------ */

table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

th,
td {
  padding: 5px 10px 5px 0;
  border-bottom: 1px solid var(--grid);
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--text-secondary);
  font-weight: 500;
}

th.num,
td.num {
  text-align: right;
}

td a {
  text-decoration-color: var(--axis);
  text-underline-offset: 3px;
}

.table-scroll {
  overflow-x: auto;
}

/* Login and consent: one narrow card in the middle of the page. */
main.auth {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 16px;
}

.auth-form {
  display: grid;
  gap: 12px;
  width: min(100%, 360px);
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.auth-form h1 {
  margin: 0;
  font-size: 1.25rem;
}

.auth-form label {
  display: grid;
  gap: 4px;
  color: var(--text-secondary);
}

.auth-form input[type="password"] {
  font: inherit;
  padding: 8px 10px;
  color: var(--text-primary);
  background: var(--page);
  border: 1px solid var(--axis);
  border-radius: 8px;
}

.auth-form button {
  font: inherit;
  padding: 8px 14px;
  color: var(--page);
  background: var(--series-1);
  border: 1px solid var(--series-1);
  border-radius: 8px;
  cursor: pointer;
}

.auth-form button.secondary {
  color: var(--text-primary);
  background: transparent;
  border-color: var(--axis);
}

.auth-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.auth-form .error {
  color: var(--bad);
}

/* Generated summary of a night. */
.summary p {
  margin: 0 0 8px;
  line-height: 1.55;
  max-width: 70ch;
}

button.link {
  padding: 0;
  border: none;
  background: none;
  color: var(--series-1);
  font: inherit;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.6;
  cursor: wait;
}

/* Shape of a GPS route, without a map. */
/* Route, heart rate and elevation of a session: on a wide screen the route
   stands next to both charts, so all three stay in view while the pointer
   moves over one of them. */
@media (min-width: 960px) {
  .grid.course:has(> :nth-child(3)) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid.course > :first-child:nth-last-child(3) {
    grid-row: span 2;
  }
}

/* Heart rate values and zone names in the color of their zone. */
.pulse {
  font-weight: 600;
}

.zone-dot {
  display: inline-block;
  width: 0.6em;
  height: 0.6em;
  margin-right: 0.45em;
  border-radius: 50%;
  vertical-align: 0.05em;
}

.route-zones > span {
  margin-right: 0.9em;
  white-space: nowrap;
}

/* Timeline: a compact dashboard of today, then events, newest first. */
.today {
  display: grid;
  grid-template-columns: minmax(160px, 220px) 1fr;
  gap: 16px;
  align-items: center;
  margin-bottom: 24px;
}

.steps-ring {
  position: relative;
}

.steps-ring .ring {
  display: block;
  width: 100%;
}

.ring-text {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
}

.ring-text .label {
  color: var(--text-secondary);
}

.ring-text .value {
  font-size: 1.8rem;
  font-weight: 700;
}

.today-figures {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
}

/* A tile fills up from the left as far as its goal is reached. */
.today-tile {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(to right, var(--progress) var(--fill, 0%), var(--surface) var(--fill, 0%));
}

a.today-tile,
a.steps-link {
  color: inherit;
  text-decoration: none;
}

a.today-tile:hover,
a.today-tile:focus-visible {
  border-color: var(--axis);
}

a.steps-link {
  display: block;
}

.today-tile .label {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.today-tile .value {
  font-size: 1.25rem;
  font-weight: 600;
}

@media (max-width: 640px) {
  /* A compact bar, it stays on screen: brand and theme switch in the first
     row, the views below in one row that scrolls sideways. */
  .topbar {
    gap: 8px 12px;
    padding: 8px 16px;
  }

  nav {
    order: 1;
    flex: 1 0 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }

  nav a {
    flex: none;
  }

  .today {
    grid-template-columns: 1fr;
  }

  .steps-ring {
    max-width: 160px;
    margin: 0 auto;
  }
}

.timeline {
  display: grid;
  gap: 16px;
}

.event-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-weight: 600;
}

.event-icon {
  width: 20px;
  height: 20px;
  color: var(--series-1);
}

.event h4 {
  margin: 16px 0 6px;
  font-size: 0.95rem;
}

.event-detail {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--wash);
}

.event-detail .big {
  font-size: 1.8rem;
  font-weight: 700;
}

a.session-link {
  display: grid;
  gap: 2px;
  color: inherit;
  text-decoration: none;
}

.summary-title {
  margin: 0 0 6px;
  font-size: 1.05rem;
  font-weight: 600;
}

.stage-bar {
  display: flex;
  height: 8px;
  max-width: 420px;
  margin: 8px 0;
  overflow: hidden;
  border-radius: 4px;
}

details.summary-block > summary {
  cursor: pointer;
  margin-bottom: 6px;
}

details.summary-block > summary:not(.summary-title) {
  color: var(--text-secondary);
}

.timeline-more {
  margin: 16px 0;
  text-align: center;
}

/* A route on a static map: tiles placed by percentage, the route on top. */
.route-map {
  position: relative;
  overflow: hidden;
  aspect-ratio: 1;
  border-radius: 10px;
  background: var(--wash);
}

/* On a touch screen the page keeps scrolling over the map. Fingers move and
   zoom it only in fullscreen, the buttons work everywhere. */
.route-map {
  cursor: grab;
  user-select: none;
}

.route-map.dragging {
  cursor: grabbing;
}

.route-tiles,
.route-map svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.route-tiles img {
  position: absolute;
  max-width: none;
  pointer-events: none;
}

.map-controls {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.map-button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-primary);
  font: inherit;
  font-size: 18px;
  line-height: 1;
  box-shadow: 0 1px 4px var(--shadow);
  cursor: pointer;
}

.map-button:hover {
  background: var(--page);
}

/* Fullscreen: the map takes most of the screen, heart rate and elevation
   stand next to it, on a phone below it. Above the chat button (z-index 3). */
body.no-scroll {
  overflow: hidden;
}

.grid.course.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 4;
  grid-template-columns: minmax(0, 2fr) minmax(320px, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 0;
  padding: 12px;
  overflow: auto;
  background: var(--page);
}

.grid.course.fullscreen > :first-child {
  grid-row: span 2;
  display: flex;
  flex-direction: column;
}

.grid.course.fullscreen > .card {
  min-height: 0;
  overflow: auto;
}

.fullscreen .route-view {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}

.fullscreen .route-map {
  flex: 1;
  min-height: 0;
  aspect-ratio: auto;
  touch-action: none;
}

.fullscreen .map-help,
.fullscreen .link-hint {
  display: none;
}

@media (max-width: 760px) {
  .grid.course.fullscreen {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 70dvh auto auto;
    align-content: start;
    padding: 8px;
  }

  .grid.course.fullscreen > :first-child {
    grid-row: auto;
  }

  .grid.course.fullscreen > .card {
    min-height: auto;
    overflow: visible;
  }
}

/* --- Chat ---------------------------------------------------------------- */

/* A round button in the lower left corner, the panel opens above it. Both stack
   above the top bar (z-index 2). */
.chat-toggle {
  position: fixed;
  bottom: 16px;
  left: 16px;
  z-index: 3;
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--text-primary);
  color: var(--page);
  box-shadow: 0 2px 10px var(--shadow);
  cursor: pointer;
}

.chat-toggle:hover,
.chat-toggle[aria-expanded="true"] {
  background: var(--text-secondary);
}

/* The button must not cover the end of a page. */
body.has-chat main {
  padding-bottom: 84px;
}

.chat-panel {
  position: fixed;
  bottom: 80px;
  left: 16px;
  z-index: 3;
  display: flex;
  flex-direction: column;
  width: min(480px, calc(100vw - 32px));
  height: min(720px, calc(100dvh - 96px));
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--page);
  box-shadow: 0 6px 24px var(--shadow);
}

.chat-panel[hidden] {
  display: none;
}

.chat-panel > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.chat-content,
.chat {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}

.chat-scroll {
  flex: 1;
  min-height: 0;
  padding: 12px;
  overflow-y: auto;
}

.chat-status:empty {
  display: none;
}

p.chat-status {
  margin: 0;
  padding: 8px 12px 0;
}

.chat-messages {
  display: grid;
  gap: 12px;
}

.chat-message {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.chat-message .bubble {
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  overflow-wrap: break-word;
}

.chat-message.user {
  justify-items: end;
}

.chat-message.user .bubble {
  max-width: 85%;
  background: var(--wash);
  white-space: pre-wrap;
}

.chat-message.assistant .bubble {
  border: 1px solid var(--border);
}

.chat-message.assistant > .hint {
  padding: 0 14px;
  font-size: 12px;
}

.chat-message .thinking {
  color: var(--text-secondary);
  font-style: italic;
}

.markdown > :last-child {
  margin-bottom: 0;
}

.markdown ul,
.markdown ol {
  margin: 0 0 8px;
  padding-left: 22px;
  line-height: 1.55;
}

.markdown pre {
  margin: 0 0 8px;
  padding: 8px;
  overflow-x: auto;
  border-radius: 6px;
  background: var(--wash);
}

.markdown code {
  font-size: 0.92em;
}

.markdown .table-scroll {
  margin-bottom: 8px;
}

.chat-tools {
  display: inline-block;
  margin-left: 8px;
}

.chat-tools summary {
  cursor: pointer;
}

.chat-tools ul {
  margin: 4px 0 0;
  padding-left: 18px;
}

.chat-examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chat-examples p.hint {
  flex-basis: 100%;
  margin: 0;
}

.chat-examples button.plain {
  text-align: left;
}

.chat-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
}

.chat-input textarea,
.chat input[type="search"] {
  flex: 1;
  min-width: 0;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-primary);
  font: inherit;
}

.chat-input textarea {
  min-height: 44px;
  max-height: 40vh;
  resize: vertical;
}

.chat-search {
  display: flex;
  padding: 12px 12px 0;
}

.chat-list {
  display: grid;
  gap: 8px;
}

.chat-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.chat-open-row {
  min-width: 0;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.chat-open-row:hover strong {
  text-decoration: underline;
}

.chat-snippet {
  display: block;
  margin-top: 4px;
  font-size: 12px;
}

/* On a phone the panel takes the whole screen and brings its own close button. */
@media (max-width: 640px) {
  .chat-panel {
    inset: 0;
    width: auto;
    height: auto;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  body.chat-open {
    overflow: hidden;
  }

  body.chat-open .chat-toggle {
    display: none;
  }
}

/* --- Settings ------------------------------------------------------------ */

.settings {
  display: grid;
  gap: 16px;
  max-width: 760px;
  margin: 0 auto;
}

.settings > .section-title,
.settings > p.hint {
  margin: 0;
}

.settings-section {
  display: grid;
  gap: 12px;
}

.settings-section > p.hint {
  margin: 0;
}

.settings-rows {
  display: grid;
  gap: 12px;
}

.settings-row {
  display: grid;
  gap: 4px;
}

.settings-field {
  display: grid;
  grid-template-columns: minmax(140px, 220px) 1fr;
  align-items: center;
  gap: 4px 12px;
}

/* Prompts take the full width, the label goes above. */
.settings-field.wide {
  grid-template-columns: 1fr;
  align-items: start;
}

.settings-row:has(> .settings-field.wide) > .hint {
  padding-left: 0;
}

.settings-field > span:first-child {
  color: var(--text-secondary);
}

.settings-input {
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-field input,
.settings-field textarea {
  min-width: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-primary);
  font: inherit;
}

.settings-field input[type="number"] {
  width: 120px;
}

.settings-field textarea {
  resize: vertical;
}

.settings-row > .hint {
  padding-left: calc(min(220px, 30%) + 12px);
  font-size: 12px;
}

.settings-field select {
  min-width: 0;
  max-width: 100%;
  padding: 6px 8px;
}

.settings-field textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.5;
}

.settings-warning {
  color: var(--bad);
}

.settings-row pre,
.prompt-preview pre {
  max-height: 420px;
  margin: 6px 0 0;
  padding: 8px;
  overflow: auto;
  border-radius: 6px;
  background: var(--wash);
  font-size: 12px;
  white-space: pre-wrap;
}

.prompt-group {
  padding: 10px 0;
  border-top: 1px solid var(--border);
}

.prompt-group > summary {
  font-weight: 600;
  cursor: pointer;
}

.prompt-group[open] {
  display: grid;
  gap: 12px;
}

.prompt-group[open] > summary {
  margin-bottom: 4px;
}

.prompt-preview-toggle > summary,
.prompt-tool > summary,
.settings-row details > summary {
  color: var(--series-1);
  cursor: pointer;
}

.prompt-preview h3 {
  margin: 12px 0 0;
  font-size: 13px;
}

/* Each piece of the instructions with its origin above it. */
.prompt-sections {
  display: grid;
  gap: 2px;
  margin-top: 6px;
}

.prompt-section {
  padding-left: 10px;
  border-left: 3px solid var(--axis);
}

.prompt-source {
  display: block;
  margin-top: 6px;
  color: var(--text-secondary);
  font-size: 12px;
}

.prompt-section pre {
  margin-top: 2px;
}

.prompt-tool {
  margin-top: 6px;
}

.prompt-tool p {
  margin: 4px 0 0;
  color: var(--text-secondary);
}

.settings-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.settings-actions p.hint {
  margin: 0;
}

@media (max-width: 640px) {
  .settings-field {
    grid-template-columns: 1fr;
  }

  .settings-row > .hint {
    padding-left: 0;
  }
}
