:root {
    color-scheme: light dark;
    --bg: #edf7ff;
    --fg: #162330;
    --surface: #ffffff;
    --surface-border: #bfd9ee;
    --surface-shadow: rgba(0, 0, 0, 0.08);
    --field-border: #b8c8d7;
    --link: #176da3;
    --btn-bg: #176da3;
    --btn-fg: #ffffff;
    --error: #9f1a1a;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1722;
        --fg: #dce6f2;
        --surface: #132031;
        --surface-border: #2c4158;
        --surface-shadow: rgba(0, 0, 0, 0.32);
        --field-border: #3b4f66;
        --link: #7fc7ff;
        --btn-bg: #2f89c8;
        --btn-fg: #f5fbff;
        --error: #ff9a9a;
    }
}

html[data-theme="light"] {
    --bg: #edf7ff;
    --fg: #162330;
    --surface: #ffffff;
    --surface-border: #bfd9ee;
    --surface-shadow: rgba(0, 0, 0, 0.08);
    --field-border: #b8c8d7;
    --link: #176da3;
    --btn-bg: #176da3;
    --btn-fg: #ffffff;
    --error: #9f1a1a;
}

html[data-theme="dark"] {
    --bg: #0f1722;
    --fg: #dce6f2;
    --surface: #132031;
    --surface-border: #2c4158;
    --surface-shadow: rgba(0, 0, 0, 0.32);
    --field-border: #3b4f66;
    --link: #7fc7ff;
    --btn-bg: #2f89c8;
    --btn-fg: #f5fbff;
    --error: #ff9a9a;
}

html[data-theme="grey"] {
    --bg: #f0f0f0;
    --fg: #1e1e1e;
    --surface: #ffffff;
    --surface-border: #cccccc;
    --surface-shadow: rgba(0, 0, 0, 0.10);
    --field-border: #bbbbbb;
    --link: #555555;
    --btn-bg: #555555;
    --btn-fg: #ffffff;
    --error: #9f1a1a;
}

body.portal-page {
    font-family: Segoe UI, Tahoma, Arial, sans-serif;
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--fg);
    display: grid;
    grid-template-rows: auto 1fr;
}

/* ── Header bar ──────────────────────────────────────────── */
.portal-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--surface);
    border-bottom: 1px solid var(--surface-border);
    box-shadow: 0 2px 8px var(--surface-shadow);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0 1.5rem;
    height: 52px;
}

.portal-header__brand {
    font-weight: 700;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0.9;
}

.portal-header__tools {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-shrink: 0;
}

/* ── Language picker (details/summary dropdown) ─────────── */
.lang-picker {
    position: relative;
}

.lang-picker[open] > .lang-picker__toggle {
    border-color: var(--link);
    color: var(--link);
}

.lang-picker__toggle {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.4rem 0.7rem;
    list-style: none;
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
}

/* Remove the native disclosure triangle */
.lang-picker__toggle::-webkit-details-marker { display: none; }
.lang-picker__toggle::marker { display: none; }

.lang-picker__chevron {
    opacity: 0.6;
    transition: transform 0.15s ease;
}

.lang-picker[open] .lang-picker__chevron {
    transform: rotate(180deg);
}

.lang-picker__current {
    font-size: 0.9rem;
}

.lang-picker__menu {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 160px;
    background: var(--surface);
    border: 1px solid var(--surface-border);
    border-radius: 10px;
    box-shadow: 0 8px 24px var(--surface-shadow);
    padding: 0.4rem 0;
    margin: 0;
    list-style: none;
    z-index: 200;
    color-scheme: normal;
}

.lang-picker__option {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 1rem;
    text-decoration: none;
    color: var(--fg);
    font-size: 0.92rem;
    transition: background 0.1s;
}

button.lang-picker__option {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--fg);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.lang-picker__option:hover {
    background: color-mix(in srgb, var(--link) 10%, transparent);
}

.lang-picker__option.is-active {
    font-weight: 700;
    color: var(--link);
}

/* ── Flag icons ──────────────────────────────────────────── */
.flag-icon {
    display: inline-block;
    width: 22px;
    height: 15px;
    border-radius: 2px;
    border: 1px solid color-mix(in srgb, var(--surface-border) 70%, transparent);
    vertical-align: middle;
    flex-shrink: 0;
    object-fit: cover;
}

.portal-shell {
    width: min(980px, 95%);
    margin: 1.8rem auto 2rem;
    display: grid;
    gap: 1.15rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--surface-border);
    border-radius: 14px;
    box-shadow: 0 12px 30px var(--surface-shadow);
    padding: 1.6rem;
}

.card--narrow {
    width: min(560px, 100%);
    justify-self: center;
}

h1 {
    margin: 0 0 0.6rem;
    font-size: 1.7rem;
}

