:root {
    --bg: #0f1419;
    --surface: #1a2332;
    --surface-hover: #243044;
    --accent: #3b82f6;
    --accent-soft: rgba(59, 130, 246, 0.15);
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --border: rgba(148, 163, 184, 0.12);
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --radius: 16px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

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

body {
    margin: 0;
    min-height: 100dvh;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background:
        radial-gradient(ellipse at top, #1e293b 0%, transparent 55%),
        var(--bg);
    color: var(--text);
    padding:
        calc(12px + var(--safe-top))
        16px
        calc(20px + var(--safe-bottom));
}

.app-shell {
    max-width: 480px;
    margin: 0 auto;
}

.app-header {
    text-align: center;
    margin-bottom: 24px;
}

.app-header h1 {
    margin: 0 0 4px;
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.app-header p {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.app-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-auto-rows: 1fr;
    gap: 12px;
}

.app-cell {
    aspect-ratio: 1;
    min-height: 0;
    min-width: 0;
}

.app-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    height: 100%;
    min-height: 0;
    padding: 12px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    color: inherit;
    text-decoration: none;
    transition: transform 0.15s ease, background 0.15s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.app-tile--empty {
    pointer-events: none;
    background: rgba(26, 35, 50, 0.45);
    box-shadow: none;
    border-style: dashed;
    border-color: rgba(148, 163, 184, 0.18);
}

.app-tile--empty .icon {
    opacity: 0.15;
    font-size: 1.5rem;
}

.app-tile--empty .label {
    visibility: hidden;
}

.app-tile:active {
    transform: scale(0.96);
    background: var(--surface-hover);
}

.app-tile .icon {
    font-size: 2.25rem;
    line-height: 1;
}

.app-tile .label {
    font-size: 0.75rem;
    font-weight: 600;
    text-align: center;
    line-height: 1.2;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.app-footer {
    margin-top: 28px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    text-align: center;
    font-size: 0.8rem;
}

.app-footer a {
    color: var(--text-muted);
    font-size: 0.8rem;
    text-decoration: none;
}

.app-footer a:active {
    color: var(--accent);
}

.logout-btn {
    padding: 8px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.8rem;
    cursor: pointer;
}

.logout-btn:active {
    color: var(--text);
    border-color: var(--text-muted);
}

.loading,
.error-state {
    text-align: center;
    padding: 48px 16px;
    color: var(--text-muted);
}

.error-state {
    color: #f87171;
}

@media (min-width: 768px) {
    body {
        padding-top: calc(32px + var(--safe-top));
    }

    .app-shell {
        max-width: 540px;
    }

    .app-tile:hover {
        background: var(--surface-hover);
        transform: translateY(-2px);
    }
}

.offline-note {
    margin: 12px 0 0;
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.app-content[hidden],
.login-gate[hidden] {
    display: none !important;
}

.login-gate {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
}

.login-card {
    width: 100%;
    max-width: 360px;
    padding: 28px 22px;
    border-radius: var(--radius);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.login-brand {
    text-align: center;
    margin-bottom: 24px;
}

.login-icon {
    font-size: 2.5rem;
    display: block;
    margin-bottom: 8px;
}

.login-brand h1 {
    margin: 0 0 6px;
    font-size: 1.4rem;
}

.login-brand p {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.login-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.login-field input {
    padding: 12px 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
}

.login-field input:focus {
    outline: 2px solid var(--accent-soft);
    border-color: var(--accent);
}

.login-error {
    margin: 0;
    font-size: 0.85rem;
    color: #f87171;
}

.login-submit {
    margin-top: 4px;
    padding: 14px;
    border: none;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
}

.login-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.logout-btn:active {
    color: var(--text);
    border-color: var(--text-muted);
}

.friends-panel {
    margin-bottom: 20px;
    padding: 16px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
}

.friends-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.friends-header h2 {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 700;
}

.friends-invite-btn {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--accent-soft);
    color: var(--accent);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    white-space: nowrap;
}

.friends-empty {
    margin: 0;
    font-size: 0.8rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.friends-list {
    list-style: none;
    margin: 12px 0 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.friend-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 12px;
    background: var(--bg);
    border: 1px solid var(--border);
}

.friend-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 700;
    font-size: 1rem;
    flex-shrink: 0;
}

.friend-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.friend-info strong {
    font-size: 0.95rem;
}

.friend-username {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.invite-modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(0, 0, 0, 0.65);
}

.invite-modal[hidden] {
    display: none;
}

.invite-modal-card {
    width: 100%;
    max-width: 340px;
    padding: 20px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    text-align: center;
}

.invite-modal-card h2 {
    margin: 0 0 8px;
    font-size: 1.05rem;
}

.invite-modal-hint {
    margin: 0 0 16px;
    font-size: 0.8rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.invite-qr {
    display: block;
    margin: 0 auto 12px;
    border-radius: 12px;
}

.invite-code-text {
    margin: 0 0 12px;
    font-size: 0.75rem;
    color: var(--text-muted);
    word-break: break-all;
}

.invite-copy-btn,
.invite-close-btn {
    width: 100%;
    padding: 12px;
    border-radius: 10px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
}

.invite-copy-btn {
    margin-bottom: 8px;
    border: none;
    background: var(--accent);
    color: #fff;
}

.invite-close-btn {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-muted);
}

.invite-copy-feedback {
    margin: -4px 0 8px;
    font-size: 0.8rem;
    color: #4ade80;
}
