:root {
    --green-900: #1a3d2e;
    --green-800: #234f3b;
    --green-700: #2d6a4f;
    --green-600: #40916c;
    --green-500: #52b788;
    --green-100: #d8f3dc;
    --green-50: #f0faf4;
    --slate-900: #0f172a;
    --slate-700: #334155;
    --slate-500: #64748b;
    --slate-200: #e2e8f0;
    --slate-100: #f1f5f9;
    --white: #ffffff;
    --danger: #dc2626;
    --warning: #d97706;
    --radius: 12px;
    --shadow: 0 4px 24px rgba(15, 23, 42, 0.08);
    --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

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

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: var(--font);
    color: var(--slate-900);
    background: var(--slate-100);
    line-height: 1.5;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
}

/* Auth */
.page-auth {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    position: relative;
    gap: 0;
}

.auth-bg {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, var(--green-900) 0%, var(--green-600) 50%, var(--green-100) 100%);
    z-index: 0;
}

.auth-card {
    position: relative;
    z-index: 1;
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 2.5rem;
    width: 100%;
    max-width: 420px;
    margin: 1rem;
}

.auth-logo { text-align: center; margin-bottom: 1.5rem; }

.brand-logo {
    display: block;
    width: auto;
    object-fit: contain;
}

.brand-logo-auth {
    height: 72px;
    max-width: 100%;
    margin: 0 auto;
    border-radius: 10px;
}

.brand-logo-header {
    height: 42px;
    max-width: 220px;
}

.topbar-brand-link {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.subtitle { color: var(--slate-500); font-size: 0.9rem; margin-top: 0.75rem; }

.auth-form label { display: block; font-weight: 600; font-size: 0.85rem; margin: 1rem 0 0.35rem; }
.auth-form input {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--slate-200);
    border-radius: 8px;
    font-size: 1rem;
}
.auth-form input:focus {
    outline: none;
    border-color: var(--green-600);
    box-shadow: 0 0 0 3px rgba(64, 145, 108, 0.2);
}

.auth-hint {
    margin-top: 1.25rem;
    font-size: 0.75rem;
    color: var(--slate-500);
    text-align: center;
}

.auth-footer {
    position: relative;
    z-index: 1;
    margin-top: 0.85rem;
    font-size: 0.625rem;
    font-weight: 400;
    letter-spacing: 0.03em;
    color: #fff;
    text-align: center;
}

.auth-footer a {
    color: #fff;
    text-decoration: none;
}

.auth-footer a:hover {
    color: #fff;
    opacity: 0.85;
}

/* App layout */
.page-app { display: flex; flex-direction: column; min-height: 100vh; }

.topbar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0 1.5rem;
    height: 60px;
    background: var(--green-900);
    color: var(--white);
    flex-wrap: wrap;
}

.topbar-brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; min-width: 0; }
.topbar-nav { display: flex; gap: 0.25rem; flex: 1; }
.topbar-nav a {
    color: rgba(255,255,255,0.75);
    text-decoration: none;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    font-size: 0.9rem;
}
.topbar-nav a:hover, .topbar-nav a.active {
    color: var(--white);
    background: rgba(255,255,255,0.12);
}

.topbar-user {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
}
.user-role { opacity: 0.7; font-size: 0.75rem; }

.main-content {
    flex: 1;
    padding: 1.5rem;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

.page-dashboard .main-content {
    max-width: 100%;
    padding-left: 1.25rem;
    padding-right: 1.25rem;
}

.app-footer {
    text-align: center;
    padding: 1rem;
    font-size: 0.8rem;
    color: var(--slate-500);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.35rem 0.5rem;
}

.app-footer a {
    color: var(--green-700);
    text-decoration: none;
    font-weight: 600;
}

.app-footer a:hover {
    text-decoration: underline;
}

.app-footer-sep {
    opacity: 0.5;
}

/* Page */
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.75rem; color: var(--green-900); }
.page-desc { color: var(--slate-500); margin-top: 0.25rem; }

