:root {
  color-scheme: light;
  --bg: var(--tg-theme-bg-color, #f4f6f1);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #17201c);
  --muted: var(--tg-theme-hint-color, #68756f);
  --line: color-mix(in srgb, var(--text) 10%, transparent);
  --green: #244c3d;
  --green-2: #356653;
  --green-soft: #e5eee8;
  --accent: var(--tg-theme-button-color, #244c3d);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #a53f3f);
  --console: #101512;
  --console-surface: #171e1a;
  --console-surface-2: #1c2520;
  --console-line: #2b3831;
  --console-text: #ecf4ef;
  --console-muted: #8d9c93;
  --console-green: #91e4ad;
  --radius: 20px;
  --safe-top: max(env(safe-area-inset-top), var(--tg-safe-area-inset-top, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
  font-family: Manrope, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { min-width: 280px; min-height: 100%; margin: 0; background: var(--bg); color: var(--text); }
button, input { font: inherit; }
button { touch-action: manipulation; }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app-shell { width: min(100%, 520px); min-height: var(--app-height, 100dvh); margin: 0 auto; background: var(--bg); position: relative; overflow: hidden; }
.app-header { min-height: calc(66px + var(--safe-top)); padding: calc(14px + var(--safe-top)) 20px 12px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
.brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; }
.brand b { font-weight: 700; }
.brand-mark { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 10px; background: var(--green); color: white; font-family: Newsreader, Georgia, serif; font-size: 18px; }
.header-status { max-width: 48%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; color: var(--muted); }
.header-status::before { content: ""; width: 6px; height: 6px; display: inline-block; margin-right: 6px; border-radius: 50%; background: #76a789; vertical-align: 1px; }
main { min-height: calc(var(--app-height, 100dvh) - 67px); }
.view, .state-view { padding: 26px 20px calc(34px + var(--safe-bottom)); }

.state-view { min-height: calc(var(--app-height, 100dvh) - 67px); display: flex; flex-direction: column; align-items: flex-start; justify-content: center; }
.state-view h1, .home-intro h1, .pending-view h1 { max-width: 370px; margin: 4px 0 12px; font-family: Newsreader, Georgia, serif; font-size: clamp(32px, 9vw, 42px); line-height: .98; letter-spacing: -.025em; font-weight: 500; }
.state-copy { max-width: 390px; margin: 0 0 26px; color: var(--muted); font-size: 14px; line-height: 1.65; }
.state-icon, .confirmation-mark { width: 52px; height: 52px; display: grid; place-items: center; margin-bottom: 26px; border-radius: 18px; background: var(--green-soft); color: var(--green); font-size: 22px; font-weight: 700; }
.state-icon-error { background: #f5e4e1; color: var(--danger); }
.state-loading { align-items: center; text-align: center; }
.state-loading h1 { max-width: 330px; }
.pulse-mark { width: 40px; height: 40px; margin-bottom: 32px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 0 rgba(36, 76, 61, .35); animation: pulse 1.7s ease-out infinite; }
@keyframes pulse { 70%, 100% { box-shadow: 0 0 0 18px rgba(36, 76, 61, 0); } }

.kicker { margin: 0 0 7px; color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; }
.muted { color: var(--muted); font-size: 13px; line-height: 1.55; }
.home-intro { padding: 10px 2px 20px; }
.home-intro h1 { margin-bottom: 8px; }
.intro-copy { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.55; }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin: 0 0 16px; }
.metric { min-height: 114px; padding: 15px; display: flex; flex-direction: column; justify-content: flex-end; border: 1px solid var(--line); border-radius: 17px; background: var(--surface); }
.metric-primary { border: 0; background: var(--green); color: #fff; }
.metric-wide { min-height: 83px; grid-column: 1 / -1; }
.metric strong { display: block; margin: 5px 0 1px; font-size: 25px; line-height: 1.15; }
.metric-wide strong { font-size: 16px; }
.metric > span:last-child { font-size: 11px; opacity: .7; }
.metric-label { font-size: 10px; letter-spacing: .08em; text-transform: uppercase; opacity: .72; }
.session-card, .last-card { padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 10px 30px rgba(30, 50, 41, .045); }
.last-card { margin-top: 12px; box-shadow: none; }
.card-heading, .section-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; }
.card-heading h2, .section-heading h2 { margin: 0 0 5px; font-size: 19px; letter-spacing: -.02em; }
.card-heading .muted { margin: 0; }
.soft-badge { flex: none; padding: 6px 8px; border-radius: 999px; background: var(--green-soft); color: var(--green); font-size: 9px; font-weight: 700; letter-spacing: .08em; }
.section-heading time { color: var(--muted); font-size: 11px; }
.button { min-height: 46px; padding: 0 18px; border: 0; border-radius: 13px; cursor: pointer; font-weight: 700; }
.button:disabled { opacity: .52; cursor: not-allowed; }
.button-primary { background: var(--accent); color: var(--accent-text); }
.button-secondary { border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.button-large { width: 100%; min-height: 52px; margin-top: 18px; display: flex; align-items: center; justify-content: space-between; }
.text-button { display: inline-block; min-height: 42px; border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 12px; font-weight: 600; }
.danger-text { color: var(--danger); }
.recent-list { margin-top: 12px; border-top: 1px solid var(--line); }
.recent-row { display: flex; align-items: center; gap: 11px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.recent-row:last-child { border-bottom: 0; padding-bottom: 0; }
.recent-number { width: 25px; height: 25px; display: grid; flex: none; place-items: center; border-radius: 8px; background: var(--green-soft); color: var(--green); font-size: 9px; font-weight: 700; }
.recent-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 650; }
.recent-detail { color: var(--muted); font-size: 10px; }

/* The live console intentionally switches to a denser register. */
.app-shell[data-mode="active"] { max-width: 560px; background: var(--console); color: var(--console-text); }
.app-shell[data-mode="active"] .app-header { border-color: var(--console-line); background: var(--console); }
.app-shell[data-mode="active"] .brand-mark { background: var(--console-green); color: #112017; }
.app-shell[data-mode="active"] .header-status { color: var(--console-green); }
.app-shell[data-mode="active"] .header-status::before { background: var(--console-green); }
.console-view { padding: 20px 14px calc(30px + var(--safe-bottom)); font-family: "DM Mono", ui-monospace, SFMono-Regular, Menlo, monospace; }
.console-topline { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; padding: 5px 1px 17px; }
.console-topline h1 { margin: 2px 0 5px; font-family: Manrope, sans-serif; font-size: 24px; line-height: 1.1; letter-spacing: -.03em; }
.console-kicker { margin: 0 0 6px; color: var(--console-green); font-size: 9px; letter-spacing: .13em; text-transform: uppercase; }
.console-muted { margin: 0; color: var(--console-muted); font-size: 10px; }
.live-dot { width: 6px; height: 6px; display: inline-block; margin-right: 6px; border-radius: 50%; background: var(--console-green); box-shadow: 0 0 0 4px rgba(145, 228, 173, .09); }
.session-clock { flex: none; color: var(--console-green); font-size: 22px; font-weight: 500; font-variant-numeric: tabular-nums; }
.console-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 16px; }
.console-stats > div { min-width: 0; padding: 10px; border: 1px solid var(--console-line); border-radius: 9px; background: var(--console-surface); }
.console-stats strong { display: block; overflow: hidden; color: var(--console-text); font-size: 15px; text-overflow: ellipsis; }
.console-stats span { color: var(--console-muted); font-size: 8px; letter-spacing: .05em; text-transform: uppercase; }
.exercise-log { display: grid; gap: 8px; margin-bottom: 12px; }
.log-empty { padding: 18px; border: 1px dashed var(--console-line); border-radius: 11px; color: var(--console-muted); font-size: 11px; line-height: 1.6; text-align: center; }
.movement { overflow: hidden; border: 1px solid var(--console-line); border-radius: 11px; background: var(--console-surface); }
.movement-heading { padding: 11px 12px; display: flex; justify-content: space-between; gap: 10px; background: var(--console-surface-2); }
.movement-heading strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; text-transform: uppercase; }
.movement-heading span { flex: none; color: var(--console-green); font-size: 9px; }
.set-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.set-table th { padding: 7px 8px; border-bottom: 1px solid var(--console-line); color: var(--console-muted); font-size: 8px; font-weight: 400; text-align: right; text-transform: uppercase; }
.set-table th:first-child, .set-table td:first-child { width: 34px; text-align: left; }
.set-table td { padding: 9px 8px; border-bottom: 1px solid rgba(43, 56, 49, .72); color: #cbd6cf; font-size: 10px; text-align: right; font-variant-numeric: tabular-nums; }
.set-table tr:last-child td { border-bottom: 0; }
.set-table .set-done { color: var(--console-green); }
.set-composer { margin-top: 10px; padding: 14px 12px 12px; border: 1px solid var(--console-line); border-radius: 12px; background: var(--console-surface); }
.composer-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.composer-heading h2 { margin: 0; font-family: Manrope, sans-serif; font-size: 17px; }
.type-toggle { display: flex; padding: 2px; border: 1px solid var(--console-line); border-radius: 7px; }
.type-button { min-height: 28px; padding: 0 7px; border: 0; border-radius: 5px; background: transparent; color: var(--console-muted); font: 500 8px "DM Mono", monospace; cursor: pointer; text-transform: uppercase; }
.type-button.is-active { background: var(--console-green); color: #0d1c12; }
.field-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.field { min-width: 0; display: block; }
.field > span { display: block; margin: 0 0 5px; color: var(--console-muted); font-size: 8px; letter-spacing: .04em; text-transform: uppercase; }
.field small { font-size: inherit; opacity: .7; }
.field input { width: 100%; min-height: 43px; padding: 0 10px; border: 1px solid #39483f; border-radius: 7px; background: #111713; color: var(--console-text); font-family: "DM Mono", monospace; font-size: 13px; }
.field input::placeholder { color: #59675f; }
.field input:focus { border-color: var(--console-green); outline-color: rgba(145, 228, 173, .18); }
.field-wide { margin-bottom: 9px; }
.notes-field { margin-top: 9px; margin-bottom: 0; }
.console-button { margin-top: 11px; background: var(--console-green); color: #102016; }
.console-actions { display: flex; flex-direction: column; align-items: stretch; margin-top: 12px; }
.finish-button { min-height: 50px; border: 1px solid var(--console-green); background: transparent; color: var(--console-green); text-transform: uppercase; letter-spacing: .08em; font-family: "DM Mono", monospace; font-size: 10px; }
.app-shell[data-mode="active"] .danger-text { color: #da8888; }

.pending-view { min-height: calc(var(--app-height, 100dvh) - 67px); display: flex; flex-direction: column; justify-content: center; }
.pending-summary { width: 100%; margin: 2px 0 5px; padding: 16px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); }
.pending-statline { display: flex; justify-content: space-between; gap: 15px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 12px; }
.pending-statline:last-child { border-bottom: 0; }
.pending-statline span { color: var(--muted); }
.pending-view > .text-button { margin-top: 8px; align-self: center; }

.toast { position: fixed; z-index: 20; left: 50%; bottom: calc(18px + var(--safe-bottom)); width: max-content; max-width: calc(100vw - 32px); padding: 11px 14px; border-radius: 11px; background: #18211d; color: #eff7f1; box-shadow: 0 8px 30px rgba(0, 0, 0, .2); opacity: 0; transform: translate(-50%, 12px); pointer-events: none; font-size: 11px; transition: opacity .18s ease, transform .18s ease; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.busy-overlay { position: fixed; z-index: 30; inset: 0; display: grid; place-items: center; background: rgba(12, 18, 14, .26); backdrop-filter: blur(2px); }
.spinner { width: 38px; height: 38px; border: 3px solid rgba(255, 255, 255, .35); border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(1turn); } }

@media (min-width: 521px) {
  body { background: #e8ece6; }
  .app-shell { box-shadow: 0 0 50px rgba(27, 45, 36, .1); }
  .app-shell[data-mode="active"] { box-shadow: 0 0 50px rgba(0, 0, 0, .35); }
}
@media (max-width: 340px) {
  .app-header, .view, .state-view { padding-left: 14px; padding-right: 14px; }
  .brand > span:last-child { display: none; }
  .console-view { padding-left: 9px; padding-right: 9px; }
  .composer-heading { display: block; }
  .type-toggle { width: max-content; margin-top: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
@media (prefers-color-scheme: dark) {
  :root:not([style*="--tg-theme-bg-color"]) { --bg: #17201c; --surface: #202b25; --text: #edf3ef; --muted: #a0ada5; --line: rgba(238, 245, 240, .11); --green-soft: #2a3c33; }
  .metric-primary { background: #315f4d; }
  .brand-mark { background: #9cddb3; color: #173025; }
}
