:root {
    --bg: #f4f6f8;
    --card: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --border: #e5e7eb;
    --error: #b91c1c;
    --error-bg: #fee2e2;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 1.5rem;
}
.topbar .brand { font-weight: 600; }
.topbar nav a {
    margin-right: 1rem;
    color: var(--text);
    text-decoration: none;
}
.topbar nav a:hover { color: var(--primary); }
.user-box { display: flex; align-items: center; gap: 1rem; color: var(--muted); font-size: 0.9rem; }
.user-box a { color: var(--primary); text-decoration: none; }

.container {
    max-width: 1000px;
    margin: 2rem auto;
    padding: 0 1.5rem;
}

h1 { font-size: 1.5rem; margin-bottom: 1rem; }

.login-box {
    max-width: 360px;
    margin: 4rem auto;
    background: var(--card);
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.login-box h1 { text-align: center; font-size: 1.25rem; }
.login-box form { display: flex; flex-direction: column; gap: 0.75rem; }
.login-box label { font-size: 0.85rem; color: var(--muted); }
.login-box input {
    padding: 0.5rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 1rem;
}
.login-box button {
    margin-top: 0.5rem;
    padding: 0.6rem;
    border: none;
    border-radius: 6px;
    background: var(--primary);
    color: white;
    font-size: 1rem;
    cursor: pointer;
}
.login-box button:hover { background: var(--primary-dark); }

.flashes { list-style: none; padding: 0; margin-bottom: 1rem; }
.flash-error, .flashes .flash-error {
    background: var(--error-bg);
    color: var(--error);
    padding: 0.6rem 0.9rem;
    border-radius: 6px;
    font-size: 0.9rem;
}

.file-picker { margin-bottom: 1rem; }
.file-picker select {
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.table-wrap,
.table-container {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow-x: auto;              /* scroll horizontal no desktop */
    overflow-y: auto;              /* scroll vertical no desktop */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    max-height: calc(100vh - 160px); /* título + nav; activa scroll vertical */
    width: 100%;
    max-width: 100%;
}

table {
    border-collapse: collapse;
    width: max-content;            /* cresce com o conteúdo; não esmaga colunas */
    min-width: 100%;               /* preenche o contentor se couber */
    font-size: 0.9rem;
    table-layout: auto;
}
th, td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border);
    text-align: left;
    white-space: nowrap;           /* nunca partir cabeçalhos/números */
}
th { background: #f9fafb; position: sticky; top: 0; }
tr:hover td { background: #f3f4f6; }

.row-count { color: var(--muted); font-size: 0.85rem; margin-top: 0.5rem; }
.empty { color: var(--muted); }

.year-list { list-style: none; padding: 0; }
.year-list li { margin-bottom: 0.5rem; }
.year-list a {
    display: inline-block;
    background: var(--card);
    border: 1px solid var(--border);
    padding: 0.5rem 1rem;
    border-radius: 6px;
    text-decoration: none;
    color: var(--text);
}
.year-list a:hover { border-color: var(--primary); color: var(--primary); }

/* ------------------------------------------------------------------
   Mobile (portrait + landscape): scroll horizontal, não esmagar colunas
   ------------------------------------------------------------------ */
@media screen and (max-width: 900px) {
    body {
        padding: 8px 10px;
        overflow-x: hidden; /* o scroll fica no contentor da tabela */
    }

    .container {
        padding: 0 0.5rem;
        margin: 1rem auto;
    }

    .topbar {
        flex-wrap: wrap;
        gap: 0.5rem;
        padding: 0.6rem 0.9rem;
    }
    .topbar nav a {
        margin-right: 0.6rem;
        font-size: 0.9rem;
    }

    .table-wrap,
    .table-container {
        width: 100%;
        max-width: 100%;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    /* NÃO usar table-layout:fixed nem min-width:100% —
       isso esmaga as colunas e parte os cabeçalhos no telemóvel. */
    table {
        width: auto !important;
        min-width: 920px !important; /* força scroll horizontal em ecrãs estreitos */
        table-layout: auto !important;
        font-size: 0.82rem !important;
    }

    th,
    td {
        white-space: nowrap !important;
        word-break: normal !important;
        overflow: visible !important;
        text-overflow: clip !important;
        padding: 6px 8px !important;
    }
}

/* Landscape telemóvel: um pouco menos de min-width se couber */
@media screen and (max-width: 900px) and (orientation: landscape) {
    table {
        min-width: 860px !important;
        font-size: 0.8rem !important;
    }
    body {
        padding: 6px 10px;
    }
    h1, h2 {
        font-size: 1.15rem !important;
        margin-bottom: 12px !important;
    }
}