.card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.55rem 1.1rem;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    border: none;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--green-700); color: var(--white); }
.btn-primary:hover { background: var(--green-800); }
.btn-secondary { background: var(--slate-200); color: var(--slate-900); }
.btn-secondary:hover { background: #cbd5e1; }
.btn-ghost { background: transparent; color: inherit; border: 1px solid rgba(255,255,255,0.3); }
.page-app .btn-ghost { color: var(--green-700); border-color: var(--slate-200); }
.btn-danger { background: var(--danger); color: var(--white); }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.8rem; }
.btn-block { width: 100%; margin-top: 1.25rem; }

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-warning { background: #fffbeb; color: var(--warning); border: 1px solid #fde68a; }
.alert a { color: inherit; font-weight: 600; }

/* Dashboard */
.dashboard-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
}

.date-nav {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 1rem 1.25rem;
    margin-bottom: 1rem;
}

.date-nav-form {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.date-nav-input {
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--slate-200);
    border-radius: 8px;
    font-size: 0.9rem;
}

.date-nav-btn {
    position: relative;
}

.date-cache-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--green-500);
    border-radius: 50%;
    margin-left: 0.35rem;
    vertical-align: middle;
}

.date-cache-hint {
    font-size: 0.85rem;
    margin: -0.5rem 0 1rem;
}

.date-cache-hint a {
    color: var(--green-700);
    text-decoration: none;
}

.date-cache-hint a:hover {
    text-decoration: underline;
}

.date-cache-hint a.date-cache-active {
    font-weight: 700;
    text-decoration: underline;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.dashboard-status {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.status-pill {
    font-size: 0.8rem;
    font-weight: 600;
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
}
.status-pill.status-ok { background: var(--green-100); color: var(--green-800); }
.status-pill.status-warn { background: #fffbeb; color: var(--warning); }
.status-pill.status-error { background: #fee2e2; color: var(--danger); }

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.kpi-card {
    background: linear-gradient(135deg, var(--green-800), var(--green-600));
    color: var(--white);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

.kpi-label {
    display: block;
    font-size: 0.8rem;
    opacity: 0.85;
    margin-bottom: 0.35rem;
}

.kpi-value {
    display: block;
    font-size: 1.75rem;
    font-weight: 700;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.25rem;
}

.widget-layout-wide {
    grid-column: 1 / -1;
}

.widget-chart .widget-body {
    padding-top: 0.25rem;
}

.chart-wrap {
    position: relative;
    width: 100%;
    height: min(42vh, 420px);
    min-height: 320px;
    margin-bottom: 0.75rem;
}

.chart-wrap canvas {
    width: 100% !important;
    height: 100% !important;
}

.chart-wrap-pie {
    height: min(50vh, 480px);
    min-height: 360px;
}

.widget-vista-toolbar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.widget-vista-toolbar .vista-btn {
    background: var(--slate-100);
    color: var(--slate-700);
    border: 1px solid var(--slate-200);
}

.widget-vista-toolbar .vista-btn.active {
    background: var(--green-700);
    color: var(--white);
    border-color: var(--green-700);
}

.widget-vista-panel[hidden] {
    display: none;
}

.chart-legend {
    display: flex;
    gap: 1.25rem;
    font-size: 0.85rem;
    margin-bottom: 1rem;
    color: var(--slate-700);
}

.legend-total { color: var(--green-700); font-weight: 600; }
.legend-qty { color: var(--warning); font-weight: 600; }

.chart-toggle-table {
    margin-bottom: 0.75rem;
    color: var(--green-700);
    border-color: var(--slate-200);
}

.chart-table-panel {
    margin-top: 0.5rem;
}

.chart-table-panel[hidden] {
    display: none;
}

.chart-table-mini {
    max-height: 220px;
}

.page-app .chart-toggle-table.btn-ghost:hover {
    background: var(--green-50);
}

.widget-meta {
    display: block;
    font-size: 0.8rem;
    color: var(--slate-500);
    font-weight: 400;
    margin-top: 0.15rem;
}

.cell-num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.widget[data-widget-id="w_artigos_detalhe"] .data-table th:nth-child(n+3),
.widget[data-widget-id="w_artigos_detalhe"] .data-table td:nth-child(n+3) {
    text-align: right;
}

.table-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem 1rem;
    margin-bottom: 0.75rem;
}

.table-filter-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--slate-700);
}

.table-filter-input {
    flex: 1;
    min-width: 220px;
    max-width: 420px;
    padding: 0.55rem 0.85rem;
    border: 1px solid var(--slate-200);
    border-radius: 8px;
    font-size: 0.9rem;
}

.table-filter-input:focus {
    outline: none;
    border-color: var(--green-600);
    box-shadow: 0 0 0 3px rgba(64, 145, 108, 0.2);
}

.table-filter-count {
    font-size: 0.8rem;
    color: var(--slate-500);
    white-space: nowrap;
}

.table-filter-empty {
    margin-top: 0.5rem;
    font-size: 0.9rem;
}

.data-table tbody tr[hidden] {
    display: none;
}

.widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--slate-200);
    gap: 0.75rem;
    flex-wrap: wrap;
}

.widget-header-with-toolbar {
    align-items: flex-start;
}

.widget-periodo-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-left: auto;
}

.widget-header h2 { font-size: 1.1rem; color: var(--green-800); }

.badge {
    font-size: 0.7rem;
    padding: 0.2rem 0.5rem;
    border-radius: 999px;
    background: var(--slate-200);
    color: var(--slate-700);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge-ok { background: var(--green-100); color: var(--green-800); }
.badge-off { background: #fee2e2; color: var(--danger); }

.stat-value {
    font-size: 3rem;
    font-weight: 700;
    color: var(--green-700);
    text-align: center;
    padding: 1.5rem 0;
}

.table-wrap { overflow-x: auto; max-height: 400px; overflow-y: auto; }
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}
.data-table th, .data-table td {
    padding: 0.5rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--slate-200);
}
.data-table th {
    background: var(--green-50);
    color: var(--green-900);
    position: sticky;
    top: 0;
}
.data-table tr:hover td { background: var(--green-50); }

.empty-state {
    text-align: center;
    padding: 3rem 1.5rem;
}
.empty-state h2 { margin-bottom: 0.5rem; }
.text-muted { color: var(--slate-500); }

/* Config */
.config-layout {
    display: grid;
    gap: 1.25rem;
}

.config-form label {
    display: block;
    font-weight: 600;
    font-size: 0.85rem;
    margin: 1rem 0 0.35rem;
}
.config-form input[type="text"],
.config-form select,
.config-form textarea {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--slate-200);
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.9rem;
}
.config-form textarea { font-family: 'Consolas', 'Courier New', monospace; resize: vertical; }

.checkbox-label {
    display: flex !important;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem !important;
    font-weight: 500 !important;
}

.form-actions { display: flex; gap: 0.75rem; margin-top: 1.25rem; }
.test-form { margin-top: 0.75rem; }

.widget-list { list-style: none; }
.widget-list-item {
    padding: 1rem 0;
    border-bottom: 1px solid var(--slate-200);
}
.widget-list-item:last-child { border-bottom: none; }

.sql-preview {
    background: var(--slate-100);
    padding: 0.75rem;
    border-radius: 8px;
    font-size: 0.8rem;
    margin: 0.5rem 0;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
}

.item-actions { display: flex; gap: 0.5rem; align-items: center; }
.inline-form { display: inline; }

.info-dl {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 0.5rem 1rem;
    margin-top: 0.75rem;
    font-size: 0.9rem;
}
.info-dl dt { color: var(--slate-500); font-weight: 600; }
.status-ok { color: var(--green-700); font-weight: 600; }
.status-error { color: var(--danger); font-weight: 600; }

code {
    background: var(--slate-100);
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    font-size: 0.85em;
}

