:root {
    --bg: #090b11;
    --bg-2: #0e1118;
    --surface: rgba(255, 255, 255, 0.05);
    --surface-2: rgba(255, 255, 255, 0.08);
    --border: rgba(255, 255, 255, 0.1);
    --border-2: rgba(255, 255, 255, 0.18);
    --text: #f0f2f5;
    --text-2: #8b95a5;
    --accent: #6d8aff;
    --accent-2: #4a6cf5;
    --success: #34d399;
    --danger: #f87171;
    --radius: 20px;
    --radius-sm: 14px;
    --radius-xs: 10px;
    --nav-h: 64px;
    --topbar-h: 62px;
}

body.theme-light {
    --bg: #f0f2f8;
    --bg-2: #e8ebf2;
    --surface: rgba(255, 255, 255, 0.6);
    --surface-2: rgba(255, 255, 255, 0.9);
    --border: rgba(0, 0, 0, 0.08);
    --border-2: rgba(0, 0, 0, 0.14);
    --text: #1a1f2e;
    --text-2: #6b7585;
    --accent: #4a6cf5;
    --accent-2: #3451d1;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
}

body {
    background: var(--bg);
    background-image:
        radial-gradient(ellipse 80% 50% at 20% -10%, rgba(74, 108, 245, 0.15), transparent),
        radial-gradient(ellipse 60% 40% at 80% 0%, rgba(109, 138, 255, 0.1), transparent),
        radial-gradient(ellipse 50% 30% at 50% 100%, rgba(74, 108, 245, 0.06), transparent);
    color: var(--text);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overscroll-behavior: none;
    touch-action: manipulation;
    user-select: none;
}

body.theme-light {
    background-image:
        radial-gradient(ellipse 80% 50% at 20% -10%, rgba(74, 108, 245, 0.08), transparent),
        radial-gradient(ellipse 60% 40% at 80% 0%, rgba(109, 138, 255, 0.06), transparent);
}

.app-shell {
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    position: relative;
}

.topbar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 18px;
    padding-top: calc(env(safe-area-inset-top, 12px) + 8px);
    height: var(--topbar-h);
    background: var(--bg);
    position: relative;
    z-index: 10;
}

.brand-lockup {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-mark {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.9);
    display: grid;
    place-items: center;
    overflow: hidden;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.logo-mark img {
    width: 26px;
    height: 26px;
    object-fit: contain;
}

.brand-info {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.eyebrow {
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--accent);
}

.brand-title {
    font-size: 16px;
    font-weight: 700;
    line-height: 1.2;
    color: var(--text);
}

.topbar-side {
    text-align: right;
}

.page-subtitle {
    font-size: 11px;
    color: var(--text-2);
    font-weight: 500;
}

.flash {
    position: absolute;
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px) + 8px);
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 32px);
    max-width: 420px;
    z-index: 50;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    border: 1px solid var(--border-2);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    font-size: 13px;
    font-weight: 500;
    text-align: center;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.flash.show {
    opacity: 1;
}

.app-content {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 20px);
}

.view {
    display: none;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.view.active {
    display: flex;
}

.view.hidden {
    display: none !important;
}

.register-card {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.register-form {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: grid;
    gap: 14px;
    padding-right: 4px;
}

.register-form::-webkit-scrollbar {
    width: 3px;
}

.register-form::-webkit-scrollbar-track {
    background: transparent;
}

.register-form::-webkit-scrollbar-thumb {
    background: var(--border-2);
    border-radius: 999px;
}

#registerView {
    overflow-y: auto;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px) + 70px);
}

#registerView .card {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

#registerForm {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: grid;
    gap: 14px;
    padding-right: 4px;
    overscroll-behavior: contain;
}

.tab-panel {
    display: none;
    flex-direction: column;
    width: 100%;
    height: 100%;
    padding: 0 16px;
    gap: 10px;
}

.tab-panel.active {
    display: flex;
}

.tab-panel > .card {
    flex-shrink: 0;
}

.tab-panel > .scroll-card {
    flex: 1;
    min-height: 0;
}

#registerView {
    overflow-y: auto;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px) + 70px);
}

#registerView .card {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#registerForm {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: grid;
    gap: 14px;
    padding-right: 4px;
    overscroll-behavior: contain;
}

