:root {
    color-scheme: light;
    --bg: #f6f7f4;
    --surface: #ffffff;
    --text: #17201b;
    --muted: #667168;
    --line: #dfe5dc;
    --accent: #176b4d;
    --accent-strong: #0f5138;
    --danger: #b33a2f;
    --success: #1d7a53;
    --shell-edge-x: 16px;
    --shell-edge-top: 20px;
    --shell-edge-bottom: 16px;
    --panel-padding: 16px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

a {
    color: inherit;
    text-decoration: none;
}

.shell {
    width: min(100%, 560px);
    min-height: 100vh;
    margin: 0 auto;
    padding: var(--shell-edge-top) var(--shell-edge-x) 28px;
}

.app-shell {
    display: flex;
    flex-direction: column;
    height: var(--app-height, var(--app-stable-height, 100dvh));
    min-height: var(--app-height, var(--app-stable-height, 100dvh));
    max-height: var(--app-height, var(--app-stable-height, 100dvh));
    overflow: hidden;
    padding-bottom: var(--shell-edge-bottom);
}

.sets-shell,
.exercises-shell,
.workouts-shell,
.workout-detail-shell {
    display: flex;
    flex-direction: column;
    height: var(--app-stable-height, 100dvh);
    min-height: var(--app-stable-height, 100dvh);
    max-height: var(--app-stable-height, 100dvh);
    overflow: hidden;
    padding-bottom: var(--shell-edge-bottom);
}

.centered {
    display: grid;
    align-content: center;
    gap: 16px;
}

.home-screen {
    align-content: center;
    gap: 28px;
}

.home-header {
    display: grid;
    gap: 8px;
}

.home-actions {
    display: grid;
    gap: 12px;
}

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

.topbar > div {
    min-width: 0;
}

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

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

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

h2 {
    margin-bottom: 6px;
    font-size: 18px;
    line-height: 1.2;
    letter-spacing: 0;
}

.topbar h2,
.home-header h1,
.home-header h2 {
    margin-bottom: 0;
}

p {
    margin-bottom: 0;
    color: var(--muted);
    line-height: 1.45;
}

.panel,
.metric {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
}

.panel {
    display: grid;
    gap: 16px;
    padding: var(--panel-padding);
    margin-bottom: 16px;
}

.sets-panel,
.exercises-panel,
.workouts-panel,
.workout-detail-panel,
.app-panel {
    flex: 1;
    min-height: 0;
    min-width: 0;
    margin-bottom: 0;
    grid-template-rows: auto minmax(0, 1fr) auto;
}

.app-panel--form,
.workouts-panel--form {
    grid-template-rows: minmax(0, 1fr) auto;
}

.status-panel {
    display: flex;
    align-items: center;
    gap: 14px;
}

.status-panel.error {
    border-color: #e6bbb6;
}

.status-panel.success {
    border-color: #a9d6c0;
}

.status-dot,
.loader {
    flex: 0 0 36px;
    width: 36px;
    height: 36px;
    border-radius: 999px;
}

.status-dot {
    background: var(--muted);
}

.success .status-dot {
    background: var(--success);
}

.error .status-dot {
    background: var(--danger);
}

.loader {
    border: 3px solid var(--line);
    border-top-color: var(--accent);
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 14px;
}

.metric {
    min-height: 86px;
    padding: 14px;
}

.metric span {
    display: block;
    color: var(--muted);
    font-size: 13px;
}

.metric strong {
    display: block;
    margin-top: 10px;
    font-size: 28px;
}

.section-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.section-title h2 {
    margin-bottom: 0;
}

.icon-button,
.primary-button,
.ghost-button,
.danger-button {
    min-height: 44px;
    border: 0;
    border-radius: 8px;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    line-height: 1.2;
    text-align: center;
}

.icon-button {
    display: inline-grid;
    place-items: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    background: var(--text);
    color: #fff;
    font-size: 24px;
}

.back-button {
    position: relative;
    overflow: hidden;
    color: transparent;
    font-size: 0;
}

.back-button::before,
.back-button::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 14px;
    height: 3px;
    border-radius: 999px;
    background: #fff;
    transform-origin: 2px 50%;
}

.back-button::before {
    transform: translate(-6px, -1.5px) rotate(45deg);
}

.back-button::after {
    transform: translate(-6px, -1.5px) rotate(-45deg);
}

.primary-button {
    display: grid;
    place-items: center;
    padding: 0 18px;
    background: var(--accent);
    color: #fff;
}

.action-button {
    display: grid;
    place-items: center;
}

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

