:root {
  color-scheme: dark;
  --bg: #0d0d12;
  --bg-elevated: #17171e;
  --bg-card: #1c1c24;
  --bg-card-hover: #24242e;
  --border: #2a2a34;
  --text: #f2f2f5;
  --text-dim: #9a9aa5;
  --text-faint: #5c5c68;
  --accent: #7c8cff;
  --accent-dim: #7c8cff33;
  --success: #34d399;
  --danger: #f87171;
  --warn: #fbbf24;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}

body { overflow: hidden; }

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  overflow-y: auto;
  padding: calc(var(--safe-top) + 12px) 16px calc(80px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}

.view--chat {
  display: flex;
  flex-direction: column;
  padding-bottom: calc(64px + var(--safe-bottom));
}

/* Auth screen */
.auth-view {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px calc(var(--safe-top)) calc(var(--safe-bottom));
}
.auth-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.auth-title { font-size: 30px; font-weight: 800; margin: 0 0 -6px; text-align: center; }
.auth-sub { color: var(--text-dim); font-size: 14px; text-align: center; margin: 0 0 8px; }
.auth-error {
  background: #3a1414;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
}
.auth-submit-btn {
  background: var(--accent);
  color: #0d0d12;
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 6px;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.topbar--simple { justify-content: flex-start; }
.topbar__title { font-size: 28px; font-weight: 700; margin: 0; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--bg-card);
  color: var(--text);
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--bg-card-hover); }

/* Constraints banner */
.constraints-banner {
  background: #3a2a14;
  border: 1px solid var(--warn);
  color: var(--warn);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
  line-height: 1.5;
}

/* Plan list (today) */
.plan-list { display: flex; flex-direction: column; gap: 10px; }
.exercise-card {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 14px;
  border: 1px solid var(--border);
}
.exercise-card__head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.exercise-card__name { font-size: 16px; font-weight: 600; }
.exercise-card__target { font-size: 12.5px; color: var(--text-dim); }

.set-row {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 6px;
}
.set-row__label { font-size: 12px; color: var(--text-faint); width: 46px; flex-shrink: 0; }
.set-row input[type="number"] {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 15px;
  width: 100%;
  outline: none;
}
.set-row input[type="number"]:focus { border-color: var(--accent); }
.set-row__x { color: var(--text-faint); font-size: 13px; }
.set-row__check {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.set-row__check.done { background: var(--success); border-color: var(--success); color: #06251a; font-weight: 700; }

.add-set-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  padding: 6px 0;
  cursor: pointer;
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  margin-top: 60px;
  font-size: 15px;
  line-height: 1.6;
}
.empty-state__hint { color: var(--text-faint); font-size: 13px; }

/* Chat */
.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 8px;
}
.chat-msg {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.chat-msg--user {
  align-self: flex-end;
  background: var(--accent);
  color: #0d0d12;
  border-bottom-right-radius: 4px;
}
.chat-msg--assistant {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.chat-msg--pending { opacity: 0.5; }

.chat-input-bar {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(64px + var(--safe-bottom));
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(20,20,26,0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  z-index: 9;
}
.chat-input-bar input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.chat-input-bar input:focus { border-color: var(--accent); }
.chat-send-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #0d0d12;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

/* Body view: cards, stats, history rows */
.card-section { margin-bottom: 28px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.section-head__actions { display: flex; gap: 14px; }
.section-title { font-size: 17px; font-weight: 700; margin: 0; }

.stat-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.stat-summary--1 { grid-template-columns: 1fr; }
.stat-box {
  background: var(--bg-card);
  border-radius: 14px;
  padding: 12px 6px;
  text-align: center;
  border: 1px solid var(--border);
}
.stat-box__value { font-size: 19px; font-weight: 700; }
.stat-box__label { font-size: 10.5px; color: var(--text-dim); margin-top: 2px; }

.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13.5px;
}
.history-row__main { display: flex; flex-direction: column; gap: 2px; }
.history-row__title { font-weight: 600; }
.history-row__sub { color: var(--text-dim); font-size: 12px; }
.history-row__value { font-weight: 700; color: var(--accent); }
.history-row__estimated { font-size: 10.5px; color: var(--text-faint); }
.history-row__resolve { background: none; border: 1px solid var(--border); color: var(--text-dim); border-radius: 8px; padding: 6px 10px; font-size: 12px; cursor: pointer; flex-shrink: 0; }

/* Plan editor (weekly) */
.plan-week { display: flex; flex-direction: column; gap: 18px; }
.plan-day__title { font-size: 15px; font-weight: 700; color: var(--text-dim); margin-bottom: 8px; }
.plan-day__list { display: flex; flex-direction: column; gap: 8px; }
.plan-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
}
.plan-row__name { font-size: 14.5px; font-weight: 600; }
.plan-row__meta { font-size: 12px; color: var(--text-dim); }
.plan-row__del { background: none; border: none; color: var(--text-faint); font-size: 18px; cursor: pointer; padding: 4px 8px; }

/* Tab bar */
.tabbar {
  display: flex;
  background: rgba(20,20,26,0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  padding: 8px 4px calc(6px + var(--safe-bottom));
  position: relative;
  z-index: 10;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-faint);
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 12px;
  padding: 8px 0;
  cursor: pointer;
}
.tab.active { color: var(--accent); font-weight: 600; }

/* Backdrop + bottom sheet */
.backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 20;
  animation: fadeIn .2s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  max-height: 88dvh;
  background: var(--bg-elevated);
  border-radius: 20px 20px 0 0;
  z-index: 21;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  animation: slideUp .25s cubic-bezier(.32,.72,0,1);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

.sheet__handle { width: 36px; height: 4px; background: var(--border); border-radius: 2px; margin: 8px auto; }
.sheet__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.sheet__title { font-size: 16px; font-weight: 600; margin: 0; }

.text-btn { background: none; border: none; color: var(--text-dim); font-size: 15px; padding: 6px; cursor: pointer; }
.text-btn--accent { color: var(--accent); font-weight: 600; }

.sheet__body {
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.field { display: flex; flex-direction: column; gap: 8px; }
.field__label { font-size: 13px; color: var(--text-dim); font-weight: 500; }
.field__hint { font-size: 12px; color: var(--text-faint); }

input[type="text"], input[type="number"], input[type="date"], textarea, select {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  width: 100%;
  font-family: inherit;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 60px; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.segmented__item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 10px 6px;
  font-size: 13px;
  cursor: pointer;
  text-align: center;
}
.segmented__item.active { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.photo-preview {
  width: 100%;
  max-height: 220px;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.danger-btn {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 12px;
  padding: 12px;
  font-size: 15px;
  cursor: pointer;
  margin-top: 8px;
}

.toast {
  position: fixed;
  left: 50%; bottom: calc(90px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
  z-index: 30;
  animation: toastIn .2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }
