:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --bg-color: #f8fafc;
    --card-bg: rgba(255, 255, 255, 0.8);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --sidebar-width: 260px;
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07);
    --row-hover: rgba(99, 102, 241, 0.04);
}

[data-theme='dark'] {
    --bg-color: #0f172a;
    --card-bg: rgba(30, 41, 59, 0.8);
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
    --row-hover: rgba(255, 255, 255, 0.05);
}

[data-theme='sepia'] {
    --bg-color: #f4ecd8;
    --card-bg: rgba(255, 253, 241, 0.9);
    --text-main: #5b4636;
    --text-muted: #8c7355;
    --border-color: #d6c4a8;
    --glass-shadow: 0 8px 32px 0 rgba(91, 70, 54, 0.05);
    --row-hover: rgba(91, 70, 54, 0.05);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

body {
    background: var(--bg-color);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    /* Evita que qualquer conteúdo largo (tabelas, imagens) empurre a página na horizontal */
    overflow-x: hidden;
}

img {
    max-width: 100%;
}

/* Glassmorphism Sidebar */
.sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    border-right: 1px solid var(--border-color);
    position: fixed;
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    z-index: 100;
    overflow-y: auto;
}

/* Botão hamburger — só aparece em ecrãs pequenos (ver media query no fim) */
.menu-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 11px;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    color: var(--text-main);
    cursor: pointer;
    flex-shrink: 0;
}

/* Fundo escurecido quando o menu está aberto em mobile */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(2px);
    z-index: 99;
}

.sidebar-backdrop.visible {
    display: block;
}

.logo {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--primary-color);
    margin-bottom: 3rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.nav-links {
    list-style: none;
    flex: 1;
}

.nav-links li {
    margin-bottom: 0.5rem;
}

.nav-links a {
    text-decoration: none;
    color: var(--text-muted);
    padding: 0.25rem 1rem;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: all 0.3s ease;
    font-weight: 500;
}

.nav-links a:hover,
.nav-links a.active {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary-color);
}

/* Main Content */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 2rem;
}

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

/* Cards */
.card {
    background: var(--card-bg);
    backdrop-filter: blur(4px);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 1.5rem;
    box-shadow: var(--glass-shadow);
    transition: transform 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
}

/* Grid Layout */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border-radius: 12px;
    border: none;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-fade {
    animation: fadeIn 0.5s ease forwards;
}

/* Table Hover Global */
table tbody tr {
    transition: background 0.2s ease;
}

table tbody tr:hover {
    background: var(--row-hover) !important;
}

/* Cartão de exercício (sessão de treino) */
.exercicio-card {
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
}

.exercicio-img {
    width: 160px;
    height: 160px;
    object-fit: cover;
    border-radius: 12px;
    flex-shrink: 0;
}

.exercicio-info {
    flex: 1;
    min-width: 0;
}

.series-table input {
    width: 78px;
    padding: 0.4rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-main);
}

@media (max-width: 640px) {

    /* Imagem passa a banner no topo, dando largura total ao conteúdo */
    .exercicio-card {
        flex-direction: column;
        gap: 1rem;
    }

    .exercicio-img {
        width: 100%;
        height: 190px;
    }

    .series-table {
        width: 100%;
    }

    .series-table input {
        width: 100%;
        min-width: 64px;
    }
}

/* Tabelas largas: rolam dentro do próprio contentor em vez de esticar a página */
.table-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table-wrap table {
    min-width: 520px;
}

/* Cards start visible by default. JS animates them on load via inline styles directly. */

/* Formulários */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.25rem;
}

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

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

.form-field.full {
    grid-column: 1 / -1;
}

.form-field label {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-main);
    letter-spacing: -0.01em;
}

.form-field input[type="text"],
.form-field input[type="number"],
.form-field input[type="email"],
.form-field input[type="password"],
.form-field input[type="date"],
.form-field select,
.form-field textarea {
    width: 100%;
    padding: 0.7rem 0.85rem;
    border-radius: 10px;
    border: 1.5px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-main);
    font-size: 0.95rem;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    background: var(--card-bg);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.form-field select {
    appearance: none;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.85rem center;
    padding-right: 2.4rem;
    cursor: pointer;
}