.ghost-button {
    display: grid;
    place-items: center;
    padding: 0 14px;
    background: #e7eee9;
    color: var(--accent-strong);
}

.danger-button {
    display: grid;
    place-items: center;
    padding: 0 14px;
    background: var(--danger);
    color: #fff;
}

.danger-button:active {
    background: #8e2f27;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(23, 32, 27, 0.42);
}

.choice-dialog {
    display: grid;
    gap: 14px;
    width: min(100%, 360px);
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    padding: 18px;
    box-shadow: 0 18px 60px rgba(23, 32, 27, 0.22);
}

.choice-dialog h2 {
    margin-bottom: 0;
}

.choice-dialog-field {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 700;
}

.choice-dialog-input.is-invalid {
    border-color: var(--danger);
    box-shadow: 0 0 0 2px rgba(179, 58, 47, 0.12);
}

.choice-dialog-error {
    margin: 0;
    color: var(--danger);
    font-size: 13px;
    font-weight: 600;
}

.choice-actions {
    display: grid;
    gap: 10px;
}

.wide {
    width: 100%;
}

.wide + .wide {
    margin-top: 10px;
}

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

.detail-list {
    display: grid;
    gap: 12px;
}

.detail-list span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.detail-list strong {
    display: block;
    margin-top: 4px;
    font-size: 16px;
    font-weight: 700;
}

.workout-exercise-row {
    display: grid;
    gap: 6px;
    width: 100%;
    min-height: 54px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
    color: var(--text);
    padding: 12px 14px;
}

.workout-exercise-row small {
    color: var(--muted);
    line-height: 1.35;
}

.workout-exercise-row.is-clickable {
    cursor: pointer;
}

.workout-exercise-row.is-clickable:hover {
    border-color: #bfd6c9;
    background: #f4f9f5;
}

.workout-exercise-row.is-clickable:active {
    background: #eef4ef;
}

.exercise-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    width: 100%;
    min-height: 54px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
    color: var(--text);
    padding: 0 14px;
    text-align: left;
    font: inherit;
}

.exercise-row small {
    color: var(--muted);
}

.exercise-edit-row {
    width: 100%;
}

.set-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
    gap: 6px;
    width: 100%;
    align-items: center;
}

.set-row input {
    min-width: 0;
}

.set-row input:disabled {
    color: var(--text);
    opacity: 1;
}

.set-icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 44px;
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: #e7eee9;
    color: var(--accent-strong);
    font: inherit;
    font-size: 18px;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
}

.set-icon-button span {
    display: block;
    line-height: 1;
}

.set-open-button span {
    transform: translateY(-1px);
}

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

.set-row.is-editing .set-edit-button {
    background: var(--accent);
    color: #fff;
}

.form.is-editing .set-edit-button {
    background: var(--accent);
    color: #fff;
}

.set-row.is-editing .set-delete-button {
    background: #e7eee9;
    color: var(--accent-strong);
}

.app-panel > *,
.app-panel > *,
.sets-panel > *,
.exercises-panel > *,
.workouts-panel > *,
.workout-detail-panel > * {
    min-width: 0;
}

.app-panel-body,
.workout-form-fields,
.workout-detail-fields {
    min-height: 0;
    align-content: start;
    overflow-y: auto;
}

.workout-detail-body {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    overflow: hidden;
}

.workout-detail-meta {
    flex: 0 0 auto;
}

.workout-detail-exercises-block {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex: 1 1 auto;
    min-height: 0;
}

.workout-detail-exercises-title {
    margin: 0;
    flex: 0 0 auto;
    font-size: 18px;
}

.workout-detail-exercises {
    flex: 1 1 auto;
    min-height: 0;
}

.workout-detail-exercises .exercise-view-row {
    cursor: default;
}

.panel-toolbar-actions {
    display: flex;
    gap: 6px;
    align-items: center;
    flex: 0 0 auto;
}

.page-actions {
    display: grid;
    gap: 16px;
}

.page-actions .wide + .wide {
    margin-top: 0;
}

.panel-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    min-width: 0;
    min-height: 44px;
}

.panel-toolbar > h2 {
    margin-bottom: 0;
    min-width: 0;
}

.panel-toolbar-title {
    min-width: 0;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
}

.panel-toolbar-title::-webkit-scrollbar {
    display: none;
}

.panel-toolbar-title h2 {
    margin: 0;
    display: inline-block;
    white-space: nowrap;
}

.panel-toolbar-title.is-overflowing {
    mask-image: linear-gradient(90deg, #000 calc(100% - 18px), transparent);
    -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 18px), transparent);
}

