:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1c1f1d;
  --muted: #66706a;
  --line: #dde2de;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --user: #e3f1ee;
  --danger: #b42318;
  --run: #0f766e;
  --trail: #7c5e10;
  --bike: #1d5fae;
  --strength: #8a3ea8;
  --other: #66706a;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514;
    --surface: #1b1f1d;
    --text: #e8ece9;
    --muted: #98a29c;
    --line: #2c322f;
    --accent: #2bb3a3;
    --accent-text: #08201d;
    --user: #1f3532;
    --danger: #f97066;
    --run: #2bb3a3;
    --trail: #d1a640;
    --bike: #6aa6ef;
    --strength: #c58ae0;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
h1 { font-size: 1.4rem; margin: 0.5rem 0; }
h2 { font-size: 1.05rem; margin: 1.25rem 0 0.5rem; }
code, pre { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.85em; }
pre { white-space: pre-wrap; word-break: break-word; margin: 0.25rem 0; color: var(--muted); }

main { max-width: 760px; margin: 0 auto; padding: 0.75rem 1rem 6rem; }
main.chat { padding-bottom: 9rem; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  padding: 0.5rem 1rem; padding-top: max(0.5rem, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar nav { display: flex; gap: 1rem; }
.topbar nav a { color: var(--muted); text-decoration: none; font-weight: 600; }
.topbar nav a.active { color: var(--text); }
.chip { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.flash { max-width: 760px; margin: 0.5rem auto 0; padding: 0.5rem 1rem; color: var(--muted); }
.hint, .muted { color: var(--muted); }
.error { color: var(--danger); }

button, input[type=submit], .ask, .card.link {
  font: inherit; border-radius: 999px; padding: 0.45rem 1rem; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.primary, input[type=submit].primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.secondary { background: transparent; }
button.link { border: none; background: none; padding: 0 0.25rem; color: var(--accent); }
form.inline, .inline { display: inline; }
.ask { display: inline-block; text-decoration: none; font-size: 0.9rem; padding: 0.3rem 0.8rem; }

/* Chat */
.messages { display: flex; flex-direction: column; gap: 0.6rem; }
.msg { max-width: 92%; padding: 0.6rem 0.85rem; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); }
.msg p { margin: 0 0 0.5rem; }
.msg p:last-child, .msg ul:last-child { margin-bottom: 0; }
.msg ul { margin: 0 0 0.5rem; padding-left: 1.2rem; }
.msg.user { align-self: flex-end; background: var(--user); border-color: transparent; }
.msg.assistant { align-self: flex-start; }
.msg.note { align-self: center; background: none; border: none; font-size: 0.85rem; color: var(--muted); padding: 0.1rem 0.5rem; text-align: center; }
.msg.pending .thinking { color: var(--muted); animation: pulse 1.4s ease-in-out infinite; }
turbo-frame.msg { display: block; }
turbo-frame.msg:has(> .msg) { padding: 0; border: none; background: none; max-width: 100%; }
@keyframes pulse { 50% { opacity: 0.4; } }

.composer {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: flex-end;
  padding: 0.6rem 1rem; padding-bottom: max(0.6rem, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line);
}
.composer .context { flex-basis: 100%; margin: 0; font-size: 0.85rem; color: var(--muted); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; font: inherit; padding: 0.55rem 0.8rem;
  border-radius: 18px; border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.composer input[type=submit] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
@media (min-width: 760px) { .composer { left: 50%; transform: translateX(-50%); width: 760px; border: 1px solid var(--line); border-bottom: none; border-radius: var(--radius) var(--radius) 0 0; } }

/* Cards under a reply */
.cards { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.6rem; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.6rem 0.75rem; background: var(--bg); }
.card.link { align-self: flex-start; text-decoration: none; background: var(--surface); }
.card p { margin: 0 0 0.4rem; }
.proposal .actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.5rem; }
.proposal .actions form { margin: 0; }
.proposal .status { font-weight: 600; color: var(--muted); margin: 0.25rem 0 0; }
.proposal.accepted { border-color: var(--accent); }
.proposal.failed .status { color: var(--danger); }
.card.load { display: flex; gap: 1rem; }
.card.load div { display: flex; flex-direction: column; }
.card.load span { font-size: 0.75rem; color: var(--muted); }
.card.load strong { font-size: 1.3rem; }
.comparison table { width: 100%; }

.structure { margin: 0.25rem 0; padding-left: 1.3rem; }
.structure li { margin: 0.15rem 0; }

/* Tables */
table { border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
th, td { padding: 0.3rem 0.5rem; text-align: right; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
thead th { color: var(--muted); font-weight: 600; }
.table-wrap { overflow-x: auto; }

/* Calendar */
.calendar-nav { display: flex; align-items: center; gap: 1rem; margin: 0.25rem 0 0.75rem; }
.calendar-nav a { text-decoration: none; font-size: 1.2rem; }
.calendar-nav a.muted { font-size: 0.9rem; }
.week { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 1rem; overflow: hidden; }
.week > header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.25rem 0.75rem; padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--line); font-weight: 600; }
.week > header .muted { font-weight: 400; font-size: 0.85rem; }
.day { display: flex; gap: 0.75rem; padding: 0.45rem 0.75rem; border-bottom: 1px solid var(--line); min-height: 2.6rem; }
.day:last-child { border-bottom: none; }
.day.today { background: var(--user); }
.day .date { width: 3rem; flex: none; color: var(--muted); font-size: 0.85rem; }
.day .date span { display: block; color: var(--text); font-size: 1.05rem; font-weight: 600; }
.day .items { flex: 1; display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }
.item { display: flex; flex-direction: column; text-decoration: none; color: var(--text); padding: 0.25rem 0.5rem; border-left: 4px solid var(--other); border-radius: 4px; background: var(--bg); }
.item .meta { font-size: 0.8rem; color: var(--muted); }
.item.planned { border-left-style: dashed; }
.item.planned.done { opacity: 0.7; }
.item.planned.cancelled { text-decoration: line-through; opacity: 0.5; }
.item.run { border-left-color: var(--run); }
.item.trail_run { border-left-color: var(--trail); }
.item.bike { border-left-color: var(--bike); }
.item.strength { border-left-color: var(--strength); }

/* Detail pages */
.detail h1 { margin-top: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); gap: 0.5rem; margin: 0.75rem 0; }
.stats div { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.4rem 0.6rem; }
.stats dt { font-size: 0.75rem; color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.push { margin: 1rem 0; }
.plain { list-style: none; padding: 0; }
.plain li { margin: 0.3rem 0; }
.sync-row { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; margin: 0.4rem 0; }
.sync-row form { margin: 0; flex: none; }

/* Agent log */
.turn { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.5rem 0.75rem; margin-bottom: 0.5rem; }
.turn summary { cursor: pointer; display: flex; flex-direction: column; }
.turn.failed { border-color: var(--danger); }
.turn .call { border-top: 1px solid var(--line); padding: 0.4rem 0; }
.turn .reply { border-top: 1px solid var(--line); padding-top: 0.4rem; }

/* Sign in */
.signin { max-width: 22rem; margin: 15vh auto 0; }
.signin form { display: flex; flex-direction: column; gap: 0.6rem; }
.signin input[type=email], .signin input[type=password] {
  font: inherit; padding: 0.6rem 0.8rem; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