p {
    margin: 0.55rem 0;
    line-height: 1.45;
}

label {
    display: block;
    font-weight: 600;
}

input {
    width: 100%;
    margin-top: 0.3rem;
    margin-bottom: 0.8rem;
    padding: 0.55rem 0.65rem;
    border-radius: 8px;
    border: 1px solid var(--field-border);
    background: var(--surface);
    color: var(--fg);
    box-sizing: border-box;
}

a {
    color: var(--link);
}

code {
    font-family: Consolas, "Courier New", monospace;
}

.btn {
    display: inline-block;
    margin-top: 0.4rem;
    text-decoration: none;
    background: var(--btn-bg);
    color: var(--btn-fg);
    padding: 0.72rem 1rem;
    border-radius: 8px;
    font-weight: 600;
    border: none;
    cursor: pointer;
}

.error {
    color: var(--error);
    font-weight: 600;
}

.btn--ghost {
    background: transparent;
    color: var(--fg);
    border: 1px solid var(--surface-border);
}

.portal-tools {
    background: color-mix(in srgb, var(--surface) 88%, var(--bg));
    border: 1px solid var(--surface-border);
    border-radius: 12px;
    padding: 0.7rem 1rem;
    display: none;
}

.portal-tools__langs {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.portal-tools__label {
    font-weight: 600;
}

.portal-tools__langs a {
    text-decoration: none;
    opacity: 0.85;
}

.portal-tools__langs a.is-active {
    font-weight: 700;
    text-decoration: underline;
    opacity: 1;
}

.portal-tools .btn {
    margin-top: 0;
}

.stack-form {
    margin-top: 1rem;
}

.top-bar {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    align-items: center;
    flex-wrap: wrap;
}

.service-list {
    display: none;
}

.service-grid {
    margin-top: 1.2rem;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1.15rem;
}

.service-tile {
    border: 1px solid var(--surface-border);
    border-radius: 12px;
    padding: 1.2rem;
    display: grid;
    gap: 0.75rem;
    align-content: start;
    min-height: 210px;
    background: color-mix(in srgb, var(--surface) 90%, var(--bg));
}

.service-tile__group {
    margin: 0;
    font-size: 0.8rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    opacity: 0.72;
}

.service-tile__title {
    margin: 0;
    font-size: 1.12rem;
    line-height: 1.35;
}

.service-status {
    margin: 0;
    font-size: 0.9rem;
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    border: 1px solid var(--surface-border);
    width: fit-content;
}

.service-status__dot {
    width: 0.55rem;
    height: 0.55rem;
    border-radius: 50%;
    background: currentColor;
}

.service-status--up {
    color: #1b7f45;
    background: color-mix(in srgb, #1b7f45 12%, var(--surface));
}

.service-status--down {
    color: #a23434;
    background: color-mix(in srgb, #a23434 12%, var(--surface));
}

.service-status--unknown {
    color: #6b7280;
    background: color-mix(in srgb, #6b7280 12%, var(--surface));
}

.service-status--unknown .service-status__dot {
    animation: service-status-pulse 1.25s ease-in-out infinite;
}

.service-status--unknown + .service-status-age {
    opacity: 0.8;
}

.service-status-age {
    margin: 0;
    font-size: 0.8rem;
    color: color-mix(in srgb, var(--fg) 72%, transparent);
}

.service-tile .btn {
    margin-top: auto;
    width: fit-content;
}

@media (max-width: 980px) {
    .service-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 640px) {
    .portal-shell {
        width: min(94%, 100%);
        margin: 1rem auto;
    }

    .card {
        padding: 1.1rem;
    }

    .portal-header {
        padding: 0 1rem;
    }

    .lang-picker__current {
        display: none;
    }

    .service-grid {
        grid-template-columns: 1fr;
        gap: 0.9rem;
    }

    .service-tile {
        min-height: 0;
        padding: 1rem;
    }
}

/* iPhone 14 Pro Max / 15 Plus (430 × 932 logical) and similar large iPhones */
@media (max-width: 430px) {
    .portal-shell {
        width: 100%;
        margin: 0.75rem auto;
        gap: 0.85rem;
        border-radius: 0;
    }

    .card {
        border-radius: 12px;
        padding: 1rem;
    }

    .portal-tools {
        border-radius: 10px;
        padding: 0.65rem 0.75rem;
        gap: 0.6rem;
    }

    .service-grid {
        gap: 0.75rem;
    }

    .service-tile {
        padding: 0.9rem;
    }
}

/* ── Theme picker swatch dots ────────────────────────────────────────────── */
.theme-swatch-dot {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1px solid rgba(0,0,0,0.18);
    flex-shrink: 0;
    vertical-align: middle;
}

.theme-swatch-dot--light { background: #edf7ff; border-color: #bfd9ee; }
.theme-swatch-dot--dark  { background: #132031; border-color: #2c4158; }
.theme-swatch-dot--grey  { background: #e0e0e0; border-color: #aaaaaa; }

/* Swatch on the toggle button (larger) */
.theme-picker__swatch {
    display: inline-block;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid rgba(0,0,0,0.20);
    flex-shrink: 0;
    vertical-align: middle;
}

.theme-picker__swatch--light { background: #edf7ff; border-color: #bfd9ee; }
.theme-picker__swatch--dark  { background: #132031; border-color: #2c4158; }
.theme-picker__swatch--grey  { background: #e0e0e0; border-color: #aaaaaa; }

.theme-picker { position: relative; }

.theme-picker__toggle {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.4rem 0.7rem;
    list-style: none;
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
}

.theme-picker__toggle::-webkit-details-marker { display: none; }
.theme-picker__toggle::marker { display: none; }

.theme-picker[open] > .theme-picker__toggle {
    border-color: var(--link);
    color: var(--link);
}

.theme-picker[open] .lang-picker__chevron {
    transform: rotate(180deg);
}

.theme-picker__current {
    font-size: 0.9rem;
}

.top-bar__actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* ── Admin page ──────────────────────────────────────────────────────────── */
.admin-section {
    margin-top: 1.8rem;
}

.admin-section__title {
    margin: 0 0 1rem;
    font-size: 1.15rem;
    border-bottom: 1px solid var(--surface-border);
    padding-bottom: 0.5rem;
}

.admin-grant-form {
    display: grid;
    gap: 0.25rem;
    max-width: 540px;
}

.admin-grant-form .btn {
    justify-self: start;
    margin-top: 0.5rem;
}

.admin-select {
    width: 100%;
    margin-top: 0.3rem;
    margin-bottom: 0.8rem;
    padding: 0.55rem 0.65rem;
    border-radius: 8px;
    border: 1px solid var(--field-border);
    background: var(--surface);
    color: var(--fg);
    box-sizing: border-box;
}

.admin-table-wrap {
    overflow-x: auto;
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.admin-table th,
.admin-table td {
    text-align: left;
    padding: 0.55rem 0.75rem;
    border-bottom: 1px solid var(--surface-border);
    vertical-align: middle;
}

.admin-table th {
    font-weight: 700;
    background: color-mix(in srgb, var(--surface) 85%, var(--bg));
}

.admin-table tr:last-child td {
    border-bottom: none;
}

.admin-table tr:hover td {
    background: color-mix(in srgb, var(--link) 6%, transparent);
}

.admin-cell--nowrap {
    white-space: nowrap;
}

.admin-badge {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    border-radius: 20px;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    white-space: nowrap;
}

.admin-badge--admin {
    background: color-mix(in srgb, var(--btn-bg) 18%, transparent);
    color: var(--link);
    border: 1px solid color-mix(in srgb, var(--link) 30%, transparent);
}

.admin-badge--service {
    background: color-mix(in srgb, #0f5132 15%, transparent);
    color: var(--fg);
    border: 1px solid color-mix(in srgb, #0f5132 30%, transparent);
}

.btn--danger {
    background: color-mix(in srgb, var(--error) 80%, #000);
    color: #fff;
    border: none;
}

.btn--danger:hover {
    background: var(--error);
}

.btn--sm {
    padding: 0.3rem 0.6rem;
    font-size: 0.82rem;
    margin-top: 0;
}

.admin-status {
    padding: 0.6rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-weight: 600;
    display: none;
}

.admin-status--ok {
    display: block;
    background: color-mix(in srgb, #0f5132 15%, transparent);
    color: var(--fg);
    border: 1px solid color-mix(in srgb, #0f5132 30%, transparent);
}

.admin-status--err {
    display: block;
    background: color-mix(in srgb, var(--error) 12%, transparent);
    color: var(--error);
    border: 1px solid color-mix(in srgb, var(--error) 30%, transparent);
}

.admin-empty {
    opacity: 0.7;
    font-style: italic;
}

.dashboard-empty {
    grid-column: 1 / -1;
    opacity: 0.7;
    font-style: italic;
    padding: 1rem 0;
}

@keyframes service-status-pulse {
    0%, 100% {
        transform: scale(0.85);
        opacity: 0.55;
    }
    50% {
        transform: scale(1.15);
        opacity: 1;
    }
}

/* iPhone 14 Pro / 15 / 15 Pro (390 × 844 logical) and smaller iPhones */
@media (max-width: 390px) {
    h1 {
        font-size: 1.45rem;
    }

    .portal-tools__langs {
        gap: 0.5rem;
    }

    .service-tile__title {
        font-size: 1rem;
    }

    .btn {
        padding: 0.65rem 0.85rem;
        font-size: 0.9rem;
    }
}