.form-field small {
    color: var(--text-muted);
    font-size: 0.78rem;
    line-height: 1.4;
}

/* Input com sufixo de unidade (kg, cm, min...) */
.input-unit {
    position: relative;
    display: flex;
    align-items: center;
}

.input-unit input {
    padding-right: 3.2rem !important;
}

.input-unit span {
    position: absolute;
    right: 0.9rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    pointer-events: none;
}

/* Secções do formulário — separadores tipográficos, sem caixas aninhadas */
.form-section {
    margin-bottom: 2.5rem;
}

.form-section:last-of-type {
    margin-bottom: 1.5rem;
}

.form-section-head {
    display: flex;
    align-items: baseline;
    gap: 0.75rem;
    margin-bottom: 1.1rem;
    padding-bottom: 0.6rem;
    border-bottom: 1px solid var(--border-color);
}

.form-section-head h3 {
    margin: 0;
    font-size: 0.78rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--primary-color);
    white-space: nowrap;
}

.form-section-head p {
    margin: 0;
    font-size: 0.8rem;
    color: var(--text-muted);
}

@media (max-width: 620px) {
    .form-section-head {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.2rem;
    }
}

/* Cartões de escolha (radio) */
.choice-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 0.75rem;
}

.choice-tile {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.55rem;
    text-align: center;
    padding: 1.15rem 0.85rem;
    border-radius: 14px;
    border: 1.5px solid var(--border-color);
    background: var(--card-bg);
    cursor: pointer;
    transition: all 0.18s ease;
}

.choice-tile input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.choice-tile .tile-icon {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    background: var(--bg-color);
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.18s ease;
}

.choice-tile .tile-label {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text-main);
    line-height: 1.25;
}

.choice-tile .tile-desc {
    font-size: 0.72rem;
    color: var(--text-muted);
    line-height: 1.3;
}

.choice-tile:hover {
    border-color: var(--primary-color);
    transform: translateY(-2px);
}

.choice-tile:has(input:checked) {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.07);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.choice-tile:has(input:checked) .tile-icon {
    background: var(--primary-color);
    color: #fff;
}

.choice-tile:has(input:checked) .tile-label {
    color: var(--primary-color);
    font-weight: 700;
}

/* Lista de escolhas em linha (para opções com texto longo) */
.choice-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.choice-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.8rem 1rem;
    border-radius: 12px;
    border: 1.5px solid var(--border-color);
    background: var(--card-bg);
    cursor: pointer;
    transition: all 0.15s ease;
}

.choice-row input {
    accent-color: var(--primary-color);
    cursor: pointer;
    margin: 0;
    flex-shrink: 0;
}

.choice-row:hover {
    border-color: var(--primary-color);
}

.choice-row:has(input:checked) {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.07);
}

.choice-row .row-label {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-main);
}

.choice-row:has(input:checked) .row-label {
    color: var(--primary-color);
}

.choice-row .row-desc {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-left: auto;
    text-align: right;
}

@media (max-width: 560px) {
    .choice-row {
        flex-wrap: wrap;
    }

    .choice-row .row-desc {
        margin-left: 0;
        width: 100%;
        text-align: left;
    }
}

/* Controlo segmentado (2-3 opções curtas) */
.segmented {
    display: inline-flex;
    padding: 4px;
    gap: 4px;
    border-radius: 12px;
    background: var(--bg-color);
    border: 1.5px solid var(--border-color);
}

.segmented label {
    position: relative;
    padding: 0.55rem 1.5rem;
    border-radius: 9px;
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s ease;
}

.segmented label input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.segmented label:has(input:checked) {
    background: var(--primary-color);
    color: #fff;
}

/* Pills selecionáveis (checkbox) */
.checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
}