/* —— Telemóvel / iPhone —— */
@media (max-width: 768px) {
    .topbar {
        display: grid;
        grid-template-columns: 1fr auto;
        grid-template-areas:
            "brand user"
            "nav nav";
        align-items: center;
        gap: 0.5rem 0.75rem;
        height: auto;
        padding: 0.65rem 0.85rem;
        padding-top: max(0.65rem, env(safe-area-inset-top, 0px));
        padding-left: max(0.85rem, env(safe-area-inset-left, 0px));
        padding-right: max(0.85rem, env(safe-area-inset-right, 0px));
    }

    .topbar-brand {
        grid-area: brand;
        min-width: 0;
    }

    .brand-logo-header {
        height: 36px;
        max-width: 160px;
    }

    .topbar-nav {
        grid-area: nav;
        width: 100%;
        flex: none;
    }

    .topbar-nav a {
        flex: 1;
        text-align: center;
        padding: 0.55rem 0.5rem;
        font-size: 0.85rem;
    }

    .topbar-user {
        grid-area: user;
        width: auto;
        justify-content: flex-end;
        flex-wrap: nowrap;
        gap: 0.4rem;
        min-width: 0;
    }

    .user-name {
        max-width: 7rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .user-role {
        display: none;
    }

    .main-content {
        padding: 1rem 0.85rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    }

    .page-dashboard .main-content {
        padding-left: max(0.85rem, env(safe-area-inset-left, 0px));
        padding-right: max(0.85rem, env(safe-area-inset-right, 0px));
    }

    .page-header h1 {
        font-size: 1.35rem;
    }

    .dashboard-header {
        flex-direction: column;
        align-items: stretch;
    }

    .dashboard-status {
        flex-wrap: wrap;
        width: 100%;
    }

    .dashboard-status .btn {
        flex: 1 1 auto;
        min-width: 0;
    }

    .date-nav {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
        padding: 0.85rem;
    }

    .date-nav-form {
        width: 100%;
    }

    .date-nav-input {
        flex: 1;
        min-width: 0;
        width: 100%;
        font-size: 16px;
    }

    .date-nav .btn {
        width: 100%;
        justify-content: center;
        min-height: 44px;
    }

    .date-nav > span.btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }

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

    .widget-layout-wide {
        grid-column: auto;
    }

    .kpi-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .kpi-value {
        font-size: 1.45rem;
    }

    .chart-wrap {
        height: 260px;
        min-height: 220px;
    }

    .chart-wrap-pie {
        height: 280px;
        min-height: 240px;
    }

    .table-wrap {
        max-height: 55vh;
        -webkit-overflow-scrolling: touch;
        margin: 0 -0.25rem;
        padding: 0 0.25rem;
    }

    .data-table {
        font-size: 0.8rem;
    }

    .data-table th,
    .data-table td {
        padding: 0.45rem 0.5rem;
    }

    .table-filter-input {
        min-width: 0;
        max-width: none;
        width: 100%;
        font-size: 16px;
    }

    .table-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .widget-vista-toolbar {
        flex-wrap: wrap;
    }

    .widget-vista-toolbar .vista-btn {
        flex: 1 1 calc(50% - 0.25rem);
        min-height: 44px;
    }

    .widget-header-with-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .widget-periodo-toolbar {
        margin-left: 0;
        width: 100%;
    }

    .widget-periodo-toolbar .btn {
        flex: 1 1 auto;
        min-height: 40px;
    }

    .stat-value {
        font-size: 2rem;
    }

    .form-actions {
        flex-direction: column;
    }

    .form-actions .btn,
    .form-actions a.btn {
        width: 100%;
        min-height: 44px;
    }

    .info-dl {
        grid-template-columns: 1fr;
        gap: 0.15rem 0;
    }

    .info-dl dt {
        margin-top: 0.65rem;
    }

    .app-footer {
        flex-direction: column;
        padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    }

    .app-footer-sep {
        display: none;
    }

    .auth-form input,
    .config-form input[type="text"],
    .config-form input[type="email"],
    .config-form input[type="password"],
    .config-form select,
    .config-form textarea {
        font-size: 16px;
    }

    .btn {
        min-height: 44px;
    }

    .auth-card {
        padding: 1.5rem 1.25rem;
        margin: 0.75rem;
        margin-left: max(0.75rem, env(safe-area-inset-left, 0px));
        margin-right: max(0.75rem, env(safe-area-inset-right, 0px));
    }

    .page-auth {
        justify-content: center;
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
}

/* Ajustes específicos iOS (Safari iPhone) */
html.device-ios body {
    min-height: -webkit-fill-available;
}

html.device-ios .page-auth {
    min-height: 100dvh;
    min-height: -webkit-fill-available;
}

html.device-ios .page-app {
    min-height: 100dvh;
    min-height: -webkit-fill-available;
}

html.device-ios .auth-form input,
html.device-ios .date-nav-input,
html.device-ios .table-filter-input {
    font-size: 16px;
    border-radius: 10px;
}

html.device-ios .btn:active {
    transform: none;
    opacity: 0.88;
}