.card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.card.scroll-card {
    display: flex;
    flex-direction: column;
}

.loading-card {
    display: grid;
    place-items: center;
    gap: 12px;
    min-height: 200px;
}

.loading-card p {
    color: var(--text-2);
    font-size: 14px;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.section-head {
    margin-bottom: 14px;
}

.section-head h2 {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 4px;
}

.section-head h3 {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 4px;
}

.section-head p {
    font-size: 12px;
    color: var(--text-2);
    line-height: 1.5;
}

.stack {
    display: grid;
    gap: 10px;
}

.stack-lg {
    display: grid;
    gap: 14px;
}

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

label {
    display: grid;
    gap: 6px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-2);
}

label > span {
    padding-left: 2px;
}

input, select, textarea {
    width: 100%;
    border: 1px solid var(--border-2);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    font-size: 14px;
    font-weight: 500;
    outline: none;
    transition: border-color 0.15s ease, background 0.15s ease;
    appearance: none;
    -webkit-appearance: none;
}

input::placeholder {
    color: var(--text-2);
    opacity: 0.6;
}

input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
    background: var(--surface-2);
}

select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238b95a5' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    padding-right: 36px;
    cursor: pointer;
}

select option {
    background: var(--bg);
    color: var(--text);
}

input[type="time"] {
    cursor: pointer;
}

.btn {
    border: none;
    background: var(--accent);
    color: #fff;
    border-radius: var(--radius-sm);
    min-height: 48px;
    padding: 0 20px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    letter-spacing: 0.01em;
}

.btn:hover {
    background: var(--accent-2);
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
    opacity: 0.9;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn-icon {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border-2);
    width: 48px;
    min-width: 48px;
    padding: 0;
    flex-shrink: 0;
}

.btn-icon:hover {
    background: var(--surface-2);
    border-color: var(--accent);
    color: var(--accent);
}

.summary-card {
    position: relative;
    overflow: hidden;
    background: linear-gradient(135deg, rgba(74, 108, 245, 0.12) 0%, rgba(74, 108, 245, 0.04) 100%);
    border-color: rgba(74, 108, 245, 0.2);
}

.summary-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 14px;
}

.today-date {
    font-size: 11px;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: 4px;
    margin-top: 2px;
}

.summary-card h2 {
    font-size: 18px;
    font-weight: 800;
    line-height: 1.3;
}

.plan-duration {
    font-size: 12px;
    color: var(--text-2);
    margin-top: 3px;
}

.summary-icon {
    width: 46px;
    height: 46px;
    border-radius: 14px;
    background: linear-gradient(135deg, rgba(74, 108, 245, 0.3), rgba(74, 108, 245, 0.1));
    border: 1px solid rgba(74, 108, 245, 0.2);
    display: grid;
    place-items: center;
    flex-shrink: 0;
}

.summary-icon svg {
    width: 22px;
    height: 22px;
    fill: var(--accent);
}

.stat-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 14px;
}

.stat-pill {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.stat-pill span {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-2);
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.stat-pill strong {
    font-size: 18px;
    font-weight: 800;
    color: var(--text);
}

.day-selector {
    display: flex;
    overflow-x: auto;
    gap: 6px;
    padding: 4px 0;
    margin-bottom: 10px;
    overscroll-behavior: contain;
}

.day-selector::-webkit-scrollbar {
    height: 0;
}

.day-btn {
    flex-shrink: 0;
    min-width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
}

.day-btn span {
    font-size: 9px;
    font-weight: 500;
    text-transform: uppercase;
}

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

.day-btn.today {
    border-color: var(--accent);
    border-width: 2px;
}

.exercise-list {
    flex: 1;
    overflow-y: auto;
    display: grid;
    gap: 8px;
    padding-right: 2px;
    overscroll-behavior: contain;
}

.exercise-list::-webkit-scrollbar {
    width: 4px;
}

.exercise-list::-webkit-scrollbar-track {
    background: transparent;
}

.exercise-list::-webkit-scrollbar-thumb {
    background: var(--border-2);
    border-radius: 999px;
}

.exercise-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    transition: border-color 0.15s ease;
}

.exercise-item:hover {
    border-color: var(--border-2);
}

.exercise-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 8px;
}

