:root {
    --app-bar-h: 64px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --touch: 44px;
    --color-primary: #0f172a;
    --color-on-primary: #ffffff;
    --color-secondary: #334155;
    --color-accent: #0369a1;
    --color-on-accent: #ffffff;
    --color-background: #f8fafc;
    --color-foreground: #020617;
    --color-card: #ffffff;
    --color-muted: #e8ecf1;
    --color-muted-foreground: #475569;
    --color-border: #e2e8f0;
    --color-destructive: #dc2626;
    --color-on-destructive: #ffffff;
    --color-ring: #0369a1;
    --radius: 16px;
    --shadow: 0 10px 30px -18px rgba(15, 23, 42, 0.35);
    --primary: var(--color-primary);
    --bg: var(--color-background);
    --card: var(--color-card);
    --border: var(--color-border);
    --text-muted: var(--color-muted-foreground);
    --accent: var(--color-accent);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
}

body {
    font-family: 'Peyda', Tahoma, Arial, sans-serif;
    background: var(--color-background);
    color: var(--color-foreground);
    line-height: 1.55;
    min-height: 100%;
    min-height: -webkit-fill-available;
    padding-top: calc(var(--app-bar-h) + var(--safe-top));
    padding-bottom: calc(20px + var(--safe-bottom));
    overscroll-behavior-y: contain;
}

a,
button,
input,
select,
textarea {
    font-family: inherit;
}

button,
a.module-tile,
.app-bar-btn,
.btn-login,
.btn-save-pwd {
    cursor: pointer;
}

.skip-link {
    position: absolute;
    inset-inline-start: 12px;
    top: 8px;
    z-index: 4000;
    padding: 10px 14px;
    background: var(--color-accent);
    color: var(--color-on-accent);
    border-radius: 10px;
    font-weight: 700;
    text-decoration: none;
    transform: translateY(-160%);
}

.skip-link:focus {
    transform: none;
}

body:has(#loginScreen:not(.hidden)) .skip-link-app { display: none; }
body:has(#loginScreen.hidden) .skip-link-login { display: none; }

.app-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    min-height: calc(var(--app-bar-h) + var(--safe-top));
    padding: calc(var(--safe-top) + 8px) 12px 8px;
    background: var(--color-primary);
    color: var(--color-on-primary);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    z-index: 1000;
    box-shadow: 0 8px 24px -16px rgba(15, 23, 42, 0.7);
}

.app-bar-side {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 1 30%;
    min-width: 0;
    z-index: 2;
}

.app-bar-side--user { justify-content: flex-end; }
.app-bar-side--actions { justify-content: flex-start; }

.app-bar-user {
    font-size: 0.8rem;
    font-weight: 600;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

.app-bar-center {
    position: absolute;
    left: 50%;
    top: var(--safe-top);
    bottom: 0;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    max-width: 54%;
    min-width: 0;
    padding: 0 4px;
    pointer-events: none;
}

.app-bar-logo {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    object-fit: contain;
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.1);
}

.app-bar-brand-text {
    min-width: 0;
    text-align: center;
}

.app-bar-btn {
    min-width: var(--touch);
    min-height: var(--touch);
    border: 1px solid transparent;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    flex-shrink: 0;
    transition: background 0.18s ease, box-shadow 0.18s ease;
}

.app-bar-btn:active { background: rgba(255, 255, 255, 0.24); }
.app-bar-btn.hidden { visibility: hidden; pointer-events: none; }
.app-bar-btn:focus-visible,
.mobile-app-menu-item:focus-visible,
.btn-login:focus-visible,
.btn-save-pwd:focus-visible,
.pwd-modal-close:focus-visible,
.login-password-toggle:focus-visible,
.module-tile:focus-visible,
.login-field input:focus-visible {
    outline: 3px solid var(--color-ring);
    outline-offset: 2px;
}

.app-bar-mobile-menu { display: none; }
.app-bar-menu-trigger { opacity: 0.92; }

.mobile-app-menu {
    position: fixed;
    inset: 0;
    z-index: 1050;
    background: rgba(15, 23, 42, 0.42);
}

.mobile-app-menu[hidden] { display: none !important; }

.mobile-app-menu-panel {
    position: absolute;
    top: calc(var(--app-bar-h) + var(--safe-top) + 8px);
    inset-inline-start: 12px;
    min-width: min(260px, calc(100vw - 24px));
    padding: 8px;
    border-radius: 18px;
    background: var(--color-primary);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 18px 40px rgba(15, 23, 42, 0.28);
}

.mobile-app-menu-user {
    padding: 10px 12px 8px;
    font-size: 0.84rem;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.94);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    margin-bottom: 4px;
    word-break: break-word;
}

