:root {
  /* Light and dark raw tokens */
  --bg-light: #f5f5f2;
  --bg-dark: #111315;
  --surface-light: #ffffff;
  --surface-dark: #1b1e22;
  --surface-2-light: #ecebe5;
  --surface-2-dark: #252930;
  --text-light: #181816;
  --text-dark: #f2f1ec;
  --muted-light: #636159;
  --muted-dark: #9c9b93;
  --border-light: #e2e0d8;
  --border-dark: #2c3139;
  --accent-light: #266f50;
  --accent-dark: #3da377;
  --accent-hover-light: #1e5940;
  --accent-hover-dark: #318a63;
  --accent-soft-light: #e6f3ec;
  --accent-soft-dark: #183428;
  --danger-light: #c0392b;
  --danger-dark: #e56b5d;

  /* Default (light) */
  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-2: var(--surface-2-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --border: var(--border-light);
  --accent: var(--accent-light);
  --accent-hover: var(--accent-hover-light);
  --accent-soft: var(--accent-soft-light);
  --danger: var(--danger-light);

  /* Fallback for browsers without light-dark() */
  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-2: var(--surface-2-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --border: var(--border-dark);
    --accent: var(--accent-dark);
    --accent-hover: var(--accent-hover-dark);
    --accent-soft: var(--accent-soft-dark);
    --danger: var(--danger-dark);
  }

  /* Progressive enhancement with light-dark() */
  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-2: light-dark(var(--surface-2-light), var(--surface-2-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --accent: light-dark(var(--accent-light), var(--accent-dark));
    --accent-hover: light-dark(var(--accent-hover-light), var(--accent-hover-dark));
    --accent-soft: light-dark(var(--accent-soft-light), var(--accent-soft-dark));
    --danger: light-dark(var(--danger-light), var(--danger-dark));
  }

  color-scheme: light dark;
  accent-color: var(--accent);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4;
}

[hidden] {
  display: none !important;
}

/* Login screen */
.login-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 24rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 2rem 1.5rem;
  text-align: center;
}

.login-logo {
  border-radius: 14px;
  margin-bottom: 0.75rem;
}

.login-card h1 {
  margin: 0 0 0.5rem;
  font-size: 1.5rem;
}

/* Main App Shell */
.app-shell {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(4.5rem + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: calc(0.75rem + env(safe-area-inset-top)) 1rem 0.6rem;
  border-bottom: 1px solid var(--border);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.app-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.btn-icon {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.45rem;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.btn-icon:active {
  transform: scale(0.95);
}

/* Search & Pills */
.search-controls {
  margin-top: 0.65rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.search-wrap {
  position: relative;
}

.search-input {
  width: 100%;
  height: 2.85rem;
  padding: 0 2.5rem 0 0.9rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  outline: none;
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.search-clear {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  width: 2.1rem;
  height: 2.1rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.35rem;
  cursor: pointer;
}

.pills, .range-pills {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: 0.1rem;
}

.pills::-webkit-scrollbar, .range-pills::-webkit-scrollbar {
  display: none;
}

.pill {
  flex: 0 0 auto;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 500;
  cursor: pointer;
}

.pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Main Content */
.main-content {
  flex: 1;
  padding: 0.75rem 1rem 1rem;
}

.last-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 0.85rem;
  margin-bottom: 0.7rem;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  font-size: 0.86rem;
}

.last-banner strong {
  color: var(--text);
}

.last-banner button {
  border: none;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 0.2rem 0.4rem;
  cursor: pointer;
}

/* Exercise List */
.exercise-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.ex-row {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 13px;
  overflow: hidden;
}

.ex-main {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: none;
  color: inherit;
  padding: 0.72rem 0.75rem 0.72rem 0.9rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.ex-main:active {
  background: var(--surface-2);
}

.ex-name {
  font-size: 0.98rem;
  font-weight: 600;
  color: var(--text);
}

.ex-meta {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}

.ex-last {
  color: var(--accent);
  font-weight: 600;
}

.ex-quick {
  flex: 0 0 3.35rem;
  border: none;
  border-left: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1.55rem;
  font-weight: 600;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.ex-quick:active {
  background: var(--accent);
  color: #fff;
}

.create-inline {
  width: 100%;
  padding: 0.85rem 1rem;
  border-radius: 13px;
  border: 1.5px dashed var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.95rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

/* History */
.history-summary {
  font-size: 0.86rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

.day-group {
  margin-bottom: 1.15rem;
}

.day-header {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0 0 0.45rem 0.2rem;
}

.log-card {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
  margin-bottom: 0.45rem;
  cursor: pointer;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 0.22rem;
}

.log-card:active {
  background: var(--surface-2);
}

.log-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.log-time {
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  flex-shrink: 0;
}

.log-metrics {
  font-size: 0.86rem;
  color: var(--accent);
  font-weight: 600;
}

.log-note {
  font-size: 0.83rem;
  color: var(--muted);
}

/* Cards & Data Tab */
.card-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  margin-bottom: 0.85rem;
}

.card-block h2 {
  margin: 0 0 0.35rem;
  font-size: 1.02rem;
}

.stack-actions {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-top: 0.75rem;
}

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}

.inline-form {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.65rem;
}

.inline-form input {
  flex: 1;
  min-width: 0;
  height: 2.65rem;
  padding: 0 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}

.token-list {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.token-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.84rem;
}

.account-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  padding: 0.6rem 1rem;
  border-radius: 10px;
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}

.btn-small {
  min-height: 2rem;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
}

.btn-full {
  width: 100%;
}

.btn-grow {
  flex: 1;
}

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

.small {
  font-size: 0.84rem;
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 25;
  max-width: 600px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.nav-item {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.5rem 0.25rem 0.55rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.nav-item.active {
  color: var(--accent);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(4.75rem + env(safe-area-inset-bottom));
  z-index: 40;
  width: calc(100% - 2rem);
  max-width: 560px;
  background: var(--text);
  color: var(--bg);
  border-radius: 12px;
  padding: 0.7rem 0.9rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  font-size: 0.88rem;
}

.toast-actions {
  display: flex;
  gap: 0.45rem;
  flex-shrink: 0;
}

.toast-btn {
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
  border-radius: 8px;
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

/* Native Modal Dialogs (light-dismiss + scroll containment) */
dialog {
  width: calc(100% - 1.5rem);
  max-width: 28rem;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.32);
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  overflow: hidden;
  overscroll-behavior: contain;
}

.sheet {
  padding: 1.15rem 1.15rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.sheet-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.sheet-header h2 {
  margin: 0;
  font-size: 1.12rem;
}

.sheet-header p {
  margin: 0.15rem 0 0;
}

.btn-close {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.field input {
  width: 100%;
  height: 2.75rem;
  padding: 0 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.stepper {
  display: flex;
  gap: 0.3rem;
}

.stepper input {
  text-align: center;
  padding: 0 0.3rem;
  min-width: 0;
}

.step-btn {
  flex: 0 0 2.5rem;
  height: 2.75rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.2rem;
  font-weight: 600;
  cursor: pointer;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.checkbox-row input {
  width: 1.15rem;
  height: 1.15rem;
}

.sheet-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.2rem;
}

.code-box {
  margin: 0;
  padding: 0.7rem 0.8rem;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.84rem;
  overflow-x: auto;
  word-break: break-all;
  white-space: pre-wrap;
}

.small-code {
  font-size: 0.76rem;
}

.empty-state {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
  font-size: 0.92rem;
}
