:root {
  --bg: #f3f0e8;
  --panel: #fffaf0;
  --ink: #1f2926;
  --muted: #65736d;
  --line: #d6d1c4;
  --accent: #0f766e;
  --accent-strong: #0b5d57;
  --accent-soft: #d7efe9;
  --warm: #b3541e;
  --danger: #b42318;
  --shadow: 0 16px 40px rgba(31, 41, 38, 0.12);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background: linear-gradient(180deg, #e9f1ed 0%, var(--bg) 38%, #ece7dc 100%);
  color: var(--ink);
  overflow-x: hidden;
}

body.agent-active {
  overflow: hidden;
}

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

button {
  cursor: pointer;
}

.app-shell {
  width: 100%;
  max-width: 980px;
  min-height: 100vh;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(88px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 13px;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 30px;
  line-height: 1.1;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 21px;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 6px;
  font-size: 17px;
  letter-spacing: 0;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.today-head,
.quick-add,
.library-form,
.backup-actions,
.calendar-toolbar,
.calendar-summary,
.history-panel,
.muscle-toolbar,
.metric-tabs,
.muscle-stats,
.muscle-card,
.backup-meta {
  background: rgba(255, 250, 240, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.today-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
}

.field {
  min-width: 0;
}

input,
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  color: var(--ink);
  padding: 9px 10px;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}

.today-stats,
.calendar-summary,
.muscle-stats,
.backup-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.today-stats span,
.calendar-summary span {
  min-width: 80px;
  padding: 8px 10px;
  border-radius: 7px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 700;
  text-align: center;
}

.quick-add,
.library-form {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) 110px 96px 72px 72px auto;
  gap: 10px;
  align-items: end;
  padding: 12px;
  margin-bottom: 14px;
}

.library-form {
  grid-template-columns: minmax(180px, 1fr) 130px auto;
}

.primary-button,
.secondary-button,
.file-button,
.icon-button,
.delete-button,
.plus-set {
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  padding: 9px 13px;
  font-weight: 800;
}

.primary-button,
.plus-set {
  background: var(--accent);
  color: white;
}

.primary-button:active,
.plus-set:active {
  background: var(--accent-strong);
}

.secondary-button,
.file-button {
  background: #e8e1d3;
  color: var(--ink);
}

.icon-button,
.delete-button {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0;
  background: #e8e1d3;
  color: var(--ink);
  font-size: 22px;
}

.delete-button {
  background: #f7dfd7;
  color: var(--danger);
}

.exercise-list,
.library-list {
  display: grid;
  gap: 10px;
}

.exercise-card,
.library-item {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.library-item {
  align-items: stretch;
}

.library-edit {
  display: grid;
  width: 100%;
  grid-template-columns: minmax(180px, 1fr) 130px auto;
  gap: 10px;
  align-items: center;
}

.library-edit .secondary-button {
  white-space: nowrap;
}

.exercise-main,
.library-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.library-item {
  align-items: stretch;
}

.exercise-meta,
.library-item p,
.empty-state span {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.progress-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  margin: 12px 0;
}

.progress-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e2ded4;
}

.progress-track span {
  display: block;
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--warm));
  transition: width 0.18s ease;
}

.set-count {
  min-width: 58px;
  text-align: right;
}

.stepper-row {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 8px;
}

.plus-set {
  min-height: 54px;
  font-size: 20px;
}

.empty-state {
  display: grid;
  gap: 6px;
  padding: 28px 16px;
  border: 1px dashed #b9b2a4;
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.calendar-toolbar {
  display: grid;
  grid-template-columns: 42px 1fr 42px;
  align-items: center;
  gap: 8px;
  padding: 12px;
  margin-bottom: 10px;
  text-align: center;
}

.calendar-summary {
  padding: 10px;
  margin-bottom: 10px;
  box-shadow: none;
  background:
    linear-gradient(135deg, rgba(215, 239, 233, 0.8), rgba(255, 250, 240, 0.92)),
    var(--panel);
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
}

.weekday-row {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.day-cell {
  position: relative;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.75);
  color: var(--ink);
  padding: 10px;
  font-weight: 800;
  line-height: 1;
  overflow: hidden;
  transition:
    transform 0.18s ease,
    border-color 0.18s ease,
    box-shadow 0.18s ease,
    background 0.18s ease;
}

.day-cell:hover {
  transform: translateY(-2px);
  border-color: rgba(15, 118, 110, 0.42);
  box-shadow: 0 10px 22px rgba(31, 41, 38, 0.12);
}

.day-cell.blank {
  visibility: hidden;
}

.day-cell.trained {
  border-color: rgba(15, 118, 110, 0.62);
  background:
    radial-gradient(circle at 30% 24%, rgba(255, 255, 255, 0.35), transparent 18%),
    radial-gradient(circle at 78% 74%, rgba(252, 211, 77, 0.72), transparent 34%),
    linear-gradient(145deg, #0f766e 0%, #0d9488 56%, #b8872f 100%);
  color: white;
  box-shadow:
    0 10px 22px rgba(15, 118, 110, 0.24),
    inset 0 0 0 1px rgba(255, 255, 255, 0.28);
  text-shadow: 0 1px 1px rgba(22, 32, 31, 0.28);
}

.day-cell.trained::before {
  position: absolute;
  left: -16%;
  right: -16%;
  bottom: -22%;
  height: 54%;
  content: "";
  background: rgba(22, 32, 31, 0.2);
  border-radius: 999px 999px 0 0;
}

.day-cell.trained::after {
  position: absolute;
  top: 4px;
  right: 4px;
  z-index: 2;
  display: grid;
  width: 15px;
  height: 15px;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 250, 240, 0.94);
  color: var(--accent-strong);
  content: "✓";
  font-size: 10px;
  line-height: 1;
  text-shadow: none;
}

.day-number {
  position: relative;
  z-index: 1;
  font-size: 18px;
}

.day-cell.trained .day-number {
  font-size: 18px;
}

.day-badge {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
  min-height: 18px;
  padding: 4px 2px 3px;
  border-radius: 0;
  background: transparent;
  color: white;
  font-size: 11px;
  line-height: 1;
  opacity: 0;
  text-align: center;
  text-shadow: none;
  white-space: nowrap;
}

.day-cell.trained .day-badge {
  opacity: 1;
}

.day-cell.selected {
  outline: 2px solid rgba(179, 84, 30, 0.5);
  outline-offset: 2px;
}

.day-cell.trained.selected {
  box-shadow:
    0 12px 26px rgba(15, 118, 110, 0.28),
    0 0 0 3px rgba(179, 84, 30, 0.16),
    inset 0 0 0 1px rgba(255, 255, 255, 0.28);
}

.history-panel {
  margin-top: 12px;
  margin-bottom: 18px;
  padding: 14px;
}

.history-panel ul {
  padding-left: 18px;
  margin: 8px 0 0;
}

.muscle-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  margin-bottom: 10px;
}

.muscle-toolbar p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.segmented,
.metric-tabs {
  display: flex;
  gap: 5px;
}

.segmented {
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #e8e1d3;
}

.segment,
.metric-tab {
  min-height: 36px;
  border: 0;
  border-radius: 7px;
  padding: 7px 11px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.segment.active,
.metric-tab.active {
  background: #b42318;
  color: white;
}

.metric-tabs {
  flex-wrap: wrap;
  padding: 8px;
  margin-bottom: 10px;
  box-shadow: none;
}

.muscle-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0;
  padding: 0;
  margin-bottom: 10px;
  overflow: hidden;
}

.muscle-stats div {
  padding: 12px;
  border-right: 1px solid var(--line);
}

.muscle-stats div:last-child {
  border-right: 0;
}

.muscle-stats dt,
.muscle-row span,
.heat-legend span {
  color: var(--muted);
  font-size: 13px;
}

.muscle-stats dd {
  margin: 5px 0 0;
  font-size: 19px;
  font-weight: 800;
}

.muscle-card {
  padding: 0;
  margin-bottom: calc(112px + env(safe-area-inset-bottom));
  overflow: hidden;
  background: #fffdf8;
}