.checkbox-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.55rem 1rem;
    border-radius: 30px;
    border: 1.5px solid var(--border-color);
    background: var(--card-bg);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-main);
    user-select: none;
    transition: all 0.15s ease;
}

.checkbox-pill:hover {
    border-color: var(--primary-color);
    transform: translateY(-1px);
}

.checkbox-pill input {
    cursor: pointer;
    accent-color: var(--primary-color);
    margin: 0;
}

.checkbox-pill:has(input:checked) {
    border-color: var(--primary-color);
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary-color);
    font-weight: 600;
}

/* Barra de ações do formulário */
.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
    padding-top: 0.5rem;
}

.form-actions .btn {
    padding: 0.85rem 2rem;
}

@media (max-width: 600px) {
    .form-actions {
        justify-content: stretch;
    }

    .form-actions .btn {
        flex: 1;
    }
}


/* ==========================================================================
   RESPONSIVO
   ========================================================================== */

/* --- Tablet (até 1024px): conteúdo mais compacto --- */
@media (max-width: 1024px) {
    :root {
        --sidebar-width: 220px;
    }

    .main-content {
        padding: 1.5rem;
    }

    .dashboard-grid {
        gap: 1rem;
    }
}

/* --- Mobile e tablet vertical (até 900px): menu passa a gaveta lateral --- */
@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
        width: 270px;
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.18);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
        padding: 1rem;
        width: 100%;
    }

    .menu-toggle {
        display: inline-flex;
    }

    .top-bar {
        gap: 0.75rem;
        margin-bottom: 1.5rem;
    }

    .top-bar h1 {
        font-size: 1.25rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Impede scroll do fundo enquanto a gaveta está aberta */
    body.menu-open {
        overflow: hidden;
    }

    .card {
        padding: 1.25rem;
        border-radius: 16px;
    }

    /* O efeito de levantar ao passar o rato não faz sentido em toque */
    .card:hover {
        transform: none;
    }
}

/* --- Smartphone (até 600px) --- */
@media (max-width: 600px) {
    .main-content {
        padding: 0.85rem;
    }

    .top-bar h1 {
        font-size: 1.1rem;
    }

    /* Esconde o nome do utilizador, mantém o ícone (poupa largura) */
    .user-info .user-name {
        display: none;
    }

    /* Seletor de tema mais estreito */
    #theme-selector {
        max-width: 62px;
        text-overflow: ellipsis;
    }

    .card {
        padding: 1rem;
        border-radius: 14px;
    }

    .dashboard-grid {
        grid-template-columns: 1fr;
        gap: 0.85rem;
    }

    /* Cartões de escolha: 2 por linha em vez de encolherem demais */
    .choice-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.6rem;
    }

    .choice-tile {
        padding: 0.9rem 0.6rem;
    }

    .choice-tile .tile-icon {
        width: 36px;
        height: 36px;
    }

    .choice-tile .tile-label {
        font-size: 0.82rem;
    }

    .choice-tile .tile-desc {
        font-size: 0.68rem;
    }

    .segmented {
        width: 100%;
    }

    .segmented label {
        flex: 1;
        text-align: center;
        padding: 0.55rem 0.75rem;
    }

    .form-section {
        margin-bottom: 1.75rem;
    }

    /* Alvos de toque maiores */
    .nav-links a {
        padding: 0.85rem 1rem;
    }

    .checkbox-pill {
        padding: 0.6rem 1rem;
    }
}

/* --- Ecrãs muito estreitos (até 380px) --- */
@media (max-width: 380px) {
    .choice-grid {
        grid-template-columns: 1fr;
    }
}

/* --- Impressão: sem menu nem botões --- */
@media print {

    .sidebar,
    .menu-toggle,
    .sidebar-backdrop,
    .top-bar,
    .form-actions,
    .btn {
        display: none !important;
    }

    .main-content {
        margin-left: 0;
        padding: 0;
    }

    .card {
        box-shadow: none;
        border: 1px solid #ddd;
        break-inside: avoid;
    }
}