.sets-table-wrap {
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
}

.exercises-table-wrap {
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
}

.workouts-list-wrap {
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
}

.workouts-list {
    display: grid;
}

.workouts-list .empty-cell {
    padding: 24px 16px;
}

.sets-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
}

.exercises-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
}

.sets-table th,
.sets-table td {
    padding: 8px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
}

.exercises-table th,
.exercises-table td {
    padding: 8px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
}

.sets-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #fbfcfa;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.exercises-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #fbfcfa;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.sets-table th:last-child,
.sets-table td:last-child {
    width: 104px;
    min-width: 104px;
    text-align: right;
}

.exercises-table th:last-child,
.exercises-table td:last-child {
    text-align: left;
}

.sets-table tr:last-child td {
    border-bottom: 0;
}

.exercises-table tr:last-child td {
    border-bottom: 0;
}

.sets-table .set-row {
    display: table-row;
}

.exercises-table .exercise-edit-row {
    display: table-row;
    cursor: pointer;
}

.exercise-row-name--static {
    display: block;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
}

.sets-table input {
    min-height: 40px;
    padding: 0 10px;
}

.exercises-table input {
    min-height: 40px;
    padding: 0 10px;
}

.exercises-table input[readonly] {
    cursor: pointer;
}

.set-action-cell {
    white-space: nowrap;
    display: inline-flex;
    gap: 6px;
    align-items: center;
    justify-content: flex-end;
    vertical-align: middle;
}

.exercise-action-cell {
    white-space: nowrap;
}

.exercise-row-cell {
    padding-top: 8px;
    padding-bottom: 8px;
}

.exercise-row-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: 8px;
    align-items: center;
    min-width: 0;
    width: 100%;
}

.exercise-row-main {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.exercise-row-name {
    min-width: 0;
    max-width: 100%;
    padding: 0;
}

.exercise-row-name.exercise-row-name--static {
    display: block;
    min-height: 0;
    padding: 0;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
    white-space: nowrap;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
}

.exercise-row-name.exercise-row-name--static::-webkit-scrollbar {
    display: none;
}

.exercise-row-actions {
    display: flex;
    gap: 6px;
    align-items: center;
    flex: 0 0 auto;
}

.exercise-sets-summary {
    display: block;
    min-width: 0;
    max-width: 100%;
    margin: 0;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    white-space: nowrap;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.3;
}

.exercise-sets-summary::-webkit-scrollbar {
    display: none;
}

.exercise-list + .action-button {
    margin-top: 2px;
}

.error-text {
    color: var(--danger);
}

.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
}

.workout-table {
    width: 100%;
    min-width: 440px;
    border-collapse: collapse;
    background: #fbfcfa;
}

.workout-table th,
.workout-table td {
    padding: 12px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.workout-table th {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.workout-table tr:last-child td {
    border-bottom: 0;
}

.workout-row {
    display: grid;
    gap: 4px;
    width: 100%;
    padding: 14px 16px;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.workout-row:last-child {
    border-bottom: 0;
}

.workout-row-name {
    font-size: 16px;
    font-weight: 700;
    line-height: 1.3;
}

.workout-row-date {
    color: var(--muted);
    font-size: 13px;
    line-height: 1.3;
}

.workout-row:hover,
.workout-row:focus {
    background: #eef4ef;
    outline: none;
}

.workout-row:active {
    background: #eef4ef;
}

.empty-cell {
    color: var(--muted);
    text-align: center;
}

.mono-cell {
    color: var(--muted);
    font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
    font-size: 12px;
}

.form {
    display: grid;
    gap: 16px;
}

label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 700;
}

input,
textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfcfa;
    color: var(--text);
    font: inherit;
    font-weight: 500;
}

input {
    min-height: 44px;
    padding: 0 12px;
}

textarea {
    resize: vertical;
    padding: 12px;
}

textarea[rows="1"] {
    min-height: 44px;
    padding: 10px 12px;
    resize: none;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
}

input:disabled,
textarea:disabled {
    background: #f3f6f2;
    color: var(--text);
    opacity: 1;
}

#workoutDetailForm.is-editing input,
#workoutDetailForm.is-editing textarea {
    border-color: var(--accent);
    background: #ffffff;
    box-shadow: 0 0 0 2px rgba(23, 107, 77, 0.12);
}

.editor-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 72px 72px;
    gap: 8px;
}

@media (max-width: 380px) {
    .summary-grid {
        grid-template-columns: 1fr;
    }

    .editor-row {
        grid-template-columns: 1fr;
    }
}