.exercise-number {
    width: 26px;
    height: 26px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: grid;
    place-items: center;
    flex-shrink: 0;
}

.exercise-name {
    font-size: 14px;
    font-weight: 700;
    line-height: 1.3;
    flex: 1;
}

.exercise-stats {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.meta-chip {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-2);
}

.exercise-guide {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    padding: 8px 12px;
    font-size: 11px;
    line-height: 1.55;
    color: var(--text-2);
}

.ai-chat-card {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex: 1;
    min-height: 0;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    height: 100%;
}

.ai-chat-card .section-head {
    flex-shrink: 0;
    margin-bottom: 0;
    padding: 14px 16px 10px;
    border-bottom: 1px solid var(--border);
}

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px 16px;
    overscroll-behavior: contain;
    contain: layout style;
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-2);
    border-radius: 999px;
}

.chat-bubble {
    max-width: 85%;
    padding: 11px 14px;
    border-radius: 16px;
    font-size: 13px;
    line-height: 1.55;
    word-break: break-word;
    flex-shrink: 0;
}

.chat-bubble.user {
    align-self: flex-end;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: #fff;
    border-bottom-right-radius: 6px;
}

.chat-bubble.assistant {
    align-self: flex-start;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    border-bottom-left-radius: 6px;
}

.chat-form {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    border-radius: 0 0 var(--radius) var(--radius);
    z-index: 10;
    position: relative;
}

.chat-form input {
    flex: 1;
    min-height: 42px;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 13px;
}

@media (max-height: 500px), (max-aspect-ratio: 3/4) {
    .ai-chat-card {
        height: auto;
        flex: 1;
    }
    
    .chat-messages {
        flex: 1;
        min-height: 120px;
    }
}

.theme-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-top: 4px;
}

.theme-card strong {
    font-size: 13px;
    font-weight: 700;
    display: block;
    margin-bottom: 2px;
}

.theme-card p {
    font-size: 11px;
    color: var(--text-2);
}

.theme-switcher {
    display: flex;
    gap: 4px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 3px;
    flex-shrink: 0;
}

.theme-btn {
    border: none;
    background: transparent;
    color: var(--text-2);
    font-size: 11px;
    font-weight: 700;
    padding: 7px 12px;
    border-radius: 9px;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

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

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 12px 0;
    border-top: 1px solid var(--border);
    margin-top: 4px;
}

.toggle-row strong {
    font-size: 13px;
    font-weight: 700;
    display: block;
    margin-bottom: 2px;
}

.toggle-row p {
    font-size: 11px;
    color: var(--text-2);
}

.switch {
    position: relative;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    inset: 0;
    background: var(--border-2);
    border-radius: 999px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.slider::before {
    content: "";
    position: absolute;
    width: 22px;
    height: 22px;
    left: 3px;
    top: 3px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s ease;
    box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.switch input:checked + .slider {
    background: var(--accent);
}

.switch input:checked + .slider::before {
    transform: translateX(20px);
}

.bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    display: flex;
    justify-content: center;
    padding: 0 16px;
    padding-bottom: calc(env(safe-area-inset-bottom, 8px) + 8px);
    background: var(--bg);
}

.bottom-nav-inner {
    display: flex;
    width: 100%;
    max-width: 480px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 8px;
    gap: 4px;
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    box-shadow: 0 -4px 24px rgba(0,0,0,0.12);
}

.view {
    display: none;
    flex-direction: column;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body.keyboard-open .app-content {
    padding-bottom: 14px;
}

body.keyboard-open .bottom-nav {
    padding-bottom: 6px;
}

body.keyboard-open .bottom-nav-inner {
    border-radius: 14px;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-height: 44px;
    border: none;
    background: transparent;
    border-radius: var(--radius-sm);
    color: var(--text-2);
    cursor: pointer;
    transition: all 0.15s ease;
    padding: 6px 4px;
}

.nav-btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: fill 0.15s ease;
}

.nav-btn span {
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.02em;
}

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

.nav-btn.active svg {
    fill: rgba(255,255,255,0.15);
    stroke: #fff;
}

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

.muted-sm {
    color: var(--text-2);
    font-size: 12px;
}

@media (min-width: 500px) {
    .app-shell {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
}