.mobile-app-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 48px;
    padding: 11px 12px;
    border: none;
    background: transparent;
    color: #fff;
    font-size: 0.9rem;
    font-weight: 600;
    border-radius: 12px;
    text-decoration: none;
    text-align: right;
}

.mobile-app-menu-item i {
    width: 18px;
    text-align: center;
}

.mobile-app-menu-item:hover,
.mobile-app-menu-item:active {
    background: rgba(255, 255, 255, 0.1);
}

.mobile-app-menu-item.hidden { display: none; }
.mobile-app-menu-item--danger { color: #fecaca; }

.login-screen {
    position: fixed;
    inset: 0;
    z-index: 3000;
    display: grid;
    grid-template-columns: 1fr;
    background:
        radial-gradient(circle at 12% 18%, rgba(3, 105, 161, 0.14), transparent 34%),
        linear-gradient(180deg, #eef4f9 0%, var(--color-background) 48%, #e8eef5 100%);
    padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
    overflow: auto;
}

.login-screen.hidden { display: none; }

body:has(#loginScreen:not(.hidden)) {
    padding-top: 0;
    padding-bottom: 0;
}

.login-aside {
    display: none;
}

.login-card {
    width: min(100%, 420px);
    margin: auto;
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: 24px;
    padding: 28px 22px 24px;
    box-shadow: var(--shadow);
}

.login-brand {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 18px;
}

.login-logo {
    width: 88px;
    height: 88px;
    border-radius: 22px;
    object-fit: contain;
    background: var(--color-muted);
    border: 1px solid var(--color-border);
}

.login-card h1 {
    font-size: 1.35rem;
    text-align: center;
    margin-bottom: 4px;
    color: var(--color-foreground);
    font-weight: 800;
}

.login-sub {
    text-align: center;
    font-size: 0.88rem;
    color: var(--color-muted-foreground);
    margin-bottom: 22px;
}

.login-field { margin-bottom: 14px; }

.login-field label {
    display: block;
    font-size: 0.84rem;
    font-weight: 700;
    margin-bottom: 6px;
    color: var(--color-secondary);
}

.login-field input {
    width: 100%;
    min-height: 48px;
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 10px 12px;
    font-size: 1rem;
    background: #fff;
    color: var(--color-foreground);
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.login-field input::placeholder { color: #94a3b8; }

.login-password-wrap {
    position: relative;
}

.login-password-wrap input {
    padding-inline-end: 52px;
}

.login-field input[aria-invalid="true"] {
    border-color: var(--color-destructive);
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.12);
}

.login-password-toggle {
    position: absolute;
    inset-inline-end: 4px;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--color-muted-foreground);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.login-error {
    color: #991b1b;
    font-size: 0.88rem;
    margin-bottom: 14px;
    display: none;
    text-align: right;
    padding: 10px 12px;
    border-radius: 12px;
    background: #fef2f2;
    border: 1px solid #fecaca;
}

.login-error.show { display: block; }

.btn-login,
.btn-save-pwd {
    width: 100%;
    min-height: 48px;
    border: none;
    border-radius: 12px;
    background: var(--color-accent);
    color: var(--color-on-accent);
    font-size: 1rem;
    font-weight: 800;
    transition: background 0.18s ease, transform 0.15s ease;
}

.btn-login:active,
.btn-save-pwd:active { transform: scale(0.985); }

.btn-login:disabled,
.btn-save-pwd:disabled { opacity: 0.65; cursor: default; }

.app-content.hidden { display: none; }

.empty-modules {
    text-align: center;
    padding: 36px 16px;
    color: var(--color-muted-foreground);
    font-size: 0.95rem;
    background: var(--color-card);
    border: 1px dashed var(--color-border);
    border-radius: var(--radius);
}

.pwd-modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 2500;
    background: rgba(15, 23, 42, 0.48);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 12px;
    padding-bottom: calc(12px + var(--safe-bottom));
}

.pwd-modal-backdrop[hidden] { display: none !important; }

.pwd-modal-box {
    background: var(--color-card);
    width: 100%;
    max-width: 420px;
    border-radius: 20px;
    box-shadow: 0 20px 50px rgba(15, 23, 42, 0.22);
    overflow: hidden;
    border: 1px solid var(--color-border);
}

.pwd-modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--color-border);
    font-weight: 800;
}

.pwd-modal-close {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 12px;
    background: var(--color-muted);
    color: var(--color-primary);
    font-size: 1.4rem;
}

.pwd-modal-body { padding: 14px 16px 18px; }

.pwd-modal-error {
    color: #991b1b;
    font-size: 0.85rem;
    margin-bottom: 10px;
    display: none;
    padding: 10px 12px;
    border-radius: 12px;
    background: #fef2f2;
    border: 1px solid #fecaca;
}