.muscle-map-host {
  width: 100%;
  overflow: hidden;
}

.muscle-map-host > svg,
.muscle-map {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
}

.muscle-map-host:not(:empty) + .muscle-map {
  display: none;
}

.muscle-map-host .muscle-map {
  width: 100%;
  max-width: 100%;
  height: auto;
}

.map-panel {
  fill: #fffdf8;
  stroke: #e2ded4;
  stroke-width: 2;
}

.map-caption,
.map-date,
.figure-name,
.legend-text {
  fill: #6b7571;
  font-weight: 750;
}

.map-caption {
  font-size: 19px;
}

.map-date {
  font-size: 20px;
  text-anchor: end;
}

.body-silhouette,
.body-head {
  fill: #edf0f2;
  stroke: #c9d2db;
  stroke-linejoin: round;
  stroke-width: 3;
}

.muscle-shape {
  fill: var(--heat-color, #e5eaf0);
  stroke: #fffdf8;
  stroke-linejoin: round;
  stroke-width: 3.4;
  transition:
    fill 0.2s ease,
    filter 0.2s ease;
}

.muscle-shape.active {
  filter: drop-shadow(0 6px 8px rgba(180, 35, 24, 0.2));
}

.muscle-detail {
  fill: none;
  stroke: rgba(255, 253, 248, 0.86);
  stroke-linecap: round;
  stroke-width: 3;
}

.label-line {
  fill: none;
  stroke: #bfc7d4;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.label-title {
  fill: #3158a7;
  font-size: 18px;
  font-weight: 850;
}

.label-value {
  fill: #dc2626;
  font-size: 18px;
  font-weight: 850;
}

.label-sub {
  fill: #7b8794;
  font-size: 12px;
  font-weight: 750;
}

.label-title.right,
.label-value.right,
.label-sub.right {
  text-anchor: end;
}

.label-line.muted,
.label-title.muted,
.label-value.muted,
.label-sub.muted {
  opacity: 0.42;
}

.figure-name {
  font-size: 16px;
  text-anchor: middle;
}

.legend-text {
  font-size: 15px;
}

.legend-low {
  fill: #e5eaf0;
}

.legend-high {
  fill: #dc2626;
}

.backup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px;
  margin-bottom: 12px;
}

.file-button {
  display: inline-flex;
  align-items: center;
}

#import-data {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.backup-meta {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0;
  padding: 0;
  overflow: hidden;
}

.backup-meta div {
  padding: 14px;
  border-right: 1px solid var(--line);
}

.backup-meta div:last-child {
  border-right: 0;
}

.backup-meta dt {
  color: var(--muted);
  font-size: 13px;
}

.backup-meta dd {
  margin: 5px 0 0;
  font-size: 20px;
  font-weight: 800;
}

.tabbar {
  position: fixed;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 5;
  display: grid;
  width: min(520px, calc(100% - 24px));
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
  transform: translateX(-50%);
  padding: 6px;
  border: 1px solid rgba(214, 209, 196, 0.9);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.92);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.tab {
  min-height: 44px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.tab.active {
  background: var(--accent);
  color: white;
}

.agent-launcher {
  position: fixed;
  right: 18px;
  bottom: calc(82px + env(safe-area-inset-bottom));
  z-index: 12;
  display: grid;
  width: 54px;
  height: 54px;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: #1f2926;
  color: white;
  box-shadow: 0 14px 30px rgba(31, 41, 38, 0.24);
  font-weight: 850;
}

.agent-panel {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-height: none;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  background:
    linear-gradient(180deg, rgba(233, 241, 237, 0.98), rgba(255, 253, 248, 0.98) 34%, rgba(243, 240, 232, 0.98)),
    #fffdf8;
  box-shadow: none;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.08);
  transform-origin: calc(100% - 45px) calc(100% - 106px);
  transition:
    opacity 0.3s ease,
    transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  will-change: opacity, transform;
}

.agent-panel.open {
  opacity: 1;
  pointer-events: auto;
  transform: scale(1);
}

.agent-panel.closing {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.08);
}

.agent-panel[hidden],
.agent-launcher[hidden] {
  display: none !important;
}

.agent-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(14px + env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 14px max(18px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.86);
  backdrop-filter: blur(18px);
}

.agent-title {
  font-size: 18px;
  font-weight: 850;
}

.agent-icon-button {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 0;
  border-radius: 7px;
  background: #e8e1d3;
  color: var(--ink);
  font-size: 20px;
  line-height: 1;
}

.agent-log {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow: auto;
  padding: 14px max(16px, env(safe-area-inset-right)) 14px max(16px, env(safe-area-inset-left));
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.agent-message {
  width: fit-content;
  max-width: min(720px, 88%);
  padding: 9px 11px;
  border-radius: 8px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.agent-message.user {
  justify-self: end;
  background: var(--accent);
  color: white;
}

.agent-message.assistant {
  justify-self: start;
  background: #e8e1d3;
  color: var(--ink);
}

.agent-card {
  display: grid;
  width: min(760px, 100%);
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffaf0;
}

.agent-card h3 {
  margin-bottom: 0;
}

.agent-card p {
  margin-bottom: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.agent-card-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.agent-mini-calendar {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 5px;
}

.agent-weekday {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
  text-align: center;
}

.agent-day {
  min-width: 0;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  color: var(--ink);
  padding: 0;
  font-size: 12px;
  font-weight: 800;
}

.agent-day.trained {
  border-color: rgba(15, 118, 110, 0.42);
  background: #0f766e;
  color: white;
}

.agent-compact-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 18px;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.4;
}

.agent-stat-list {
  display: grid;
  gap: 6px;
}

.agent-stat-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 8px 9px;
  border-radius: 7px;
  background: #f2eadb;
}

.agent-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 10px max(16px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.9);
  backdrop-filter: blur(18px);
}

.agent-input {
  width: 100%;
  min-height: 42px;
  max-height: 120px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  color: var(--ink);
  padding: 9px 10px;
  outline: none;
  font: inherit;
}

.agent-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}

.agent-send {
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: white;
  padding: 9px 13px;
  font-weight: 850;
}

.agent-send:disabled {
  opacity: 0.68;
}

@media (max-width: 720px) {
  .app-shell {
    padding-inline: 12px;
  }

  .today-head {
    align-items: stretch;
    flex-direction: column;
  }

  .today-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .quick-add,
  .library-form {
    grid-template-columns: 1fr 1fr;
  }

  .library-edit {
    grid-template-columns: 1fr auto;
  }

  .library-edit input {
    grid-column: 1 / -1;
  }

  .calendar-grid {
    gap: 6px;
  }

  .day-cell {
    padding: 7px;
    border-radius: 8px;
  }

  .day-number {
    font-size: 17px;
  }

  .day-cell.trained .day-number {
    font-size: 17px;
  }

  .day-badge {
    min-height: 16px;
    padding: 3px 1px 2px;
    font-size: 10px;
  }

  .day-cell.trained::before {
    height: 52%;
  }

  .day-cell.trained::after {
    top: 3px;
    right: 3px;
    width: 12px;
    height: 12px;
    font-size: 9px;
  }

  .history-panel {
    margin-bottom: 28px;
  }

  .muscle-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  .metric-tabs {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }

  .muscle-stats {
    grid-template-columns: repeat(2, 1fr);
  }

  .muscle-stats div:nth-child(2) {
    border-right: 0;
  }

  .muscle-stats div:nth-child(-n + 2) {
    border-bottom: 1px solid var(--line);
  }

  .muscle-map {
    min-height: 0;
  }

  .field.wide,
  .primary-button {
    grid-column: 1 / -1;
  }

  .backup-meta {
    grid-template-columns: 1fr;
  }

  .backup-meta div {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .backup-meta div:last-child {
    border-bottom: 0;
  }

  .tabbar {
    left: 12px;
    right: 12px;
    width: auto;
    transform: none;
  }

  .agent-launcher {
    right: 14px;
    bottom: calc(76px + env(safe-area-inset-bottom));
  }
}