.pwd-modal-error.show { display: block; }

.pwd-modal-body .btn-save-pwd { margin-top: 8px; }

.app-bar-title {
    font-size: 0.95rem;
    font-weight: 800;
    line-height: 1.25;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-bar-sub {
    font-size: 0.72rem;
    opacity: 0.88;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: none;
}

.offline-banner {
    position: fixed;
    top: calc(var(--app-bar-h) + var(--safe-top));
    left: 0;
    right: 0;
    z-index: 999;
    background: var(--color-destructive);
    color: var(--color-on-destructive);
    text-align: center;
    padding: 8px 12px;
    font-size: 0.85rem;
    font-weight: 700;
    display: none;
}

.offline-banner.show { display: block; }

body.is-offline {
    padding-top: calc(var(--app-bar-h) + var(--safe-top) + 36px);
}

.main {
    max-width: 720px;
    margin: 0 auto;
    padding: 16px 14px 8px;
}

.section { margin-bottom: 24px; }

.section-title {
    font-size: 0.82rem;
    font-weight: 800;
    color: var(--color-secondary);
    margin-bottom: 12px;
    padding-right: 2px;
}

.module-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.module-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    min-height: 116px;
    padding: 16px 12px;
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: 18px;
    text-decoration: none;
    color: var(--color-foreground);
    box-shadow: 0 8px 20px -16px rgba(15, 23, 42, 0.35);
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}

.module-tile:active {
    transform: scale(0.98);
}

.module-icon {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    background: #e8f3fa;
    color: var(--color-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    flex-shrink: 0;
}

.module-icon img.module-icon-img {
    width: 1.35rem;
    height: 1.35rem;
    object-fit: contain;
    display: block;
}

.module-tile:nth-child(6n+2) .module-icon { color: #0f766e; background: #e6f6f3; }
.module-tile:nth-child(6n+3) .module-icon { color: #1d4ed8; background: #e8eefc; }
.module-tile:nth-child(6n+4) .module-icon { color: #b45309; background: #fff4e5; }
.module-tile:nth-child(6n+5) .module-icon { color: #334155; background: #eef2f6; }

.module-label {
    font-size: 0.86rem;
    font-weight: 800;
    text-align: center;
    line-height: 1.4;
    overflow-wrap: anywhere;
}

.page-footer {
    text-align: center;
    padding: 24px 12px 8px;
    font-size: 0.75rem;
    color: var(--color-muted-foreground);
    line-height: 1.7;
}

@media (min-width: 380px) {
    .app-bar-center { max-width: 58%; }
    .app-bar-title { font-size: 1.08rem; }
    .app-bar-sub { display: block; font-size: 0.8rem; }
    .app-bar-user { font-size: 0.84rem; }
    .app-bar-logo { width: 48px; height: 48px; }
}

@media (max-width: 519px) {
    :root { --app-bar-h: 72px; }
    .app-bar-actions-desktop { display: none; }
    .app-bar-mobile-menu { display: block; }
    .app-bar-side--user { display: none; }
    .app-bar-center { max-width: 62%; }
}

@media (min-width: 520px) {
    .mobile-app-menu { display: none !important; }
    .pwd-modal-backdrop { align-items: center; }
    .module-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 900px) {
    .login-screen {
        grid-template-columns: minmax(0, 1.1fr) minmax(420px, 520px);
        padding: 0;
        overflow: auto;
        min-height: 100%;
        min-height: 100dvh;
    }

    .login-aside {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 32px;
        background: #d2e0f2;
    }

    .login-poster {
        margin: 0;
        width: min(100%, 420px);
        max-height: min(68vh, calc(100dvh - 140px));
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .login-poster-img {
        display: block;
        width: auto;
        height: auto;
        max-width: 100%;
        max-height: min(68vh, calc(100dvh - 140px));
        object-fit: contain;
        background: transparent;
        border-radius: 0;
        box-shadow: none;
    }

    .login-card {
        margin: auto 48px auto 48px;
        padding: 36px 32px 32px;
    }

    .main {
        max-width: 1080px;
        padding-top: 24px;
    }

    .module-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
    .module-tile { min-height: 132px; }
}

@media (hover: hover) {
    .module-tile:hover {
        border-color: #bae6fd;
        box-shadow: 0 14px 28px -18px rgba(3, 105, 161, 0.35);
    }

    .btn-login:hover,
    .btn-save-pwd:hover { background: #075985; }

    .app-bar-btn:hover { background: rgba(255, 255, 255, 0.18); }

    .login-password-toggle:hover { background: var(--color-muted); color: var(--color-primary); }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
    }

    .module-tile:active,
    .btn-login:active,
    .btn-save-pwd:active {
        transform: none;
    }
}
